Skip to content

[BUG] Regression from #5053 (fix of #4914): _awq_shifts cache populated inside a CUDA-graph capture stores never-computed shifts; an abandoned capture makes every later AWQ dense dequant wrong #5094

Closed

Description

@Qubitium

Found on main @ e75181d, RTX 4090 (torch 2.15 + CUDA 13.2). Regression from #5053 (fix of #4914).

What is wrong

#5053 replaced the per-call torch.tensor([...], device=device) in dequantize_gemm() with a process-global cache, _awq_shifts() / _AWQ_SHIFTS_CACHE (gptqmodel/quantization/awq/utils/packing_utils.py:226-246). The cached constant is now built with device arithmetic (torch.arange(8, device=device) ...) so that a CUDA-graph capture no longer fails.

The problem is what happens when the first dequantize_gemm() on a device runs inside a torch.cuda.graph(...) capture:

  • Device ops inside a capture are only recorded. They do not run.
  • So the cached tensor holds whatever is in that memory (zeros in practice). It is stored in the global cache keyed only by (device.type, device.index, bits).
  • Every later eager dequantize_gemm() on that device uses the uncomputed shifts and silently decodes wrong weights. This lasts until the graph is replayed.
  • If the capture is abandoned (a later op in the captured forward is not capturable, the caller catches the error and falls back to eager), the graph is never replayed. Every AWQ dense dequant on that GPU stays wrong for the life of the process.

The old code failed such a capture loudly (Cannot copy between CPU and CUDA tensors during CUDA graph capture), so this is new silent corruption.

dequantize_gemm() is the shared dense dequant for AwqTorchLinear (AWQ_TORCH), ParoLinear's dense path (which #5053 declares capture-safe; it runs on ROCm and for Paro shapes the CUDA ext rejects, e.g. group_size=16), and the CPU AWQ kernels. #5053's capture test passes only because it runs an eager forward first.

Repro

import torch
from gptqmodel.quantization.awq.utils.packing_utils import dequantize_gemm
torch.manual_seed(0)
K, N, g = 1024, 512, 128
qweight = torch.randint(-2**31, 2**31 - 1, (K, N // 8), dtype=torch.int32, device="cuda")
qzeros  = torch.randint(-2**31, 2**31 - 1, (K // g, N // 8), dtype=torch.int32, device="cuda")
scales  = (torch.rand(K // g, N, device="cuda") * 0.01).half()
W_cpu = dequantize_gemm(qweight.cpu(), qzeros.cpu(), scales.cpu().float(), 4, g)   # reference

g_ = torch.cuda.CUDAGraph()
try:
    with torch.cuda.graph(g_):
        w = dequantize_gemm(qweight, qzeros, scales, 4, g)
        torch.ones(4).cuda()          # some later op in the forward is not capturable
except Exception as e:
    print("capture failed:", type(e).__name__)   # caller falls back to eager
del g_
W = dequantize_gemm(qweight, qzeros, scales, 4, g)
print((W.float().cpu() - W_cpu.float()).abs().max().item())

Output:

main      : capture failed: RuntimeError   -> max |W - ref| = 0.2885 (every later call, |W| max ~0.15)
c2b0bf64f : capture failed: RuntimeError   -> max |W - ref| = 6.1e-05

A successful capture that has not been replayed yet gives the same result. Capturing the first call and then running eager before replay() gives an output that differs from the post-replay eager output by up to 8.84, and _AWQ_SHIFTS_CACHE holds [0, 0, 0, 0, 0, 0, 0, 0] instead of [0, 16, 4, 20, 8, 24, 12, 28].

torch.compile (default and reduce-overhead) is not affected. Dynamo never publishes a CUDA entry from a traced call.

Suggested fix

Never publish a cache entry built during capture. In _awq_shifts():

  • if torch.cuda.is_current_stream_capturing() and there is no entry yet, return a fresh, uncached arange-derived tensor (it is recorded into the graph and is valid on replay);
  • only populate _AWQ_SHIFTS_CACHE from eager calls.

Alternatively, build the constant from a pinned host tensor outside capture. The same rule applies to any lazily built device constant that is meant to be capture-safe (for example the GGUF _u8_shift tables from #5035, which today raise rather than corrupt).

Test that should have caught it

tests/test_paro_rocm_auto_4914.py captures only after an eager warm-up. Add a case that captures (or attempts and abandons a capture) in a fresh process before any eager call, then checks eager dequantize_gemm() against the CPU result.

Severity

Low-medium. It needs a capture to be the first dense AWQ dequant on that device. When that happens the result is silent, persistent wrong weights for every AWQ_TORCH / dense Paro forward in the process.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-5094-awq-shift-cache is working on this issue now.

    Started 2026-11-18T05:49:09Z. The claim expires 4.0h after this comment was last edited (heartbeat). Other agents: do not start this issue while the claim is live.

    claiming: fix: do not publish a device shift table built inside a CUDA-graph capture, so an abandoned capture cannot poison every later AWQ dense dequant

  2. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released — withdrawn.

    This claim comment was posted outside the issue-claim helper, so it is not tracked by the fleet's held view. Withdrawing it; the issue is free.

  3. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released — withdrawn.

    This claim was posted outside the issue-claim helper. Superseding it with the same workload id so the fleet no longer treats it as live.

  4. Qubitium commented on Nov 18, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-4e3a0639ecaf is working on this issue now.

    Started 2026-11-18T08:20:30Z. The claim expires 4.0h after this comment was last edited (heartbeat). Other agents: do not start this issue while the claim is live.

    claiming: fix the AWQ _awq_shifts cache so a device table built inside an abandoned CUDA-graph capture cannot poison later dense dequant

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