Skip to content

Commit b20cfd2

Browse files
committed
Addressing PR#19096 review comments
1 parent a132809 commit b20cfd2

File tree

10 files changed

+246
-112
lines changed

10 files changed

+246
-112
lines changed

xla/hlo/builder/lib/math_test.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ class MathTypedTest : public MathTest {
9696

9797
bool has_inf = std::numeric_limits<T>::has_infinity;
9898
bool has_nan = std::numeric_limits<T>::has_quiet_NaN;
99-
bool is_finite = !has_inf && !has_nan;
100-
bool is_nan_only = !has_inf && has_nan;
99+
bool has_finite = !has_inf && !has_nan;
100+
bool has_nan_only = !has_inf && has_nan;
101101

102102
auto expected = LiteralUtil::MakeTupleOwned(
103-
LiteralUtil::CreateR1<bool>({true, true, true, true, true, is_finite,
104-
is_finite, is_finite, is_finite}),
103+
LiteralUtil::CreateR1<bool>({true, true, true, true, true, has_finite,
104+
has_finite, has_finite, has_finite}),
105105
LiteralUtil::CreateR1<bool>({false, false, false, false, false, has_inf,
106106
has_inf, false, false}),
107107
LiteralUtil::CreateR1<bool>(
108108
{false, false, false, false, false, has_inf, false, false, false}),
109109
LiteralUtil::CreateR1<bool>(
110110
{false, false, false, false, false, false, has_inf, false, false}),
111111
LiteralUtil::CreateR1<bool>({false, false, false, false, false,
112-
is_nan_only, is_nan_only, has_nan,
112+
has_nan_only, has_nan_only, has_nan,
113113
has_nan}));
114114
ComputeAndCompareLiteral(&b, expected, {});
115115
}

xla/literal.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -593,13 +593,6 @@ class LiteralBase {
593593
static_assert(8 % bits_per_element == 0);
594594

595595
constexpr int elements_per_byte = 8 / bits_per_element;
596-
constexpr auto cast = [](NativeT x) -> uint8_t {
597-
if constexpr (primitive_util::IsFloatingPointType(primitive_type)) {
598-
return Eigen::numext::bit_cast<uint8_t>(x);
599-
}
600-
return static_cast<uint8_t>(x);
601-
};
602-
603596
int64_t bytes = elements.size() / elements_per_byte;
604597
for (int64_t i = 0; i < bytes; ++i) {
605598
uint8_t byte = 0;
@@ -710,14 +703,14 @@ class LiteralBase {
710703
static_assert(!primitive_util::IsComplexType(primitive_type));
711704
static_assert(8 % bits_per_element == 0);
712705

713-
constexpr int elements_per_byte = 8 / bits_per_element;
714706
constexpr auto cast = [](uint8_t x) -> NativeT {
715707
if constexpr (primitive_util::IsFloatingPointType(primitive_type)) {
716708
return Eigen::numext::bit_cast<NativeT>(x);
717709
}
718710
return static_cast<NativeT>(x);
719711
};
720712

713+
constexpr int elements_per_byte = 8 / bits_per_element;
721714
int64_t bytes = elements.size() / elements_per_byte;
722715
for (int64_t i = 0; i < bytes; ++i) {
723716
uint8_t byte;

xla/literal_comparison_test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ TYPED_TEST(LiteralComparisonTest, CompareNear_NotEqual_4ulps) {
7373
auto actual = LiteralUtil::CreateR0<TypeParam>(TypeParam(1.0));
7474
float expV = 1.5; // F8E4M3*
7575
if (type == F8E5M2 || type == F8E5M2FNUZ)
76-
expV = 1.75;
76+
expV = 2.0;
7777
else if (type == F8E3M4)
7878
expV = 1.25;
7979
else if (type == F4E2M1FN)
@@ -99,7 +99,7 @@ TYPED_TEST(LiteralComparisonTest, FloatUsingCompareNear_NotEqual_4ulps) {
9999
auto actual = LiteralUtil::CreateR0<float>(1.0);
100100
float expV = 1.51; // F8E4M3*
101101
if (type == F8E5M2 || type == F8E5M2FNUZ)
102-
expV = 1.76;
102+
expV = 2.01;
103103
else if (type == F8E3M4)
104104
expV = 1.26;
105105
else if (type == F4E2M1FN)

xla/python/ifrt/dtype.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ message DTypeProto {
8181
// collision.
8282
KIND_STRING = 99;
8383

84-
// Next: 31
84+
// Next: 32
8585
}
8686
// LINT.ThenChange()
8787
Kind kind = 1;

xla/python/ifrt/dtype_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ TEST(DTypeTest, BitSize) {
7474
{DType::kF8E3M4, 8}, {DType::kF8E4M3, 8},
7575
{DType::kF8E4M3FN, 8}, {DType::kF8E4M3B11FNUZ, 8},
7676
{DType::kF8E4M3FNUZ, 8}, {DType::kF8E5M2, 8},
77-
{DType::kF8E5M2FNUZ, 8}, {DType::kF8E8M0FNU, 4},
77+
{DType::kF8E5M2FNUZ, 8}, {DType::kF8E8M0FNU, 8},
7878
{DType::kS16, 16}, {DType::kU16, 16},
7979
{DType::kF16, 16}, {DType::kBF16, 16},
8080
{DType::kS32, 32}, {DType::kU32, 32},

xla/service/elemental_ir_emitter.cc

Lines changed: 168 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -795,118 +795,217 @@ llvm::Value* EmitF8e4m3b11fnuzToF16(llvm::Value* f8_value,
795795

796796
absl::StatusOr<llvm::Value*> EmitF16ToF4e2m1fn(llvm::Value* f16_value,
797797
llvm::IRBuilder<>* b) {
798+
auto i8_const = [&](int val) {
799+
return llvm::ConstantInt::get(b->getInt8Ty(), val);
800+
};
801+
auto i16_const = [&](int val) {
802+
return llvm::ConstantInt::get(b->getInt16Ty(), val);
803+
};
804+
constexpr int mantissa_diff = 9; // 10 for F16, 1 for F4
805+
constexpr int bias_diff = 14; // 15 for F16, 1 for F4
806+
807+
// Cast the input value to an integer for bitwise manipulation.
808+
// Get the absolute value of the input (discard the sign).
809+
// f16_bits = bitcast(f16_value, int)
810+
// f16_abs_bits = f16_bits & 0x7FFF
811+
llvm::Value* f16_bits = b->CreateBitCast(f16_value, b->getInt16Ty());
812+
llvm::Value* f16_abs_bits = b->CreateAnd(f16_bits, i16_const(0x7FFF));
813+
814+
// If the input absolute value is >= 7.0 or an infinity, the result saturates
815+
// to max value (6.0). If (0.75 <= input < 1), the result is rounded to 1.0.
816+
// If (0 <= input <= 0.25), the result is rounded to 0.0.
817+
// If the input is NaN, the result is undefined (implemented as minus zero).
818+
// The rest of the cases are handled by the "happy path".
819+
// is_overflow = f16_abs_bits >= 0x1.Cp2
820+
// is_one = f16_abs_bits >= 0x1.8p-1 (used only if exponent underflows)
821+
// is_zero = f16_abs_bits <= 0x1p-2 (used only if exponent underflows)
822+
// is_nan = f16_abs_bits > 0x7C00 (F16 NaN threshold)
823+
llvm::Value* is_overflow =
824+
b->CreateICmpUGE(f16_abs_bits, i16_const(0x4700)); // 7.0
825+
llvm::Value* is_one =
826+
b->CreateICmpUGE(f16_abs_bits, i16_const(0x3A00)); // 0.75
827+
llvm::Value* is_zero =
828+
b->CreateICmpULE(f16_abs_bits, i16_const(0x3400)); // 0.25
829+
llvm::Value* is_nan =
830+
b->CreateICmpUGT(f16_abs_bits, i16_const(0x7C00)); // inf
831+
832+
// Truncate the mantissa to 1 bit and the exponent to 3 bits (not 2 bits, as
833+
// the type doesn't have Inf/NaN and can represent unbiased exponent 2).
834+
// This case, as well as the denormal, is handled below.
798835
TF_ASSIGN_OR_RETURN(
799836
llvm::Value * reduced_precision,
800837
EmitReducePrecisionIR(
801838
/*src_ty=*/F16, f16_value,
802839
/*dest_exponent_bits=*/primitive_util::ExponentWidth(F4E2M1FN) + 1,
803840
/*dest_mantissa_bits=*/primitive_util::SignificandWidth(F4E2M1FN) - 1,
804841
/*quiet_nans=*/false, b));
842+
843+
// Cast the reduced precision value to an integer for bitwise manipulation.
844+
// Discard the least significant (9) mantissa bits leaving 1 bit.
845+
// Truncate to
846+
// as_int16 = bitcast(reduced_precision, int)
847+
// as_int8 = as_int16 >> (f16_mantissa - f4_mantissa)
805848
llvm::Value* as_int16 = b->CreateBitCast(reduced_precision, b->getInt16Ty());
806849
llvm::Value* as_int8 =
807-
b->CreateTrunc(b->CreateLShr(as_int16, 9), b->getInt8Ty());
850+
b->CreateTrunc(b->CreateLShr(as_int16, mantissa_diff), b->getInt8Ty());
808851

809-
// Extract sign, exponent and mantissa from reduced precision value.
810-
auto i8_const = [&](int val) {
811-
return llvm::ConstantInt::get(b->getInt8Ty(), val);
812-
};
852+
// Get the sign (0 or 1).
853+
// f4_sign = as_int8 >> 6
813854
llvm::Value* f4_sign = b->CreateLShr(as_int8, 6);
855+
856+
// Get exponent and mantissa bits without the sign.
857+
// Important: the mask is 0x3F (not 0x7F), discard bit #6.
858+
// f4_bits = as_int8 & 0x3F
814859
llvm::Value* f4_bits = b->CreateAnd(as_int8, i8_const(0x3F));
815-
llvm::Value* f4_normal = b->CreateSub(f4_bits, i8_const(28));
816860

817-
// Special case for exponent overflow.
818-
auto i16_const = [&](int val) {
819-
return llvm::ConstantInt::get(b->getInt16Ty(), val);
820-
};
821-
llvm::Value* f16_bits = b->CreateAnd(
822-
b->CreateBitCast(f16_value, b->getInt16Ty()), i16_const(0x7FFF));
823-
llvm::Value* is_overflow =
824-
b->CreateICmpUGE(f16_bits, i16_const(0x4700)); // 7.0
825-
llvm::Value* is_nan = b->CreateICmpUGT(f16_bits, i16_const(0x7C00)); // inf
826-
llvm::Value* max_or_nan =
827-
b->CreateSelect(is_nan, i8_const(0x8), i8_const(0x7));
828-
llvm::Value* f4_normal_or_overflow =
829-
b->CreateSelect(is_overflow, max_or_nan, f4_normal);
830-
831-
// Special case for exponent underflow.
861+
// Convert F16 exponent to F4 exponent by readjusting the exponent bias.
862+
// This produces the "normal" result, i.e. not Inf or NaN or denormal.
863+
// f4_normal = f4_bits - ((f16_bias - f4_bias) << f4_mantissa)
864+
constexpr int f4_exponent_offset = bias_diff << 1;
865+
llvm::Value* f4_normal = b->CreateSub(f4_bits, i8_const(f4_exponent_offset));
866+
867+
// If the rounding resulted in zero exponent, the value is incorrect.
868+
// This happens when the input is < 1.0
869+
// is_underflow = f4_normal <= 1
832870
llvm::Value* is_underflow = b->CreateICmpSLE(f4_normal, i8_const(1));
833-
llvm::Value* is_one = b->CreateICmpUGE(f16_bits, i16_const(0x3A00)); // 0.75
834-
llvm::Value* is_zero = b->CreateICmpULE(f16_bits, i16_const(0x3400)); // 0.25
835-
llvm::Value* denorm_or_zero =
836-
b->CreateSelect(is_zero, i8_const(0x0), i8_const(0x1));
837-
llvm::Value* f4_small =
838-
b->CreateSelect(is_one, i8_const(0x2), denorm_or_zero);
839-
llvm::Value* f4_result =
840-
b->CreateSelect(is_underflow, f4_small, f4_normal_or_overflow);
871+
872+
// Chain of selects that handles the special cases.
873+
// f4_result =
874+
// is_underflow ? (is_one ? 1.0 : (is_zero ? 0.0 : 0.5)) :
875+
// is_overflow ? (is_nan ? -0.0 : 6.0) :
876+
// f4_normal
877+
llvm::Value* f4_result = b->CreateSelect(
878+
is_underflow,
879+
// If underflow, the input is < 1.0; the result is either 0.0, 0.5 or 1.0
880+
b->CreateSelect(is_one, i8_const(0x2),
881+
b->CreateSelect(is_zero, i8_const(0x0), i8_const(0x1))),
882+
// If overflow, the input is >= 7.0 or infinity or NaN.
883+
b->CreateSelect(is_overflow,
884+
b->CreateSelect(is_nan, i8_const(0x8), i8_const(0x7)),
885+
f4_normal));
841886

842887
// Add sign to the resulting value.
888+
// f4_signed_result = (f4_sign << 3) | f4_result
843889
return b->CreateOr(f4_result, b->CreateShl(f4_sign, 3));
844890
}
845891

846892
llvm::Value* EmitF4e2m1fnToF16(llvm::Value* f8_value, llvm::IRBuilder<>* b) {
847-
llvm::Value* as_int16 = b->CreateZExt(f8_value, b->getInt16Ty());
848-
849-
// Extract sign, exponent and mantissa from reduced precision value.
850893
auto i16_const = [&](int val) {
851894
return llvm::ConstantInt::get(b->getInt16Ty(), val);
852895
};
853-
llvm::Value* sign = b->CreateLShr(as_int16, 3);
854-
llvm::Value* sign_shifted = b->CreateShl(sign, 15);
855-
llvm::Value* bits = b->CreateAnd(as_int16, i16_const(0x7));
856-
llvm::Value* bits_shifted = b->CreateShl(bits, 9);
857-
858-
// Re-bias the exponent and handle denormals.
859-
llvm::Value* f16_normal = b->CreateAdd(bits_shifted, i16_const(14 << 10));
860-
llvm::Value* is_denorm_or_zero = b->CreateICmpULE(bits, i16_const(1));
861-
llvm::Value* is_zero = b->CreateICmpEQ(bits, i16_const(0));
862-
llvm::Value* denorm_or_zero =
863-
b->CreateSelect(is_zero, i16_const(0x0000), i16_const(0x3800));
864-
llvm::Value* f16_result =
865-
b->CreateSelect(is_denorm_or_zero, denorm_or_zero, f16_normal);
896+
constexpr int mantissa_diff = 9; // 10 for F16, 1 for F4
897+
constexpr int bias_diff = 14; // 15 for F16, 1 for F4
898+
899+
// The input value is a 8-bit integer, extend it to 16-bit integer.
900+
// as_int16 = bitcast(f8_value, int)
901+
llvm::Value* as_int16 = b->CreateZExt(f8_value, b->getInt16Ty());
902+
903+
// Get the sign and shift it to F16 position.
904+
// f4_sign = as_int16 >> 3
905+
// f16_sign_bit = f4_sign << 15
906+
llvm::Value* f4_sign = b->CreateLShr(as_int16, 3);
907+
llvm::Value* f16_sign_bit = b->CreateShl(f4_sign, 15);
908+
909+
// Get exponent and mantissa bits without the sign.
910+
// f4_bits = as_int16 & 0x7
911+
// f16_bits = f4_bits << (f16_mantissa - f4_mantissa)
912+
llvm::Value* f4_bits = b->CreateAnd(as_int16, i16_const(0x7));
913+
llvm::Value* f16_bits = b->CreateShl(f4_bits, mantissa_diff);
914+
915+
// Convert F16 exponent to F4 exponent by readjusting the exponent bias.
916+
// f4_normal = f4_bits - ((f16_bias - f4_bias) << f4_mantissa)
917+
constexpr int f16_exponent_offset = bias_diff << 10;
918+
llvm::Value* f16_normal =
919+
b->CreateAdd(f16_bits, i16_const(f16_exponent_offset));
920+
921+
// For denormal and zero, the exponent is different. Handle these cases
922+
// separately below.
923+
// is_denorm_or_zero = f4_bits <= 1
924+
// is_zero = f4_bits == 0
925+
llvm::Value* is_denorm_or_zero = b->CreateICmpULE(f4_bits, i16_const(1));
926+
llvm::Value* is_zero = b->CreateICmpEQ(f4_bits, i16_const(0));
927+
928+
// Chain of selects that handles the special cases.
929+
// f16_result = is_denorm_or_zero ? (is_zero ? 0.0 : 0.5) : f16_normal
930+
llvm::Value* f16_result = b->CreateSelect(
931+
is_denorm_or_zero,
932+
b->CreateSelect(is_zero, i16_const(0x0000), i16_const(0x3800)),
933+
f16_normal);
866934

867935
// Add sign to the resulting value.
868-
llvm::Value* f16_signed = b->CreateOr(f16_result, sign_shifted);
869-
return b->CreateBitCast(f16_signed, b->getHalfTy());
936+
// f16_signed_result = f16_sign_bit | f16_result
937+
llvm::Value* f16_signed_result = b->CreateOr(f16_result, f16_sign_bit);
938+
return b->CreateBitCast(f16_signed_result, b->getHalfTy());
870939
}
871940

872941
llvm::Value* EmitF32ToF8e8m0fnu(llvm::Value* f32_value, llvm::IRBuilder<>* b) {
873-
llvm::Value* as_int32 = b->CreateBitCast(f32_value, b->getInt32Ty());
874-
875-
// Result is NaN if input is zero, negative, infinity or NaN.
876942
auto i32_const = [&](int val) {
877943
return llvm::ConstantInt::get(b->getInt32Ty(), val);
878944
};
879-
llvm::Value* is_denorm = b->CreateICmpULE(as_int32, i32_const(0x400000));
880-
llvm::Value* is_nan =
881-
b->CreateOr(b->CreateICmpSLE(as_int32, i32_const(0)),
882-
b->CreateICmpSGE(as_int32, i32_const(0x7F400000)));
883945

884-
// Round the value and extract exponent.
885-
llvm::Value* rounded = b->CreateAdd(as_int32, i32_const(0x400000));
886-
llvm::Value* shifted = b->CreateAShr(rounded, 23);
887-
llvm::Value* finite = b->CreateSelect(is_denorm, i32_const(0x00), shifted);
888-
llvm::Value* f32_result = b->CreateSelect(is_nan, i32_const(0xFF), finite);
946+
// Cast the input value to an integer for bitwise manipulation.
947+
// as_int32 = bitcast(f32_value, int)
948+
llvm::Value* as_int32 = b->CreateBitCast(f32_value, b->getInt32Ty());
949+
950+
// Check if the input is zero, negative, overflow, infinity or NaN.
951+
// All of these cases cannot be represented in the E8M0 format.
952+
// is_zero_or_negative = as_int32 <= 0
953+
// is_overflow_or_nan = as_int32 >= 0x1.8p127
954+
// is_nan = is_zero_or_negative | is_overflow_or_nan
955+
llvm::Value* is_zero_or_negative = b->CreateICmpSLE(as_int32, i32_const(0));
956+
llvm::Value* is_overflow_or_nan =
957+
b->CreateICmpSGE(as_int32, i32_const(0x7F400000)); // 1.5 * 2^127
958+
llvm::Value* is_nan = b->CreateOr(is_zero_or_negative, is_overflow_or_nan);
959+
960+
// Check if the input is a denormal which should round to the minimum value
961+
// (2^-127), as there is no zero value.
962+
// is_denorm = as_int32 <= 0x1p-127
963+
llvm::Value* is_denorm =
964+
b->CreateICmpULE(as_int32, i32_const(0x400000)); // 1.0 * 2^-127
965+
966+
// Round the value (always up) and discard the mantissa.
967+
// rounded = as_int32 + 0x1p-127
968+
// f8_normal = as_int32 >> f32_mantissa
969+
llvm::Value* rounded =
970+
b->CreateAdd(as_int32, i32_const(0x400000)); // 1.0 * 2^-127
971+
llvm::Value* f8_normal = b->CreateAShr(rounded, 23);
972+
973+
// Chain of selects that handles the special cases.
974+
// f8_result = is_nan ? 0xFF : (is_denorm ? 0x00 : f8_normal)
975+
llvm::Value* f8_result =
976+
b->CreateSelect(is_nan, i32_const(0xFF),
977+
b->CreateSelect(is_denorm, i32_const(0x00), f8_normal));
889978

890979
// Truncate to the result type.
891-
return b->CreateTrunc(f32_result, b->getInt8Ty());
980+
return b->CreateTrunc(f8_result, b->getInt8Ty());
892981
}
893982

894983
llvm::Value* EmitF8e8m0fnuToF32(llvm::Value* f8_value, llvm::IRBuilder<>* b) {
895-
// Shift exponent to the left for the normal case.
896-
llvm::Value* as_int32 = b->CreateZExt(f8_value, b->getInt32Ty());
897-
llvm::Value* shifted = b->CreateShl(as_int32, 23);
898-
899-
// Special values for 0x00 (denorm) and 0xFF (NaN).
900984
auto i32_const = [&](int val) {
901985
return llvm::ConstantInt::get(b->getInt32Ty(), val);
902986
};
987+
988+
// The input value is a 8-bit integer, extend it to 32-bit integer.
989+
// as_int32 = bitcast(f8_value, int)
990+
llvm::Value* as_int32 = b->CreateZExt(f8_value, b->getInt32Ty());
991+
992+
// Check if the input is a denormal or NaN.
993+
// is_zero = as_int32 == 0x00
994+
// is_nan = as_int32 == 0xFF
903995
llvm::Value* is_zero = b->CreateICmpEQ(as_int32, i32_const(0));
904996
llvm::Value* is_nan = b->CreateICmpEQ(as_int32, i32_const(0xFF));
905-
llvm::Value* denorm_or_shifted =
906-
b->CreateSelect(is_zero, i32_const(0x00400000), shifted);
907-
llvm::Value* f32_result =
908-
b->CreateSelect(is_nan, i32_const(0x7FC00000), denorm_or_shifted);
909997

998+
// Shift exponent to the left for the normal case.
999+
// f32_normal = as_int32 << mantissa_diff
1000+
llvm::Value* f32_normal = b->CreateShl(as_int32, 23);
1001+
1002+
// Chain of selects that handles the special cases.
1003+
// f32_result = is_nan ? 0x7FC00000 : (is_zero ? 0x1p-127 : f32_normal)
1004+
llvm::Value* f32_result = b->CreateSelect(
1005+
is_nan, i32_const(0x7FC00000),
1006+
b->CreateSelect(is_zero, i32_const(0x400000), f32_normal));
1007+
1008+
// Bitcast integer bits to the result type.
9101009
return b->CreateBitCast(f32_result, b->getFloatTy());
9111010
}
9121011

0 commit comments

Comments
 (0)