Skip to content

fix(compile): let dynamo fall back per frame instead of replaying the forward (#5119) - #5163

Merged
Qubitium merged 2 commits into
ModelCloud:mainfrom
Qubitium:fix/compile-fallback-replay-5119
Nov 18, 2026
Merged

Qubitium merged 2 commits into
ModelCloud:mainfrom
Qubitium:fix/compile-fallback-replay-5119

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

optimize() installs torch.compile with a fallback that, on a compiler failure, re-ran the original forward from the beginning. With fullgraph=False dynamo 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: in generate() 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 first generate() after optimize() therefore always raised:

RuntimeError: The expanded size of the tensor (19) must match the existing size (38) at non-singleton dimension 3.

Fixes #5119

What Changed

Tests

  • Unit tests executed and passed.
  • Added a targeted regression that fails on the old replay.

Red on main @ a978bf18 (test copied onto an unmodified worktree):

/root/venv/bin/python -m pytest tests/test_compile_fallback_no_replay_5119.py -q -p no:cacheprovider
# FAILED test_resume_frame_failure_runs_the_forward_once
# E  AssertionError: the partially executed forward was replayed
# FAILED test_module_forward_is_not_replayed_on_a_resume_frame_failure
# E  AssertionError: the module forward was replayed
# 2 failed, 3 passed

Green on 3af43265:

/root/venv/bin/python -m pytest tests/test_compile_fallback_no_replay_5119.py \
  tests/test_torch_compile_fallback.py tests/test_silent_exception_handling.py \
  tests/test_model_load_lock_4204.py tests/test_generation_compile_policy_4669.py -q
# 73 passed

/root/venv/bin/python -m pytest tests/test_optimize_generate_4669.py \
  tests/test_optimize_no_reference_cycle_4937.py -q
# 7 passed

bash format/check.sh
# ruff 0.14.2: All checks passed

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, that fullgraph still 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

Kernel Accuracy Requirements

Not applicable: no kernel or fusion changes.

Notes

suppress_errors is 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.

… 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
Qubitium marked this pull request as ready for review November 18, 2026 07:06
@Qubitium
Qubitium merged commit fe9d824 into ModelCloud:main Nov 18, 2026
1 check passed
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.
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