Skip to content

fix(numerics): keep portable Torch reductions and bias epilogues wide (#4471) - #4533

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4471-portable-narrowing
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4471-portable-narrowing

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4471

Root cause

Three portable Torch inference paths narrowed to the activation dtype before a
finite reduction or bias epilogue:

  1. ParoQuant CPU ATen — ParoLinear._forward_cpu_aten builds a biasless
    TorchAtenAwqLinear child, so _fused_op_forward asked
    _cpu_int4pack_mm for out_dtype=None and narrowed the native product to
    FP16/BF16. The parent then added its bias in FP32, but the pre-bias product
    had already overflowed.
  2. Bias-free FP8 fallback — TorchFP8Linear._forward_dequant_matmul only
    decoded the weight in FP32 when a bias was present. With bias=False, a
    stored weight outside the activation range (e.g. 65536 decoded from an FP8
    row scale) was rounded to FP16 before the reduction, turning an exactly zero
    dot product into NaN.
  3. Local Mamba incremental update — selective_state_update formed
    dt * B and dB * x in FP16, then narrowed the FP32 updated_state to
    C.dtype before the readout. Half intermediates overflowed before they
    reached the FP32 state/reduction.

Fix

  • TorchAtenAwqLinear._fused_op_forward(x, out_dtype=None) now accepts an
    explicit product dtype. Its default behavior is unchanged for standalone
    biased layers. ParoLinear passes out_dtype=torch.float32 when the parent
    owns a bias, so the wide product crosses the child/parent boundary and the
    bias is added in FP32 before one visible cast.
  • TorchFP8Linear._forward_dequant_matmul uses the existing temporary FP32
    decode + FP32 matmul path for all half activations, with or without a bias,
    and adds an optional bias in FP32 before the single output cast.
  • selective_state_update computes dt * B, dB * x, the FP32 state readout,
    and the optional D term in FP32; the final visible output cast is unchanged.

All widened operands are local temporaries for the current call; no dense or
expanded weight is cached.

Error vs independent FP64 oracle

Max absolute / relative error over the complete output tensor:

Path Before After
ParoQuant CPU ATen + outer bias (K=32, N=16, FP16) inf / inf (non-finite) 0.0 / 0.0
Bias-free FP8 fallback (K=2, N=1, FP16) nan / nan (non-finite) 0.0 / 0.0
Mamba incremental output (FP16 out, FP32 state) inf / inf (non-finite) 0.0 / 0.0
Mamba incremental state inf / inf (non-finite) 0.0 / 0.0

The oracle is independent float64 arithmetic over the decoded/represented
weights; it does not call the implementation under test.

Regression tests

  • tests/test_portable_wide_epilogues_4471.py — ParoQuant CPU ATen outer-bias
    case and Mamba incremental state/output case.
  • tests/test_fp8_bias_range.py::test_fp8_bias_free_fallback_keeps_decoded_weight_range
    — bias-free FP8 fallback case.

Before fix (source fixes stashed, tests kept): 3 failed.
After fix: 3 passed.

Tests run

CUDA venv (Python 3.14t, torch 2.16.0.dev20261102+cu130), thread caps = 4:

PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_portable_wide_epilogues_4471.py tests/test_fp8_bias_range.py -q
-> 24 passed
PYTHONPATH=$PWD /opt/venvs/cu130t/bin/python -m pytest tests/test_paroquant.py -q
-> 239 passed

ROCm venv (torch 2.16.0.dev20261025+rocm10.1), HIP_VISIBLE_DEVICES=3, thread caps = 4:

PYTHONPATH=$PWD /opt/venvs/rocm101t/bin/python -m pytest \
  tests/test_portable_wide_epilogues_4471.py tests/test_fp8_bias_range.py -q
-> 22 passed, 2 skipped

Lint:

ruff check gptqmodel docs/eora tests setup.py
-> All checks passed!

Final test/lint run was after git fetch origin main && git merge --no-edit origin/main
(88cd6d75b).

Hardware

CPU-only numeric verification; AMD Eng Sample 100-000001458-01. The two ROCm
skips are the existing GPU-only FP8 tests in tests/test_fp8_bias_range.py.

Limitations

  • This change covers the Torch portable paths in the report. The MLX QQQ
    sub-case is not changed here: no MLX/Metal runtime is available on this Linux
    host, and its existing tests encode the historical FP16 intermediate, so it
    needs an Apple-host change with independent Metal numerics.
  • The FP8 GPU fallback branch is source-reachable but was not executed on a GPU
    in this validation; only the CPU fallback was measured.
  • No full-model or performance claims; the widened decode is a per-call
    temporary and was not benchmarked.

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.

[BUG] Portable inference paths still narrow intermediates before finite reductions and bias epilogues

1 participant