Repository navigation
fix(compile): let dynamo fall back per frame instead of replaying the forward (#5119) - #5163
Merged
Qubitium merged 2 commits intoNov 18, 2026
Merged
Conversation
… forward (ModelCloud#5119) _install_callable_compile_fallback / _install_module_compile_fallback re-ran the original forward eagerly after a compiler failure. With fullgraph=False dynamo runs compiled fragments plus resume frames, so a failure can arrive after earlier frames already mutated state; the retry applied those side effects twice. The first generate() after optimize() on a GGUF Triton model appended every KV entry a second time and died on the mask length. Enable torch._dynamo.config.suppress_errors for the call so dynamo skips only the failing frame and runs that frame eagerly, restoring the previous flag in a finally block. fullgraph=True keeps its exact eager retry because a single graph has executed nothing when the compiler fails.
…replay-5119 # Conflicts: # gptqmodel/utils/torch.py
Qubitium
marked this pull request as ready for review
November 18, 2026 07:06
This was referenced Nov 18, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 18, 2026
…5240) `torch_compile` installs lazy wrappers, so they can be entered from inside another `torch.compile` trace: transformers compiles a static-cache `generate()` forward, and `VPTQLinear.optimize()` installs one of these wrappers on `dequantize` at post-init. Since #5163 that wrapper wrote `torch._dynamo.config.suppress_errors` for the call. Dynamo config overrides are ContextVars, so the write is an `Unsupported: ContextVar mutation not supported` graph break in the outer trace. Every VPTQ layer then breaks out of the compiled forward, and the cudagraph static-cache decode returns outputs a later partition overwrote (`accessing tensor output of CUDAGraphs that has been overwritten`). Nested calls now stay read-only: when dynamo is already tracing, the wrapper returns the eager callable/module body so it is inlined into the outer graph, mutating no config and taking no `model_global_state_lock`. Top-level non-fullgraph behaviour is unchanged (#5163 still enables suppression for the call and restores it in `finally`); `_call_with_dynamo_suppression` carries the same guard as defence in depth. `_CompiledModelForward._release_trace_garbage` also skipped its `gc.collect()` while dynamo is tracing: `gc.collect` is untraceable, so it was one more graph break per model call on the #5171 path. The frame count is left unchanged so the next eager call does the release. Repro (Qwen2.5-0.5B-Instruct 2-layer VPTQ_TORCH fp16, 4 prompts x 32 greedy, static cache, RTX 5090): before the fix the first no-optimize static generate raised the CUDAGraph overwrite error; after it, no-optimize and three post-`optimize()` runs report 0 graph breaks, 2 unique graphs each, and tokens equal to eager. Tests: tests/test_compile_nested_suppression_5224.py (6 cases; 4 fail without the fix). Existing VPTQ/compile suites unchanged and green.
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.
Summary
optimize()installstorch.compilewith a fallback that, on a compiler failure, re-ran the original forward from the beginning. Withfullgraph=Falsedynamo runs a forward as compiled fragments plus resume frames, so the failure can arrive after earlier frames already mutated state. The retry applied those side effects twice: ingenerate()the layers that ran before the failure held their keys/values twice, and the eager re-run then died on the mask length. On a freshly loaded GGUF Triton model the firstgenerate()afteroptimize()therefore always raised:Fixes #5119
What Changed
gptqmodel/utils/torch.py: the callable and module fallback wrappers no longer replay the forward. They invoke the compiled callable undertorch._dynamo.config.suppress_errors, which makes dynamo skip only the failing frame and run that frame eagerly — nothing executes twice. The previous flag value is restored in afinallyblock, so this stays a per-call switch rather than a process setting.fullgraph=Truekeeps its exact eager retry: a single graph has executed nothing when the compiler fails, so re-running is not a replay. It does not enable suppression.exceptbranch remains as a backstop for failures that happen outside the traced frame (for example dynamo's guardAssertionError, [BUG] Concurrency: parallel quantize() races process-global BLAS/TF32 guards (non-reproducible ckpt), cold concurrent AWQ Triton JIT aborts under PYTHON_GIL=0, TorchLinear compile guard AssertionError during concurrent load #4094); it latches eager execution as before.maininto the branch and kept [BUG] #4140/#4102 (fixes of #4129/#4094) incomplete: TorchLinear's lazy first compile is outside _MODEL_LOAD_LOCK; dynamo's default-dtype save/restore races a concurrent load (PYTHON_GIL=0: load fails 'corrupted state', process default dtype left bf16) #5127'smodel_global_state_lock/state["compiled"]structure intact: suppression now happens inside that wrapper'srun_compiled(), so the first-compile lock and the no-replay guarantee compose.Tests
Red on
main@a978bf18(test copied onto an unmodified worktree):Green on
3af43265:The new test drives the case the old code got wrong: a backend that compiles the first graph and fails on the resume frame, with a side effect recorded in the first graph. It asserts the side effect happened once (
runs == [1]), that the flag is restored, thatfullgraphstill falls back, and that user errors still propagate.Unverified here: the end-to-end
optimize()+generate()run on a real GGUF Triton checkpoint — that needs the CUDA AUTO GGUF kernel and the issue's model. The regression covers the replay mechanism at the wrapper, which is where the issue places the bug.Model Support / Module Plan
Not applicable: no model definitions, aliases, or module plans changed.
AWQ onboarding safety
Not applicable: no AWQ grouping, search, replay, or fold changes.
Logic Flow Documentation
Not applicable: the change replaces "catch and re-run the forward" with "let dynamo fall back for the failing frame". The wrapper's selection order and the eager-latch lifecycle are unchanged.
Review Requirements
suppress_errorsswitch.Kernel Accuracy Requirements
Not applicable: no kernel or fusion changes.
Notes
suppress_errorsis scoped with try/finally rather than left set, because a concurrent first compile in another thread must not observe it (a GLOBAL_STATE guard failure from that race is #4094). Dynamo's per-frame skip is sticky, so subsequent calls stay eager without the flag.