Skip to content

fix(kernels): gate GGUF AUTO selection by qtype and resolve AWQ GEMV on ROCm (#4107) - #4114

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4107-gguf-auto
Nov 15, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4107-gguf-auto

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #4107

TL;DR

Two AUTO-selection bugs. GGUF selection compared only the integer bit width, so Q4_0/Q5_0 selected the fused Triton kernel and aborted in quantize(); the load path survived because it retried candidates, the weight-only finalize path did not. AWQ GEMV hardcoded the CUDA-only native kernel on ROCm. The selector now resolves the concrete ggml layout, the weight-only finalize path retries the validated AUTO candidates, and AWQ GEMV resolves through awq_gemv_pack_backend(is_rocm=...). 12 files, +480/-37.

1. The problem

Item 1: GGUF Q4_0/Q5_0 selected GGUFTritonKernel

A GGUF bit width is ambiguous: width 4 is Q4_0 by default but Q4_K and IQ4_NL are also 4-bit. select_quant_linear reduced the GGUFBits spec to an integer and asked only bits in SUPPORTS_BITS.

flowchart TD
    A["select_quant_linear(bits)"] --> B["quant_bits_width(bits)<br/>4 / 5 / 6"]
    B --> C{"bits in<br/>SUPPORTS_BITS?"}
    C -- "yes" --> D["GGUFTritonKernel selected"]
    C -- "no" --> E["next candidate"]
    D --> F["constructor<br/>Q4_0 / Q5_0 rejected"]
    F --> G["load: create_quant_layer retries<br/>-> GGUFTorchLinear"]
    F --> H["quantize: one class, no retry<br/>-> NotImplementedError aborts"]
    style F fill:#f8d7da,stroke:#b02a37,stroke-width:2px
    style H fill:#f8d7da,stroke:#b02a37,stroke-width:2px
Loading
Mechanism Before Evidence
AUTO layout match integer width only gptqmodel/utils/importer.py:1307 (quant_bits_width(...))
Triton layout contract constructor-only set literal gptqmodel/nn_modules/qlinear/gguf_triton.py:1790
Load fallback retries candidates gptqmodel/utils/model.py:884-889
Quantize finalize one class, no retry gptqmodel/looper/weight_only_processor.py:212-229 (old)

Item 2: AWQ GEMV AUTO picked CUDA-only AwqGEMVLinear on ROCm

flowchart TD
    A["quantize(format=GEMV, backend=AUTO)"] --> B["preferred_backend =<br/>BACKEND.AWQ_GEMV"]
    B --> C["select_quant_linear<br/>AwqGEMVLinear"]
    C --> D["validate_device(DEVICE.ROCM)"]
    D --> E["ValueError before calibration<br/>suggests *Torch, which GEMV rejects"]
    style E fill:#f8d7da,stroke:#b02a37,stroke-width:2px
Loading

The hardcoded branch was gptqmodel/models/base.py:1941 (preferred_backend = BACKEND.AWQ_GEMV). The generic rejection message also called every non-ROCm kernel "CUDA-only", which is wrong for TorchFusedLinear/TorchFusedAwqLinear (SUPPORTS_DEVICES = [CPU, XPU]).

2. The new flow

flowchart TD
    A["select_quant_linear(bits, format=GGUF)"] --> B["_normalize_quant_bits<br/>-> GGUFBits"]
    B --> C["gguf_tensor_qtype(concrete_bits)<br/>Q4_0 / Q5_0 / Q4_K / ..."]
    C --> D{"candidate.supports_gguf_qtype?"}
    D -- "no" --> X["skip candidate<br/>NotImplementedError recorded"]
    D -- "yes" --> E["validate_quant_linear<br/>device, dtype, bits, shape"]
    E -- "reject" --> X
    E -- "accept" --> F["candidate order<br/>Triton -> cpp -> Torch"]
    X --> F
    F --> G["make_quant / create_quant_layer<br/>per-module retry"]
    F --> H["WeightOnlyProcessor.submodule_finalize<br/>per-module retry"]
    G --> I["concrete kernel"]
    H --> I
    style X fill:#f8d7da,stroke:#b02a37,stroke-width:2px
Loading
flowchart TD
    A["quantize(format=GEMV, backend=AUTO)"] --> B{"IS_ROCM?"}
    B -- "yes" --> C["awq_gemv_pack_backend<br/>BACKEND.AWQ_GEMV_TRITON"]
    B -- "no" --> D["awq_gemv_pack_backend<br/>BACKEND.AWQ_GEMV"]
    C --> E["select_quant_linear<br/>AwqGEMVTritonLinear"]
    D --> F["select_quant_linear<br/>AwqGEMVLinear"]
    E --> G["quantize -> save -> reload<br/>with BACKEND.AWQ_GEMV_TRITON"]
    F --> H["quantize -> save -> AUTO reload"]
    G -. "AUTO load keeps the existing refusal" .-> I["AWQ_GEMV_ROCM_UNSUPPORTED"]
    style I fill:#f8d7da,stroke:#b02a37,stroke-width:2px
Loading

3. Per-item detail

Item 1

  • BaseQuantLinear.supports_gguf_qtype(bits) (gptqmodel/nn_modules/qlinear/__init__.py:651) is the new kernel hook; the default accepts every layout.
  • GGUFTritonKernel declares SUPPORTS_GGUF_QTYPES = {Q1_0, Q1_0_g128, Q4_K, Q5_K, Q6_K} and overrides the hook (gptqmodel/nn_modules/qlinear/gguf_triton.py:1736,1799). The constructor now reads the same declaration.
  • select_quant_linear resolves gguf_tensor_qtype(concrete_bits) and rejects a candidate in validate_candidate (gptqmodel/utils/importer.py:1316,1354-1360), for both AUTO and explicit backends.
  • WeightOnlyProcessor._quant_linear_candidates rebuilds the AUTO candidate order for the module's resolved GGUFBits; submodule_finalize retries the next candidate on NotImplementedError (gptqmodel/looper/weight_only_processor.py:181-211,247-279). The retry is gated on model.quantize_backend in {AUTO, AUTO_TRAINABLE}, so an explicit BACKEND.GGUF_TRITON still fails loudly, matching the load path.
  • The Torch fallback now packs and dequantizes Q5_0 without optional dependencies (gptqmodel/nn_modules/qlinear/gguf.py:304,672,1079,1120). The implementation is bit-identical to gguf 0.19.0 on random blocks (pack bytes equal; dequant max error 0).

Item 2

  • awq_gemv_pack_backend(is_rocm) (gptqmodel/utils/backend.py:279) mirrors the existing awq_gemm_pack_backend; models/base.py:1961 uses it.
  • BaseQModel.quantize_backend records the effective pack backend (gptqmodel/models/base.py:731,2006).
  • The ROCm rejection message now only says "CUDA-only" when DEVICE.CUDA is actually declared; CPU/XPU kernels name their real families (gptqmodel/nn_modules/qlinear/__init__.py:719-744).

4. Failure and ambiguity contract

Situation Before After
GGUFConfig(bits=4), AUTO quantize GGUFTritonKernel selected, abort on first module GGUFTorchLinear selected, completes
GGUFConfig(bits=5, format="q_0"), AUTO same abort GGUFTorchLinear packs Q5_0
Q4_K base + dynamic Q4_0 module, AUTO base kernel selected, abort on the Q4_0 module per-module Torch fallback
Explicit BACKEND.GGUF_TRITON + Q4_0 abort in the constructor fail during selection with the layout named
ROCm AWQ GEMV AUTO quantize CUDA-only AwqGEMVLinear, abort before calibration AwqGEMVTritonLinear
ROCm TorchFusedLinear rejection claimed "CUDA-only" names cpu, xpu

5. Change inventory

Area File +/- Purpose
GGUF selection gptqmodel/utils/importer.py +18/-1 qtype-aware candidate gate
GGUF kernel gptqmodel/nn_modules/qlinear/gguf_triton.py +12/-2 SUPPORTS_GGUF_QTYPES, hook
GGUF base gptqmodel/nn_modules/qlinear/__init__.py +33/-8 default hook, accurate ROCm message
GGUF packing gptqmodel/nn_modules/qlinear/gguf.py +57/-0 Q5_0 pure-NumPy pack/dequant
Weight-only finalize gptqmodel/looper/weight_only_processor.py +89/-31 per-module AUTO fallback
AWQ GEMV gptqmodel/models/base.py, gptqmodel/utils/backend.py +35/-2 ROCm-aware backend resolution
Tests tests/test_weight_only.py, tests/test_rocm_awq_gemv_gating.py +129/-2 fail-before regressions
Docs docs/gguf-kernel-selection.md, docs/rocm-rdna4.md, README.md +110/-6 flow, contract, matrix

6. Tests / guarantees

Test Invariant
test_baseqmodel_quantize_allows_direct_gguf_export[q4_0...] AUTO quantize uses GGUFTorchLinear, packed shape correct
test_baseqmodel_quantize_allows_direct_gguf_export[q5_0...] same for Q5_0
test_gguf_auto_selection_rejects_triton_for_non_fused_qtype[q4_0,q5_0] selector rejects Triton without executing a kernel
test_baseqmodel_quantize_gguf_dynamic_qtype_falls_back_to_torch Q4_K base + Q4_0 dynamic module picks per-module kernels
test_awq_gemv_auto_backend_resolves_triton_on_rocm AUTO backend is AWQ_GEMV_TRITON on ROCm, AWQ_GEMV otherwise
test_awq_gemv_auto_quantize_selects_triton_on_rocm quantize resolution yields AwqGEMVTritonLinear
test_torch_fused_rocm_rejection_names_its_real_families CPU/XPU kernels are not called CUDA-only

Fail-before proof: on a detached origin/main worktree with the new test files copied in, the deterministic tests fail as expected:

4 failed: q4_0/q5_0 selection (Triton chosen), awq_gemv_pack_backend ImportError,
          TorchFused "CUDA-only" assertion
1 failed: test_baseqmodel_quantize_gguf_dynamic_qtype_falls_back_to_torch
          NotImplementedError: GGUFTritonKernel ... Actual GGUF qtype: Q4_0

Post-merge test run (both venvs):

CUDA_VISIBLE_DEVICES=GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae \
  PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest -q \
  tests/test_rocm_awq_gemv_gating.py tests/test_weight_only.py -k "gguf or awq"
-> 80 passed, 9 skipped, 11 deselected

HIP_VISIBLE_DEVICES=0 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest -q \
  tests/test_rocm_awq_gemv_gating.py tests/test_weight_only.py -k "gguf or awq"
-> 80 passed, 9 skipped, 11 deselected

ruff check gptqmodel docs/eora tests setup.py -> All checks passed!

7. Validation

Hardware: RTX 4090 (Ada, SM 8.9, GPU-1892aea4-cc09-827c-b9a5-93d4eef7f5ae, torch 2.16.0.dev+cu130, Triton 3.9.0) and AMD R9700 gfx1201 (HIP_VISIBLE_DEVICES=0, torch 2.16.0.dev+rocm10.1). Model: Qwen2.5-0.5B-Instruct.

Item 1 fail-before

GGUFConfig(bits=4)  -> NotImplementedError: GGUFTritonKernel only supports fused GGUF
                       Triton formats (Q1_0, Q4_K, Q5_K, Q6_K). Actual GGUF qtype: Q4_0.
GGUFConfig(bits=5, format="q_0") -> same with Actual GGUF qtype: Q5_0.

Reproduced on both the RTX 4090 and the R9700.

Item 1 pass-after (quantize -> save -> reload -> generate/PPL)

Platform Layout Quantize kernel Reload Generation PPL sanity
RTX 4090 Q4_0 GGUFTorchLinear GGUFTorchLinear "The capital of France is Paris. It is the largest city in" 21.34
RTX 4090 Q5_0 GGUFTorchLinear GGUFTorchLinear "The capital of France is Paris. It is the largest city in" 17.36
R9700 Q4_0 GGUFTorchLinear GGUFTorchLinear "The capital of France is Paris. It is the largest city in" 21.32
R9700 Q5_0 GGUFTorchLinear GGUFTorchLinear "The capital of France is Paris. It is the largest city in" 17.31

Q4_0 PPL 21.34 matches the reporter's 21.135 for BACKEND.GGUF_TORCH within calibration noise.

Item 2 fail-before (R9700)

ValueError: <class 'gptqmodel.nn_modules.qlinear.gemv_awq.AwqGEMVLinear'> does not support
device: DEVICE.ROCM (AwqGEMVLinear is CUDA-only ...)

Item 2 pass-after (quantize -> save -> reload -> generate/PPL)

Platform Quantize kernel Reload backend Reload kernel Generation PPL sanity
R9700 AwqGEMVTritonLinear BACKEND.AWQ_GEMV_TRITON AwqGEMVTritonLinear "The capital of France is Paris. It is located in the south" 18.62
RTX 4090 AwqGEMVLinear BACKEND.AUTO AwqGEMVLinear "The capital of France is Paris. The capital of France is Paris" 17.77

AWQ GEMV is FP16-only on both platforms, so both runs load dtype=torch.float16.

8. Reviewer notes / limitations

  • AUTO load of a GEMV checkpoint on ROCm is unchanged. The generic AUTO load order still keeps AwqGEMVTritonLinear at priority 0 and refuses the native layout with AWQ_GEMV_ROCM_UNSUPPORTED; reload with BACKEND.AWQ_GEMV_TRITON. Changing load AUTO would silently change existing checkpoints, so it is left out of this fix. The docs state this explicitly.
  • AWQ GEMV remains FP16-only. A BF16 model still fails with a clear only supports [torch.float16] dtype error on both CUDA and ROCm; this is the kernel's existing contract, not a selection bug.
  • Q5_0 packing is new. The pure-NumPy pack/dequant is required because the CUDA/ROCm venvs do not install the optional gguf extra; it is validated bit-exactly against gguf 0.19.0.
  • Pre-existing failures, not caused by this PR (verified on a detached origin/main worktree): tests/test_weight_only.py::test_baseqmodel_quantize_uses_weight_only_rtn_pipeline (qzero_format 2 vs 1) and tests/test_rocm_kernel_contracts.py::test_audit_covers_every_registered_kernel (MLX kernel inventory).
  • tests/test_awq_gemv.py is a CUDA-targeted model/slow test that already failed on ROCm before this change at the native-kernel rejection; it now stops at the FP16-only dtype check. It is not part of the ROCm gate.

@Qubitium
Qubitium merged commit 33ad833 into main Nov 15, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4107-gguf-auto branch November 15, 2026 09:42
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