[Bug] GroupNorm kernels overflow int32 sample offsets (`batch_idx * X_row_stride`) past 2^31 elements
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):
_group_norm_forward_kernelL53, L56-L57, L97-L98_group_norm_backward_kernelL132, L136-L138, L141-L142
(#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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from linkedin/Liger-Kernel
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
linkedin/Liger-Kernel#1693 ·
Maintainers usually reply within 3 days
-
[Bug] LigerGroupNormFunction.backward crashes on any 4D (N, C, H, W) tensor (H > 1) and hardcodes tl.bfloat16 for float16 inputsPossibly taken @shotsan claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
linkedin/Liger-Kernel#1676 · 2 comments ·
Maintainers usually reply within 3 days
-
[Bug] LigerFusedGateUpSiLUMulFunction.backward crashes with shape mismatch on any 3D (B, T, 2*H) input tensorPossibly taken @shotsan claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
linkedin/Liger-Kernel#1675 · 1 comment ·
Maintainers usually reply within 3 days
-
[Bug] Silent backward gradient corruption in LigerCrossEntropy on non-contiguous logits (returns raw input logits as dL/dlogits)Possibly taken @shotsan claimed this 5 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
linkedin/Liger-Kernel#1674 ·
Maintainers usually reply within 3 days
-
GroupNorm: int32 batch offset overflows once a sample starts past 2**31 elementsPossibly taken @0z5a claimed this 7 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
linkedin/Liger-Kernel#1667 ·
Maintainers usually reply within 3 days
All issues in linkedin/Liger-Kernel
Similar issues
-
day_or=True union silently becomes the ISC cron AND bug when a day field expands to its full range (e.g. '0 0 */1 * */2' fires only Sun/Tue/Thu/Sat)Possibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
pallets-eco/croniter#309 ·
Maintainers usually reply within 1 day
-
[Bug]: MLX text downloads omit the requested Hub token and cache directoryPossibly taken @abhijithneilabraham claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
vllm-project/vllm-metal#1423 ·
Maintainers usually reply within 1 day
-
Growl: a server-rejected notification is reported as a successPossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
gke-labs/kube-agents#7441 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100