Repository navigation
Block compile: per-region recompile limit with isolate_recompiles on torch 2.13+ - #15157
Conversation
…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.
|
@codex review |
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. |
|
Codex Review: Didn't find any major issues. Hooray! 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". |
…off compile the same limit [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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". |
[skip ci]
Block compile: per-region recompile limit (torch 2.13+)
Each compiled decoder block (
unsloth/models/_block_compile.py) shared the process-wide dynamorecompile_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_limitalso 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 whentorch.compileaccepts both keywords (checked once withinspect.signature):recompile_limit = max(8 * kinds + 8, 16), wherekindsis the number of layer kinds of that class, i.e. twice the 4-per-kind budget_raise_cache_limitalready assumed, plus slack.UNSLOTH_BLOCK_COMPILE_RECOMPILE_LIMIToverrides it._raise_cache_limitno longer runs on 2.13+, so the global limit is left alone.torch.compilecall and the global bump stay exactly as they were.Past the limit, dynamo warns and runs that region eager. Without
fail_on_recompile_limit_hitit does not raise, even under thesuppress_errors=Falsepatch in the first-call path, so_wrapdoes 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: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.compileof 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):
New test
test_recompile_storm_stops_at_the_region_limitintests/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_tracefails on both, because it needs a CUDA device and this host has none.Not covered
fail_on_recompile_limit_hit, a storm block now raises at 16 instead of at 1024.Follow-up (df4a21e)
max(16 * kinds + 16, 32)(32 for one kind, 48 for two).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 sameisolate_recompiles/recompile_limit(new testtest_graphs_off_compile_gets_the_same_region_limit, fails without it).