[Bug] GroupNorm kernels overflow int32 sample offsets (`batch_idx * X_row_stride`) past 2^31 elements

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

I maintainer di solito rispondono entro 3 giorni

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Leggi src/liger_kernel/ops/group_norm.py, concentrandoti sui calcoli degli offset in _group_norm_forward_kernel e _group_norm_backward_kernel. Riproduci il caso N=65 se è disponibile memoria GPU sufficiente, quindi verifica che entrambi i wrapper gestiscano offset oltre 2^31 elementi senza accessi illegali alla memoria. L’ampliamento di batch_idx proposto nell’issue indica una modifica specifica da verificare; conferma anche che il caso N=64 continui a funzionare.

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

Descrizione

🐛 Describe the bug
🐛 Describe the bug

_group_norm_forward_kernel and _group_norm_backward_kernel in group_norm.py move to their sample with batch_idx * X_row_stride. batch_idx is tl.program_id(0) and X_row_stride is passed as an i32 (it is one sample's size, below 2^31), so the product is int32 (mul.lo.s32 in the PTX). The wrappers pass the strides of the contiguous (N, C, *) input, so X_row_stride = C * H * W. The first sample whose offset reaches 2^31 elements (batch_idx * C * H * W >= 2**31) wraps to a negative offset, and the kernel loads X and stores Y (forward), or loads X and dY and stores dX (backward), before the tensors. In practice this is an illegal memory access.

LigerGroupNormFunction reaches both kernels whenever the input has more than 2^31 elements and one sample has fewer (a sample of 2^31 or more elements makes Triton pass the stride as i64, and the product is then 64-bit). Nothing on the host side rejects such a shape. For example, N = 65, C = 512, H = W = 256 is enough (4.3 GB in bf16).

This is the same class of 32-bit offset arithmetic as #804, #1483 and #1659 (program_id * stride), and it is still the same on current main (85a9c40):

(#1573 is a different overflow in the same file: the loop counter of range(0, hidden_size, BLOCK_SIZE) near 2^31.)

Reproduce
Reproduce

The module's own wrappers on one (N, 512, 256, 256) input, 2**25 elements per sample, so 2**31 elements is N = 64. To fit the input and the output in 8 GB, the tensors are int8 (the offsets are in elements, so bf16 overflows at the same N, with 4.3 GB per tensor). Every sample is the same, so every sample's output must equal sample 0's:

import torch
from liger_kernel.ops.group_norm import group_norm_backward, group_norm_forward

N, C, G, H, W = 65, 512, 32, 256, 256   # N * C * H * W = 2**31 + 2**25
x0 = torch.randint(-3, 4, (C, H, W), device="cuda", dtype=torch.int8)
X = x0.expand(N, C, H, W).contiguous()
w = torch.randn(C, device="cuda", dtype=torch.bfloat16)
b = torch.randn(C, device="cuda", dtype=torch.bfloat16)

Y, _, Mean, RSTD, _ = group_norm_forward(X, C, G, w, b, 1e-6)
torch.cuda.synchronize()
# torch.AcceleratorError: CUDA error: an illegal memory access was encountered

# backward, in its own process: the same Mean (0) and RSTD (1) for every sample, dY = X
Mean = torch.zeros(N, G, device="cuda", dtype=torch.int8)
RSTD = torch.ones(N, G, device="cuda", dtype=torch.int8)
DX, DW, DB = group_norm_backward(X, X, w, b, Mean, RSTD, C, G)
torch.cuda.synchronize()
# torch.AcceleratorError: CUDA error: an illegal memory access was encountered

With N = 64 (exactly 2**31 elements, the last sample's offset 2**31 - 2**25) both wrappers return the same output for every sample. Each case ran in its own process (repro.py fwd|bwd N):

wrapper N = 64 N = 65 N = 65, batch_idx int64
group_norm_forward (_group_norm_forward_kernel) ✅ 0 of 64 samples differ ❌ illegal memory access ✅ 0 of 65 samples differ
group_norm_backward (_group_norm_backward_kernel) ✅ 0 of 64 samples differ ❌ illegal memory access ✅ 0 of 65 samples differ

The fix is to widen batch_idx before the multiply, in both kernels:

-    batch_idx = tl.program_id(0)
+    batch_idx = tl.program_id(0).to(tl.int64)

The patch is attached. The other offsets stay int32 and are below one sample's size: group_idx * X_col_stride and channel_idx * X_col_stride (forward and backward), and group_idx * Mean_col_stride.

Versions
Versions

torch 2.9.1+cu128, triton 3.5.1, CUDA 12.8, NVIDIA GeForce RTX 4060 Ti (8 GB), driver 595.91.07. Liger-Kernel main @ 85a9c40.

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.