Skip to content

fix(group_norm): use matched Triton dtype for float16 weight and bias gradients (#1676) - #1682

Open
shotsan wants to merge 1 commit into
linkedin:mainfrom
shotsan:fix-group-norm-float16-backward-dtype
Open

shotsan wants to merge 1 commit into
linkedin:mainfrom
shotsan:fix-group-norm-float16-backward-dtype

Conversation

@shotsan

@shotsan shotsan commented Nov 24, 2026

Copy link
Copy Markdown

Summary

Closes #1676.

In group_norm_backward (liger_kernel/ops/group_norm.py:258), the kernel launches with:

triton_dtype = tl.float32 if X.dtype == torch.float32 else tl.bfloat16

This hardcoded tl.bfloat16 for all non-fp32 tensors, including torch.float16. When inputs and affine weights are in torch.float16, the Triton backward kernel cast accumulated dW and dB through tl.bfloat16 before atomic-adding to the float16 DW and DB tensors. 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

  • In src/liger_kernel/ops/group_norm.py:
    • Import torch_to_triton_dtype from liger_kernel.ops.utils, matching the pattern used in rms_norm.py, fused_add_rms_norm.py, and modulated_rms_norm.py.
    • Derive triton_dtype using torch_to_triton_dtype.get(W.dtype, tl.bfloat16), ensuring torch.float16 maps to tl.float16.
  • In test/transformers/test_group_norm.py:
    • Added test_liger_group_norm_low_precision_backward covering both torch.float16 and torch.bfloat16 across spatial and 3D shapes, asserting gradient parity against torch.nn.GroupNorm.

Testing

  • Tested on CUDA: verified float16 gradient error vs PyTorch reference is halved.
  • Full test suite in test/transformers/test_group_norm.py passes with 15/15 tests green.
  • Clean ruff check.

… 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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] LigerGroupNormFunction.backward crashes on any 4D (N, C, H, W) tensor (H > 1) and hardcodes tl.bfloat16 for float16 inputs

1 participant