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

オープン 初心者向け
#5,156 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@shotsan がすでに取り組んでいます。

2026年11月18日 から。

  • #5165 @shotsan による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, pytorch

調査の方向性

issueでリンクされているlauncherの行にある lmdeploy/pytorch/kernels/cuda/apply_rotary_pos_emb.py から始め、次にカーネルの出力書き込み範囲と inplace=False でのCUDAバックエンド呼び出しを調べます。提供された再現手順をTritonのインタープリターモードで実行します。完了の条件は、queryとkeyの個別の出力が回転幅より後の入力列をコピーし、どちらの末尾にもNaNが含まれないことです。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
8.2k
フォーク
797
平均マージ
3日 15時間
マージ済み PR(30日)
52

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

InternLM/lmdeploy のほかの issue

InternLM/lmdeploy の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。