[Bug] Qwen2-VL M-RoPE kernel overflows the int32 cos/sin section offset (`bs * sl * hd`) at 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
- Domain
- backend, performance
Research direction
Start by reading src/liger_kernel/ops/qwen2vl_mrope.py, especially _triton_qwen2vl_mrope and the forward and backward wrappers. Check how the section offset is calculated and compare it with the int64 calculation provided in the issue; the reproduction scripts and reported boundary cases show how to verify the overflow. Done means both wrappers avoid the illegal access at the reported size and the reproductions match the reference.
Written by the indexing model from the issue text.
Description
🐛 Describe the bug
🐛 Describe the bug
_triton_qwen2vl_mrope in qwen2vl_mrope.py finds the height and width sections of cos and sin, shape (3, bs, sl, hd), at t_cos + bs * sl * hd and h_cos + bs * sl * hd. bs and hd are constexprs and sl is passed as an i32, so the product is int32 (a 32-bit shl/mul.lo of sl in the PTX, then sign-extended). pid is already widened to int64, so the per-token offsets pid * hd and pid * (n_qh * hd) are fine; the section offset is not. Once one section reaches 2^31 elements (bs * sl * hd >= 2**31), the offset wraps to a negative value and every program loads the height and width entries of cos and sin from before the tensors. In practice this is an illegal memory access.
LigerQwen2VLMRopeFunction reaches the kernel in both forward (qwen2vl_mrope_forward) and backward (qwen2vl_mrope_backward), and neither wrapper checks the shape. At hd = 128 it takes bs * sl >= 2**24 tokens in one call (for example bs = 1 and a 16,777,216-token sequence, or bs = 64 at 262,144 tokens). This is unlikely to happen in practice: cos and sin alone are then 12 GiB each in bf16. Still, when it does happen the result is an out-of-bounds access rather than an error. It is the same class of 32-bit offset arithmetic as #1570, #1573 and #1659, and it is still the same on current main (85a9c40):
Reproduce
Reproduce
bs = 1, hd = 128, mrope_section = [16, 24, 24], fp32: sl = 2**24 - 1 puts the height section at 2**31 - 128 elements and sl = 2**24 at 2**31. cos and sin are then 24 GiB each, more than the 8 GB GPU, so the reproduction maps each onto one 1 GiB block of device memory over and over with CUDA's virtual memory API (element j reads element j mod 2**28), with 16 GiB of unmapped address space before it, so a load before the tensor faults (vmm_alias.cpp, repro.py). Two checks, each case in its own process:
- the module's wrapper,
qwen2vl_mrope_forward(q, k, cos, sin, [16, 24, 24]), onq(1, 2, sl, 128)andk(1, 1, sl, 128)mapped the same way (their values not checked); - the kernel launched as the wrapper launches it but on its first 16 programs (each program is one token and reads only its own row), on 16 tokens of
qandkin their own memory, checked againstq1 * cos - q2 * sin,q2 * cos + q1 * sinwith thecosandsinentries each token should read.
| check | sl = 2**24 - 1 |
sl = 2**24 |
sl = 2**24, section offset int64 |
|---|---|---|---|
qwen2vl_mrope_forward |
✅ completes | ❌ illegal memory access | ✅ completes |
| first 16 tokens vs. reference | ✅ 0 of 16 tokens wrong | ❌ illegal memory access | ✅ 0 of 16 tokens wrong |
The fix is to compute the section offset in int64 once:
+ # one section of cos / sin: bs * sl * hd elements, past 2^31 for long sequences
+ section = bs * tl.cast(sl, tl.int64) * hd
t_cos = cos + pid * hd
- h_cos = t_cos + bs * sl * hd
- w_cos = h_cos + bs * sl * hd
+ h_cos = t_cos + section
+ w_cos = h_cos + section
t_sin = sin + pid * hd
- h_sin = t_sin + bs * sl * hd
- w_sin = h_sin + bs * sl * hd
+ h_sin = t_sin + section
+ w_sin = h_sin + section
The patch is attached. tl.cast rather than sl.to(...) because Triton specializes an sl of 1 into a constexpr.
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#1692 ·
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