Skip to content

RoPE: build grown caches on the GPU again, 32768 rows at a time - #14206

Merged
danielhanchen merged 1 commit into
mainfrom
dh/fu-rope-grown-cache
Oct 19, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
dh/fu-rope-grown-cache

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

#13976 made the RoPE cache lazy and built the first cache on CPU, even when the first forward had already grown it past 4 * 8192 rows. That happens on every model with more than 32K positions (max_position_embeddings), e.g. any 128K-context model. Two effects on the default path compared with the last release, which built grown caches on the GPU:

  • cos / sin differ. The CPU build rounds differently: 188 fp16 and 16 bf16 entries differ from the release, some in rows below 2048.
  • The first forward is slower: about 0.15 to 0.5 s at 128K positions and 1.2 to 1.6 s at 1M.

Grown sizes are built on inv_freq's device again, as the eager extend_rope_embedding did; the starting size is still built on CPU, as the eager __init__ did. The build now fills the output 32768 rows at a time, so its fp32 temporaries no longer scale with the cache size. Rows are bit-identical to the one-shot build.

Measured on one B200 (last release / main / this PR):

release main this PR
cos / sin rows vs release (fp16, bf16; 32K / 128K / 1M; with and without position_ids) - differ in 128K and 1M cells bit-identical in all 12 cells
peak build memory at 128K 128 MiB 136.5 MiB
peak build memory at 1M 2084 MiB 1024 MiB 588 MiB
first-forward cache build at 1M ms 1.2 to 1.6 s ms

The larger cache when position_ids are passed (generation prefill, packing) is the out-of-bounds fix from #13976 and is kept; its test still passes. The cache is still built only on the GPU that uses it.

Tests

tests/test_rope_cache_current_device.py: the new test_first_grown_cache_is_computed_on_inv_freq_device fails on main and passes here; it replaces the test that asserted the CPU build. Six chunk-equality tests are added and pass on both. 24 passed.

Not covered here

  • No end-to-end long-context generate or training run; the comparison is at the cache level.
  • torch 2.13 only.

The first lazily built cache was computed on CPU even when the first forward had already grown
the size past 4 * 8192 rows (every 128K+ context model). That took 0.15-1.6 s on the first
forward and rounded cos/sin differently from every earlier release. Grown sizes are computed on
inv_freq's device again, as the eager extend_rope_embedding did, and the starting size stays on
CPU. The build fills the output 32768 rows at a time, so the fp32 temporaries no longer scale
with max_position_embeddings; rows are bit-identical to the one-shot build.
@danielhanchen danielhanchen self-assigned this Oct 19, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 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 ✅ Completed 2026-10-19T06:37:06.884788Z ebe0b45 Manual request
🔒 Security Review ⚠️ Failed 2026-10-19T06:33:47.988708Z ebe0b45 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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: ebe0b457bf

ℹ️ 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 0cf4929 into main Oct 19, 2026
@danielhanchen
danielhanchen deleted the dh/fu-rope-grown-cache branch October 19, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant