Skip to content

fix(awq): add the bias to the FP32 GEMV reduction and keep split-K partials wide (#5736) - #5947

Merged
Qubitium merged 1 commit into
mainfrom
fix/awq-gemv-fp32-bias-5736
Nov 20, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/awq-gemv-fp32-bias-5736

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

The native AWQ GEMV, GEMV_FAST and gemmv2 kernels accumulate in FP32 but
stored their result (and every gemmv2 split-K partial) as FP16. The Python
bias epilogue then added the bias to that already-rounded value. When the
product is above the FP16 maximum but the biased sum is finite, the stored
value becomes inf; a split-K cancellation across partials becomes inf
or nan.

This keeps the wide result until the bias has joined the complete
reduction, so the value is rounded exactly once.

Fixes #5736

What Changed

  • gemv_forward_cuda, gemv_forward_cuda_decode, gemm_forward_cuda_prefill
    and gemmv2_forward_cuda gained a trailing fp32_output flag. Output
    stores are templated (store_gemv_output / store_gemm_output /
    store_accum_value); the default stays FP16, so every other caller is
    unchanged.
  • gemmv2_forward_cuda and grouped_gemm_forward now always keep their
    split-K partials in FP32 and reduce once before the single cast, matching
    the existing gemm_forward_fp32_reduce split-K contract.
  • AwqGEMVLinear / AwqGEMVFastLinear request the wide output whenever a
    bias is present, add the stored bias in place, and cast once. This mirrors
    the wide-bias epilogue already used by AwqGEMMLinear.
  • Removed the incorrect "CUDA FP16 add already accumulates in FP32 and is
    bitwise identical" comment that justified the old FP16 store.

Out of scope: ROCm/Triton GEMV epilogues and the CUTLASS/machete paths were
not touched.

Tests

New tests/test_awq_gemv_bias_epilogue_5736.py (6 nodes, CUDA). All six
fail on the PR base (inf / nan) and pass on the head. Two of the nodes
drive the real AwqGEMVLinear / AwqGEMVFastLinear modules (including the
M>192 gemm_w4a16_T2 prefill kernel and the M>8 gemmv2 path); one
drives the raw gemmv2 entry point.

# extension (JIT) built from this tree with CUDA 13.2 nvcc
CUDA_HOME=<nvidia cu13 wheel> PYTHONPATH=. GPTQMODEL_AWQ_FORCE_REBUILD=1 \
  python -c "from gptqmodel.utils.awq import prewarm_awq_extension; prewarm_awq_extension()"

pytest tests/test_awq_gemv_bias_epilogue_5736.py tests/test_awq_gemv_fast_jit.py \
       tests/test_kernel_dispatch_caching.py tests/kernels/test_awq_gemv_splitk.py \
       tests/test_awq_cuda_contract.py tests/test_awq_fp32_output.py \
       tests/test_awq_dequant_layouts.py -q -p no:randomly
# 162 passed, 2 skipped
  • Unit tests executed and passed.
  • New targeted regression added.
  • bash format/check.sh (ruff) passes.

Pre-existing environment failures: checkpoint-dependent tests that need a
local model directory (absent here) fail identically before and after this
change; they are unrelated to the kernels.

Measured Impact

Environment: one NVIDIA GB10 (sm_121), torch 2.15.0+cu132, CUDA 13.2 nvcc
from the nvidia/cu13 wheel, Python 3.14t. Base = origin/main sources with
a forced rebuild; head = this PR with a forced rebuild. Only this GPU was
tested; ROCm and other SM architectures are unverified.

Correctness, bias-overflow fixture (in=out=128, group_size=128,
codes 15, scales 1, zeros 0, activation 52 → FP32 product 99,840; bias
-50,000 stored in FP16 is -49,984, so the contract sum is 49,856):

Metric (unit) Before After Change
GEMV M=1 output inf 49,856 finite
GEMV_FAST decode M=1 output inf 49,856 finite
GEMV_FAST prefill M=2 output inf 49,856 finite
GEMV_FAST prefill M=256 (T2 kernel) inf 49,856 finite
gemmv2 M=16 split-K cancellation (±144,000 per split, sum 0) nan 0 exact

Accuracy against an independent FP64 dequantized-weight oracle (random
codes/scales, max relative error):

Metric (unit) Before After Change
GEMV M=1 rel. error 4.59e-4 3.23e-4 -1.36e-4
GEMV_FAST decode rel. error 6.12e-4 5.31e-4 -8.1e-5
GEMV_FAST prefill M=2 rel. error 3.09e-4 3.09e-4 0
gemmv2 M=16 rel. error 4.14e-4 3.38e-4 -7.6e-5
gemmv2 M=16, FP32 output rel. error n/a (unsupported) 1.83e-4 new

Latency (median of 300 iterations after 3,000 warmup iterations, CUDA
events, microseconds; same script for both revisions):

Metric (unit) Before After Change
GEMV M=1, biased layer 58.4 64.5 +6.1 µs (+10.4%)
GEMV M=1, unbiased layer 45.7 46.2 +0.5 µs (+1.1%, run noise ≤0.5 µs)
gemmv2 M=16, biased layer 67.2 71.5 +4.3 µs (+6.4%)

The biased overhead is the single FP32→activation cast that replaces the old
FP16 store plus FP16 add; unbiased layers take the unchanged FP16 store path.
The split-K partial buffer doubles in bytes (FP32 instead of FP16) but stays
small relative to the packed weights.

Benchmark commands: /tmp scripts driving AwqGEMVLinear forward and
awq_gemmv2_forward with the fixtures above; latency uses
torch.cuda.Event pairs around each call.

Model Support / Module Plan

Not applicable: no model, alias, registry, or module-plan change.

Logic Flow Documentation

Not applicable: no control-flow or resolution-order change. The same
dispatch sites now pass a dtype flag to the existing kernels.

Review Requirements

  • Reviewed this PR.
  • Matches existing project structure, APIs, and conventions (same
    fp32_output pattern as gemm_forward_fp32_reduce).
  • No monkeypatching; the extension schema gained optional trailing
    arguments with defaults.
  • Impacted code surface minimized.
  • Considered regressions: default fp32_output=False keeps every
    existing caller bit-identical; only biased GEMV/GEMV_FAST modules and
    split-K partial storage change.

Kernel Accuracy Requirements

  • Accuracy drift measured against an independent FP64 dequantized
    oracle (table above); every case is within the 2e-3 relative contract.
  • No fusion support changes.
  • Inventory limits: only the AWQ CUDA JIT extension on sm_121 was
    exercised; ROCm, other SM architectures and the machete/CUTLASS paths
    were not run.

Notes

  • The fp32_output flag defaults to False at every binding and wrapper,
    so out-of-tree callers keep the previous dtype.
  • grouped_gemm_forward always returns the activation dtype; its internal
    partials are now FP32 unconditionally.

…rtials wide (#5736)

The native AWQ GEMV and GEMV_FAST kernels accumulate in FP32 but stored
the result as FP16 before the Python bias epilogue.  When the product
exceeds 65504 the store overflows to inf even though adding the bias
afterwards would be finite.  gemmv2 (and the grouped MoE path) had the
same problem for every split-K partial, so a cancellation across splits
turned into inf/nan.

- gemv_forward_cuda / gemv_forward_cuda_decode / gemm_forward_cuda_prefill
  / gemmv2_forward_cuda gained a trailing fp32_output flag and templated
  output stores; the default stays FP16 for compatibility.
- gemmv2 partials and the grouped-GEMM partials are now always FP32 and
  are reduced once before the single cast to the activation dtype.
- AwqGEMVLinear / AwqGEMVFastLinear request the wide output whenever a
  bias is present and add the bias to that FP32 result.

Fixes #5736
@Qubitium
Qubitium force-pushed the fix/awq-gemv-fp32-bias-5736 branch from bd2e862 to 1b05c4a Compare November 20, 2026 17:01
@Qubitium
Qubitium marked this pull request as ready for review November 20, 2026 17:06
@Qubitium
Qubitium merged commit 140673d into main Nov 20, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/awq-gemv-fp32-bias-5736 branch November 20, 2026 17:06
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.

[Audit][awq-gemv] GEMV/GEMV_FAST CUDA paths round to FP16 before the bias add; gemmv2 (rows>8) stores split-K partials in FP16

1 participant