Repository navigation
fix(awq): never publish the shift table from a CUDA-graph capture (#5094) - #5180
Merged
Merged
Conversation
#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.
Qubitium
marked this pull request as ready for review
November 18, 2026 08:44
This was referenced Nov 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #5094. This is a regression from #5053 (the fix for #4914).
_awq_shifts()is the process-global four-bit extraction table used bydequantize_gemm(). #5053 moved it from a per-calltorch.tensor([...], device=...)to device arithmetic in_AWQ_SHIFTS_CACHE, keyed by(device.type, device.index, bits), so a CUDA-graph capture no longer tripsover 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(...)holdsuninitialized 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 byAwqTorchLinear,ParoLinear's dense path, andthe CPU kernels, so all three were affected.
Mechanism
_awq_shifts()now publishes only from an eager call: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;
The warm path returns before the capture check, so there is no per-call host
allocation or H2D copy regression.
AwqTorchLinearalso now declaresSUPPORTS_GRAPH_CAPTURE = True(the eager forward captures and replays on thetested GPU); the stale comment and
docs/kernel-compile-matrix.mdare updated.Before / after
replay()torch.compiletraceWrong-value evidence on the pre-fix commit
7d4797c0e(RTX 4090, torch2.15.0+cu132,
CUDA_VISIBLE_DEVICES=3):tests/test_awq_shift_capture_5094.py: 7 failed, 2 passed._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).replay()had max|diff| = 0.2789.AwqTorchLinearafter an abandoned capture: 445/512 (FP16) and 447/512(BF16) elements mismatched, greatest absolute difference 2.671875 against
a 0.005 tolerance.
ParoLinearafter 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.equaland themodule 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 failureAfter #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 addedTests
PYTHONPATH=/root/work/issue-5094 HF_HUB_OFFLINE=1, GPU 3 for CUDA)pytest tests/test_awq_shift_capture_5094.py -q -rapytest tests/kernels/test_awq_*.py -q -rapytest tests/test_paro*.py -q -rapytestover the 54 non-modeltests/test_awq_*.pyfiles (excludespytest.mark.model/pytest.mark.slow/ HF download imports)pytest tests/kernels/test_kernel_compile_protocol.py -q -rapytest "tests/kernels/test_kernel_compile_levels_awq.py::test_awq_kernel_reaches_declared_compile_level[AwqTorchLinear]" -q -rapytest tests/test_docs_contract_refs.py -q -rauvx [email protected] check gptqmodel tests setup.pyThe 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 CPUAwqTorchLinearkernel, and eager table reuse (no per-call rebuild).Unverified
torch.cuda.is_current_stream_capturing()(ROCm reports its device ascuda), andParoLinearalready declared capture-safe on ROCm, butAwqTorchLinearcapture was not re-measured on ROCm.pytest.mark.model) were not run: theyneed HF checkpoints and the offline cache does not contain them.
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.mdregenerated from the declarations.