diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/OrtFloat16.shared.cs b/csharp/src/Microsoft.ML.OnnxRuntime/OrtFloat16.shared.cs index c71218ba62f7c..a059208ef6373 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/OrtFloat16.shared.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/OrtFloat16.shared.cs @@ -259,7 +259,7 @@ internal static float CreateSingle(bool sign, byte exponent, uint significand) /// <summary> /// Float16 NaN /// </summary> - public static Float16 NaN => new Float16(PositiveQNaNBits); // quiet NaN + public static Float16 NaN => new Float16(NegativeQNaNBits); // Same as System.Half.NaN /// <summary> /// Float16 Zero value @@ -903,7 +903,7 @@ private static ushort RoundPackToFloat16(bool sign, short exp, ushort sig) /// <summary> /// BFloat16 NaN /// </summary> - public static BFloat16 NaN => new BFloat16(PositiveQNaNBits); // quiet NaN + public static BFloat16 NaN => new BFloat16(NegativeQNaNBits); // .Net has no BFloat16. Follow Float16 style. /// <summary> /// BFloat16 Positive Zero diff --git a/include/onnxruntime/core/framework/float16.h b/include/onnxruntime/core/framework/float16.h index 64ffe3ccdfc46..dac0a01fbc3fe 100644 --- a/include/onnxruntime/core/framework/float16.h +++ b/include/onnxruntime/core/framework/float16.h @@ -318,6 +318,8 @@ class numeric_limits<onnxruntime::MLFloat16> { } static constexpr onnxruntime::MLFloat16 quiet_NaN() noexcept { + // The most significant fraction bit shall be 1, and no limitation on other fraction bits. + // Note that most frameworks use 0x7E00; while CUDA uses 0x7FFF; .Net System.Half.NaN uses 0xFE00; return onnxruntime::MLFloat16::FromBits(0x7E00U); // Bits: sign(0), exponent(111,11), fraction(10,0000,0000) }