Skip to content

Qwen3 decode: size the RoPE read by the explicit position, not only the KV length - #14156

Merged
danielhanchen merged 3 commits into
mainfrom
dh/v2-qwen3-decode-offset-rope
Oct 19, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/v2-qwen3-decode-offset-rope

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

#13855 made the RoPE cache lazy: it only builds the rows that are asked for. Qwen3Attention_fast_forward_inference sized the cache read by the KV length only. A decode step at an explicit position past the KV length therefore indexed past the built rows and hit a CUDA device-side assert. For example: a prefill without position_ids, then a hand-made decode at position >= 32768. Base (before #13855) returned finite logits. Normal generate is not affected.

The model-level decode loop already computes rotary_seq_len = max(kv_seq_len, position_ids.max() + 1) once per step and passes it to every layer. Qwen3 attention now sizes the RoPE extension and read from it, so this adds no host sync. Without rotary_seq_len the previous sizing is used.

Tests

  • New tests/test_qwen3_decode_offset_rope.py (tiny Qwen3, GPU): decode at an explicit position past the lazily built rows. It fails on main with the device-side assert and passes here.

Not fixed here: Gemma2, Cohere, Granite and FalconH1 read the cache the same way. Their model-level loops do not compute rotary_seq_len, so a fix there needs one host sync per decode step. Those are left for a follow-up.

…he KV length

The RoPE cache builds only the rows a forward asks for, so after a prefill without
position_ids a decode step at a position past those rows indexed out of range (CUDA device
assert). Use the rotary_seq_len the shared decode loop already computes once per step, as
the Llama decode path does; no extra host sync.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-19T00:55:40.336977Z dd75104 New commits
🔒 Security Review ⚠️ Failed 2026-10-18T20:23:40.225033Z a077fd4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Oct 18, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 7b96ef74bb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 3697c7d into main Oct 19, 2026
@danielhanchen
danielhanchen deleted the dh/v2-qwen3-decode-offset-rope branch October 19, 2026 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant