Repository navigation
fix(cuda): add quantized linear bias before output rounding - #3649
Merged
Merged
Conversation
This was referenced Nov 9, 2026
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 CUDA portable quantized linear paths rounded a wide GEMM to FP16/BF16 before adding bias. A finite result could become
infbefore bias cancellation. Add bias in FP32 before the output cast for GPTQ eager and streaming, AWQ, FP8 dense and native scaled matmul, and GGUF dense fallback.Previous mechanism
_numerics.py:35cast the FP32 GEMM to activation dtype;torch.py:413,torch_awq.py:288,fp8.py:648, andgguf.py:1839added bias afterwardmatmul_fp32_reduceuses FP32torch.addmmwith bias, then casts oncetorch.py:571wrote a narrow tile;torch.py:591added bias after all tilesfp8.py:614requested low precision output;fp8.py:648added biasNew flow
flowchart TD A["Quantized linear forward"] --> B{"NVIDIA CUDA and FP16 or BF16?"} B -- "No" --> C["Existing CPU or ROCm arithmetic"] B -- "Yes" --> D{"Backend"} D -- "Dense GPTQ, AWQ, FP8, GGUF" --> E["matmul_fp32_reduce with bias"] D -- "GPTQ streaming" --> F["matmul_fp32_reduce per output tile"] D -- "FP8 scaled matmul" --> G["_forward_scaled_mm with FP32 output"] E --> H["Add bias in FP32"] F --> H G --> H H --> I["Cast once to activation dtype"] D -- "Native FP8 call fails" --> J["Disable native path and use FP8 dense fallback"] J --> E classDef failure fill:#fce8e6,stroke:#a33,color:#111 class J failureReference: CUDA quantized linear bias epilogue, linked from the README.
Actual-device validation
NVIDIA GB10, SM 12.1; PyTorch 2.15.0+cu132. The packed-layer regression tests use an independent constant-weight FP64 oracle and cover FP16/BF16, repeated forwards, GPTQ eager and three-tile streaming on a nondefault stream, AWQ, FP8 dense and native scaled matmul, and GGUF dense fallback. All 12 cases matched the rounded oracle exactly and retained no dense weight.
The original FP16 outputs were
inffor finite oracle outputs: GPTQ and AWQ64992, FP860640, and GGUF62752. The streamed and native FP8 cases also reproducedinfbefore the fix.Tests:
pytest -q tests/kernels/test_cuda_bias_epilogue.py— 12 passedpytest -q tests/test_torch_kernel_accuracy.py -k forward_matches_reference— 22 passedgit diff --checkand Python compileall — passedSpeed and storage
CUDA event timing on resident 1024×1024 FP16/BF16 operands, 50 warmups then median of 7×100 calls. For M=1, the old matmul-then-bias path took 0.029–0.030 ms and the corrected
addmmpath 0.035–0.036 ms; for M=128, 0.032 ms versus 0.037–0.038 ms. Measured allocator peak delta, including cuBLAS scratch, fell from about 32–32.5 MiB to 1–1.5 MiB. Full-path wall timing at M=2, K=32: three-tile GPTQ streaming 0.621→0.735 ms and native FP8 scaled matmul 0.026→0.042 ms. These small-shape timings include Python dispatch and synchronization. The old paths returnedinf; the corrected paths returned64992and60640respectively. No persistent dense or expanded weight cache is introduced.The preexisting CUDA
out_dtypeGEMM autograd limitation is outside this inference fix; it will be handled separately.