Repository navigation
fix(awq): give FORMAT.GEMV_FAST and LLM_AWQ a CPU in-session reader (#4736) - #4753
Conversation
|
@n-o-t-mugen Thank you for your first PR. Checking. Will merge soon. |
…odelCloud#4736) quantize(format=GEMV_FAST / LLM_AWQ) packs every layer and leaves it on CPU, but the only kernels for that interleaved layout are CUDA-only. rehome_quantized_kernel() therefore raised "No valid quant linear" -> RuntimeError after the whole calibration, before save() could run. ModelCloud#4599 fixed only FORMAT.GEMV. Add AwqGEMVFastTorchLinear and LLMAwqTorchLinear, portable CPU readers of the same packed INT16 qweight and FP16 scales/offset buffers. They reuse the existing _dequantize_awq_gemv_fast layout inverse as a bounded per-forward temporary. They are explicit-only (SUPPORTS_FORMATS priority 0), so the accelerator AUTO order does not change, and select_quant_linear() reaches them for a CPU target through PORTABLE_CPU_FALLBACK. LLMAwqTorchLinear stays an LLMAwqLinear so layout-keyed consumers keep routing it as LLM_AWQ. A CPU-only quantize host now packs these formats with BACKEND.AWQ_TORCH instead of refusing before calibration. The saved checkpoint keeps the requested format and its interleaved packing, and AUTO reload on CPU selects the same reader.
The rebase onto main brings the new `AwqGEMVTorchLinear` CPU reader, which main registers in `KERNELS`. This PR already owns the `OTHER_KERNELS` inventory, so add it there; without it `test_audit_covers_every_registered_kernel` reports the kernel as unclassified (it fails the same way on unmodified main).
b8ffb62 to
9534238
Compare
Qubitium
left a comment
There was a problem hiding this comment.
Review
Reviewed the diff and ran it on Linux aarch64 (torch 2.11.0+cu130, 1x GB10 sm_121) with the model present in the CPU reader path.
Verified
tests/test_awq_gemv_insession_4554.py: 10/10 pass both withCUDA_VISIBLE_DEVICES=""and with the GB10 visible.- The reader reuses the already-tested
_dequantize_awq_gemv_fastlayout inverse, so the packedqweight/scales/ zero-offset contract is unchanged. The rehome test proves the buffers stay byte-identical and the output matches an independent fake-quant oracle. quantize() -> save() -> AUTO reloadon CPU keeps the requestedgemv_fast/llm_awqformat and the INT16[out/4, in]layout.- Accelerator AUTO order is untouched: priority-0
SUPPORTS_FORMATSplusSUPPORTS_DEVICES=[CPU]andPORTABLE_CPU_FALLBACK. docs/kernel-compile-matrix.mdmatches the declarations (test_docs_matrix_matches_the_declarationspasses).- Rebased head (
9534238d) includes theAwqGEMVTorchLinearROCm-audit classification, sotest_audit_covers_every_registered_kernelis green again.
One gap left (not blocking this PR, follow-up lands it)
tests/module_tree/test_fusion_kernel_backends.py::test_every_registered_gptq_awq_kernel_has_an_explicit_fusion_contract
fails on this branch: the two new classes are registered kernels but are in neither kernel_specs() nor the EXCLUDED
inventory. That assertion was already red on main for AwqGEMVTorchLinear (#4599 left it out), so this PR adds two
more names to an already-failing assertion rather than introducing a new failure class.
I had prepared the fix locally (inventory entries with reasons + measured AOT_EAGER cases for both readers in
tests/kernels/test_kernel_compile_levels_awq.py, which pin the declaration the way #4737 requires), but
"Allow edits by maintainers" is currently off on this PR, so I could not push it. I will land it as an immediate
follow-up PR against main together with the AwqGEMVTorchLinear entry, which also clears the pre-existing red test.
Notes
SUPPORTS_DTYPES = [torch.float16]mirrors the native kernels, but the forward already works for BF16/FP32 inputs
(it casts to FP32 and back). Widening that declaration needs its own dtype tests; not done here.docs/support-matrix.md'sMETHOD.AWQ CPUrow still lists onlyFORMAT.GEMM; it was already incomplete after #4599
and that row documents the AVX512 backend, not the portable readers.- The two CUDA model-generation tests (
test_awq_gemv_fast.py,test_awq_llm_awq.py) could not run here:
/monster/data/model/Llama-3.2-1Band the C4 calibration file are not present on this host.
Test runs
tests/test_awq_gemv_insession_4554.py: 10 passed (CPU-only and GPU).tests/test_rocm_kernel_contracts.py+tests/module_tree/test_fusion_kernel_backends.py: 512 passed, 161 skipped
once the inventory follow-up is applied; without it, only the fusion inventory assertion fails (see above).- Broader AWQ suite failure set is identical to unmodified
main(AVX512 kernels on this aarch64 host, QQQ group
scales) - no new failures.
Merging now; the follow-up PR will close the inventory gap.
|
Reviewed and verified on GB10 + CPU-only; posting the review as a review comment (it did not attach to the discussion). Merging now, with the fusion-inventory follow-up landing immediately after. |
) #4753 gave FORMAT.GEMV_FAST and FORMAT.LLM_AWQ portable Torch readers, but only for a CPU-only host: on ROCm every path refused before calibration. AUTO raised "No valid quant linear", BACKEND.AWQ_TORCH rejected DEVICE.ROCM ("only supports cpu"), and BACKEND.AWQ_GEMV_TRITON does not declare either format, so the documented format/platform combination could not be produced at all. Resolve the pack backend from the device family: awq_gemv_fast_pack_backend() returns BACKEND.AWQ_GEMV_FAST on NVIDIA and BACKEND.AWQ_TORCH wherever the extension cannot build (CPU, ROCm). Both portable readers now declare DEVICE.CPU/CUDA/ROCM and are reached by the per-family portable fallback in select_quant_linear, which is now keyed on the target families instead of "CPU only" (PORTABLE_FALLBACK; the GEMV reader keeps its CPU-only declaration). The readers move the packed words to the input device inside forward() rather than rewriting module state, so an AUTO reload of a saved checkpoint works on ROCm without an explicit backend and keeps only packed INT16/FP16 storage. An AUTO CUDA load still selects the native kernel, and the GEMV-on-ROCm refusal now names only FORMAT.GEMV and the readers that do apply. Fixes #5109
) (#5176) #4753 gave FORMAT.GEMV_FAST and FORMAT.LLM_AWQ portable Torch readers, but only for a CPU-only host: on ROCm every path refused before calibration. AUTO raised "No valid quant linear", BACKEND.AWQ_TORCH rejected DEVICE.ROCM ("only supports cpu"), and BACKEND.AWQ_GEMV_TRITON does not declare either format, so the documented format/platform combination could not be produced at all. Resolve the pack backend from the device family: awq_gemv_fast_pack_backend() returns BACKEND.AWQ_GEMV_FAST on NVIDIA and BACKEND.AWQ_TORCH wherever the extension cannot build (CPU, ROCm). Both portable readers now declare DEVICE.CPU/CUDA/ROCM and are reached by the per-family portable fallback in select_quant_linear, which is now keyed on the target families instead of "CPU only" (PORTABLE_FALLBACK; the GEMV reader keeps its CPU-only declaration). The readers move the packed words to the input device inside forward() rather than rewriting module state, so an AUTO reload of a saved checkpoint works on ROCm without an explicit backend and keeps only packed INT16/FP16 storage. An AUTO CUDA load still selects the native kernel, and the GEMV-on-ROCm refusal now names only FORMAT.GEMV and the readers that do apply. Fixes #5109
Fixes #4736 (the GEMV_FAST / LLM_AWQ half of #4554).
Summary
quantize()withformat="gemv_fast"orformat="llm-awq"always raised at the very end, after calibration had finished and beforesave()could run:The quantize loop leaves packed layers on CPU, and
rehome_quantized_kernel()must switch them to a kernel that runs there. #4599 added that CPU reader forFORMAT.GEMVonly. The GEMV_FAST interleaved layout (shared by LLM_AWQ) had only CUDA-only kernels, soselect_in_session_quant_linear(qcfg, cpu)returnedNo valid quant linearand the whole calibration was lost.This PR applies the #4599 approach to GEMV_FAST and LLM_AWQ: it adds a portable Torch CPU reader for the layout.
What Changed
AwqGEMVFastTorchLinear/LLMAwqTorchLinear(gptqmodel/nn_modules/qlinear/gemv_fast_awq.py): CPU readers of the exact packed buffers the CUDA kernels use (INT16 interleavedqweight, FP16scales, FP16 offset inqzeros/scaled_zeros)._dequantize_awq_gemv_fastlayout inverse, accumulates in FP32 and rounds once. The dense tile is a bounded temporary and is never cached on the module.SUPPORTS_FORMATSpriority0plusPORTABLE_CPU_FALLBACK = True, so they are reached only when AUTO has no CPU kernel. The accelerator AUTO order is unchanged.int16pack) matches the native classes, so a CPU-packed checkpoint stays loadable by the CUDA extension.LLMAwqTorchLinearalso derives fromLLMAwqLinear, so layout-keyed consumers (utils/mlx.py) still route it as LLM_AWQ.QUANT_TYPEkeeps theawq_prefix becausepack_moduledispatches AWQ packers on it.SUPPORTS_COMPILE_LEVEL = AOT_EAGER(measured, see below) andSUPPORTS_GRAPH_CAPTURE = False(CPU-only).gptqmodel/models/base.py): GEMV_FAST / LLM_AWQ now pack withBACKEND.AWQ_TORCHinstead of the CUDA-onlyAWQ_GEMV_FAST. Before, they were refused before calibration withdoes not support device: DEVICE.CPU.docs/in-session-model-after-offload.md; alsodocs/quantization-guide.md,docs/support-matrix.mdanddocs/kernel-compile-matrix.md(two new rows).Behaviour after this PR
gemv_fast/llm-awqRuntimeErrorafter calibration, nothing savedsave()writes the requested formatAWQ_TORCH; quantize -> save -> AUTO reload on CPUBACKEND.GEMV_FAST/ AUTO)AwqGEMVFastLinear/LLMAwqLineargemv,gemm, other methodsUsage (no API change):
The in-session model after
quantize()runs on the CPU reader, which is correct but dequantizes per forward, as with GEMV since #4599. For fast inference, save and reload on the GPU.Out of scope / not fixed
select_quant_linear(backend=AWQ_GEMV_FAST, device=DEVICE.ROCM)check rejects it before calibration (verified directly), so no calibration is lost there, but ROCm still cannot produce these formats. That needs a Triton GEMV_FAST reader and is a separate change.main, untouched:test_audit_covers_every_registered_kernelwas already red becauseAwqGEMVTorchLinear(fix(awq): portable CPU GEMV reader so quantize() completes in session (#4554) #4599) is missing from the audit buckets. This PR classifies only its own two classes (asOTHER_KERNELS).Tests
New tests in the existing #4554 family suite,
tests/test_awq_gemv_insession_4554.py, parametrized overgemv_fastandllm_awq:test_awq_gemv_fast_cpu_insession_kernel_is_the_documented_fallback: CPU selection returns the new reader with priority0.test_rehome_switches_native_gemv_fast_module_to_cpu_reader: a module packed by the native class is rehomed; the packed buffers stay byte-identical; the output matches an independent fake-quant oracle (its ownround/clampcodes and FP16 offset, FP32 accumulate); no FP32 buffer remains after the forward.test_awq_gemv_fast_quantize_save_and_reload_on_cpu: tiny Llama, quantize -> in-session forward ->save()(format kept,int16[out/4, in]qweight) -> AUTO reload on CPU, with bit-identical logits.Red on unmodified
main@b225a438(new tests run against amainworktree, with the new class names stubbed):Green on this branch (rebased on
ebe5558a):Compile level evidence:
torch.compile(module, fullgraph=True, backend=...)on both readers (CPU, torch 2.15.0+cpu) giveseagerOK andaot_eagerOK, both bit-identical to the eager forward. Inductor was not measured (no host C++ compiler), so the declaration stops atAOT_EAGER.Environment and limits, please read:
Validated on CPU only: Windows, Python 3.14, torch 2.15.0+cpu.
pypcrehas no Windows wheel andfcntlis POSIX-only, so local test shims for both were used outside the repo. The 88 shared failures are Triton/CUDA-unavailable tests, plustest_docs_matrix_matches_the_declarations, whose only diff here is theParoTritonLinearrow, unregistered without Triton.Not run:
tests/test_awq_gemv_fast.py::test_awq_gemv_fast_quantized_model_loads_and_generatestests/test_awq_llm_awq.py::test_llm_awq_checkpoint_loads_with_gemv_fast_backend_and_generatesThey need CUDA and the
/monster/datamodel and dataset. The step they failed on (the rehome) is covered above, and their CUDA reload/generate path is not modified. Could a maintainer run these two on CUDA?ROCm: not run (see Out of scope).
Model Support / Module Plan
Not applicable: no model, alias or definition change.
AWQ onboarding safety
Not applicable: no model onboarding or AWQ grouping/search/replay/fold change. Only the runtime reader of an existing checkpoint layout and the pack-backend choice for a CPU host.
Logic Flow Documentation
file:lineevidence.docs/repeats the diagrams, titled for the mechanism, and is linked fromREADME.md.Diagram summary:
docs/in-session-model-after-offload.md("AWQ CPU readers for the GEMV layouts", already linked fromREADME.md) showsrehome_quantized_kernel->select_in_session_quant_linear-> AUTO CPU kernel, elsePORTABLE_CPU_FALLBACKreader (GEMV / GEMV_FAST / LLM_AWQ), else theRuntimeErrorpath. It includes a before/after table per format.flowchart TD A["quantize() finishes<br/>packed AWQ layers on cpu"] --> B["rehome_quantized_kernel"] B --> C["select_in_session_quant_linear(qcfg, cpu)"] C --> D{"AUTO CPU kernel<br/>for the format?"} D -- "GEMM" --> E["AwqTorchLinear"] D -- "no" --> F{"PORTABLE_CPU_FALLBACK<br/>reader for the format?"} F -- "GEMV" --> G["AwqGEMVTorchLinear (#4554)"] F -- "GEMV_FAST" --> H["AwqGEMVFastTorchLinear (new)"] F -- "LLM_AWQ" --> I["LLMAwqTorchLinear (new)"] F -- "none" --> X["RuntimeError: reload guidance"] E & G & H & I --> S["_switch_quant_linear_class<br/>packed buffers unchanged"] S --> T["in-session forward, save(), AUTO reload on cpu"] style X fill:#f8d7da,stroke:#b02a37,stroke-width:2pxReview Requirements
I or an AI agent (such as Codex or Claude) have:
Kernel Accuracy Requirements
fusion_kernels.kernel_specs(), same asAwqGEMVTorchLinear.)Oracle: the output was compared against an independent FP32 fake-quant reference within
rtol=1e-3, atol=1e-2, FP16 output, FP32 accumulation.Notes
main(ebe5558a), including the per-kernel compile-capability protocol from feat(kernels): declare torch.compile and CUDA-graph capability per kernel #4737.