Skip to content

Found that exp2f_rcp has a precision-handling issue when dealing with 0xfe value #2408

Closed

Description

@Monekyzoon

TransformerEngine 2.0:
https://github.com/NVIDIA/TransformerEngine/blob/v2.0/transformer_engine/common/utils.cuh#L967

// TransformerEngine 2.0 Implement
__device__ __forceinline__ float exp2f_rcp(e8m0_t biased_exp) {
  return (biased_exp == 0) ? 1 : exp2f(FP32_EXPONENT_BIAS - static_cast<float>(biased_exp));
}

TransformerEngine 2.9:
https://github.com/NVIDIA/TransformerEngine/blob/v2.9/transformer_engine/common/util/ptx.cuh#L249

// TransformerEngine 2.9 Implement
__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)
}

Activity

  1. added
    bug
    on Nov 21, 2025
  2. Monekyzoon commented on Nov 21, 2025

    @Monekyzoon
    Author

    (1) When the TE version is v2.0, if the value of biased_exp is 0xFE, the return value is 2^-127.

    (2) When the TE version is v2.9, if the value of biased_exp is 0xFE, the return value is 0.

    Which of these two TE versions is the expected behavior?

  3. Monekyzoon commented on Nov 21, 2025

    @Monekyzoon
    Author

    Another question I have is, why do we directly return 1 when biased_exp == 0? What is the reason behind this? I would really appreciate it if you could help answer this question.

  4. Monekyzoon commented on Nov 21, 2025

    @Monekyzoon
    Author

    @timmoon10 @ptrendx can you have a look?thanks

  5. self-assigned this
    on Dec 1, 2025
  6. assigned and unassigned on Jan 9, 2026
  7. kainzhong commented on Jan 13, 2026

    @kainzhong
    Collaborator

    Hi @Monekyzoon. I think v2.9 has the expected behavior. That's because in function float_to_e8m0 we return 0xFE when input is inf, so it makes more sense to just return 0 rather than 2^-127 since 0xFE doesn't really represent 2^127 but instead infinity here.

    As for your second question, when biased_exp is 0, since we only keep the exponential part when encoding it to e8m0, we can't restore the mantissa part, and the original float scale factor could be anything between 0 to 2^-127 (these won't be rounded up to exp=0x1), which means the reciprocal could be very large or even inf when the scale factor is really small. I think returning 1 is a reasonable choice here.

    I can ask the numeric team if you still have questions about why we decided to compute this way. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions