Repository navigation
fix(qlinear): refresh fused dispatch and bias epilogues (#4028) - #4032
Merged
Merged
Conversation
This was referenced Nov 14, 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 #4028
TL;DR
This follow-up fixes stale GPTQ fused-dispatch metadata, the remaining FP16 overflow in
TorchFusedLinear, BF16 CPU fused-path dispatch, biased ExLlamaV2 activation upcasts, AwqExLlamaV2 LoRA dtype promotion, and the safe BitsAndBytes INT8 fused-epilogue case. It adds lifecycle, overflow, dtype, and numerical regressions plus the committed fused kernel epilogue reference page.Root cause and fix
1. Stale
_gptq_gemm_uniform_groupafterload_state_dictRoot cause:
TorchLinear.post_initcached the uniform-group decision once._load_from_state_dict,.half(),.to(), deepcopy, and in-placeg_idxwrites could change the tensor without recomputing it, so M=2..64 used one scale/zero row per K tile for act-order weights.Fix: torch.py keys the decision on the live
g_idxidentity and_version, normalizes packed inference tensors to a normal metadata tensor so in-place writes are observable, and rechecks before the fused GEMM. The repeated lookup measured ~0.6 µs on the assigned 4090; it avoids the oldtensor_cache_keytuple construction.flowchart TD A["TorchLinear._forward_eager"] --> B["_gptq_gemm_uniform_group_for_current_g_idx"] B --> C{"g_idx identity and version unchanged"} C -- "yes" --> D["reuse uniform-group flag"] C -- "no" --> E["compare g_idx with arange // group_size"] E --> F["store flag, identity, and version"] D --> G["gptq_gemm uniform_group"] F --> GBefore: issue repro M=8 relative error 5.93e-1 (
TorchLinear) and 7.02e-1 (TritonV2Linear). After, against an independent FP64 decoder oracle, maximum relative error across M=2,8,16,32,64 was 4.37e-4 forTorchLinearand 4.48e-4 forTritonV2Linear.2.
TorchFusedLinearFP16 overflowRoot cause: torch_fused.py selected the fused kernel with a bias and then did
out.add_(bias), after the FP16 product had already becomeinf.Fix: FP16 biased inference uses
matmul_fp32_reduce; the dense train branch uses the same wide reduction. BF16 may use the fused kernel because its exponent range is FP32's, with the bias added in FP32 before the visible cast.Before: the issue repro returned
[inf, inf, inf]in inference and train mode. After: both paths return65216and the new regression covers both modes.3. CPU BF16 biased layers forced off the fused kernel
Root cause: torch_aten_kernel.py, torch_aten_kernel_awq.py, and torch_fused_awq.py excluded every biased layer, including BF16.
Fix: only FP16 biased layers take the dense wide reduction. BF16 biased layers keep the fused kernel and add the bias in an FP32 epilogue.
flowchart TD A["CPU quantized linear forward"] --> B{"stored bias"} B -- "none" --> C["fused int4 kernel"] B -- "BF16 bias" --> D["fused int4 kernel"] D --> E["add bias in FP32"] E --> F["single BF16 cast"] B -- "FP16 bias" --> G["dense FP32 reduction"] G --> H["add bias before FP16 cast"]Measured on CPU at K=N=4096, M=1, 4 threads, best of 9:
TorchAtenLinearTorchAtenAwqLinearTorchFusedAwqLinearThe BF16 regression compares the fused result against an independent FP64-derived
65200 + biasvalue with zero tolerance after the required BF16 output rounding.4. Biased ExLlamaV2 kernels took the FP32 dense-reconstruction path
Root cause: exllamav2.py and exllamav2_awq.py upcast FP16 activations whenever a bias existed.
Fix: the extension now accepts FP16 activations with an FP32 output. GPTQ reduces split accumulators directly to FP32; AWQ accumulates its K blocks into FP32. Python adds the bias in FP32 and casts once. The FP32 output is transient and no dense weight is retained.
Measured on RTX 4090 at K=N=4096, FP16, best of 7:
AWQ is bitwise identical to the previous wide path in this fixture. GPTQ uses a different reduction order and is within one FP16 ULP on 20 of 65,536 elements; maximum relative difference was 9.14e-4. This is the one numerical limitation called out below.
5. AwqExLlamaV2 bias plus LoRA returned FP32
Root cause: the adapter received the upcast FP32 kernel activation, causing
Lora.applyto promote its weights in place.Fix: exllamav2_awq.py keeps the original-dtype activation for the adapter and applies it after the final cast. The regression asserts the output and both LoRA weights remain FP16.
6. BitsAndBytes INT8 lost the native fused epilogue
Root cause: the FP32 overflow-safe epilogue replaced
int8_mm_dequantfor every INT8 call.Fix: bitsandbytes.py restores the native fused epilogue for FP16 activations without a bias, where no later cancellation can be damaged. BF16 and biased calls keep the FP32 epilogue because the installed BitsAndBytes build has no BF16 quantizer. This is the second scope limitation below.
Failure and ambiguity contract
flowchart TD A["FP16 fused product may exceed 65504"] --> B{"bias present"} B -- "yes, FP16" --> C["FP32 reduction plus bias, then one cast"] B -- "yes, BF16" --> D["fused kernel plus FP32 bias epilogue"] B -- "no" --> E["native fused kernel"] F["g_idx changed after load"] --> G["identity/version mismatch"] G --> H["recompute uniform-group decision"] H --> I["gather fallback when nonuniform"]Tests and validation
Exact commands and results:
ruff check gptqmodel docs/eora tests setup.py->All checks passed!GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42:CUDA_VISIBLE_DEVICES=GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42 PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest -q tests/kernels/test_uniform_group_lifecycle.py tests/kernels/test_tritonv2_dispatch.py tests/kernels/test_cuda_bias_epilogue.py tests/kernels/test_awq_cpu_fused_post_init.py tests/test_inference_fp16_overflow.py tests/kernels/test_inference_numeric_range.py -k 'not test_exllamav2_dot_and_activation_range or test_exllamav2'->
454 passed, 1 skipped(the skip is the unavailable CUDA BitsAndBytes package).HIP_VISIBLE_DEVICES=4 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest -q tests/test_inference_fp16_overflow.py -k 'cpu'->
10 passed.HIP_VISIBLE_DEVICES=4 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest -q tests/kernels/test_uniform_group_lifecycle.py->
1 passed, 10 skipped; the ROCm build reportstorch.cuda.is_available() == Falseon this host, so the fused GPU cases could not run.The CUDA tests include the newly compiled GPTQ and AWQ ExLlamaV2 JIT extensions, the M=1 and M=64 wide-output paths,
[inf, inf, inf] -> 65216, LoRA dtype preservation, the M=2..64 FP64 oracle matrix, and no retained dense weight scratch.Documentation
The committed fused kernel dispatch and bias epilogues page contains the new Mermaid flows, before/after table, and failure contract. It is linked from
README.md. Mermaid validation found 3 balanced blocks, all starting withflowchart TD.Reviewer notes and limitations