Repository navigation
Conversation
… gradients (linkedin#1676) - In group_norm_backward, derive triton_dtype using torch_to_triton_dtype from weight dtype rather than hardcoding tl.bfloat16 for all non-fp32 inputs. - Prevents precision truncation and bit mismatches when atomic-adding gradients to float16 weight and bias buffers. - Add regression test in test/transformers/test_group_norm.py validating backward gradient precision in float16 and bfloat16 against torch.nn.GroupNorm.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1676.
In
group_norm_backward(liger_kernel/ops/group_norm.py:258), the kernel launches with:This hardcoded
tl.bfloat16for all non-fp32 tensors, includingtorch.float16. When inputs and affine weights are intorch.float16, the Triton backward kernel cast accumulateddWanddBthroughtl.bfloat16before atomic-adding to the float16DWandDBtensors. This caused loss of 3 bits of mantissa precision (truncating 10-bit float16 mantissa to 7-bit bfloat16) and numerical discrepancy against the PyTorch reference.Changes
src/liger_kernel/ops/group_norm.py:torch_to_triton_dtypefromliger_kernel.ops.utils, matching the pattern used inrms_norm.py,fused_add_rms_norm.py, andmodulated_rms_norm.py.triton_dtypeusingtorch_to_triton_dtype.get(W.dtype, tl.bfloat16), ensuringtorch.float16maps totl.float16.test/transformers/test_group_norm.py:test_liger_group_norm_low_precision_backwardcovering bothtorch.float16andtorch.bfloat16across spatial and 3D shapes, asserting gradient parity againsttorch.nn.GroupNorm.Testing
test/transformers/test_group_norm.pypasses with 15/15 tests green.ruff check.