Skip to content

fix(kernels): add bias to the FP32 accumulator for Machete/AWQ-Machete/QQQ (#5725) - #5952

Merged
Qubitium merged 2 commits into
mainfrom
fix/5725-wide-bias-epilogue
Nov 20, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/5725-wide-bias-epilogue

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5725

QQQ and Machete (GPTQ and AWQ) compute in FP32 but narrow the accumulator to
the activation dtype in the epilogue, and both Python wrappers then added the
layer bias to that narrowed tensor. A pre-bias partial sum above 65504 became
inf even when the biased result is finite, and every other row was rounded
twice. The contract is now: add the bias once to the complete reduction, then
round once.

What Changed

  • QQQ native (gptqmodel_ext/qqq/qqq_gemm.{cu,h}, qqq.cpp)
    • Marlin takes a trailing const float* bias (nullptr = no bias).
    • With a bias, write_result stages the FP32 dequantized values in the
      existing shared reorder buffer (2x the fp16 layout, aliasing the A-stage
      scratch after a __syncthreads), then the final write loop adds the
      per-column bias and performs the only float2_to_half2 cast.
    • Split-K is unaffected: global_reduce accumulates INT32 partials exactly
      and write_result runs once, after the reduce, only for the last block.
    • qqq_cuda(..., const void* bias, ...); host qqq_gemm(...) gains a
      trailing std::optional<torch::Tensor> and validates FP32 / numel ==
      prob_n / contiguous.
    • Torch schema: qqq_gemm(..., int max_par=8, Tensor? bias=None) -> ().
  • QQQ Python (gptqmodel/utils/qqq.py, gptqmodel/nn_modules/qlinear/qqq.py)
    • qqq_gemm / mul gain a trailing bias=None.
    • QQQLinear.forward passes self._kernel_bias(A.device); the old post-hoc
      (D.float() + bias).to(dtype) block is removed.
    • _kernel_bias returns an FP32, 1-D, contiguous launch operand; the
      checkpoint self.bias keeps its stored dtype and order.
  • Machete (gptqmodel_ext/machete/generate.py)
    • GPTQ and AWQ TypeConfig loops emit both out=a (unchanged 16-bit image)
      and out=DataType.f32. Generated: dispatch entries 8 -> 16 (8 f32-out),
      kernel impls 72 -> 144 (72 f32-out).
  • Machete wrappers (machete.py, machete_awq.py)
    • Bias-free layers keep the exact previous call (no out_type, no copy).
    • Biased layers request out_type=torch.float32, then compute
      (out + bias.float()).to(activation_dtype): one rounding after the bias
      joins the complete reduction.
  • Tests
    • New tests/kernels/test_qqq_machete_wide_bias_5725.py (10 tests): 4 GPU
      QQQ tests driving the real QQQLinear.forward, 6 CPU Machete tests pinning
      the launch contract for GPTQ and AWQ in fp16/bf16.
    • Updated the two existing QQQ mocks for the new bias argument.

Out of scope: no checkpoint layout change, no new kernel schedules, no change
to the bias-free fast path.

Tests

cd /path/to/GPTQModel   # branch fix/5725-wide-bias-epilogue @ 8dce95c2
export PYTHONPATH=$PWD TORCH_NO_COMPILER_WRAPPER=1

# new regression test
python -m pytest tests/kernels/test_qqq_machete_wide_bias_5725.py -q
# 10 passed

# QQQ neighbors
python -m pytest tests/kernels/test_qqq_strided_activation_4740.py \
  tests/kernels/test_qqq_workspace.py tests/test_qqq_jit.py \
  tests/test_qqq_kernel_memory_4387.py -q
# 58 passed

python -m pytest tests/test_qqq_bias_dtype_5603.py \
  tests/test_qqq_scale_order_5608.py tests/test_qqq_runtime_state_4940.py \
  tests/test_qqq_save_after_forward_5010.py tests/test_qqq_export_scale.py \
  tests/test_qqq_per_channel_checkpoint_5126.py tests/test_qqq_config.py \
  tests/test_qqq_mock_rtn_5839.py tests/test_machete_asymmetric_zero_point_5800.py \
  tests/kernels/test_awq_machete_group_size.py -q
# 80 passed

# lint gate
bash format/check.sh
# ruff 0.14.2
# All checks passed!

# Machete SM90a compile of the new f32 epilogue configs (sm_80 host: build only)
TORCH_CUDA_ARCH_LIST=9.0a python -m pytest \
  tests/test_machete_jit.py::test_machete_cuda_smoke_build_only_with_forced_arch_override -q
# 1 passed in 302.72s

Red on base 5a155e1f7 (identical test file, pristine checkout of that commit):

git checkout 5a155e1f7
python -m pytest tests/kernels/test_qqq_machete_wide_bias_5725.py -q
# 7 failed, 3 passed
  • test_qqq_bias_joins_the_fp32_accumulator_before_the_output_cast: 1024/1024
    non-finite (inf) before.
  • test_qqq_bias_is_not_double_rounded_through_the_activation_dtype: 1.0
    before, 1.0009765625 after.
  • test_qqq_kernel_bias_operand_is_fp32_and_contiguous: AttributeError
    (_kernel_bias absent on base).
  • 4x Machete out_type cases: KeyError: 'out_type' (the bias path never
    requested the fp32 image).
  • The 3 passes are the bias-free controls, which must keep passing.

Measured Impact

GPU: 8x A100-class (sm_80), torch 2.15.0+cu132, CUDA 13.3, QQQ JIT kernel.

Metric (unit) Before After Change (after - before)
QQQ FP16 out, pre-bias sum 71680, bias -20000 (finite expected 51680) inf (1024/1024 non-finite) 51680.0 (0/1024 non-finite) finite
QQQ FP16 out, pre-bias sum ~1.0003, bias 0.0004 (single-cast oracle 1.0009765625) 1.0 1.0009765625 +0.0009765625
QQQ casts after the reduction 2 (epilogue, then bias add) 1 -1

Machete native accuracy is not measured here: the generated kernels are
SM90a-only and this host is sm_80. The generated FP32-out variants compile for
sm_90a (above), and the Python launch contract is covered on CPU; native SM90
accuracy remains unverified in this PR.

Performance: the bias-free path is unchanged (same call, same image dtype, no
extra copy). A biased layer now pays one FP32 epilogue image plus one
add+cast. A quick 4096x4096 (rows=64) bench was too noisy to publish; no
performance claim is made.

Model Support / Module Plan

Not applicable: no model definitions, aliases, or MODEL_MAP entries change.

Logic Flow Documentation

Not applicable: no control-flow or resolution mechanism changes; the bias now
joins an existing reduction before its existing cast.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • For kernel-related changes, accuracy drift was measured against an
    independent Torch FP32 oracle, or the reason it is not applicable is
    documented (Machete native: SM90a-only, unverified here).
  • The complete kernel inventory/device report lists unavailable
    dependencies, architecture restrictions and unqualified formats; skips
    are not validation.

Notes

  • Machete kernels are SM90a-only; this PR verifies compilation but not native
    execution. A sm_90a runner should confirm the fp32 epilogue path end-to-end
    before relying on it.
  • No checkpoint or state-dict layout changes; self.bias keeps its stored dtype.
  • The FP32 bias operand is created per call only when a bias exists; bias-free
    layers allocate nothing extra.

CodeQL flagged the module alias import and the symbol import of the same
module. Keep the module aliases (the tests patch ``machete_mm`` on them) and
reach the classes through the aliases.
@Qubitium
Qubitium marked this pull request as ready for review November 20, 2026 18:37
@Qubitium
Qubitium merged commit 9c22da0 into main Nov 20, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5725-wide-bias-epilogue branch November 20, 2026 19:32
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.

[Audit][machete/qqq] Bias added after the kernel narrows to FP16/BF16: pre-bias overflow becomes inf, double rounding otherwise

1 participant