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

Aperta Adatta ai principianti
#1,676 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

@shotsan ci sta già lavorando.

Dal 24/11/2026.

  • #1682 di @shotsan — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch

Direzione di ricerca

Leggi group_norm_backward in liger_kernel/ops/group_norm.py:242-255 e confrontalo con il modo in cui forward appiattisce le dimensioni spaziali. Esegui il reproducer pubblicato su una GPU per vedere il crash su DX.view(*shape), poi correggi hidden_size in modo che copra tutte le dimensioni spaziali (ad esempio math.prod(shape[2:])) e mappa torch.float16 su tl.float16 invece di fissare tl.bfloat16. È fatto quando il backward 4D restituisce gradienti (N, C, H, W) che corrispondono a F.group_norm e i gradienti fp16 di peso e bias non vengono più convertiti tramite bfloat16.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
6.7k
Fork
673
Merge medio
4g 20h
PR unite (30g)
19

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di linkedin/Liger-Kernel

Tutte le issue di linkedin/Liger-Kernel

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.