Repository navigation
[BUG] Regression from #5171 (fix of #5102) + #5163: VPTQ static-cache generate() crashes ('accessing tensor output of CUDAGraphs that has been overwritten') - #5163's suppress_errors ContextVar write inside the nested dequantize torch_compile graph-breaks every VPTQ layer #5224
Description
Found on main @ 396b5f0, AMD Radeon AI PRO R9700 (gfx1201), torch 2.16.0.dev+rocm10.1. The nested-compile mechanism also reproduces on CPU with torch 2.15.0+cu132.
What is wrong
Two recent merges combine so that a plain static-cache generate() on any VPTQ checkpoint crashes, with no optimize() call:
- fix(compile): let dynamo fall back per frame instead of replaying the forward (#5119) #5163 (fe9d824, fix of [BUG] optimize() compile fallback re-runs a partially executed forward: a compiler failure in a resume frame replays KV-cache updates; first generate() after optimize() always crashes on GGUF Triton (AUTO) models #5119):
_call_with_dynamo_suppression(gptqmodel/utils/torch.py:453-463) now executesconfig.suppress_errors = True/= previousinside every non-fullgraph callable returned bytorch_compile().torch._dynamo.configoverrides are ContextVars. When an outertorch.compiletraces into such a wrapped callable, dynamo cannot trace the write:Unsupported: ContextVar mutation not supported ... Developer debug context: ContextVar('suppress_errors').set().VPTQLinear.optimize()runs atpost_initand replacesself.dequantizewith exactly such a callable (vptq_torch.py:319). As a result, every VPTQ layer graph-breaks under any model-level compile. - perf(qqq,vptq): opt portable torch kernels into compiled generation (#5102) #5171 (17a488e, fix of [PERF] #5006 (fix of #4984) incomplete: QQQTorchLinear and VPTQLinear stay off the static-cache compile with a stale/unmeasured reason though they trace as 1 graph/0 breaks; 2.6-4.5x slower generate on ROCm AUTO and CUDA bf16 QQQ #5102) set
VPTQLinear.SUPPORTS_COMPILE = True, which lets transformers auto-compile static-cachegenerate()(cudagraphreduce-overhead). Its measurement ("+1 model graph, 0 breaks", 5.0x) cannot hold on top of item 1. With a graph break inside every layer, cudagraph trees hand out outputs that the next partition overwrites, and the first static-cachegenerate()dies.
Repro
QwenL2_vptq = Qwen2.5-0.5B-Instruct (2 layers) quantized with VPTQConfig(), reloaded with AUTO (VPTQLinear).
import torch, time
from gptqmodel import GPTQModel, BACKEND
m = GPTQModel.load(ckpt, backend=BACKEND.VPTQ_TORCH, dtype=torch.float16)
tok = m.tokenizer; tok.padding_side = "left"
enc = tok(["The capital of France is", "Write a haiku about the sea:", "def fib(n):", "In 1905 Einstein"],
return_tensors="pt", padding=True).to("cuda")
for _ in range(2):
m.generate(**enc, max_new_tokens=32, do_sample=False, cache_implementation="static") # no optimize()
m.optimize()
for _ in range(3):
m.generate(**enc, max_new_tokens=32, do_sample=False, cache_implementation="static")
# graph breaks counted with torch._dynamo.utils.counters["graph_break"] after optimize()| tree | static-cache generate | after optimize() |
|---|---|---|
| fe9d824^ (before #5163) | ok, 0.48 s | ok, 0.24 s, 0 graph breaks, 2 graphs, same tokens as eager |
| fe9d824 (#5163) | ok, 0.13 s | 0.11 s, 32 breaks / 32 graphs, all ContextVar mutation not supported |
| 17a488e^ | ok, 0.13 s | 0.17 s (slower than without it), 32 breaks |
17a488e (#5171) and main 396b5f0 |
crash on the first static-cache generate | - |
RuntimeError: Error: accessing tensor output of CUDAGraphs that has been overwritten by a subsequent run.
Stack trace: File ".../transformers/models/qwen2/modeling_qwen2.py", line 272, in forward
The mechanism without any model:
import torch
from gptqmodel.utils.torch import torch_compile
inner = torch_compile(lambda t: t * 2 + 1, backend="inductor") # what VPTQLinear.optimize() installs
outer = lambda t: inner(t).sum()
outer(torch.randn(8))
torch.compile(outer, fullgraph=True, backend="eager")(torch.randn(8))
# torch 2.16 rocm and torch 2.15 cu132 alike:
# Unsupported: ContextVar mutation not supported | Developer debug context: ContextVar('suppress_errors').set()On this box the kernel-level tests are red as well:
tests/kernels/test_kernel_compile_levels_quant.py::test_kernel_reaches_declared_compile_level[VPTQLinear] reports "declared CUDAGRAPH but probe reported ... dynamo=False ... ContextVar('suppress_errors')", and tests/kernels/test_compile_policy_levels_4984.py::test_a_trace_clean_kernel_is_not_forced_eager[VPTQLinear] fails the same way.
Suggested fix
- Skip the suppression toggle when dynamo is already tracing: in
_install_callable_compile_fallback.call/_call_with_dynamo_suppression, iftorch.compiler.is_compiling()is true, returnoriginal(*args, **kwargs)(or the compiled callable) without touchingsuppress_errorsor the load lock. A nestedtorch_compilewrapper then inlines cleanly into the outer graph, as it did before fix(compile): let dynamo fall back per frame instead of replaying the forward (#5119) #5163. - Alternatively, have
VPTQLinearstop compilingdequantizeseparately when the model forward is being compiled. - Re-measure perf(qqq,vptq): opt portable torch kernels into compiled generation (#5102) #5171's static-cache claim on a real VPTQ checkpoint after the fix, and add a model-level static-cache
generate()smoke test for every kernel withSUPPORTS_COMPILE = True(the kernel-level probe already catches it, but it is red).
Severity
High: static-cache generate() crashes for every VPTQ model on ROCm (where VPTQLinear is the AUTO kernel), and on CUDA when VPTQ_TORCH is selected. Before #5171 it silently lost the compile speedup instead (32 breaks; slower than eager after optimize()).
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 18, 2026
Agent claim:
agent-4e3a0639ecafis working on this issue now.Started 2026-11-18T12:06:24Z. The claim expires 4.0h after this comment is last edited (heartbeat). Other agents: do not start this issue while the claim is live.
Scope: restore static-cache generate() on VPTQ: stop the suppress_errors ContextVar write inside the nested dequantize compile graph (hoist it out of the traced callable), keep #5163's error suppression for non-compiled calls, and re-verify 0 graph breaks + correct tokens
fix(compile): keep nested torch_compile wrappers trace-clean (#5224) (#…
Qubitium commentedon Nov 18, 2026
Agent claim released:
agent-4e3a0639ecaffinished this issue.PR: #5240 — base
main, headbd1581d7a(branchfix/5224-vptq-static-cache), 3 files, +219/−1.Fix
torch_compile's wrappers no longer mutatetorch._dynamo.config.suppress_errorswhen they are entered from inside an outer trace. Newdynamo_is_compiling()short-circuits both fallback wrappers (and_call_with_dynamo_suppression) to the eager body while a trace is active, so a VPTQ layer inlined into transformers' static-cachegenerate()compile produces noContextVar mutation not supportedbreak._CompiledModelForward._release_trace_garbagelikewise defersgc.collect()while an outer trace is active (it was a second once-per-call break on the same path). Top-level non-fullgraphcalls still enable/restoresuppress_errors, so #5163's fallback is unchanged.Evidence
Unsupported: ContextVar mutation not supported; after 0 breaks, 2 unique graphs, same value.tests/test_compile_nested_suppression_5224.py: RED 4 failed / 2 passed with the fixes stashed; GREEN 6 passed.generate(), 2-layer Qwen2.5-0.5B VPTQ_TORCH fp16: beforeRuntimeError: accessing tensor output of CUDAGraphs that has been overwrittenon the first no-optimize generate; after no-optimize andoptimize()×3 → 0 breaks, 2 graphs, tokens == eager, ALL_TOKENS_EQUAL: True.tests/test_*vptq*338 passed;tests/test_*compile*51 passed, 1 skipped;tests/kernels/test_*compile*170 passed, 179 skipped;test_optimize_dynamo_release_5020.py5 passed.uvx [email protected] check gptqmodel docs/eora tests setup.pyclean.Not verified: ROCm/gfx1201 hardware (mechanism is device-independent and the CPU repro exercises the same path); fused
VPTQCudaLinear(not built here).fix(compile): serialize every compiled call with model loads (#5209) (#…