Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions tests/cpp/test_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,14 @@ inline fp8e8m0 float_to_e8m0(float val) {
}

inline float exp2f_rcp(fp8e8m0 biased_exp) {
if (biased_exp == 0) {
return 1.0f;
int32_t int_val = 0;
if (biased_exp == 255) {
int_val = 0x7fffffff;
} else if (biased_exp == 254) {
int_val = 0x00400000;
} else {
int_val = (254 - biased_exp) << FP32_MANTISSA_BITS; // 127 - (biased_exp - 127)
}
int32_t int_val = (254 - biased_exp) << FP32_MANTISSA_BITS; // 127 - (biased_exp - 127)
float fp32_val = *reinterpret_cast<float*>(&int_val);
return fp32_val;
}
Expand Down
10 changes: 7 additions & 3 deletions transformer_engine/common/util/ptx.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,13 @@ constexpr uint32_t FP32_MANTISSA_BITS = 23;
constexpr uint32_t FP32_EXPONENT_BIAS = 127;

__device__ __forceinline__ float exp2f_rcp(e8m0_t biased_exp) {
return (biased_exp == 0) ? 1
: __int_as_float((254 - biased_exp)
<< FP32_MANTISSA_BITS); // 127 - (biased_exp - 127)
// Handle the special case of NaN.
if (biased_exp == 255) return __int_as_float(0x7fffffff);
// Handle the special case where the unbiased exponent is 127, so the reciprocal is 2^-127 which needs the first bit of
// the mantissa to be 1, which can't be obtained by shifting `FP32_MANTISSA_BITS` bits to the left.
if (biased_exp == 254) return __int_as_float(0x00400000);
// Fast calculation when the unbiased exp is in [-126, 126], and only the exponent part is used to express the reciprocal.
return __int_as_float((254 - biased_exp) << FP32_MANTISSA_BITS);
}

__device__ __forceinline__ float exp2f(e8m0_t biased_exp) {
Expand Down
Loading