You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Question] How to get NDarray into .NET native types array effectivly? #139
I promise i've tried using GetData<Single>() or flatten first then using GetData<Single>(),non of them are correct actually.So i'm just looking for a more effective and fast way to do this. : )
Manually looping over a 3-dimensional ndarray in C# is the most inefficient way of retrieving the data I can think of. You are crossing the Python-C# boundary for every single entry and thus have cubic overhead.
I don't believe that GetData<float> is not working. I have here a testcase that proves otherwise:
Sorry lately for the reply,since i've played video games in this whole weekend😂
Here is a little code to demostrate what i've met:
usingNumpy;conststringnumpyFilePath=@"D:\My Code\quantize_test\callibration_data\phone.npy";varnpArray=np.load(numpyFilePath);Console.WriteLine($"Array Shape: {string.Join(", ",npArray.shape)}");Console.WriteLine($"Array Data Type: {npArray.dtype}");varnativeViaGetData=npArray.GetData<float>();Console.WriteLine("First 10 elements via ToCsharp<float>:");
Well,the real problem is not reproduce on my laptop but anothere has appeared:
Array Shape: (1,150,768)
Array Data Type: float16
Fatal error. Internal CLR error. (0x80131506)
at System.Buffer._Memmove(Byte ByRef, Byte ByRef, UIntPtr)
at System.Runtime.InteropServices.Marshal.CopyToManaged[[System.Single,System.Private.CoreLib,Version=8.0.0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e]](IntPtr, Single[], Int32, Int32)
at Numpy.NDarray.GetData[[System.Single,System.Private.CoreLib,Version=8.0.0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e]]()
at Numpy.NDarray.GetData[[System.Single,System.Private.CoreLib,Version=8.0.0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e]]()
at Program.<Main>$(System.String[])
usingNumpy;usingMicrosoft.ML.OnnxRuntime.Tensors;conststringnumpyFilePath=@"D:\My Code\quantize_test\callibration_data\phone.npy";varnpArray=np.load(numpyFilePath).astype(np.float32);Console.WriteLine($"Array Shape: {string.Join(", ",npArray.shape)}");Console.WriteLine($"Array Data Type: {npArray.dtype}");varnativeViaGetData=npArray.GetData<float>();varfp16Array=nativeViaGetData.Select(x =>(Half)x).ToArray();varonnxTensor=newDenseTensor<Half>(fp16Array,newint[]{npArray.shape[0],npArray.shape[1],npArray.shape[2]});Console.WriteLine("First 10 elements via ToCsharp<float>:");
Activity
henon commentedon Oct 24, 2025
Manually looping over a 3-dimensional ndarray in C# is the most inefficient way of retrieving the data I can think of. You are crossing the Python-C# boundary for every single entry and thus have cubic overhead.
I don't believe that
GetData<float>is not working. I have here a testcase that proves otherwise:Output is
Proof: 1.1,2.2,3.141596,4.4Add testcase for #139
ElinLiu0 commentedon Oct 27, 2025
Sorry lately for the reply,since i've played video games in this whole weekend😂
Here is a little code to demostrate what i've met:
Well,the real problem is not reproduce on my laptop but anothere has appeared:
Any idea on this?? : (
phone.zip
ElinLiu0 commentedon Oct 27, 2025
Well,i think i found the solution way out:
And it's worked fine:
ElinLiu0 commentedon Oct 27, 2025
The problem used is that:
See Microsoft.ML.OnnxRuntime also provide a dtype called Float16,but it actually looks in this way:
I really don't know why?May be it's a predefined strcuture somehow i guess?When i turn Float16 into Half it as expected as upon post.
henon commentedon Oct 27, 2025
I just remembered, there was an issue with Float16: #115
ElinLiu0 commentedon Oct 27, 2025
Yeah,but the FP16 you mentioned is different like mine.But,anyway the problem is solved in...
Right? 🤣
henon commentedon Oct 27, 2025
Here is a specification of C#'s Half type: This is the IEEE floating point specification, a very common format: https://devblogs.microsoft.com/dotnet/introducing-the-half-type/
I can't believe that numpy uses a different format internally but I don't have time to research it. Maybe it is an endianness problem?
henon commentedon Oct 27, 2025
But like I showed in #115 you have the option to copy the bytes and transform the data into floats in C#, provided that you know the binary format.
ElinLiu0 commentedon Oct 27, 2025
Opps..Not numpy's problem at all cause it working as expected on Python.Seems the Float16 structure in ONNX c# quite be Special...
ElinLiu0 commentedon Oct 27, 2025
Yeah.I've seen this annda will that in mind.
henon commentedon Oct 27, 2025
Actually the The IEEE 754 specification is nothing special. It is the most common representation of floating point values of 16, 32, 64 and 128 bits.
henon commentedon Oct 27, 2025
I found information about Float16 here https://numpy.org/doc/stable/user/basics.types.html
Half precision float: sign bit, 5 bits exponent, 10 bits mantissa.That looks like IEEE to me