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

Open Beginner friendly
#5,156 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@shotsan is already working on this.

Since Nov 18, 2026.

  • #5165 by @shotsan — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch

Research direction

Start with lmdeploy/pytorch/kernels/cuda/apply_rotary_pos_emb.py at the launcher lines linked in the issue, then inspect the kernel’s output store range and the CUDA backend call with inplace=False. Run the provided reproduction in Triton's interpreter mode; done means separate query and key outputs copy input columns after the rotation width, with no NaNs in either tail.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
8.2k
Forks
797
Avg merge
3d 15h
Merged PRs (30d)
52

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

All issues in InternLM/lmdeploy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.