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

Open Beginner friendly
#1,676 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

@shotsan is already working on this.

Since Nov 24, 2026.

  • #1682 by @shotsan — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch

Research direction

Read group_norm_backward at liger_kernel/ops/group_norm.py:242-255 and compare it with how forward flattens spatial dims. Run the posted reproducer on a GPU to see the DX.view(*shape) crash, then fix hidden_size to cover all spatial dims (e.g. math.prod(shape[2:])) and map torch.float16 to tl.float16 instead of hardcoding tl.bfloat16. Done means 4D backward returns (N, C, H, W) grads matching F.group_norm and fp16 weight/bias grads are no longer cast through bfloat16.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
6.7k
Forks
673
Avg merge
4d 20h
Merged PRs (30d)
19

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from linkedin/Liger-Kernel

All issues in linkedin/Liger-Kernel

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.