Skip to content

fix(qlinear): keep FP16 reductions wide and stop BF16-through-FP16 quantization (#3977) - #4001

Merged
Qubitium merged 3 commits into
mainfrom
fix/issue-3977
Nov 14, 2026
Merged

Qubitium merged 3 commits into
mainfrom
fix/issue-3977

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3977

Root cause

Three independent paths narrowed or rounded low-precision data before it was safe:

  1. QQQ (QQQLinear.forward) cast BF16 activations to FP16 before the per-token int8 quantizer and advertised BF16 in SUPPORTS_DTYPES. An activation above 65504 became inf, so the row scale was inf and x / scale produced NaN.
  2. BitsAndBytes 8-bit called int8_vectorwise_quant(x.to(float16)), packed the weight through weight.to(float16), and ran the int8_mm_dequant epilogue in FP16. Any BF16/FP32 magnitude above 65504 became inf/NaN, and a finite FP16-range input whose true output was above 65504 overflowed the epilogue.
  3. Biased epilogues rounded the reduction to the activation dtype and then added the stored bias. A pre-bias sum outside the dtype range (128 * 12.5 * 42 = 67200) became inf even though the biased result (65200) is representable.

Fix

Path Change
QQQLinear BF16 removed from SUPPORTS_DTYPES, so AUTO selects the FP32 QQQTorchLinear; forward rejects non-FP16 instead of silently narrowing; stored bias added in FP32.
BitsAndBytesLinear INT8 New _int8_vectorwise_quant_fp32 derives the row absmax/scale in FP32 from the source dtype; the weight scale is packed from an FP32 source; the epilogue is mm.float() * x_stats[:,None] * weight_scb[None,:] / 127**2 + bias.float() and casts once.
_numerics.matmul_fp32_reduce _can_fuse_hip_bias now also covers CPU (and requires weight.dtype == x.dtype), so addmm keeps the sum and bias in FP32.
CPU TorchAten/TorchAtenAwq/TorchFusedAwq/TorchCpuAvx512Awq Biased layers use matmul_fp32_reduce; unbiased layers keep the fused kernel.
AwqGEMMLinear The native split-K branch now uses gemm_forward_fp32_reduce and adds the bias in FP32. The large-M branch already produced an FP32 output upstream and is unchanged.
ExllamaV2Linear / AwqExllamaV2Linear Biased layers take the extension's FP32 path and add the bias in FP32; the visible cast happens once.
Marlin / AwqMarlin epilogue (marlin_template.h) Per-column scale and bias are applied in FP32 before the single float2num.
flowchart TD
    A["quantized linear forward"] --> B{"activation dtype"}
    B -- "BF16 QQQ" --> C["QQQLinear rejects BF16; AUTO selects QQQTorchLinear (FP32)"]
    B -- "FP16 QQQ" --> D["dynamic_quant on the original FP16 A"]
    B -- "BF16/FP32 BnB INT8" --> E["_int8_vectorwise_quant_fp32"]
    B -- "FP16 BnB INT8" --> F["bnb int8_vectorwise_quant"]
    E --> G["int8_linear_matmul then FP32 row/col scales and bias"]
    F --> G
    G --> H["single cast to the visible dtype"]
    A --> I{"stored bias"}
    I -- "CPU low-precision" --> J["matmul_fp32_reduce -> addmm FP32 epilogue"]
    I -- "AwqGEMM" --> K["gemm_forward_fp32_reduce + bias in FP32"]
    I -- "ExllamaV2 / AwqExllamaV2" --> L["extension FP32 path + bias in FP32"]
    I -- "Marlin / AwqMarlin" --> M["epilogue adds bias in FP32 before float2num"]
    I -- "no bias" --> N["native low-precision path unchanged"]
Loading

Before / after

Repro: W=42, K=128, x=12.5, bias=-2000, FP16. Pre-bias product 67200 (outside FP16), true result 65200, FP16 result 65216.

Sink origin/main this PR
matmul_fp32_reduce (CPU) inf 65216
CPU TorchAten (GPTQ) inf 65216
CPU TorchAten (AWQ) inf 65216
CPU TorchFused (AWQ) inf 65216
AwqTorch (CPU) inf 65216
CUDA ExllamaV2 / AwqExllamaV2 inf 65216
CUDA Marlin / AwqMarlin inf 65216
CUDA AwqGEMM (rows 1) inf 65216
CUDA AwqGEMM (rows 1025) 65216 (already fixed upstream) 65216

QQQ, BF16 activation with one outlier channel at 70000 whose weight column is 0:

origin/main this PR
QQQLinear (BF16) 512/512 NaN rejected; AUTO selects QQQTorchLinear, finite
QQQTorchLinear finite finite

BitsAndBytes INT8, BF16 (measured on R9700, the only venv with bitsandbytes):

Input origin/main this PR
outlier 70000, zero-weight channel 512/512 NaN finite
all-604, true output 3.22e6 512/512 inf finite, within BF16 output rounding
flowchart TD
    A["FP16 pre-bias sum = 67200"] --> B{"bias added before the cast"}
    B -- "no (before)" --> C["float2num(67200) = inf; inf + (-2000) = inf"]
    B -- "yes (after)" --> D["float2num(67200 - 2000) = 65216"]
    E["BF16 activation = 70000"] --> F{"quantizer scale dtype"}
    F -- "FP16 (before)" --> G["scale = inf; x / scale = NaN"]
    F -- "FP32 (after)" --> H["scale = 551.18; codes finite"]
Loading

Tests

New tests/test_inference_fp16_overflow.py pins the 65200 repro and the QQQ/BnB dtype repros. Every case uses an FP64/independent oracle and fails on origin/main (2dc639ed) except AwqGEMM rows=1025, whose large-M FP32-output path landed upstream separately.

Command origin/main (2dc639ed) this PR (merged with 2dc639ed)
pytest tests/test_inference_fp16_overflow.py -q (CUDA venv) 11 failed, 1 passed, 1 skipped 12 passed, 1 skipped
pytest tests/test_inference_fp16_overflow.py -q -k bitsandbytes (ROCm venv) 1 failed (NaN) 1 passed
pytest tests/kernels/test_awq.py test_gptq.py test_awq_cuda_fp32_reduce.py test_awq_torch.py test_awq_machete_marlin.py -q n/a 60 passed, 14 skipped
pytest tests/kernels/test_marlin_fp32_schedule.py -q n/a 124 passed
pytest tests/kernels/test_cuda_bias_epilogue.py test_gptq_torch_cpu_bias_epilogue.py test_linear_fp32_reduce.py test_torch_wide_bias_fallback.py test_inference_numeric_range.py test_inference_numeric_fusion.py test_qlinear_hierarchy.py test_selection.py -q n/a 725 passed, 49 skipped
pytest tests/test_awq_fp32_output.py tests/test_awq_scale_folding.py -q n/a 17 passed
pytest tests/test_bitsandbytes.py -q (ROCm venv) n/a 14 passed, 12 skipped
ruff check gptqmodel docs/eora tests setup.py n/a All checks passed!

Two existing oracles encoded the old double-rounding and were updated to the new contract:

  • tests/kernels/test_awq_torch.py compares against an FP64 product + bias instead of a BF16 matmul followed by a BF16 add.
  • tests/test_bitsandbytes.py computes the INT8 oracle from x.float() instead of x.to(float16).float().
  • tests/kernels/test_marlin_fp32_schedule.py compares against the FP32-before-cast epilogue and fixes the stale 8-bit column order from fix(awq): decode 8-bit packed readers in natural order (#3974) #3993.

Hardware

  • CUDA: RTX 4090, GPU-d458708b-c760-bbf1-08fb-8b8803a2157d, torch 2.16.0.dev+cu130.
  • ROCm: R9700 (HIP_VISIBLE_DEVICES=2), torch 2.16.0.dev+rocm10.1, bitsandbytes 0.50.2.

Notes / limitations

  • Biased ExllamaV2/AwqExllamaV2 layers take the extension's FP32 dense path, which reconstructs the FP16 weight transiently per call. The reconstruction is not retained (temp_dq_size() == 0); unbiased layers keep the native FP16 kernel.
  • marlin_template.h is not part of the extension cache key, so a stale build must be cleared (extension.load(name="marlin", use_cache=False)) after editing it.
  • Full-model GPTQModel.quantize could not be exercised in this checkout because of an unrelated NamedModule StopIteration on modules without parameters (Qwen2.5 and TinyLlama); the repros are unit-level.
  • No CUDA measurement for BitsAndBytes: bitsandbytes is not installed in the CUDA venv. The BnB numbers above are ROCm only.

Docs: docs/inference-fp16-overflow-epilogues.md, linked from README.md.

…antization (#3977)

- QQQLinear: drop BF16 from SUPPORTS_DTYPES so AUTO selects the FP32
  QQQTorchLinear, reject non-FP16 activations in forward, and add the
  stored bias in FP32.
- BitsAndBytes INT8: derive the row absmax/scale in FP32 from the source
  dtype, pack the weight scale from an FP32 source, and dequantize with an
  FP32 mm * x_stats * weight_scb / 127**2 + bias epilogue.
- _numerics.matmul_fp32_reduce: extend the addmm bias fusion to CPU.
- ExllamaV2/AwqExllamaV2, AwqGEMM (both row bands), Marlin/AwqMarlin and
  the CPU ATen/TorchFused/AVX512 AWQ paths add the stored bias before the
  single visible cast.
- Add tests/test_inference_fp16_overflow.py and the epilogue docs page.
# Conflicts:
#	gptqmodel/nn_modules/qlinear/gemm_awq.py
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.

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

1 participant