Repository navigation
fix(kernels): gate GGUF AUTO selection by qtype and resolve AWQ GEMV on ROCm (#4107) - #4114
Merged
Merged
Conversation
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.
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 throughawq_gemv_pack_backend(is_rocm=...). 12 files, +480/-37.1. The problem
Item 1: GGUF Q4_0/Q5_0 selected
GGUFTritonKernelA GGUF bit width is ambiguous: width 4 is Q4_0 by default but Q4_K and IQ4_NL are also 4-bit.
select_quant_linearreduced theGGUFBitsspec to an integer and asked onlybits 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:2pxgptqmodel/utils/importer.py:1307(quant_bits_width(...))gptqmodel/nn_modules/qlinear/gguf_triton.py:1790gptqmodel/utils/model.py:884-889gptqmodel/looper/weight_only_processor.py:212-229(old)Item 2: AWQ GEMV AUTO picked CUDA-only
AwqGEMVLinearon ROCmflowchart 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:2pxThe 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 forTorchFusedLinear/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:2pxflowchart 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:2px3. 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.GGUFTritonKerneldeclaresSUPPORTS_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_linearresolvesgguf_tensor_qtype(concrete_bits)and rejects a candidate invalidate_candidate(gptqmodel/utils/importer.py:1316,1354-1360), for both AUTO and explicit backends.WeightOnlyProcessor._quant_linear_candidatesrebuilds the AUTO candidate order for the module's resolvedGGUFBits;submodule_finalizeretries the next candidate onNotImplementedError(gptqmodel/looper/weight_only_processor.py:181-211,247-279). The retry is gated onmodel.quantize_backend in {AUTO, AUTO_TRAINABLE}, so an explicitBACKEND.GGUF_TRITONstill fails loudly, matching the load path.gptqmodel/nn_modules/qlinear/gguf.py:304,672,1079,1120). The implementation is bit-identical togguf0.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 existingawq_gemm_pack_backend;models/base.py:1961uses it.BaseQModel.quantize_backendrecords the effective pack backend (gptqmodel/models/base.py:731,2006).DEVICE.CUDAis actually declared; CPU/XPU kernels name their real families (gptqmodel/nn_modules/qlinear/__init__.py:719-744).4. Failure and ambiguity contract
GGUFConfig(bits=4), AUTO quantizeGGUFTritonKernelselected, abort on first moduleGGUFTorchLinearselected, completesGGUFConfig(bits=5, format="q_0"), AUTOGGUFTorchLinearpacks Q5_0BACKEND.GGUF_TRITON+ Q4_0AwqGEMVLinear, abort before calibrationAwqGEMVTritonLinearTorchFusedLinearrejectioncpu, xpu5. Change inventory
gptqmodel/utils/importer.pygptqmodel/nn_modules/qlinear/gguf_triton.pySUPPORTS_GGUF_QTYPES, hookgptqmodel/nn_modules/qlinear/__init__.pygptqmodel/nn_modules/qlinear/gguf.pygptqmodel/looper/weight_only_processor.pygptqmodel/models/base.py,gptqmodel/utils/backend.pytests/test_weight_only.py,tests/test_rocm_awq_gemv_gating.pydocs/gguf-kernel-selection.md,docs/rocm-rdna4.md,README.md6. Tests / guarantees
test_baseqmodel_quantize_allows_direct_gguf_export[q4_0...]GGUFTorchLinear, packed shape correcttest_baseqmodel_quantize_allows_direct_gguf_export[q5_0...]test_gguf_auto_selection_rejects_triton_for_non_fused_qtype[q4_0,q5_0]test_baseqmodel_quantize_gguf_dynamic_qtype_falls_back_to_torchtest_awq_gemv_auto_backend_resolves_triton_on_rocmAWQ_GEMV_TRITONon ROCm,AWQ_GEMVotherwisetest_awq_gemv_auto_quantize_selects_triton_on_rocmAwqGEMVTritonLineartest_torch_fused_rocm_rejection_names_its_real_familiesFail-before proof: on a detached
origin/mainworktree with the new test files copied in, the deterministic tests fail as expected:Post-merge test run (both venvs):
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
Reproduced on both the RTX 4090 and the R9700.
Item 1 pass-after (quantize -> save -> reload -> generate/PPL)
GGUFTorchLinearGGUFTorchLinearGGUFTorchLinearGGUFTorchLinearGGUFTorchLinearGGUFTorchLinearGGUFTorchLinearGGUFTorchLinearQ4_0 PPL 21.34 matches the reporter's 21.135 for
BACKEND.GGUF_TORCHwithin calibration noise.Item 2 fail-before (R9700)
Item 2 pass-after (quantize -> save -> reload -> generate/PPL)
AwqGEMVTritonLinearBACKEND.AWQ_GEMV_TRITONAwqGEMVTritonLinearAwqGEMVLinearBACKEND.AUTOAwqGEMVLinearAWQ GEMV is FP16-only on both platforms, so both runs load
dtype=torch.float16.8. Reviewer notes / limitations
AwqGEMVTritonLinearat priority 0 and refuses the native layout withAWQ_GEMV_ROCM_UNSUPPORTED; reload withBACKEND.AWQ_GEMV_TRITON. Changing load AUTO would silently change existing checkpoints, so it is left out of this fix. The docs state this explicitly.only supports [torch.float16] dtypeerror on both CUDA and ROCm; this is the kernel's existing contract, not a selection bug.ggufextra; it is validated bit-exactly againstgguf0.19.0.origin/mainworktree):tests/test_weight_only.py::test_baseqmodel_quantize_uses_weight_only_rtn_pipeline(qzero_format 2 vs 1) andtests/test_rocm_kernel_contracts.py::test_audit_covers_every_registered_kernel(MLX kernel inventory).tests/test_awq_gemv.pyis a CUDA-targetedmodel/slowtest 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.