Skip to content

Block compile: per-region recompile limit with isolate_recompiles on torch 2.13+ - #15157

Merged
danielhanchen merged 6 commits into
mainfrom
dh/s6-block-compile-isolate
Oct 24, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
dh/s6-block-compile-isolate

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Block compile: per-region recompile limit (torch 2.13+)

Each compiled decoder block (unsloth/models/_block_compile.py) shared the process-wide dynamo recompile_limit, which unsloth-zoo raises to 1024. So a block whose guards fail on every call (a guarded attribute or input that changes each step) could compile up to 1024 times before falling back to eager. On a real model each compile takes seconds, which looks like a hang. _raise_cache_limit also changed the global limit for every other compiled function in the process.

torch 2.13 added torch.compile(..., isolate_recompiles=True, recompile_limit=N). This PR uses it when torch.compile accepts both keywords (checked once with inspect.signature):

  • every block class gets its own cache bucket and recompile_limit = max(8 * kinds + 8, 16), where kinds is the number of layer kinds of that class, i.e. twice the 4-per-kind budget _raise_cache_limit already assumed, plus slack. UNSLOTH_BLOCK_COMPILE_RECOMPILE_LIMIT overrides it.
  • _raise_cache_limit no longer runs on 2.13+, so the global limit is left alone.
  • On torch before 2.13 the torch.compile call and the global bump stay exactly as they were.

Past the limit, dynamo warns and runs that region eager. Without fail_on_recompile_limit_hit it does not raise, even under the suppress_errors=False patch in the first-call path, so _wrap does not read a limit hit as a compile failure and demote the layer.

Evidence (CPU, torch 2.13.0)

Storm toy block, global recompile_limit=1024, 40 calls, each with a new guard value:

tree compiles outputs
base (main) 40 (one per call, capped only at 1024) correct
this PR 16 (region limit), then eager correct

Two classes that share one code object: the storm region stops at 16, the other region still compiles (1 entry), nothing is stored in the default bucket, and a user torch.compile of the same function still compiles.

Legitimate entries per region on tiny real models (eager backend, train steps at (bs, L) = (2,16), (2,24), (2,33), (1,40), (3,17), plus a no_grad pass, with reentrant and non-reentrant checkpointing):

model kinds entries (reentrant / non-reentrant) limit
Llama 1 5 / 4 16
Qwen3 1 5 / 4 16
Gemma3 1 5 / 4 16
Gemma2 2 5 / 4 24

New test test_recompile_storm_stops_at_the_region_limit in tests/test_block_compile.py. The CPU block compile suites (test_block_compile.py, test_block_compile_discovery.py, test_legacy_block_compile_install.py, test_fsdp2_native_units.py, utils/test_hybrid_packing_default.py): 152 passed, 6 skipped on both base and head. test_sm_count_is_read_outside_the_trace fails on both, because it needs a CUDA device and this host has none.

Not covered

  • No GPU run. The entry counts above come from the eager backend on CPU. The flex SPLIT retry can double a class's entries on GPU (the limit allows for that), but I did not measure it.
  • If a user turns on fail_on_recompile_limit_hit, a storm block now raises at 16 instead of at 1024.

Follow-up (df4a21e)

  • Measured compiled graphs per block region on GPU for 17 workloads (SFT padded / packed, eval loop, GRPO notebook flow, DPO, vision SFT, QLoRA, GC on / off; Llama, Qwen3, Gemma3, hybrid Qwen3.5) on torch 2.14: one-kind models use at most 4, Gemma3 (2 kinds) SFT 12, DPO 14, GRPO 20 (flat from step 2 to 30). Counts are identical with and without this PR and no region hit its limit; step time on L4 ABBA +2.0% inside a 5.3% A/A spread.
  • Gemma3 GRPO left only 4 graphs of headroom under the first limit, so the default is now max(16 * kinds + 16, 32) (32 for one kind, 48 for two).
  • Main now has the opt-in UNSLOTH_BLOCK_CUDAGRAPHS (Opt-in CUDA graphs for compiled blocks under gradient checkpointing (UNSLOTH_BLOCK_CUDAGRAPHS=1) #15154), which adds a graphs-off compile per block; it now gets the same isolate_recompiles / recompile_limit (new test test_graphs_off_compile_gets_the_same_region_limit, fails without it).
  • torch < 2.13 is unchanged (same compile kwargs and the global limit bump).

…torch 2.13+

Each compiled decoder block now gets its own dynamo cache bucket and a
recompile_limit of max(8 * kinds + 8, 16) (UNSLOTH_BLOCK_COMPILE_RECOMPILE_LIMIT
overrides), so a block whose guards fail every call stops recompiling there and
runs eager instead of compiling up to the global limit of 1024. The global
recompile_limit is no longer raised on torch 2.13+; older torch is unchanged.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 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-24T22:40:43.529869Z df4a21e Manual request
ℹ️ 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

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: b954ba2108

ℹ️ 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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: df4a21e391

ℹ️ 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".

[skip ci]
@danielhanchen
danielhanchen merged commit 5b7953b into main Oct 24, 2026
@danielhanchen
danielhanchen deleted the dh/s6-block-compile-isolate branch October 24, 2026 23:18
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