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

Aperta Adatta ai principianti
#1,693 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, pytorch

Direzione di ricerca

Inizia leggendo src/liger_kernel/ops/qwen2vl_mrope.py, in particolare _triton_qwen2vl_mrope e i wrapper di forward e backward. Verifica come viene calcolato l’offset della sezione e confrontalo con il calcolo int64 fornito nell’issue; gli script di riproduzione e i casi limite segnalati mostrano come verificare l’overflow. Il lavoro è completato quando entrambi i wrapper evitano l’accesso illegale alla dimensione segnalata e le riproduzioni corrispondono al riferimento.

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

Descrizione

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

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.