Repository navigation
fix(compile): check for an active trace before reading dynamo's frame counter (#5266) - #5283
Merged
Merged
Conversation
… counter (#5266) #5240 made `_CompiledModelForward._release_trace_garbage` skip `gc.collect()` while an outer `torch.compile` traces the call, but it read `counters["frames"]["total"]` first. Dynamo traced that read and installed a guard on the counter mapping; every later compile mutates the mapping, so the guard failed on the next call and every static-cache `generate()` after `optimize()` re-traced a frame forever. Check `dynamo_is_compiling()` first, so a traced call never reads the counter and never guards it. The eager path is unchanged: it still collects once per newly traced frame, which is the #5020 contract. VPTQ_TORCH, 2-layer Qwen2.5-0.5B, static-cache generate(), 12 calls after optimize(): steady state 2.347 s -> 0.038 s per call (61x), new dynamo frames per call 1 -> 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5266.
Problem
#5240 (fix of #5224) made
_CompiledModelForward._release_trace_garbageskipgc.collect()while an outertorch.compiletraces the call — but it read dynamo's global frame counter before that check:Transformers' static-cache
generate()compiles the model call, and that trace runs throughoptimize()'s proxy. Dynamo therefore traced the counter read and installed a guard on thecounters["frames"]mapping. Every later compile mutates that mapping, so the guard failed on the very next call: eachgenerate()re-traced a frame and the compiled static decode never reached steady state (2.7 s vs 0.045 s per call).flowchart TD subgraph before["Before: counter read is traced"] A1["compiled generate()"] --> B1["_release_trace_garbage()"] B1 --> C1["read counters['frames']['total']"] C1 --> D1["dynamo guards the counter mapping"] D1 --> E1["any later compile mutates it"] E1 --> F1["guard fails -> re-trace every call<br/>2.35 s/call, 1 new frame each"] end subgraph after["After: trace check first"] A2["compiled generate()"] --> B2["_release_trace_garbage()"] B2 --> C2["dynamo_is_compiling() -> return"] C2 --> D2["counter never read, never guarded"] D2 --> E2["cached graph reused<br/>0.038 s/call, 0 new frames"] endFix
Swap the two checks: return on an active trace before touching the counter. The eager path is unchanged — it still collects once per newly traced frame, which is the #5020 contract, and the deferred release still happens on the next eager call.
Evidence
VPTQ_TORCH, 2-layer Qwen2.5-0.5B-Instruct, 4 prompts x 32 greedy tokens,
cache_implementation="static", 12 calls afteroptimize()(RTX 5090, torch 2.15):main0aa2d4b4861x faster steady state, matching the 0.045 s the issue predicted from the order swap.
New
tests/test_compiled_forward_no_retrace_5266.py(CPU-only, no model):test_counter_is_not_read_while_tracing— a spy ondynamo_traced_framesmust record no read whiletorch.compile(fullgraph=True)traces through the proxy.test_counter_mutation_does_not_invalidate_the_proxy_call— mutatingcounters["frames"](what a later compile does) must not add aunique_graphsentry on the next call.Both fail on pristine
main(2 failed) and pass with the fix. Related suites:test_optimize_dynamo_release_5020.py+ new file 4 passed, 3 skipped;test_compile_nested_suppression_5224.py+test_compile_load_race_5209.py11 passed;uvx [email protected] check gptqmodel docs/eora tests setup.pyclean.Not verified: ROCm; a full transformers
generate()run inside the test suite (the repro above is the end-to-end evidence, the tests pin the mechanism).