-
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.
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.
|
@Silv3S , what's the behavior on CUDA? In general, we should align with CUDA because running CUDA models on XPU is the use case in my mind. |
|
@Silv3S I noticed that community also perceives the inconsistency between CPU and CUDA but there is currently no clear solution. This PR is a temporary solution, right? |
|
@EikanWang Our current kernel is aligned with CUDA, but it feels wrong to execute bf16/fp16 operator in fp32 precision. We lose benefits of low precision by using extra casts and computing it as normal floats. In my opinion proper solution is to either 1) calculate everything in original precision or 2) keep the T_ACC for sub and div but do log in original precision. @CuiYifeng Thanks for pointing to community PR pytorch/pytorch#140252, I wasn't aware that someone else encountered this bug. I think we should align to CPU regarding expected accuracy, so it's not a temporary solution |
Summary
torch.special.logitfor 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