Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[CORE] fix subnormal rounding logic and add unit tests (#34165)

barnasm13f24a5cc598d
diff --git a/src/core/src/type/float8_e8m0.cpp b/src/core/src/type/float8_e8m0.cpp
index c0ba89fc859..83db20098db 100644
--- a/src/core/src/type/float8_e8m0.cpp
+++ b/src/core/src/type/float8_e8m0.cpp
@@ -24,7 +24,8 @@ namespace {
constexpr uint8_t f32_mantissa_bits{23u};
constexpr uint32_t f32_exponent_bits_mask{0x7f800000u};
constexpr uint32_t f32_mantissa_bits_mask{0x007fffffu};
-constexpr uint32_t round_even{0x00400000u};
+constexpr uint32_t normal_boundary_value{0x00400000u};
+constexpr uint32_t subnormal_boundary_value{0x00600000u};
uint8_t f32_to_f8e8m0_bits(const float value) {
const auto input = util::f32_to_u32_bits(value);
@@ -34,12 +35,15 @@ uint8_t f32_to_f8e8m0_bits(const float value) {
return 0b00000000;
} else if (input_exponent_bits >= 0b11111110) {
return input_exponent_bits - static_cast<uint8_t>(std::isinf(value));
+ } else if (const auto input_mantissa_bits = input & f32_mantissa_bits_mask; input_exponent_bits == 0b00000000) {
+ // subnormal values
+ return (input_mantissa_bits <= subnormal_boundary_value) ? 0b00000000 : 0b00000001;
} else {
// normal values
- const auto input_mantissa_bits = input & f32_mantissa_bits_mask;
- return input_exponent_bits + static_cast<uint8_t>((input_mantissa_bits > round_even) || // round to nearest
- ((input_mantissa_bits == round_even) && // round to even
- (input_exponent_bits & 0x1)));
+ return input_exponent_bits +
+ static_cast<uint8_t>((input_mantissa_bits > normal_boundary_value) || // round to nearest
+ ((input_mantissa_bits == normal_boundary_value) && // round tie to even
+ (input_exponent_bits & 0x1)));
}
}
} // namespace
diff --git a/src/core/tests/float8_e8m0.cpp b/src/core/tests/float8_e8m0.cpp
index e610c9284ff..d37f63a6164 100644
--- a/src/core/tests/float8_e8m0.cpp
+++ b/src/core/tests/float8_e8m0.cpp
@@ -69,6 +69,60 @@ TEST(F8E8M0Test, f32_negative_zero) {
EXPECT_EQ(f8.to_bits(), 0b00000000);
}
+TEST(F8E8M0Test, f32_subnormal_to_lower) {
+ const auto f8 = ov::float8_e8m0(8.816206229868691984948453E-39f);
+
+ EXPECT_EQ(f8.to_bits(), 0b00000000);
+}
+
+TEST(F8E8M0Test, f32_subnormal_boundary_to_even) {
+ const auto f8 = ov::float8_e8m0(8.816207631167156309765524E-39f);
+
+ EXPECT_EQ(f8.to_bits(), 0b00000000);
+}
+
+TEST(F8E8M0Test, f32_subnormal_to_greater) {
+ const auto f8 = ov::float8_e8m0(8.816209032465620634582595E-39f);
+
+ EXPECT_EQ(f8.to_bits(), 0b00000001);
+}
+
+TEST(F8E8M0Test, f32_normal_to_lower) {
+ const auto f8 = ov::float8_e8m0(1.175494350822287507968737E-38);
+
+ EXPECT_EQ(f8.to_bits(), 0b00000001);
+}
+
+TEST(F8E8M0Test, f32_normal_boundary_to_even) {
+ const auto f8 = ov::float8_e8m0(1.763241526233431261953105E-38);
+
+ EXPECT_EQ(f8.to_bits(), 0b00000010);
+}
+
+TEST(F8E8M0Test, f32_normal_to_greater) {
+ const auto f8 = ov::float8_e8m0(1.763241666363277694434812E-38);
+
+ EXPECT_EQ(f8.to_bits(), 0b00000010);
+}
+
+TEST(F8E8M0Test, f32_normal_to_lower_2) {
+ const auto f8 = ov::float8_e8m0(3.526482772207169658942795E-38);
+
+ EXPECT_EQ(f8.to_bits(), 0b00000010);
+}
+
+TEST(F8E8M0Test, f32_normal_boundary_to_even_2) {
+ const auto f8 = ov::float8_e8m0(3.526483052466862523906210E-38);
+
+ EXPECT_EQ(f8.to_bits(), 0b00000010);
+}
+
+TEST(F8E8M0Test, f32_normal_to_greater_2) {
+ const auto f8 = ov::float8_e8m0(3.526483332726555388869624E-38);
+
+ EXPECT_EQ(f8.to_bits(), 0b00000011);
+}
+
TEST(F8E8M0Test, f32_negative_one) {
const auto f8 = ov::float8_e8m0(-1.0f);