Skip to content

perf(fp8): keep the native GEMM for range-safe bias-free weights (#4545) - #4594

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4545-fp8-biasfree-fastpath
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4545-fp8-biasfree-fastpath

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4545

Root cause

#4533 made TorchFP8Linear._forward_dequant_matmul take the wide FP32 path
whenever bias is None, because a decoded FP8 weight can exceed the activation
dtype's finite range. That is correct but unconditional: every bias-free
FP16/BF16 linear on CUDA then dequantized the whole weight to FP32 and ran an
FP32 SGEMM instead of the native tensor-core GEMM with an FP32 reduction.

Fix

Keep the wide path exactly where range safety requires it, and restore the
native path otherwise:

  • BF16 is always range-safe. BF16 has the FP32 exponent range, so a
    decoded FP8 weight that is finite in FP32 cannot overflow it.
  • FP16 gets a per-load bound. _decoded_weight_absmax_bound() bounds
    |decoded weight| from the FP8 code maximum and the (small) scale tensor
    only: code_max * max|scale| for multiplier scales, code_max / min|scale|
    for inverse scales. No dense FP32 weight copy is materialised or cached.
    FP16 narrows only when that bound is <= 65504.
  • The bound is cached and invalidated in _invalidate_compiled(), so reloads
    (_load_from_state_dict), .to()/.half() and re-packing recompute it.
  • Bias handling is unchanged: a bias stored wider than the activation dtype,
    autocast, non-CUDA devices and out-of-range weights still use the wide
    FP32 decode + matmul. In-range weights use matmul_fp32_reduce, which keeps
    the reduction and a same-dtype bias in FP32 before rounding.

No dense weight cache, no new GPTQMODEL_* switch.

Performance (RTX 4090, sm89, torch 2.16.0.dev+cu130, GPU idle)

Same box, same process settings, same checkpoint payload, median of 10-50
iterations after warmup. "Before" is origin/main (cf56aa662, i.e. #4533);
"after" is this branch.

case before after speedup
4096x14336, M=4096, fp16 11.039 ms 4.128 ms 2.67x
4096x14336, M=4096, bf16 10.208 ms 4.401 ms 2.32x
4096x4096, M=512, fp16 0.644 ms 0.397 ms 1.62x
4096x14336, M=1, fp16 (decode proxy) 1.643 ms 1.568 ms 1.05x

These reproduce the issue's regression and its removal: the issue measured
4.108 -> 10.353 ms (fp16) and 4.395 -> 10.182 ms (bf16) for 4096x14336/M=4096;
we measure 4.128 -> 11.039 ms and 4.401 -> 10.208 ms with the same settings.
The post-fix numbers match the pre-#4533 baseline.

Thread caps: OMP_NUM_THREADS=4 MKL_NUM_THREADS=4 OPENBLAS_NUM_THREADS=4 NUMEXPR_NUM_THREADS=4. They were not raised for this measurement: the timed
region is a GPU GEMM, so CPU thread caps do not affect it, and the repo caps
are mandatory.

Correctness (no regression)

  • pytest tests/test_fp8_bias_range.py tests/test_portable_wide_epilogues_4471.py -q
    -> 28 passed (after merging origin/main @ 68cc727f0).
  • New dispatch tests fail before the fix (fp8.py reverted, tests kept):
    3 failed, 1 passed -- both in-range keep_native_path cases and the
    reload test fail; the out-of-range case passes because it was already wide.
  • pytest tests/test_fp8.py tests/test_fp8_dequant_inplace.py tests/test_fp8_packed_weight_lifecycle.py tests/test_fp8_bf16_scale_reload.py -q
    -> 57 passed.
  • ruff check gptqmodel docs/eora tests setup.py -> All checks passed.

The out-of-range FP16 case from #4471 (65536 decoded weight) still takes the
FP32 path and stays finite; a new test pins that dispatch.

Limitations

  • The 15% end-to-end decode figure was not re-measured with a full model; the
    M=1 module-level proxy above is the evidence supplied here.
  • float8_e8m0fnu (DeepSeek-style) stays on the wide path under FP16 because
    finfo reports a 1.7e38 range for that format; BF16 e8m0 modules do take the
    native path.
  • The bound is conservative: a checkpoint with an oversized scale but small
    codes keeps the wide path rather than risking an overflow.

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.

[PERF] FP8 bias-free forward now always dequantizes to FP32 and runs an FP32 GEMM on CUDA (#4533 regression): 2.1x prefill, -15% decode

1 participant