Skip to content

fix(compile): check for an active trace before reading dynamo's frame counter (#5266) - #5283

Merged
Qubitium merged 1 commit into
mainfrom
fix/5266-release-garbage-order
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5266-release-garbage-order

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #5266.

Problem

#5240 (fix of #5224) made _CompiledModelForward._release_trace_garbage skip gc.collect() while an outer torch.compile traces the call — but it read dynamo's global frame counter before that check:

traced = dynamo_traced_frames()          # reads counters["frames"]["total"]
if traced == self._traced_frames:
    return
if dynamo_is_compiling():
    return

Transformers' static-cache generate() compiles the model call, and that trace runs through optimize()'s proxy. Dynamo therefore traced the counter read and installed a guard on the counters["frames"] mapping. Every later compile mutates that mapping, so the guard failed on the very next call: each generate() 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"]
    end
Loading

Fix

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 after optimize() (RTX 5090, torch 2.15):

tree call 1 steady state (last 4) new dynamo frames/call
main 0aa2d4b48 2.59 s 2.347 s 1
this PR 0.45 s 0.038 s 0

61x 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 on dynamo_traced_frames must record no read while torch.compile(fullgraph=True) traces through the proxy.
  • test_counter_mutation_does_not_invalidate_the_proxy_call — mutating counters["frames"] (what a later compile does) must not add a unique_graphs entry 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.py 11 passed; uvx [email protected] check gptqmodel docs/eora tests setup.py clean.

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).

… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant