Skip to content

fix(awq): never publish the shift table from a CUDA-graph capture (#5094) - #5180

Merged
Qubitium merged 1 commit into
mainfrom
fix/5094-awq-shift-capture
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5094-awq-shift-capture

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5094. This is a regression from #5053 (the fix for #4914).

_awq_shifts() is the process-global four-bit extraction table used by
dequantize_gemm(). #5053 moved it from a per-call torch.tensor([...], device=...) to device arithmetic in _AWQ_SHIFTS_CACHE, keyed by
(device.type, device.index, bits), so a CUDA-graph capture no longer trips
over a host-to-device copy.

The cache entry was published unconditionally. Device work inside a capture is
recorded, not run, so a table first built inside torch.cuda.graph(...) holds
uninitialized memory (zeros) until the graph is replayed. If the capture is
abandoned (a later op is not capturable and the caller falls back to eager),
every later eager AWQ dense dequant on that GPU silently decodes with those
zeros for the rest of the process. A capture that succeeds but has not been
replayed has the same problem for an eager call in between. The shared
dequantize_gemm() is used by AwqTorchLinear, ParoLinear's dense path, and
the CPU kernels, so all three were affected.

Mechanism

_awq_shifts() now publishes only from an eager call:

  • warm cache hit: unchanged, return the one cached tensor;
  • torch.compiler.is_compiling(): unchanged, derive an uncached tensor;
  • torch.cuda.is_current_stream_capturing(): derive a fresh uncached tensor;
    it is recorded into the graph and is valid on replay;
  • eager miss: derive and publish.

The warm path returns before the capture check, so there is no per-call host
allocation or H2D copy regression. AwqTorchLinear also now declares
SUPPORTS_GRAPH_CAPTURE = True (the eager forward captures and replays on the
tested GPU); the stale comment and docs/kernel-compile-matrix.md are updated.

Before / after

Situation Before #5094 After #5094
First four-bit dequant on the device runs inside a capture cache published with zero offsets capture gets an uncached table recorded into the graph
Capture is abandoned every later eager dequant decodes wrong weights for the life of the process the next eager call derives and publishes the correct table
Capture succeeds, eager call before replay() eager call reads the zero table eager call publishes its own correct table; the graph keeps its recorded tensor
Eager steady state one cached table per device/bits unchanged: one cached table, no per-call allocation or H2D copy
torch.compile trace uncached derived tensor unchanged

Wrong-value evidence on the pre-fix commit 7d4797c0e (RTX 4090, torch
2.15.0+cu132, CUDA_VISIBLE_DEVICES=3):

  • tests/test_awq_shift_capture_5094.py: 7 failed, 2 passed.
  • Abandoned first-call capture left _AWQ_SHIFTS_CACHE[("cuda", 0, 4)] == [0, 0, 0, 0, 0, 0, 0, 0]; the next eager dequant had max |W - W_cpu| = 0.2885 (the weight magnitude is ~0.15).
  • A successful capture followed by an eager call before replay() had max
    |diff| = 0.2789.
  • AwqTorchLinear after an abandoned capture: 445/512 (FP16) and 447/512
    (BF16) elements mismatched, greatest absolute difference 2.671875 against
    a 0.005 tolerance.
  • Dense ParoLinear after an abandoned capture: 205/256 (FP16) and 206/256
    (BF16) elements mismatched, greatest absolute difference 2.27-2.28
    against a 0.02 tolerance.

After the fix the helper matches the CPU reference with torch.equal and the
module tests match an independent FP64 oracle.

Diagrams

Before #5094, a cold miss inside a capture published a never-computed table:

flowchart TD
    A["dequantize_gemm: bits == 4"] --> B["_awq_shifts: cache miss"]
    B --> C["derive offsets with device arithmetic"]
    C --> D["publish _AWQ_SHIFTS_CACHE[key]"]
    D --> E["return table"]
    E -. "inside capture: recorded, not run" .-> F["table holds zeros"]
    F -. "capture abandoned" .-> G["later eager dequant decodes wrong weights"]
    classDef failure fill:#fce8e6,stroke:#a33,color:#111
    class F,G failure
Loading

After #5094, only an eager miss publishes:

flowchart TD
    A["dequantize_gemm: bits == 4"] --> B["_awq_shifts(device, bits)"]
    B --> C{"cache hit?"}
    C -- "yes" --> D["return cached table"]
    C -- "no" --> E{"torch.compiler.is_compiling()?"}
    E -- "yes" --> F["derive fresh, uncached"]
    E -- "no" --> G{"is_current_stream_capturing()?"}
    G -- "yes" --> H["derive fresh, uncached<br/>recorded into the graph"]
    G -- "no" --> I["derive and publish<br/>_AWQ_SHIFTS_CACHE[key]"]
    I --> D
    classDef added fill:#e6f4ea,stroke:#2e7d32,color:#111
    class F,H added
Loading

Tests

Command (all with PYTHONPATH=/root/work/issue-5094 HF_HUB_OFFLINE=1, GPU 3 for CUDA) Result
pytest tests/test_awq_shift_capture_5094.py -q -ra 11 passed
pytest tests/kernels/test_awq_*.py -q -ra 604 passed, 39 skipped
pytest tests/test_paro*.py -q -ra 297 passed
pytest over the 54 non-model tests/test_awq_*.py files (excludes pytest.mark.model / pytest.mark.slow / HF download imports) 804 passed, 63 skipped
pytest tests/kernels/test_kernel_compile_protocol.py -q -ra 65 passed, 151 skipped
pytest "tests/kernels/test_kernel_compile_levels_awq.py::test_awq_kernel_reaches_declared_compile_level[AwqTorchLinear]" -q -ra 1 passed
pytest tests/test_docs_contract_refs.py -q -ra 33 passed
uvx [email protected] check gptqmodel tests setup.py All checks passed

The new regression file covers: abandoned capture then eager dequant vs CPU
reference, eager before replay, replayed first-call capture, AwqTorchLinear
(FP16/BF16, abandoned and real capture), dense ParoLinear (FP16/BF16), the CPU
AwqTorchLinear kernel, and eager table reuse (no per-call rebuild).

Unverified

  • ROCm/HIP: no ROCm host available here. The helper uses
    torch.cuda.is_current_stream_capturing() (ROCm reports its device as
    cuda), and ParoLinear already declared capture-safe on ROCm, but
    AwqTorchLinear capture was not re-measured on ROCm.
  • XPU/NPU/MPS: no CUDA-graph capture API; their eager cache path is unchanged.
  • Model-marked AWQ compatibility tests (pytest.mark.model) were not run: they
    need HF checkpoints and the offline cache does not contain them.
  • Machete, Swordfish, RDNA4, and ROCm-specific kernel tests were skipped by
    their hardware guards (39 kernel-family skips; 63 in the AWQ unit family).

Docs

  • docs/awq-dequantize-tiling.md#capture-safe-extraction-offsets (new section,
    before/after diagrams, linked from README.md)
  • docs/kernel-compile-matrix.md regenerated from the declarations.

#5094)

`_awq_shifts()` builds `_AWQ_SHIFTS_CACHE` with device arithmetic so a capture
does not need a host-to-device copy (#5053). Device work inside a capture is
recorded, not run, so a table first built there holds uninitialized memory
(zeros). Because the table was published immediately, an abandoned capture left
every later eager AWQ dense dequant decoding with those zeros; a capture that
succeeded but had not been replayed had the same problem for an eager call in
between.

Only publish from an eager call. A capture now gets a fresh uncached table that
is recorded into the graph and is valid on replay; the next eager call derives
and publishes the real one. The fast path is unchanged: a warm eager call still
returns the one cached tensor with no allocation or H2D copy.

The shared `dequantize_gemm()` fixes `AwqTorchLinear`, `ParoLinear`'s dense
path, and the CPU kernels. `AwqTorchLinear` now declares
`SUPPORTS_GRAPH_CAPTURE = True`, which a first-call capture/replay test measures.

Tests: tests/test_awq_shift_capture_5094.py covers an abandoned capture, an
eager call before replay, a replayed first-call capture, AwqTorchLinear,
dense ParoLinear, the CPU kernel, and eager table reuse.
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