microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
14.7k stars 2.93k forks source link

[Feature Request] C# Float16 DEMO, and float convert api #14303

Open WuNein opened 1 year ago

WuNein commented 1 year ago

Describe the feature request

C# Float16 related function. Currently, Float16 in C# do not have any demo, while the value saved in Float16 is in ushort format. Float16 Struct

public ushort value

my converting code below made 0.56 -> 0, which is incorrent as np.float16 result in python, (I think it become float -> ushort in c#)

var tensor = new DenseTensor<Float16>(new[] { 1, 3, bitmap.Height, bitmap.Width });
tensor[0, 0, y, x] = (Float16)(row[x * bytesPerPixel + 2] / 255.0F); // r
tensor[0, 1, y, x] = (Float16)(row[x * bytesPerPixel + 1] / 255.0F); // g
tensor[0, 2, y, x] = (Float16)(row[x * bytesPerPixel + 0] / 255.0F); // b

API functions missing: 1.Convert float(.net native) to Float16 2.Convert Float16 to float(.net native)

Describe scenario use case

ONNXruntime-gpu, CUDA, c#, influence, windows , .net6, using fp16 to accurate

yuslepukhin commented 1 year ago

Float16 and BFloat16 are often represented as unsigned 16-bit integers. Please, indicate what is missing in the documentation, because there is a code sample.

We do not provide conversion API, it is outside the scope of the API regardless of the language. We will incorporate the latest type in .NET when it is fully supported.

yuslepukhin commented 1 year ago

https://github.com/microsoft/onnxruntime/commit/51c42ae64ae71a32e85065288e738fe585de2942

WuNein commented 1 year ago

greatly thanks, i saw the converting code in https://github.com/microsoft/onnxruntime/blob/main/csharp/test/Microsoft.ML.OnnxRuntime.Tests.Common/OrtFloat16Tests.cs I will test it later.

gjiremadzeBECKMAN commented 3 months ago

greatly thanks, i saw the converting code in https://github.com/microsoft/onnxruntime/blob/main/csharp/test/Microsoft.ML.OnnxRuntime.Tests.Common/OrtFloat16Tests.cs I will test it later.

Hello @WuNein, were you able to test out the converting code? I am looking for the same functionality to convert an array of inputs from float32 to float16 precision and then back to float32 to feed into the onnx model.

Our tensor model was trained on float16 but onnx models only support 32bit inputs.

WuNein commented 3 months ago

@gjiremadzeBECKMAN onnx convert to float16 is easy https://github.com/microsoft/onnxconverter-common/blob/209a47e18e6a4c3474273a0b2a5e8f1fda481643/onnxconverter_common/auto_mixed_precision_model_path.py#L23

I haven't try the c# code, to be honest.