Skip to content

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

Closed

Description

@Qubitium

Summary

The AWQ GEMV-format and GEMV_FAST CUDA paths (AwqGEMVLinear, AwqGEMVFastLinear, LLMAwqLinear) break the linear-bias / wide-reduction contract in .agents/skills/gptqmodel-torch-oracle-numerics in two places:

  1. gemmv2 (GEMV format, rows > 8, i.e. every prefill): the kernel writes each of the split_k_iters = 8 split-K partials as FP16 (_out_feats is allocated with the input dtype). The host then returns _out_feats.sum(0). Each partial is rounded to FP16, and can overflow, before the reduction finishes. The GEMM-format path already fixed this: gemm_forward_cuda_fp32_reduce / launch_gemm_forward_cuda_fp32_reduce keep FP32 partials and use a fused reduce.
  2. Bias epilogue (GEMV, GEMV_FAST decode and prefill): the native kernels return an FP16 product. The wrapper then does torch.add(out, self.bias) in FP16. The full reduction is rounded to FP16 first, and only then is the bias added and rounded again. The contract says to add the bias to the wide reduction before the single cast. A partial sum outside the FP16 range turns into inf even when the bias brings it back into range.

Impact

  • Post-quant accuracy. Severity is low to medium.
    • Normal data: about 2x more output error than the single-rounding contract. In the emulation below, relative error is 7.0e-4 for gemmv2 and 5.7e-4 for round-then-bias, against 3.1e-4 for the contract. That is still inside the 2e-3 inference limit.
    • Edge case: inf output where the contract result is finite. This happens when a split-K partial or the pre-bias sum is greater than 65504.
  • Speed is not affected.

Location (@ 85caf6c)

  • gptqmodel/nn_modules/qlinear/gemv_awq.py:230-253:
if inputs.shape[0] > 8:
    out = awq_gemmv2_forward(inputs, self.qweight, self.scales, self.qzeros, self.group_size, self.split_k_iters)
else:
    out = awq_gemv_forward(...)
if self.bias is not None:
    if input_dtype == torch.float16 and self.bias.dtype == torch.float16:
        out = torch.add(out, self.bias)          # out is already an FP16-rounded product
  • gptqmodel/nn_modules/qlinear/gemv_fast_awq.py:228-237: the same epilogue after awq_fast_gemv_forward_decode / awq_fast_gemm_forward_prefill.
  • gptqmodel_ext/awq/quantization/gemm_cuda_gen.cu:1301-1342 (gemmv2_forward_cuda):
auto options = torch::TensorOptions().dtype(_in_feats.dtype())...;
at::Tensor _out_feats = torch::empty({split_k_iters, num_in_feats, _kernel.size(0)}, options);  // FP16 partials
...
return _out_feats.sum(0);

The same pattern appears at :1115/:1156 for the MoE grouped variant.

  • gptqmodel_ext/awq/quantization/gemv_cuda.cu:272 and quantization_new/gemv/gemv_cuda.cu:263: FP16 output allocations, so the epilogue has no FP32 product to add the bias to.

Mechanism

The FP32 accumulators in each CTA are stored as half. For gemmv2 that happens per split-K slice. The reduction and the bias add then run on values that were already rounded, which gives two or more roundings instead of one. A finite partial above 65504 saturates to inf. The comment at gemv_awq.py:245 ("A CUDA FP16 add already accumulates in FP32 and rounds once") is true only for the add itself. The operand out has already been rounded.

Reproduction / Validation

The native kernels need CUDA, so the kernels themselves were not executed. What follows is a pure-torch CPU emulation of the arithmetic, audit_kernels/emulate_awq_gemv_epilogue.py, executed on CPU with torch 2.14 dev. Shape M=16, K=N=4096, split_k=8, FP16, fp64 oracle:

rel err vs fp64: contract 3.06e-04  gemmv2(rows>8) 7.01e-04  gemv(round-then-bias) 5.66e-04
overflow case: contract 49984.0  round-then-bias inf

Suggested fix

  • In gemmv2_forward_cuda: allocate _out_feats as kFloat32, have the kernel store float partials (the template already accumulates in FP32), and reduce with the existing maybe_fused_reduce_splitk_fp32_to_output. Better, add a fp32_output flag so the wrapper can add the bias in FP32. This mirrors gemm_forward_cuda_fp32_reduce.
  • For the GEMV / GEMV_FAST decode kernels: add an FP32-output (or fused-bias) variant and in Python do out = (out_fp32 + bias.float()).to(input_dtype). Until that exists, the contract's fallback rule applies: route biased layers whose pre-bias sum can leave the FP16 range to a wide path.

Notes

  • Emulation only. The exact error depends on the kernel's internal accumulation order, and the overflow case requires partial sums above 65504.
  • The AwqGEMVTriton and torch (portable) variants were not reviewed for this; they appear to use FP32 epilogues already.

Found by automated code audit (Claude Code).

Activity

  1. added
    bug
    on Nov 20, 2026
  2. added
    scanned
    safe
    on Nov 20, 2026
  3. Qubitium commented on Nov 20, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-fb5145b84190 finished this issue.

    Pull request: #5947

    fix AWQ GEMV/GEMV_FAST FP32 split-K partials and bias epilogue; GB10 sm_121 with venv CUDA 13.5 nvcc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugsafescanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions