Skip to content

[BUG] Inference fp16 overflow: QQQ & BnB-int8 route bf16 through fp16 (NaN), several kernels add bias after fp16 rounding (inf) #3977

Closed

Description

@Qubitium

Audit of main @ a16977e. A differential harness against an fp64 dequant reference on an RTX 4090 found these paths clean (finite, relerr ≤ 1e-3 fp16 / 8e-3 bf16): TorchLinear, TritonV2, Marlin, ExllamaV2, TorchAten, TorchFused, AWQ Torch/GEMM/Triton/Marlin/ExllamaV2/Aten. The issues below are the exceptions. Related closed items: #3866, #3743 (same cancellation class, other modules).

1. QQQLinear sends bf16 activations through fp16 → NaN/inf (medium-high)

gptqmodel/nn_modules/qlinear/qqq.py:533-537 casts A to fp16 before per-token int8 quantization, then allocates D and adds the bias in fp16. SUPPORTS_DTYPES (line 125) includes bf16.

  • Any |x| > 65504 → scale inf → x/scale NaN.
  • bf16 outputs > 65504 → inf.

Repro: bf16 input with one outlier channel at 70000 whose weight column is 0 (true output max ~2.3).

  • QQQLinear: 512/512 NaN.
  • QQQTorchLinear: finite.

With large-but-finite outputs, QQQLinear gives inf and QQQTorchLinear has relerr 7.8e-3.

Fix: run dynamic_quant on the original-dtype A (it already accepts bf16/fp32). Then either fall back to QQQTorchLinear for bf16 or drop bf16 from SUPPORTS_DTYPES. Add the bias in fp32.

2. BitsAndBytes 8-bit quantizes bf16/fp32 activations via fp16 (medium)

gptqmodel/nn_modules/qlinear/bitsandbytes.py:958-960: int8_vectorwise_quant(x.to(torch.float16)), followed by an fp16 int8_mm_dequant epilogue (~968). The weight is also packed through weight.to(torch.float16) (~818). SUPPORTS_DTYPES includes bf16 and fp32.

Repro (bits=8, bf16, CUDA):

  • Outlier 70000 on a zero-weight channel: 512/512 NaN.
  • |x| ≤ 604 with true output max 1.68e5: 134 inf.

The 4-bit path is fine.

Fix: compute the row absmax/scale in fp32 from the source dtype, and dequant as mm.float() * x_stats[:,None] * SCB[None,:] / 127**2 + bias.float(), then cast. The CPU tiled path and tests/test_bitsandbytes.py:410 encode the same fp16 reference and need the same update.

3. Bias added after low-precision output rounding (inf before a cancelling bias)

These kernels round the pre-bias result to fp16 and then add the bias:

  • _numerics.py matmul_fp32_reduce: output = torch.matmul(x, weight); return output + bias. It has a ROCm-only _can_fuse_hip_bias, but no CPU branch, unlike linear_fp32_reduce. Callers include torch_awq.py (~309) on CPU and ParoQuant.
  • exllamav2.py:260: out.add_(self.bias).
  • gemm_awq.py:100-104: rows > 1024 use a plain torch.matmul, so no fp32 reduce either.
  • AwqExllamaV2 and Marlin/AwqMarlin (epilogue).
  • CPU TorchAten/TorchFused/TorchCpuAvx512 GPTQ and AWQ (torch_aten_kernel.py:370-373).

Repro: W=42 everywhere, K=128, x=12.5, bias=-2000, fp16; expected 65200.

  • TorchLinear / TritonV2 / AwqTorch-CUDA / AwqGEMMTriton: 65216.
  • matmul_fp32_reduce (CPU), AwqTorch-CPU, Marlin, ExllamaV2, AwqGEMM, AwqMarlin, AwqExllamaV2, CPU Aten/Fused/Avx512: inf.

Fix: add an fp32-output or addmm path with the bias before the final cast, e.g. extend _can_fuse_hip_bias to CPU, and use matmul_fp32_reduce(x, W, bias) in AwqGEMM's large-M branch.

Minor (unverified impact): because of the plain torch.matmul, AwqGEMM's large-M path has ~2× the reduction error of the fp32-reduce kernels (K=11008: 9e-4 vs 4e-4 fp16).

🤖 Generated with Claude Code

Activity

  1. added
    bug
    on Nov 14, 2026
  2. Qubitium commented on Nov 14, 2026

    @Qubitium
    CollaboratorAuthor

    Follow-up review of #4001 found that the fix misses GPTQ TorchFusedLinear, which still overflows to inf. It also found biased layers now take much slower paths: ExLlamaV2 about 7× slower at M=1, CPU fused int4 2.6–21× slower including BF16, and BnB INT8 about 2× slower. AwqExLlamaV2 with bias and LoRA now returns fp32. See #4028.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions