-
Notifications
You must be signed in to change notification settings - Fork 60
Run torch.special.logit in reduced precision for bf16/f16 inputs #2156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a precision issue with torch.special.logit
for bfloat16 and float16 inputs by modifying the kernel to run computations in reduced precision instead of casting to higher precision (float32). The change ensures consistency between CPU and XPU device results for half-precision floating point types.
- Simplified logit computation to use native input precision instead of accumulate type casting
- Renamed functors for clarity (Logit0Functor → LogitFunctor, Logit1Functor → LogitEpsFunctor)
- Updated parameter names and types to match the new precision-preserving approach
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
scalar_t x_clamped = x < low_ ? low_ : (x > high_ ? high_ : x); | ||
return std::log(x_clamped / (1 - x_clamped)); | ||
} | ||
Logit1Functor(const T_ACC lo, const T_ACC hi) : lo_(lo), hi_(hi) {} | ||
LogitEpsFunctor(const T_ACC low, const T_ACC high) : low_(low), high_(high) {} | ||
|
||
private: | ||
T_ACC lo_; | ||
T_ACC hi_; | ||
scalar_t low_; | ||
scalar_t high_; |
Copilot
AI
Oct 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type mismatch: low_
and high_
are of type scalar_t
but are being compared with x
and assigned from T_ACC
values in the constructor. This could cause precision loss or incorrect comparisons when scalar_t
and T_ACC
differ.
Copilot uses AI. Check for mistakes.
scalar_t x_clamped = x < low_ ? low_ : (x > high_ ? high_ : x); | ||
return std::log(x_clamped / (1 - x_clamped)); | ||
} | ||
Logit1Functor(const T_ACC lo, const T_ACC hi) : lo_(lo), hi_(hi) {} | ||
LogitEpsFunctor(const T_ACC low, const T_ACC high) : low_(low), high_(high) {} | ||
|
||
private: | ||
T_ACC lo_; | ||
T_ACC hi_; | ||
scalar_t low_; | ||
scalar_t high_; |
Copilot
AI
Oct 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Constructor parameters low
and high
are of type T_ACC
but member variables low_
and high_
are of type scalar_t
. This implicit conversion may cause precision loss when T_ACC
has higher precision than scalar_t
.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but we need to come up with the way of handling such discrepancies between CPU and CUDA results in future, and stick to it. Currently CPU gives different results than CUDA for these ops.
Summary
torch.special.logit
for bfloat16 and float16 input runs in higher precision, because input is casted to AccumulateTypeDevice, which is float32 (pytorch/aten/src/ATen/AccumulateType.h). Output is casted back to lower precision, but because intermediate results are in float32, we have different results than CPU. It might affect other tests so I wanted to clarify if this is expected or we should always try to match CPU reference in our kernels.Minimal repro
Results