Skip to content

Decode: cover explicit positions past the lazy RoPE cache in Gemma2, Cohere, Granite and FalconH1 - #14199

Merged
danielhanchen merged 2 commits into
mainfrom
dh/fu-decode-rope-offset
Oct 19, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/fu-decode-rope-offset

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

The RoPE cache is built lazily, with only the rows asked for. #13976 fixed Llama's decode loop for explicit position_ids past the KV length (for example after left padding or a prefill without position_ids). Gemma2, Cohere, Granite and FalconH1 have their own decode loops and still read cos / sin at the KV length, so a position past the cached rows hit a CUDA index-out-of-bounds assert.

Each of the four decode loops now computes the rows one step needs once, with a new helper _decode_rotary_seq_len in llama.py: the larger of the KV length and the highest explicit position + 1. It then passes that value to every layer. Attention only extends the cache and reads the larger table when the position is past the KV length; otherwise it makes exactly the same calls as before.

Cost: one .item() host sync per decode step, the same one Llama's loop already pays. It is skipped entirely (zero syncs) when the cache built at load already covers max_position_embeddings, which holds for any model with 32768 or fewer positions, such as Gemma2 (8192). Nothing is computed per layer.

Tests

tests/test_decode_offset_rope_model_loops.py uses tiny models with max_position_embeddings = 40960. It prefills 4 tokens without position_ids, then decodes at position 35000.

Gemma2 Cohere Granite FalconH1 sync helper
main index out of bounds index out of bounds index out of bounds index out of bounds fails
this PR pass pass pass pass pass

For normal decoding, logits are bitwise equal to main:

  • Gemma2: checked through generate at batch 1 and 2, on both the one-sync and the zero-sync path.
  • Cohere, Granite, FalconH1: checked with a direct decode loop.

tests/test_rope_cache_current_device.py needs a one line update for the new argument; all 23 tests in the two files pass on GPU.

Not covered here

  • In practice the crash reaches users only through Gemma2 configs with more than 32768 positions. FastLanguageModel does not route Cohere, Granite or FalconH1 to these decode loops, so they are only reached through their patchers directly.
  • No T4, ROCm, 4-bit or torch.compile graph-break run; torch 2.13 / transformers 5.17 only (FalconH1 also on 4.57.6).

…ohere, Granite, FalconH1

The RoPE cache only builds the rows a forward asks for, so after a prefill without
position_ids on a model whose max_position_embeddings exceeds the 32768-row starting
cache, a decode step at a later explicit position indexed past the built rows and hit
a CUDA device-side assert. Each model decode loop now computes rotary_seq_len once per
step (as LlamaModel_fast_forward_inference does) and passes it to every layer, which
extends and reads the cache only when it runs past the KV length. The host sync is
skipped when the eager cache already holds every supported position.
@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-19T05:49:10.554991Z a703313 Manual request
🔒 Security Review ⚠️ Failed 2026-10-19T05:45:22.156880Z a703313 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. Bravo.

Reviewed commit: a703313c85

ℹ️ 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 7e3e0d9 into main Oct 19, 2026
@danielhanchen
danielhanchen deleted the dh/fu-decode-rope-offset branch October 19, 2026 14:21
danielhanchen added a commit that referenced this pull request Oct 19, 2026
## Summary

The `Core zoo` cells in `consolidated-tests-ci.yml` keep hitting their
25 minute job cap after the tests have already passed. In one cancelled
cell:

- the zoo `pytest -n 4` step ran from 08:41 to 09:03 and passed (3015
passed, 620 skipped),
- the job was then cancelled at 25:17 during `test_apply_fused_lm_head`
and the pip cache save.

The comment above the cap assumed a ~7.6 min zoo suite. It now takes up
to about 23 minutes on a busy runner, so the cap cancels cells that have
already passed. Today that cancelled Core zoo cells on #14152, #14165,
#14188, #14192, #14199 and #14207, and on staging runs. Each one needs a
manual rerun before the PR can merge.

This raises the cap to 35 minutes, the same as `consolidated`. Hung
tests are still bounded: `PYTEST_TIMEOUT: 300` in the step still names
the culprit per test.

## Tests

The workflow-parsing tests (`git grep -l consolidated-tests-ci --
tests`): 158 passed, 3 skipped.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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