Skip to content

fix(paroquant): gate native AWQ GEMM on its real shape and device contract (#3919) - #3934

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-3919
Nov 14, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-3919

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #3919

TL;DR

ParoLinear sent every CUDA tensor into the native AWQ GEMM, whose validator accepts a much narrower shape set than ParoQuant advertises, so an advertised group_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 in linear_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:#c00
Loading
Mechanism at origin/main (8ecc2952) Location Consequence
Unconditional CUDA dispatch gptqmodel/nn_modules/qlinear/paroquant.py:330-334 Any CUDA tensor enters the native GEMM
Native shape contract gptqmodel_ext/awq/quantization/gemm_cuda_gen.cu:1392-1396 OC % 64 == 0, group_size % 32 == 0, OC % group_size == 0
Advertised support paroquant.py:66,73 (SUPPORTS_GROUP_SIZE = [-1, 16, 32, 64, 128], SUPPORTS_OUT_FEATURES_DIVISIBLE_BY = [1]) group_size=16 and any output width are accepted, but cannot run natively
Unmatched dtype pair gptqmodel/nn_modules/qlinear/_numerics.py:189-195 torch.mm(FP16, FP32, out_dtype=FP32) → RuntimeError: input dtypes must be the same

Measured on an RTX 4090 (torch 2.16.0.dev20261102+cu130) before the fix:

in=16, out=8, group_size=16   -> RuntimeError: OC is not multiple of cta_N = 64
in=128, out=64, group_size=16 -> RuntimeError: Group size should be a multiple of 32
R9700, in=32, out=64, gs=32   -> RuntimeError: The native AWQ JIT extension is CUDA-only ...
FP16 x [1,16], FP32 w [8,16]  -> RuntimeError: input dtypes must be the same

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:#c00
Loading

group_size is derived from the packed tensors the kernel reads — in_features // scales.size(0) — not from the config value, because group_size=-1 packs one scale row per input channel and the kernel recomputes it as num_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 --> F
Loading

The out_dtype fast 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 extra promote_types(..., float32) keeps the previous .float() arithmetic for same-dtype inputs bit for bit.

4. Failure and ambiguity contract

Situation Before After
group_size=16, out_features=64 RuntimeError: Group size should be a multiple of 32 Dense fallback
out_features=8 (any group size) RuntimeError: OC is not multiple of cta_N = 64 Dense fallback
group_size=-1, in=128, out=128 Native kernel (derived group size 128) Native kernel, unchanged
group_size=32, out=64 Native kernel Native kernel, unchanged
ROCm, any shape RuntimeError: extension is CUDA-only Portable path
BF16 activation on SM < 8.0 RuntimeError: requires compute capability >= 8.0 Dense fallback
FP16/BF16 activation + FP32 reconstructed grid RuntimeError: input dtypes must be the same Promoted FP32 matmul, one cast

The gate is a capability query. There is no environment switch and no new public option; SUPPORTS_GROUP_SIZE and SUPPORTS_DEVICES are unchanged because the portable paths already honor them.

5. Change inventory

File +/− Purpose
gptqmodel/nn_modules/qlinear/paroquant.py +71 awq_gemm_shape_supported, awq_gemm_bf16_supported, _cuda_awq_kernel_supported, gate call
gptqmodel/nn_modules/qlinear/_numerics.py +23/−8 dtype-aware linear_wide_bias reduction
tests/kernels/test_paroquant.py +215 15 shape-matrix cases + 6 CUDA/ROCm/dtype/storage regressions
docs/paroquant.md +75 Native dispatch gate reference page
README.md +4 Link from the FP32 accumulation section

6. Tests

Test Invariant
test_awq_gemm_shape_supported_matches_native_validator (15 cases) Predicate matches validate_awq_gemm_args, including group_size=-1 and non-divisible input widths
test_cuda_awq_kernel_gate_is_false_off_cuda A CPU module never enters the extension
test_paroquant_cuda_falls_back_for_native_unsupported_shape The issue's 16×8, gs=16 case returns the oracle's 65024, and the native wrappers are never called
test_paroquant_cuda_fallback_promotes_bf16_activations BF16 variant matches the FP32 oracle exactly
test_paroquant_cuda_gate_admits_the_native_supported_shape 32×64, gs=32 stays native; BF16 follows the SM capability probe
test_paroquant_cuda_fallback_retains_only_packed_storage Two fallback forwards leave only packed int32 buffers
test_paroquant_rocm_skips_the_native_cuda_kernel HIP build uses the portable path and matches the oracle
test_linear_wide_bias_promotes_mismatched_cuda_dtypes (fp16, bf16) Promoted matmul matches the FP64 oracle exactly

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.

Command Result
pytest tests/kernels/test_paroquant.py -q (4090) 44 passed, 1 skipped
same file, original source behavior restored 5 failed (the new regressions), 39 passed
pytest tests/kernels/test_paroquant.py -q (R9700) 38 passed, 7 skipped (CUDA-only tests)
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 -q 462 passed, 18 skipped
ruff check gptqmodel/nn_modules/qlinear/paroquant.py gptqmodel/nn_modules/qlinear/_numerics.py tests/kernels/test_paroquant.py All checks passed
issue snippet, 4090 _forward_cuda_awq_kernel -> None, forward -> 65024.0
issue snippet, R9700 _forward_cuda_awq_kernel -> None, forward -> 65024.0

Shape routing measured on the 4090 before/after:

in out group_size derived group size before after
16 8 16 16 OC is not multiple of cta_N = 64 fallback
64 64 32 32 native native
128 64 16 16 Group size should be a multiple of 32 fallback
128 128 -1 128 native native
256 128 128 128 native native
16 8 -1 16 OC is not multiple of cta_N = 64 fallback
512 512 128 128 native native

8. Reviewer notes

  • No validation-time rejection. ParoQuant genuinely supports group_size=16 and narrow outputs through the CPU, ATen and dense paths, so failing closed at _validate would remove working configurations. The gate is per-forward because the same layer can be moved between devices.
  • BF16 SM gate included. validate_bf16_device raises 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).
  • ROCm behavior change. ParoLinear declares DEVICE.ROCM, but every forward previously raised because it tried the CUDA-only extension. It now honors the declaration. test_rocm_kernel_contracts.py passes unchanged.
  • Pre-existing, not caused by this PR: tests/test_paroquant.py has 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_torch fails only when run after tests/test_paroquant.py in the same session, also identically on the base commit.

… 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
@Qubitium
Qubitium merged commit 221cd56 into main Nov 14, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-3919 branch November 14, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ParoQuant CUDA dispatch accepts shapes the native kernel rejects, while fallback has a dtype error

1 participant