Skip to content

fix(awq): pack and load GEMV_FAST/LLM_AWQ on ROCm with the portable reader - #5176

Merged
Qubitium merged 1 commit into
mainfrom
fix/5109-awq-gemv-fast-rocm
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5109-awq-gemv-fast-rocm

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #5109

Summary

FORMAT.GEMV_FAST and FORMAT.LLM_AWQ can now be quantized, run in-session and reloaded on ROCm. Before this change every backend refused them on a HIP host, so a documented format/platform combination was unusable.

What Changed

  • awq_gemv_fast_pack_backend() resolves the AUTO pack backend from the device family: BACKEND.AWQ_GEMV_FAST on NVIDIA, BACKEND.AWQ_TORCH where the native extension cannot build (CPU and ROCm). BaseQModel.quantize() uses it instead of hardcoding AWQ_TORCH for CPU and AWQ_GEMV_FAST otherwise.
  • AwqGEMVFastTorchLinear / LLMAwqTorchLinear declare DEVICE.CPU, DEVICE.CUDA and DEVICE.ROCM. Their forward moves the packed words to the input device per call instead of rewriting module state, so a partially placed or offloaded layer never materializes a dense weight on the wrong device.
  • select_quant_linear()'s portable fallback is now keyed on the resolved target families (_portable_fallback, PORTABLE_FALLBACK) rather than "CPU only". A reader is eligible only when it declares every target family, so the CPU-only AwqGEMVTorchLinear still never serves ROCm; the pre-existing PORTABLE_CPU_FALLBACK spelling keeps working.
  • _reject_native_awq_gemv_on_rocm() now refuses only FORMAT.GEMV (which has the portable Triton reader, needing an explicit AWQ_GEMV_TRITON). GEMV_FAST/LLM_AWQ are no longer rejected before selection, and AWQ_GEMV_ROCM_UNSUPPORTED no longer claims those formats are unreadable.
  • AUTO ordering on NVIDIA is unchanged: a CUDA target still selects AwqGEMVFastLinear/LLMAwqLinear. The portable readers keep SUPPORTS_FORMATS priority 0, so they are never AUTO-selected where a native kernel exists.
  • Docs: rocm-rdna4.md (backend row and the GEMV-family AUTO resolution page), quantization-guide.md, in-session-model-after-offload.md, kernel-compile-matrix.md, and the AWQ extension error text.

Logic flow

Old flow (ROCm), with the failure paths this fixes:

flowchart TD
    A["quantize(GEMV_FAST/LLM_AWQ, backend=AUTO) on ROCm"] --> B["preferred_backend = BACKEND.AWQ_GEMV_FAST"]
    B --> C["select_quant_linear: native class is CUDA-only"]
    C --> D["AUTO: no candidate, no CPU-only fallback for ROCM"]
    D --> E["ValueError: No valid quant linear"]
    A2["explicit BACKEND.AWQ_TORCH"] --> F["AwqGEMVFastTorchLinear.SUPPORTS_DEVICES = [CPU]"]
    F --> G["ValueError: only supports cpu ... does not run on ROCm"]
    A3["explicit BACKEND.AWQ_GEMV_TRITON"] --> H["no kernel declares GEMV_FAST/LLM_AWQ"]
    H --> I["ValueError: Unsupported backend"]
    style E fill:#f8d7da,stroke:#b02a37,stroke-width:2px
    style G fill:#f8d7da,stroke:#b02a37,stroke-width:2px
    style I fill:#f8d7da,stroke:#b02a37,stroke-width:2px
Loading

New flow:

flowchart TD
    A["quantize(GEMV_FAST/LLM_AWQ, backend=AUTO)"] --> B{"awq_gemv_fast_pack_backend"}
    B -- "cuda, not HIP" --> C["BACKEND.AWQ_GEMV_FAST -> AwqGEMVFastLinear / LLMAwqLinear"]
    B -- "cpu or IS_ROCM" --> D["BACKEND.AWQ_TORCH -> AwqGEMVFastTorchLinear / LLMAwqTorchLinear"]
    D --> E["pack, in-session forward, save"]
    E --> F["AUTO reload: no native candidate for ROCM"]
    F --> G{"_portable_fallback<br/>declares every target family?"}
    G -- "yes" --> H["portable reader; packed buffers moved per forward"]
    G -- "no" --> I["No valid quant linear"]
    C --> J["AUTO CUDA load keeps the native kernel"]
    style I fill:#f8d7da,stroke:#b02a37,stroke-width:2px
Loading

FORMAT.GEMV is unchanged: AUTO quantize resolves AWQ_GEMV_TRITON, an AUTO load still raises the actionable AWQ_GEMV_ROCM_UNSUPPORTED and reloading needs an explicit BACKEND.AWQ_GEMV_TRITON.

Tests

  • Unit tests executed and passed.
  • New targeted unit tests added.
# lint
bash format/check.sh
# ruff 0.14.2, All checks passed!

# affected families, CPU + NVIDIA host
python -m pytest tests/test_rocm_awq_gemv_gating.py \
  tests/test_awq_gemv_insession_4554.py tests/test_rocm_kernel_contracts.py \
  tests/kernels/test_kernel_compile_protocol.py -q
# 559 passed, 151 skipped

# same nodes on a ROCm (HIP) host, real HIP build
python -m pytest tests/test_rocm_awq_gemv_gating.py tests/test_awq_gemv_insession_4554.py -q
# 33 passed, 1 skipped
# skip: test_awq_gemv_quantize_save_and_reload_on_cpu - GEMV pack needs the native CUDA extension

# AWQ GEMV_FAST JIT/pack + compile-level measurement on a CUDA host
python -m pytest tests/test_awq_gemv_fast_jit.py tests/test_awq_gemv_fast_pack_vectorized.py \
  tests/kernels/test_kernel_compile_levels_awq.py -q
# 24 passed, 5 skipped

Red/green evidence for the new nodes (fix stashed, same tree):

FAILED tests/test_rocm_awq_gemv_gating.py::test_loader_allows_portable_gemv_fast_layouts_on_rocm
FAILED tests/test_rocm_awq_gemv_gating.py::test_awq_gemv_fast_auto_backend_resolves_portable_torch_on_rocm
FAILED tests/test_rocm_awq_gemv_gating.py::test_awq_gemv_fast_selects_the_portable_torch_reader_on_rocm[gemv_fast]
FAILED tests/test_rocm_awq_gemv_gating.py::test_awq_gemv_fast_selects_the_portable_torch_reader_on_rocm[llm_awq]
FAILED tests/test_rocm_awq_gemv_gating.py::test_awq_gemv_fast_portable_readers_declare_rocm_and_cpu_only_native_ones_do_not
FAILED tests/test_awq_gemv_insession_4554.py::test_awq_gemv_fast_portable_reader_serves_a_rocm_target[gemv_fast]
FAILED tests/test_awq_gemv_insession_4554.py::test_awq_gemv_fast_portable_reader_serves_a_rocm_target[llm_awq]

End-to-end on a HIP host, Qwen2.5-0.5B-Instruct, 32 calibration texts, AWQConfig(bits=4, group_size=128):

Format In-session kernel save -> AUTO reload In-session vs CPU reload logits CPU vs GPU reload logits
FORMAT.GEMV_FAST AwqGEMVFastTorchLinear AwqGEMVFastTorchLinear exact (max 0) max 0.195, mean 0.02 (FP16 kernel-order drift)
FORMAT.LLM_AWQ LLMAwqTorchLinear LLMAwqTorchLinear exact (max 0) max 0.195, mean 0.02 (same drift)

The same CPU-vs-GPU drift is measured for the FORMAT.GEMM control on the same host (max 0.230, mean 0.02), so it is the host's arithmetic order, not the new layout. No dense weight buffer (FP32 or otherwise, at in_features * out_features elements) exists after load or after repeated forwards.

Torch-oracle coverage: the portable forward is asserted against the independent fake-quant oracle in test_awq_gemv_fast_portable_reader_moves_packed_storage_per_call and test_rehome_switches_native_gemv_fast_module_to_cpu_reader (rtol=1e-3, atol=1e-2).

Magnitude/packed-storage: AwqGEMVFastTorchLinear/LLMAwqTorchLinear keep SUPPORTS_COMPILE_LEVEL = INDUCTOR; the measured probe on CPU and HIP builds is eager=True graph_breaks=0 dynamo/aot_eager/inductor=True with output matching eager. Eager CUDA-graph capture is not claimed.

Pre-existing, unrelated (fails identically on a pristine origin/main worktree, tests/kernels/test_kernel_compile_levels_cpu.py): test_cpu_kernel_reaches_declared_cpu_level[TorchLinear], test_torch_quant_embeddings_reaches_declared_cpu_level[whole_table].

Model Support / Module Plan

Not applicable: no model type, alias, definition, module plan or fusion eligibility change. tests/module_tree/test_fusion_kernel_backends.py only updates the exclusion reason text for the two portable readers.

AWQ onboarding safety

Not applicable: no new model/definition and no AWQ grouping, search, replay or folding change. The two layouts keep their existing packing, scales and offsets; only backend resolution and device declarations changed.

Logic Flow Documentation

  • Does not alter a major logic flow; the diagrams and the mechanism page are included.
  • New-flow Mermaid diagram uses real symbol names (awq_gemv_fast_pack_backend, _portable_fallback, PORTABLE_FALLBACK, AwqGEMVFastTorchLinear).
  • Removed mechanism shown above (CPU-only fallback, unconditional ROCm refusal).
  • Failure path is shown distinct from success.
  • docs/rocm-rdna4.md (AWQ GEMV-family AUTO resolution) and docs/in-session-model-after-offload.md repeat the diagrams; README.md already links both.

Review Requirements

  • Reviewed this PR.
  • Matches existing project structure, APIs and conventions (awq_gemv_pack_backend is the sibling pattern).
  • No unnecessary monkeypatching; extends the existing selector and pack-backend hooks.
  • Code surface kept minimal: one pack-backend helper, one selector parameterization, one refusal narrowed.
  • Regressions considered: AUTO CUDA order, FORMAT.GEMV behavior, AwqGEMVTorchLinear CPU-only declaration and the pre-existing PORTABLE_CPU_FALLBACK spelling are all unchanged and covered.

Kernel Accuracy Requirements

  • Accuracy measured against an independent Torch fake-quant oracle and against CPU reload expectations; no tolerance was widened.
  • No fusion support change; the readers stay excluded from the fusion inventory.
  • Kernel inventory/device report updated: the two readers move from the CPU-only bucket to ROCM_KERNELS in tests/test_rocm_kernel_contracts.py, which still runs every registered kernel's declaration audit.

Notes

  • Runtime requirement on ROCm: none beyond the existing portable Torch path (no new extension, no Triton requirement).
  • Follow-up, intentionally out of scope: there is still no fused Triton kernel for the interleaved GEMV_FAST layout. This change makes the format usable, not fast; FORMAT.GEMM remains the recommended decode/prefill layout.
  • docs/quantization-guide.md previously said a GEMV_FAST checkpoint could only be produced on CPU or CUDA; it now documents CPU, CUDA and ROCm.

)

#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
@Qubitium
Qubitium marked this pull request as ready for review November 18, 2026 08:18
@Qubitium
Qubitium merged commit f02e1ef into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5109-awq-gemv-fast-rocm branch November 18, 2026 08:18
Qubitium added a commit that referenced this pull request Nov 18, 2026
…#5201) (#5217)

`docs/generate-cpu-offload-autograd.md` cited `gptqmodel/models/base.py:3939`
for `generate`. Three merges after #5165 repaired it, #5176 and #5182 moved
`def generate` to 3948, outside `LINE_WINDOW`, so
`test_docs_contract_refs.py::test_contract_reference_resolves` was red on
`main`.

Cite the symbol instead, as the test's own header already recommends for a
file that moves often. The registry keeps one `SYMBOL_CITATION` entry instead
of the line-anchored duplicate.

Also convert the remaining guarded `base.py:359` citation
(`_quantize_summary_counts`) and add
`test_models_base_citations_use_the_symbol_form`, which rejects any future
line-numbered citation into `models/base.py` in the registry. That file grows
with nearly every fix PR, so a line citation drifts within a couple of merges.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants