Skip to content

fix(paroquant): keep fp32 reduction and bias epilogue (#3866) - #3893

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-3866
Nov 14, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-3866

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #3866

Root cause

ParoLinear._forward_dense called
_dequantize_weight_for_inference(x_flat, dtype=x_flat.dtype), ran
torch.matmul in that dtype, then added the bias in that dtype. For the
issue's layer the finite FP16 result is 65024, but the FP16 dot reaches
66048 first and rounds to inf, so the bias can no longer cancel it.

The CUDA AWQ path had the same epilogue in a different place:
gemm_forward_cuda_fp32_reduce already reduces split-K partials in FP32, but
gemm_forward_cuda_fp32_reduce() casts the result to the activation dtype
before returning it, and _forward_cuda_awq_kernel then adds a bias narrowed
to that dtype. The same finite result therefore came back as inf on the
native kernel too.

Fix

  • ParoLinear._forward_dense reconstructs the grid in FP32 and calls
    linear_wide_bias, which keeps the reduction and the stored bias in FP32 and
    casts once.
  • The AWQ fp32 split-K reducer gained an optional fp32_output flag (default
    false, so every existing caller keeps the activation-dtype result). The
    Python wrapper awq_gemm_forward_fp32_reduce passes True.
  • ParoLinear._forward_cuda_awq_kernel uses that float32 result, adds the
    stored bias in float32 and casts once. The no-bias path and the
    fp32_accum=False opt-out path are unchanged.
  • The two existing ParoQuant dense references now reconstruct with FP32 scales
    and apply the bias before the single visible cast; the upstream-contract test
    feeds the same rotated input to both sides so it measures the GEMM, not the
    separate fused-rotation implementation.

No GPTQMODEL_* behaviour switches were added. The dense fallback still
releases its temporary weight at the end of the call; the native path still
reads packed codes and retains no dense weight.

Before / after

Exact issue geometry (bits=4, group_size=16, grid weight 8, bias
-1024), and a kernel-supported native geometry (group_size=32,
in=32, out=64, x=258). "Before" was captured from clean origin/main
(daa5dae).

path before after FP32 oracle
CPU dense fallback, FP16 inf 65024 65024
CUDA RTX 4090 native AWQ inf 65024 65024

Tests

New regression tests in tests/kernels/test_paroquant.py (both fail on
daa5dae with inf):

  • test_paroquant_dense_fallback_accumulates_reduction_and_bias_in_fp32
  • test_paroquant_cuda_kernel_adds_bias_after_the_fp32_reduction (fails if the
    native path delegates to the dense fallback)

Both compare against _fp32_awq_oracle, an independent FP32 reconstruction of
the packed AWQ layout.

# CUDA venv (RTX 4090, GPU-d458708b-...)
PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest tests/kernels/test_paroquant.py tests/kernels/test_awq_cuda_fp32_reduce.py tests/kernels/test_cuda_bias_epilogue.py -q
88 passed

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest tests/test_awq_rank_and_empty.py tests/test_paroquant_rotation_lifecycle.py -q
60 passed

# ROCm venv (R9700, HIP_VISIBLE_DEVICES=2)
HIP_VISIBLE_DEVICES=2 PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest tests/kernels/test_paroquant.py -q
20 passed, 3 skipped

Before/after on the pre-fix worktree (origin/main + the new test file):
both new tests fail with tensor([[inf, ...]]).

Hardware / environment

  • NVIDIA RTX 4090, CUDA_VISIBLE_DEVICES=GPU-d458708b-c760-bbf1-08fb-8b8803a2157d, torch 2.16.0.dev+cu130
  • AMD Radeon AI PRO R9700, HIP_VISIBLE_DEVICES=2, torch 2.16.0.dev+rocm10.1

Reviewer notes

No logic-flow diagrams: this is a localized numerical fix in one fallback and
one binding epilogue, which the logic-flow skill lists as not required.

The AWQ op schema change is additive (fp32_output=False default). The
native fix is gated on self.bias is not None and self.fp32_accum, so a user
who explicitly opts out of FP32 accumulation keeps the previous kernel and
epilogue.

ParoLinear._forward_dense decoded the AWQ grid to the activation dtype, ran the matmul there and added the bias in that dtype, so a finite FP16 result overflowed before the bias could cancel it (inf instead of 65024). The CUDA AWQ path had the same epilogue: the split-K reduction is float32, but the kernel rounded its result to the activation dtype before Python added the bias.

The dense fallback now reconstructs the grid in float32 and uses linear_wide_bias, which keeps the reduction and the stored bias in float32 and casts once. The CUDA path asks the existing fp32 split-K reducer for a float32 result, adds the stored bias in float32 and casts once; the no-bias path is unchanged. The AWQ binding gains an optional fp32_output flag (default false, so existing callers are unchanged).

The existing ParoQuant dense and upstream-contract references now reconstruct the grid with float32 scales and apply the bias before the single cast, matching the corrected visible contract.
@Qubitium
Qubitium merged commit a53b53e into main Nov 14, 2026
4 of 6 checks passed
@Qubitium
Qubitium deleted the fix/issue-3866 branch November 14, 2026 08:29
Qubitium added a commit that referenced this pull request Nov 14, 2026
The fp32 bias epilogue change (#3893) added `from ._numerics import
linear_wide_bias` after `from ._runtime_cache import tensor_cache_key`, so
`format/check.sh` (ruff I001) fails on main. Reorder the two local imports;
no behavior change.
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.

ParoQuant dense fallback overflows before a cancelling bias is added

1 participant