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

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

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

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

Research direction

Read src/liger_kernel/ops/group_norm.py, focusing on _group_norm_forward_kernel and _group_norm_backward_kernel at the reported offset calculations. Reproduce with the issue’s N=65 case if suitable GPU memory is available, then verify both wrappers handle offsets past 2^31 elements without illegal memory access. The issue’s proposed widening of batch_idx gives a specific change to check; confirm the N=64 case still works as well.

Written by the indexing model from the issue text.

Description

🐛 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.

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.