[Bug] LigerGroupNormFunction.backward crashes on any 4D (N, C, H, W) tensor (H > 1) and hardcodes tl.bfloat16 for float16 inputs
Description
LigerGroupNormFunction has two bugs in group_norm_backward (liger_kernel/ops/group_norm.py:242-255):
- 4D backward truncation crash: On any standard 4D
(N, C, H, W)tensor withH > 1,forwardsucceeds and matchesF.group_norm, while.backward()crashes withRuntimeError: shape '[2, 16, 8, 8]' is invalid for input of size 256. - Hardcoded
tl.bfloat16forfloat16inputs: Line 255 setstriton_dtype = tl.float32 if X.dtype == torch.float32 else tl.bfloat16, silently castingtorch.float16weight/bias gradients throughbfloat16(7-bit mantissa instead of 10-bit).
- Affected Version:
liger-kernel==0.8.3(torch==2.15.0+cu132,triton==3.9.0; also reproduced on Colab L4sm_89withtorch==2.14.1) - Component:
liger_kernel/ops/group_norm.py:242-255(group_norm_backward)
Minimal Reproducer
import torch
import torch.nn.functional as F
from liger_kernel.ops import LigerGroupNormFunction
N, C, H, W, G = 2, 16, 8, 8, 4
x = torch.randn((N, C, H, W), device="cuda", dtype=torch.float32, requires_grad=True)
w = torch.randn((C,), device="cuda", dtype=torch.float32, requires_grad=True)
b = torch.randn((C,), device="cuda", dtype=torch.float32, requires_grad=True)
y = LigerGroupNormFunction.apply(x, w, b, C, G, 1e-5)
y_ref = F.group_norm(x.detach(), G, w.detach(), b.detach(), 1e-5)
print(f"4D Forward PASS: shape={tuple(y.shape)}, fwd_err={(y.detach() - y_ref).abs().max().item():.3e}")
# Crashes with: RuntimeError: shape '[2, 16, 8, 8]' is invalid for input of size 256
y.sum().backward()
Root Cause & Suggested Fix
In group_norm_backward (group_norm.py:243-250), hidden_size = dY.shape[-1] is read before flattening spatial dimensions (dY = dY.view(batch_size, num_groups, -1)), capturing only W (8) instead of H * W (64). Consequently DX is allocated with size B * C * W (256) instead of B * C * H * W (2048), the Triton kernel only processes a truncated slice, and DX.view(*shape) crashes on return.
Computing hidden_size = math.prod(shape[2:]) (or reading it after flattening spatial dimensions) and mapping torch.float16 -> tl.float16 (via torch_to_triton_dtype) fixes both issues.
Thanks for the report. Following up on both parts against current main:
-
4D backward crash — this looks fixed on
mainalready by #1376 (group_norm_backwardnow computeshidden_sizeafter flattening, so(N, C, H, W)inputs work). It reproduces on the 0.8.3 release you tested. -
fp16 weight/bias grads rounded through bf16 — still present on
main(triton_dtype = tl.float32 if X.dtype == torch.float32 else tl.bfloat16). On an L4, fp16LigerGroupNormweight/bias grads vs an fp32torch.nn.GroupNormreference:shape current (bf16 cast) max abs err cast as fp16 / fp32 accumulation (2, 32, 1024) 0.21 0.09 (2, 64, 16, 16) 0.19 0.07 (4, 128, 512) 0.25 0.13 (gradients are ~50–145 here, where the fp16 step is 0.0625 and the bf16 step is 0.5)
Accumulating
DW/DBin fp32 and casting to the parameter dtype once at the end removes the bf16 rounding. The remaining error comes from the fp16tl.sumreductions inside the kernel, which #1608 moves to fp32, so the two changes fit together, and a tight fp16 test only becomes reliable once both are in. I have theDW/DBchange ready (existingtest_group_norm.pypasses) and am happy to send it as a follow-up after #1608 lands, or @bhargavchintam could fold it into #1608 if that's simpler.
Thanks for checking this against main. I'd keep #1608 as it is: it has been waiting for a first review since 11-09, and I left the triton_dtype cast alone there because #1253 already has an open change on that line. Your DW/DB change is on the same cast, so a follow-up from you after #1608 lands sounds right to me. If the maintainers would rather have one PR, I can take it into #1608 instead.