Skip to content

fix(qlinear): refresh fused dispatch and bias epilogues (#4028) - #4032

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4028
Nov 14, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4028

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

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_group after load_state_dict

Root cause: TorchLinear.post_init cached the uniform-group decision once. _load_from_state_dict, .half(), .to(), deepcopy, and in-place g_idx writes 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_idx identity 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 old tensor_cache_key tuple 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 --> G
Loading

Before: 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 for TorchLinear and 4.48e-4 for TritonV2Linear.

2. TorchFusedLinear FP16 overflow

Root cause: torch_fused.py selected the fused kernel with a bias and then did out.add_(bias), after the FP16 product had already become inf.

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 return 65216 and 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"]
Loading

Measured on CPU at K=N=4096, M=1, 4 threads, best of 9:

kernel dense path ms fused BF16 ms speedup
TorchAtenLinear 336.2 48.9 6.9x
TorchAtenAwqLinear 1199.7 81.5 14.7x
TorchFusedAwqLinear 316.3 23.4 13.5x

The BF16 regression compares the fused result against an independent FP64-derived 65200 + bias value 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:

kernel old wide path µs new FP32 output µs
GPTQ M=1 178.2 59.3
GPTQ M=16 199.7 51.5
AWQ M=1 176.1 61.6
AWQ M=16 198.7 56.3

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.apply to 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_dequant for 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"]
Loading

Tests and validation

Exact commands and results:

  • ruff check gptqmodel docs/eora tests setup.py -> All checks passed!
  • CUDA 4090 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).
  • ROCm Python, CPU cases:
    HIP_VISIBLE_DEVICES=4 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest -q tests/test_inference_fp16_overflow.py -k 'cpu'
    -> 10 passed.
  • ROCm Python, lifecycle file:
    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 reports torch.cuda.is_available() == False on 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 with flowchart TD.

Reviewer notes and limitations

  • GPTQ ExLlamaV2 FP32 output is not bitwise identical to the removed dense reconstruction because the reduction orders differ. It stays within one FP16 ULP in the measured fixture and within the inference tolerance.
  • BF16 BitsAndBytes and biased BitsAndBytes remain on the FP32 Python epilogue until a fused BF16 quant-plus-epilogue kernel exists. The available CUDA environment has no BitsAndBytes package, so that path could not be timed here.

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

Labels

None yet

Projects

None yet

1 participant