Liger-Kernel icon indicating copy to clipboard operation
Liger-Kernel copied to clipboard

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

Open ttyang57 opened this issue 2 weeks ago • 2 comments

Description

LigerGroupNormFunction has two bugs in group_norm_backward (liger_kernel/ops/group_norm.py:242-255):

  1. 4D backward truncation crash: On any standard 4D (N, C, H, W) tensor with H > 1, forward succeeds and matches F.group_norm, while .backward() crashes with RuntimeError: shape '[2, 16, 8, 8]' is invalid for input of size 256.
  2. Hardcoded tl.bfloat16 for float16 inputs: Line 255 sets triton_dtype = tl.float32 if X.dtype == torch.float32 else tl.bfloat16, silently casting torch.float16 weight/bias gradients through bfloat16 (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 L4 sm_89 with torch==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.

ttyang57 avatar Nov 20 '26 12:11 ttyang57

Thanks for the report. Following up on both parts against current main:

  1. 4D backward crash — this looks fixed on main already by #1376 (group_norm_backward now computes hidden_size after flattening, so (N, C, H, W) inputs work). It reproduces on the 0.8.3 release you tested.

  2. 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, fp16 LigerGroupNorm weight/bias grads vs an fp32 torch.nn.GroupNorm reference:

    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/DB in fp32 and casting to the parameter dtype once at the end removes the bf16 rounding. The remaining error comes from the fp16 tl.sum reductions 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 the DW/DB change ready (existing test_group_norm.py passes) and am happy to send it as a follow-up after #1608 lands, or @bhargavchintam could fold it into #1608 if that's simpler.

ankitgala007 avatar Nov 21 '26 16:11 ankitgala007

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.

bhargavchintam avatar Nov 22 '26 02:11 bhargavchintam