Repository navigation
fix(qlinear): keep FP16 reductions wide and stop BF16-through-FP16 quantization (#3977) - #4001
Merged
Merged
Conversation
…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
This was referenced Nov 14, 2026
This was referenced Nov 16, 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.
Fixes #3977
Root cause
Three independent paths narrowed or rounded low-precision data before it was safe:
QQQLinear.forward) cast BF16 activations to FP16 before the per-token int8 quantizer and advertised BF16 inSUPPORTS_DTYPES. An activation above 65504 becameinf, so the row scale wasinfandx / scaleproducedNaN.int8_vectorwise_quant(x.to(float16)), packed the weight throughweight.to(float16), and ran theint8_mm_dequantepilogue in FP16. Any BF16/FP32 magnitude above 65504 becameinf/NaN, and a finite FP16-range input whose true output was above 65504 overflowed the epilogue.128 * 12.5 * 42 = 67200) becameinfeven though the biased result (65200) is representable.Fix
QQQLinearSUPPORTS_DTYPES, soAUTOselects the FP32QQQTorchLinear;forwardrejects non-FP16 instead of silently narrowing; stored bias added in FP32.BitsAndBytesLinearINT8_int8_vectorwise_quant_fp32derives the row absmax/scale in FP32 from the source dtype; the weight scale is packed from an FP32 source; the epilogue ismm.float() * x_stats[:,None] * weight_scb[None,:] / 127**2 + bias.float()and casts once._numerics.matmul_fp32_reduce_can_fuse_hip_biasnow also covers CPU (and requiresweight.dtype == x.dtype), soaddmmkeeps the sum and bias in FP32.TorchAten/TorchAtenAwq/TorchFusedAwq/TorchCpuAvx512Awqmatmul_fp32_reduce; unbiased layers keep the fused kernel.AwqGEMMLineargemm_forward_fp32_reduceand adds the bias in FP32. The large-M branch already produced an FP32 output upstream and is unchanged.ExllamaV2Linear/AwqExllamaV2Linearmarlin_template.h)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"]Before / after
Repro:
W=42,K=128,x=12.5,bias=-2000, FP16. Pre-bias product67200(outside FP16), true result65200, FP16 result65216.matmul_fp32_reduce(CPU)inf65216TorchAten(GPTQ)inf65216TorchAten(AWQ)inf65216TorchFused(AWQ)inf65216AwqTorch(CPU)inf65216ExllamaV2/AwqExllamaV2inf65216Marlin/AwqMarlininf65216AwqGEMM(rows 1)inf65216AwqGEMM(rows 1025)65216(already fixed upstream)65216QQQ, BF16 activation with one outlier channel at 70000 whose weight column is 0:
QQQLinear(BF16)512/512 NaNAUTOselectsQQQTorchLinear, finiteQQQTorchLinearBitsAndBytes INT8, BF16 (measured on R9700, the only venv with
bitsandbytes):512/512 NaN3.22e6512/512 infflowchart 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"]Tests
New
tests/test_inference_fp16_overflow.pypins the65200repro and the QQQ/BnB dtype repros. Every case uses an FP64/independent oracle and fails onorigin/main(2dc639ed) exceptAwqGEMMrows=1025, whose large-M FP32-output path landed upstream separately.2dc639ed)2dc639ed)pytest tests/test_inference_fp16_overflow.py -q(CUDA venv)11 failed, 1 passed, 1 skipped12 passed, 1 skippedpytest tests/test_inference_fp16_overflow.py -q -k bitsandbytes(ROCm venv)1 failed(NaN)1 passedpytest tests/kernels/test_awq.py test_gptq.py test_awq_cuda_fp32_reduce.py test_awq_torch.py test_awq_machete_marlin.py -q60 passed, 14 skippedpytest tests/kernels/test_marlin_fp32_schedule.py -q124 passedpytest 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 -q725 passed, 49 skippedpytest tests/test_awq_fp32_output.py tests/test_awq_scale_folding.py -q17 passedpytest tests/test_bitsandbytes.py -q(ROCm venv)14 passed, 12 skippedruff check gptqmodel docs/eora tests setup.pyAll checks passed!Two existing oracles encoded the old double-rounding and were updated to the new contract:
tests/kernels/test_awq_torch.pycompares against an FP64 product + bias instead of a BF16 matmul followed by a BF16 add.tests/test_bitsandbytes.pycomputes the INT8 oracle fromx.float()instead ofx.to(float16).float().tests/kernels/test_marlin_fp32_schedule.pycompares 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
GPU-d458708b-c760-bbf1-08fb-8b8803a2157d, torch2.16.0.dev+cu130.HIP_VISIBLE_DEVICES=2), torch2.16.0.dev+rocm10.1,bitsandbytes 0.50.2.Notes / limitations
temp_dq_size() == 0); unbiased layers keep the native FP16 kernel.marlin_template.his not part of the extension cache key, so a stale build must be cleared (extension.load(name="marlin", use_cache=False)) after editing it.GPTQModel.quantizecould not be exercised in this checkout because of an unrelatedNamedModuleStopIterationon modules without parameters (Qwen2.5 and TinyLlama); the repros are unit-level.bitsandbytesis not installed in the CUDA venv. The BnB numbers above are ROCm only.Docs:
docs/inference-fp16-overflow-epilogues.md, linked fromREADME.md.