[Bug] Qwen2-VL M-RoPE kernel overflows the int32 cos/sin section offset (`bs * sl * hd`) at 2^31 elements

Open Beginner friendly
#1,693 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, pytorch

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]), on q (1, 2, sl, 128) and k (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 q and k in their own memory, checked against q1 * cos - q2 * sin, q2 * cos + q1 * sin with the cos and sin entries 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

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.