Skip to content

RoPE cache: build cos/sin outside inference_mode so GRPO keeps the compiled decoder block - #14584

Merged
danielhanchen merged 3 commits into
mainfrom
dh/s2-grpo-compile
Oct 22, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/s2-grpo-compile

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

The per-device RoPE cache is built lazily since #13855. In GRPO without vLLM the first model call is TRL's HF
generate, which runs under torch.inference_mode(), so cos / sin (and inv_freq) become inference tensors. The
first training forward passes them into the compiled decoder block; AOTAutograd cannot save inference tensors for
backward, so the block raises Inference tensors cannot be saved for backward and every layer falls back to the
stock forward for the rest of training:

Unsloth: Qwen3DecoderLayer block compile failed (Inference tensors cannot be saved for backward ...); keeping the stock forward for this layer.

The cache is now built under torch.inference_mode(False) + torch.no_grad() when inference mode is on (no
change otherwise), also for LongRope's short cache copy. Values are bitwise identical.

Tests: 5 new CPU tests in tests/test_rope_cache_current_device.py (4 fail on main). Tiny Qwen3 GRPO LoRA and
QLoRA, tiny Llama GRPO: 0 fallbacks (main: 2), compiled block engaged, per-step grad norms bitwise equal to main,
0 graph breaks, 0 recompiles. SFT unchanged. Qwen3-0.6B GRPO step time on L4 unchanged within noise (generation
dominates).

Not covered

  • GRPO with vLLM (not affected by reasoning: HF generate is not used there); speed on longer sequences / bigger models (at 0.6B / 256 tokens generation is 94% of the step); Gemma / LongRope end to end (unit tests only); transformers 4.57.6 / TRL floor at runtime.

…mpiled decoder block

The lazy per-device cache is first built inside generate's inference_mode in
GRPO without vLLM. The training forward then hands those inference tensors to
the compiled block, AOTAutograd cannot save them for backward, and every
decoder layer fell back to the stock forward.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-22T01:48:13.983632Z 9537c03 Manual request
🔒 Security Review ⚠️ Failed 2026-10-22T01:46:23.125603Z
We couldn’t complete this review. Comment @codex security review to try again.
b0ee1c4 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

The account paying for this security review has reached its Codex usage limits. The payer can check the Codex usage dashboard. For personal accounts, using credits requires enabling “Use credits for security reviews” in Code review settings. If you do not manage the paying account, contact this repository's admins.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 9537c03de1

ℹ️ 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 77e742c into main Oct 22, 2026
@danielhanchen
danielhanchen deleted the dh/s2-grpo-compile branch October 22, 2026 02:39
danielhanchen added a commit to LeoBorcherding/unsloth that referenced this pull request Oct 25, 2026
…inference_mode_enabled under compile) (unslothai#14728)

unslothai#14584 made the lazy per-device RoPE cache build outside
`inference_mode`, so a cache first built during `generate` stays
trainable. It asks `torch.is_inference_mode_enabled()` on every build,
and Dynamo cannot trace that call ("torch.is_inference_mode_enabled() is
not supported"). When a compiled region builds a device's cache first,
the call becomes a graph break, and an error under `fullgraph=True`.
That happens with a `device_map` split, where the outer forward warms
only the first device, or an attention path that reads the cache itself
(Mistral, Qwen3, Cohere, Gemma 2, Falcon-H1). The resumed frames then
break again on `torch.device(...)`.

Fix: `_rope_build_mode` returns a null context while compiling.
Unsloth's compiled decoder blocks only run in training, outside
`inference_mode`, so the escape is not needed there. Eager builds keep
unslothai#14584's behaviour exactly.

Measured on one B200, torch 2.11.0, attention-style block compiled with
`dynamic=True`, cache cold on the first call, lengths 64 / 80 / 96 /
127:

| Tree | default compile | fullgraph=True |
|---|---|---|
| aa0b22b (unslothai#13855, lazy cache) | 0 graph breaks, 2 graphs | pass |
| main | 7 graph breaks, 7 graphs | Unsupported:
is_inference_mode_enabled |
| this branch | 0 graph breaks, 2 graphs | pass |

With the cache already warm, as the outer forward leaves it on
single-GPU runs, main and this branch both trace in one graph per shape
bucket, so single-GPU training is unchanged.

Tests:
-
`tests/test_rope_cache_current_device.py::test_cold_build_under_compile_never_asks_for_inference_mode`
(CPU, Llama / Gemma / LongRoPE): fails on main, passes here. The
existing inference-mode trainability tests still pass.
-
`tests/test_rms_rope_compile.py::test_rope_cache_cold_build_traces_without_graph_breaks`
(GPU, default and fullgraph): a cold build inside a compiled function,
no graph break, matches eager.

---------

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant