Repository navigation
fix(paroquant): gate native AWQ GEMM on its real shape and device contract (#3919) - #3934
Merged
Merged
Conversation
… contract
ParoLinear.forward called the native AWQ CUDA GEMM for every CUDA tensor, but
validate_awq_gemm_args requires uint4b codes, a group size divisible by 32, an
output width divisible by the 64-column CTA tile, and an output width divisible
by the group size. ParoQuant advertises group_size=16 and unconstrained output
widths because its CPU, ATen and dense paths support them, so an advertised
configuration raised from inside the kernel ("OC is not multiple of cta_N =
64", "Group size should be a multiple of 32") instead of using the fallback. A
HIP build reached the CUDA-only extension and raised as well.
Add _cuda_awq_kernel_supported, which mirrors the kernel's own validator using
the packed tensors it actually reads (in_features / scales.size(0), so
group_size=-1 is handled) plus the BF16 SM 8.0 rule, and skip the native path
when it fails. The extension is CUDA-only, so a HIP build always takes the
portable path.
The dense fallback also failed on CUDA with FP16/BF16 activations: it
reconstructs an FP32 grid, and linear_wide_bias then called
torch.mm(FP16, FP32, out_dtype=FP32), which ATen rejects with "input dtypes
must be the same". Use the out_dtype fast path only when both operands share a
low-precision dtype, and otherwise promote the pair to at least FP32 before the
matmul. Nothing is narrowed below FP32, so same-dtype arithmetic is unchanged
bit for bit.
Fixes #3919
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 #3919
TL;DR
ParoLinearsent every CUDA tensor into the native AWQ GEMM, whose validator accepts a much narrower shape set than ParoQuant advertises, so an advertisedgroup_size=16/ narrow-output layer raised from inside the kernel instead of using the fallback. Dispatch now asks the kernel's own shape and device contract first. The dense fallback's CUDA dtype bug (FP16 activation vs the reconstructed FP32 grid) is fixed inlinear_wide_bias. +380/−8 across 5 files.1. The problem
flowchart TD A["ParoLinear.forward"] --> B["_forward_cuda_awq_kernel(rotated)"] B --> C{"x_flat.device.type == 'cuda'?"} C -- No --> F["portable fallbacks"] C -- Yes --> D["_awq_cuda_gemm_forward / _fp32_reduce"] D --> E["validate_awq_gemm_args"] E -- "shape violates gs%32 / OC%64" --> X["RuntimeError from inside the kernel"] E -- "HIP build" --> Y["RuntimeError: extension is CUDA-only"] E -- "shape accepted" --> G["native GEMM"] style X fill:#fee,stroke:#c00 style Y fill:#fee,stroke:#c00origin/main(8ecc2952)gptqmodel/nn_modules/qlinear/paroquant.py:330-334gptqmodel_ext/awq/quantization/gemm_cuda_gen.cu:1392-1396OC % 64 == 0,group_size % 32 == 0,OC % group_size == 0paroquant.py:66,73(SUPPORTS_GROUP_SIZE = [-1, 16, 32, 64, 128],SUPPORTS_OUT_FEATURES_DIVISIBLE_BY = [1])group_size=16and any output width are accepted, but cannot run nativelygptqmodel/nn_modules/qlinear/_numerics.py:189-195torch.mm(FP16, FP32, out_dtype=FP32)→RuntimeError: input dtypes must be the sameMeasured on an RTX 4090 (torch 2.16.0.dev20261102+cu130) before the fix:
2. The new flow
flowchart TD A["ParoLinear.forward"] --> B["_forward_cuda_awq_kernel(rotated)"] B --> C["_cuda_awq_kernel_supported(compute_dtype)"] C --> D{"HIP build or non-CUDA packed weight?"} D -- Yes --> R["return None"] D -- No --> E{"bits == 4?"} E -- No --> R E -- Yes --> F{"group_size % 32 == 0?"} F -- No --> R F -- Yes --> G{"out_features % 64 == 0 and out_features % group_size == 0?"} G -- No --> R G -- Yes --> H{"BF16 activation on SM below 8.0?"} H -- Yes --> R H -- No --> I["Native AWQ GEMM"] R --> J["_forward_cpu_avx512 / _forward_cpu_aten / _forward_dense"] style R fill:#fee,stroke:#c00group_sizeis derived from the packed tensors the kernel reads —in_features // scales.size(0)— not from the config value, becausegroup_size=-1packs one scale row per input channel and the kernel recomputes it asnum_in_channels / scaling_factors.size(0).3. Dense fallback dtype contract
flowchart TD A["linear_wide_bias(x, weight, bias)"] --> B{"CUDA and no wide autograd and x.dtype == w.dtype in fp16/bf16?"} B -- Yes --> C["torch.mm(x, w.t(), out_dtype=fp32): narrow operands, wide reduction"] B -- No --> D["promote_types(promote_types(x.dtype, w.dtype), float32)"] D --> E["torch.mm(x.to(common), w.t().to(common))"] C --> F["add bias in fp32, cast once"] E --> FThe
out_dtypefast path still avoids an expanded FP32 weight copy for same-dtype low-precision operands. A mismatched pair is promoted to at least FP32, never narrowed:promote_types(f16, f32) = f32, and the extrapromote_types(..., float32)keeps the previous.float()arithmetic for same-dtype inputs bit for bit.4. Failure and ambiguity contract
group_size=16,out_features=64RuntimeError: Group size should be a multiple of 32out_features=8(any group size)RuntimeError: OC is not multiple of cta_N = 64group_size=-1,in=128,out=128group_size=32,out=64RuntimeError: extension is CUDA-onlyRuntimeError: requires compute capability >= 8.0RuntimeError: input dtypes must be the sameThe gate is a capability query. There is no environment switch and no new public option;
SUPPORTS_GROUP_SIZEandSUPPORTS_DEVICESare unchanged because the portable paths already honor them.5. Change inventory
gptqmodel/nn_modules/qlinear/paroquant.pyawq_gemm_shape_supported,awq_gemm_bf16_supported,_cuda_awq_kernel_supported, gate callgptqmodel/nn_modules/qlinear/_numerics.pylinear_wide_biasreductiontests/kernels/test_paroquant.pydocs/paroquant.mdREADME.md6. Tests
test_awq_gemm_shape_supported_matches_native_validator(15 cases)validate_awq_gemm_args, includinggroup_size=-1and non-divisible input widthstest_cuda_awq_kernel_gate_is_false_off_cudatest_paroquant_cuda_falls_back_for_native_unsupported_shape16×8, gs=16case returns the oracle's65024, and the native wrappers are never calledtest_paroquant_cuda_fallback_promotes_bf16_activationstest_paroquant_cuda_gate_admits_the_native_supported_shape32×64, gs=32stays native; BF16 follows the SM capability probetest_paroquant_cuda_fallback_retains_only_packed_storageint32bufferstest_paroquant_rocm_skips_the_native_cuda_kerneltest_linear_wide_bias_promotes_mismatched_cuda_dtypes(fp16, bf16)7. Validation
Hardware: RTX 4090 (SM 8.9) with torch 2.16.0.dev20261102+cu130; AMD R9700 (gfx1201) with torch 2.16.0.dev20261025+rocm10.1. Python 3.14.10t.
pytest tests/kernels/test_paroquant.py -q(4090)pytest tests/kernels/test_paroquant.py -q(R9700)pytest tests/kernels/test_linear_fp32_reduce.py tests/test_cpu_bias_cancellation.py tests/kernels/test_fp4_cpu.py tests/kernels/test_awq_cuda_fp32_reduce.py tests/test_rocm_kernel_contracts.py -qruff check gptqmodel/nn_modules/qlinear/paroquant.py gptqmodel/nn_modules/qlinear/_numerics.py tests/kernels/test_paroquant.py_forward_cuda_awq_kernel -> None,forward -> 65024.0_forward_cuda_awq_kernel -> None,forward -> 65024.0Shape routing measured on the 4090 before/after:
OC is not multiple of cta_N = 64Group size should be a multiple of 32OC is not multiple of cta_N = 648. Reviewer notes
group_size=16and narrow outputs through the CPU, ATen and dense paths, so failing closed at_validatewould remove working configurations. The gate is per-forward because the same layer can be moved between devices.validate_bf16_deviceraises below SM 8.0; falling back is strictly better than raising, and it is covered by the capability assertion rather than a hardware-specific test (no pre-Ampere GPU here).ParoLineardeclaresDEVICE.ROCM, but every forward previously raised because it tried the CUDA-only extension. It now honors the declaration.test_rocm_kernel_contracts.pypasses unchanged.tests/test_paroquant.pyhas 15 failures on this host (SimpleNamespace has no attribute 'resolve_module_config', prewarm['load']), identical before and after;tests/kernels/test_paroquant.py::test_paroquant_identity_forward_matches_awq_torchfails only when run aftertests/test_paroquant.pyin the same session, also identically on the base commit.