Repository navigation
RoPE cache: build cos/sin outside inference_mode so GRPO keeps the compiled decoder block - #14584
Conversation
…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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
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. |
[skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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>
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 (andinv_freq) become inference tensors. Thefirst 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 backwardand every layer falls back to thestock forward for the rest of training:
The cache is now built under
torch.inference_mode(False)+torch.no_grad()when inference mode is on (nochange 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 andQLoRA, 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