Skip to content

[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

Closed

Description

@Qubitium

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:

  1. 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 executes config.suppress_errors = True / = previous inside every non-fullgraph callable returned by torch_compile(). torch._dynamo.config overrides are ContextVars. When an outer torch.compile traces 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 at post_init and replaces self.dequantize with exactly such a callable (vptq_torch.py:319). As a result, every VPTQ layer graph-breaks under any model-level compile.
  2. 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-cache generate() (cudagraph reduce-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-cache generate() 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, if torch.compiler.is_compiling() is true, return original(*args, **kwargs) (or the compiled callable) without touching suppress_errors or the load lock. A nested torch_compile wrapper 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 VPTQLinear stop compiling dequantize separately 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 with SUPPORTS_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

  1. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-4e3a0639ecaf is 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

  2. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-4e3a0639ecaf finished this issue.

    PR: #5240 — base main, head bd1581d7a (branch fix/5224-vptq-static-cache), 3 files, +219/−1.

    Fix

    torch_compile's wrappers no longer mutate torch._dynamo.config.suppress_errors when they are entered from inside an outer trace. New dynamo_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-cache generate() compile produces no ContextVar mutation not supported break. _CompiledModelForward._release_trace_garbage likewise defers gc.collect() while an outer trace is active (it was a second once-per-call break on the same path). Top-level non-fullgraph calls still enable/restore suppress_errors, so #5163's fallback is unchanged.

    Evidence

    • Minimal nested-compile repro: before Unsupported: ContextVar mutation not supported; after 0 breaks, 2 unique graphs, same value.
    • New tests/test_compile_nested_suppression_5224.py: RED 4 failed / 2 passed with the fixes stashed; GREEN 6 passed.
    • GPU 2 (RTX 5090) VPTQ static-cache generate(), 2-layer Qwen2.5-0.5B VPTQ_TORCH fp16: before RuntimeError: accessing tensor output of CUDAGraphs that has been overwritten on the first no-optimize generate; after no-optimize and optimize() ×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.py 5 passed.
    • uvx [email protected] check gptqmodel docs/eora tests setup.py clean.

    Not verified: ROCm/gfx1201 hardware (mechanism is device-independent and the CPU repro exercises the same path); fused VPTQCudaLinear (not built here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions