Repository navigation
fix(awq): add the bias to the FP32 GEMV reduction and keep split-K partials wide (#5736) - #5947
Merged
Merged
Conversation
…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
force-pushed
the
fix/awq-gemv-fp32-bias-5736
branch
from
November 20, 2026 17:01
bd2e862 to
1b05c4a
Compare
Qubitium
marked this pull request as ready for review
November 20, 2026 17:06
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.
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 becomesinfor
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_prefilland
gemmv2_forward_cudagained a trailingfp32_outputflag. Outputstores are templated (
store_gemv_output/store_gemm_output/store_accum_value); the default stays FP16, so every other caller isunchanged.
gemmv2_forward_cudaandgrouped_gemm_forwardnow always keep theirsplit-K partials in FP32 and reduce once before the single cast, matching
the existing
gemm_forward_fp32_reducesplit-K contract.AwqGEMVLinear/AwqGEMVFastLinearrequest the wide output whenever abias is present, add the stored bias in place, and cast once. This mirrors
the wide-bias epilogue already used by
AwqGEMMLinear.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 sixfail on the PR base (
inf/nan) and pass on the head. Two of the nodesdrive the real
AwqGEMVLinear/AwqGEMVFastLinearmodules (including theM>192gemm_w4a16_T2prefill kernel and theM>8gemmv2 path); onedrives the raw
gemmv2entry point.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/cu13wheel, Python 3.14t. Base =origin/mainsources witha 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):
M=1outputinfM=1outputinfM=2outputinfM=256(T2 kernel)infM=16split-K cancellation (±144,000 per split, sum 0)nanAccuracy against an independent FP64 dequantized-weight oracle (random
codes/scales, max relative error):
M=1rel. errorM=2rel. errorM=16rel. errorM=16, FP32 output rel. errorLatency (median of 300 iterations after 3,000 warmup iterations, CUDA
events, microseconds; same script for both revisions):
M=1, biased layerM=1, unbiased layerM=16, biased layerThe 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:
/tmpscripts drivingAwqGEMVLinearforward andawq_gemmv2_forwardwith the fixtures above; latency usestorch.cuda.Eventpairs 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
fp32_outputpattern asgemm_forward_fp32_reduce).arguments with defaults.
fp32_output=Falsekeeps everyexisting caller bit-identical; only biased GEMV/GEMV_FAST modules and
split-K partial storage change.
Kernel Accuracy Requirements
oracle (table above); every case is within the 2e-3 relative contract.
exercised; ROCm, other SM architectures and the machete/CUTLASS paths
were not run.
Notes
fp32_outputflag defaults toFalseat every binding and wrapper,so out-of-tree callers keep the previous dtype.
grouped_gemm_forwardalways returns the activation dtype; its internalpartials are now FP32 unconditionally.