Skip to content

fix(cuda): add quantized linear bias before output rounding - #3649

Merged
Qubitium merged 3 commits into
mainfrom
audit/cuda-next-20261108
Nov 8, 2026
Merged

Qubitium merged 3 commits into
mainfrom
audit/cuda-next-20261108

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The CUDA portable quantized linear paths rounded a wide GEMM to FP16/BF16 before adding bias. A finite result could become inf before 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

Path Before After
Dense GPTQ, AWQ, FP8, GGUF _numerics.py:35 cast the FP32 GEMM to activation dtype; torch.py:413, torch_awq.py:288, fp8.py:648, and gguf.py:1839 added bias afterward matmul_fp32_reduce uses FP32 torch.addmm with bias, then casts once
GPTQ streaming torch.py:571 wrote a narrow tile; torch.py:591 added bias after all tiles Each biased NVIDIA tile adds its bias slice before the output copy
FP8 native scaled matmul fp8.py:614 requested low precision output; fp8.py:648 added bias Biased NVIDIA calls request FP32 output and cast after bias

New 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 failure
Loading

Reference: 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 inf for finite oracle outputs: GPTQ and AWQ 64992, FP8 60640, and GGUF 62752. The streamed and native FP8 cases also reproduced inf before the fix.

Tests:

  • pytest -q tests/kernels/test_cuda_bias_epilogue.py — 12 passed
  • Neighboring AWQ, FP8, GGUF and Torch storage tests — 114 passed, 7 skipped
  • pytest -q tests/test_torch_kernel_accuracy.py -k forward_matches_reference — 22 passed
  • git diff --check and Python compileall — passed

Speed 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 addmm path 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 returned inf; the corrected paths returned 64992 and 60640 respectively. No persistent dense or expanded weight cache is introduced.

The preexisting CUDA out_dtype GEMM autograd limitation is outside this inference fix; it will be handled separately.

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.

1 participant