[Bug] apply_rotary_pos_emb: leaves output tails unwritten with partial rotation

Aperta Adatta ai principianti
#5,156 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@shotsan ci sta già lavorando.

Dal 18/11/2026.

  • #5165 di @shotsan — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch

Direzione di ricerca

Inizia da lmdeploy/pytorch/kernels/cuda/apply_rotary_pos_emb.py, alle righe del launcher collegate nell’issue, poi esamina l’intervallo di scrittura dell’output del kernel e la chiamata al backend CUDA con inplace=False. Esegui la riproduzione fornita nella modalità interprete di Triton; il lavoro è concluso quando gli output separati di query e key copiano le colonne di input successive all’ampiezza di rotazione e nessuna delle due code contiene NaN.

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

Descrizione

Describe the bug

apply_rotary_pos_emb supports a rotation width smaller than the head width. If its output tensors are separate from the inputs, it leaves the remaining columns unwritten. The reproduction uses head width 64 and rotation width 32. It initializes the output buffers with NaN and uses an identity rotation. The first 32 columns are correct. All 256 query-tail elements and 128 key-tail elements remain NaN.

The launcher allocates outputs with torch.empty_like and selects half_size from the smaller rotation width. The kernel writes only the rotated columns. It never copies the tail from the inputs.

The default kernel call allocates separate outputs. The CUDA backend also reaches this case when inplace=False. Model calls that reuse the input buffers retain the original tail. The cited kernel lines are unchanged on main as of 2026-11-17.

Reproduce
import torch
from lmdeploy.pytorch.kernels.cuda.apply_rotary_pos_emb import apply_rotary_pos_emb

dev = "cuda" if torch.cuda.is_available() else "cpu"
torch.set_default_device(dev)
torch.manual_seed(0)
S, H, D, R = 4, 2, 64, 32
q = torch.randn(S, H, D, dtype=torch.float16)
k = torch.randn(S, 1, D, dtype=torch.float16)
cos = torch.ones(S, R, dtype=torch.float16)
sin = torch.zeros_like(cos)
qo, ko = torch.full_like(q, float("nan")), torch.full_like(k, float("nan"))
apply_rotary_pos_emb(q, k, cos, sin, qo, ko)
print("Rotated query columns equal input:", torch.equal(qo[..., :R], q[..., :R]))
print("Unwritten query tail elements:", torch.isnan(qo[..., R:]).sum().item())
print("Unwritten key tail elements:", torch.isnan(ko[..., R:]).sum().item())
print("Expected unwritten elements: 0; both outputs equal their inputs.")
assert torch.equal(qo, q) and torch.equal(ko, k), "Partial rotary embedding does not copy the output tail."

Output:

Rotated query columns equal input: True
Unwritten query tail elements: 256
Unwritten key tail elements: 128
Expected unwritten elements: 0; both outputs equal their inputs.
AssertionError: Partial rotary embedding does not copy the output tail.
Expected behavior

Columns after the rotation width must equal the input columns. With cos = 1 and sin = 0, both full outputs must equal their inputs. No output element must retain the NaN initialization.

Suggested fix

When the output does not alias the input, copy the columns after the rotation width into the output. Apply this to both query and key outputs, including the default allocations.

Versions / environment

Library commit: fc33f69. Source checked: 2026-11-17.
torch 2.14.1, triton 3.8.0.
Reproduced with Triton's interpreter mode (TRITON_INTERPRET=1).
The CUDA kernel has the same store range. It also leaves the tail unwritten when the output does not alias the input.

Lingua principale
Python
Stelle
8.2k
Fork
797
Merge medio
3g 15h
PR unite (30g)
52

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 InternLM/lmdeploy

Tutte le issue di InternLM/lmdeploy

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.