Skip to content

perf(fp8): let a wide bias use the native FP8 GEMM path (#4681) - #4716

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4681-fp8-bias
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4681-fp8-bias

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #4681. TorchFP8Linear._forward_dequant_matmul dequantized the whole weight to FP32 and ran an FP32 SGEMM for every biased layer, because FP8 checkpoints store the bias in FP32 and the guard treated bias.dtype != x.dtype as a reason to narrow the weight. The fast helper matmul_fp32_reduce already keeps a bias wider than the activations in its FP32 epilogue (linear_needs_wide_bias → addmm(bias.float(), ...)), so the bias rule was unnecessary and cost 2-5x on prefill (Qwen1.5-7B 375.7 → 284.6 ms).

Change

The decision moves into _native_gemm_eligible(x_flat, device_type=None):

  • no longer looks at the bias dtype at all;
  • keeps the weight-range guard (_weights_fit_activation_dtype), the CPU exclusion and the autocast exclusion;
  • device_type is overridable so the CUDA decision is testable on a CPU host.

_forward_dequant_matmul now branches on not self._native_gemm_eligible(x_flat), so a wide bias reaches matmul_fp32_reduce(x, weight, bias=bias) and stays wide through the reduction. The CPU/autocast/out-of-range paths are unchanged.

layer before after
bias-free, in-range weight, CUDA native GEMM + FP32 reduce unchanged
biased (FP32 bias), in-range weight, CUDA dequant FP32 + SGEMM native GEMM + FP32 reduce
out-of-range decoded weight (e.g. 65536 under FP16) dequant FP32 unchanged
CPU or autocast active dequant FP32 unchanged

Tests

tests/test_fp8_bias_native_gemm_4681.py (new, 6 tests; 5 fail on unmodified main):

  • the wide-bias decision is True on CUDA for FP16 and BF16 activations, False for CPU, FP32 activations, and an out-of-range FP16 weight (the range guard survives);
  • the eligible path forwards the wide bias object to matmul_fp32_reduce instead of dequantizing;
  • the CPU forward still matches the FP32 oracle for a 70000.0 bias.

FP8 suites (test_fp8, test_fp8_bias_range, test_fp8_bias_dtype_4575, test_fp8_dequant_inplace, test_fp8_nonfinite_source, test_fp8_packed_weight_lifecycle): 237 passed, 16 skipped. ruff check clean.

Resolves #4681.

`TorchFP8Linear._forward_dequant_matmul` sent every biased layer to the
dequantize-to-FP32 + SGEMM fallback because FP8 checkpoints store the bias in
FP32 and the guard treated `bias.dtype != x.dtype` as a reason to narrow the
weight. `matmul_fp32_reduce` already keeps such a bias in its FP32 epilogue
(`linear_needs_wide_bias`), so the bias rule was both unnecessary and the
reason biased layers ran 2-5x slower (Qwen1.5-7B prefill 375.7 -> 284.6 ms).

Extract the decision into `_native_gemm_eligible`, which no longer looks at
the bias dtype but keeps the weight-range guard (`_weights_fit_activation_dtype`)
and the CPU/autocast exclusions. `device_type` is overridable so the CUDA
decision can be tested on a CPU host.

Tests: `tests/test_fp8_bias_native_gemm_4681.py` (6 tests; 5 fail on
unmodified main) covers the wide-bias decision for FP16/BF16, the retained
range guard, CPU/FP32 exclusions, the bias forwarded to the FP32 reducer, and
the CPU oracle. FP8 suites: 237 passed, 16 skipped.
@Qubitium
Qubitium merged commit 8587f0b into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4681-fp8-bias branch November 17, 2026 10:43
Qubitium added a commit that referenced this pull request Nov 17, 2026
Fix the test-side fallout of #4716, #4657, #4627 and #4508. Only entries
that fail on this sm_80 host (PG506) and whose stated reasoning reproduces
were changed; the rest are recorded with their outcome.

1. 8587f0b (#4716, native FP8 GEMM for a wide bias)
   - tests/kernels/test_fp8_kernel.py::test_fp8_default_forward_is_w8a16
     REPRODUCED on sm_80: 6 bf16 cases failed with 1-ulp (0.0078125) diffs
     against rtol/atol 2e-3. The oracle now narrows the decoded weight to
     the activation dtype, matching the native path; the bound is the
     half-ulp at unity (2^-9 ~= 2e-3). FIXED.
   - tests/test_fp8_bias_range.py::test_fp8_gpu_dequant_fallback_keeps_bias_wide
     REPRODUCED on sm_80 (fp16: actual 0.0 vs oracle 0.0078125). Renamed to
     test_fp8_gpu_native_gemm_keeps_bias_wide and rewritten for the #3737
     intent: finite output, FP32 bias, residual bounded by one activation-
     dtype ulp of |x.w|. FIXED.

2. bf5adeb (#4657, Marlin bias kept in checkpoint order)
   - tests/test_marlin_scratch_cuda.py 288/200 cases REPRODUCED on sm_80:
     8 failed with "b_bias.size(0) != size_n". scripts/marlin_scratch_fixture.py
     now passes layer._kernel_bias(a) (padded, tile-ordered FP32) instead of
     the checkpoint-order layer.bias. FIXED (8 failed -> 68 passed, 2 skipped).

3. 60376e9 (#4714, default placement in one capability group)
   - tests/test_tenstorrent_support.py already passes on current main: the
     monkeypatched _record accepts **kwargs (fixed upstream by 50f1752).
     ALREADY PASSES on sm_80.

4. 34672a7 (#4585, modules snapshot the kernel policy)
   - tests/kernels/test_bitsandbytes_rocm_triton.py triton cases SKIP on
     sm_80 (no tuned bitsandbytes tile for this family). DEFERRED: the
     stated failure cannot be reproduced here.

5. dfa180d (#4627, GGUF path chosen from static gates)
   - both tests REPRODUCED on sm_80 with AttributeError
     _benchmark_forward_runner. The removed timing hook is no longer patched;
     each test asserts the hook stays absent. FIXED.

6. fd8d524 (#4599, AwqGEMVTorchLinear CPU reader)
   - both kernel-registry contract tests already pass on current main
     (AwqGEMVTorchLinear is in EXCLUDED and the ROCm audit table; fixed
     upstream by ab151fe). ALREADY PASSES on sm_80.

7. d2a0508 (#4283, dense-module adapters)
   - tests/kernels/test_gptq.py::TestKernelOutput errors on sm_80 before the
     adapter path: this host's flash_attn_2_cuda has an undefined symbol, so
     the stated Lora assertion is never reached. DEFERRED: blocked by the
     pre-existing host flash_attn failure; the fix cannot be verified here.

8. f201837 (#4508, activation-quantization wording)
   - both tests REPRODUCED on sm_80. The refusal still fires; the regex now
     matches "activation quantized|activation scales". FIXED.

9. b225a43 (#4727, static-cache compile test)
   - ROCm-only failure. On sm_80 the CUDA leg runs and fails earlier on the
     host flash_attn symbol, so the ROCm gate cannot be reproduced.
     DEFERRED.

Test run (GPU 4):
  tests/kernels/test_fp8_kernel.py tests/test_fp8_bias_range.py
  tests/test_marlin_scratch_cuda.py tests/test_marlin_scratch_benchmark.py
  tests/kernels/test_gguf_cpp_activation_quantization.py
  tests/kernels/test_gguf_cpp_path_selection.py tests/test_auto_module_decoder.py
  => 254 passed, 35 skipped, 1 failed.
The single failure is the pre-existing sm_80
test_fp8_w8a8_forward_is_batch_invariant (native torch._scaled_mm needs
sm_89+); it fails identically on origin/main.
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 biased layers still dequantize to FP32 + SGEMM on CUDA (#4594 fixed only bias-free): 2-4.8x slower GEMM, +32% prefill on Qwen1.5-7B

1 participant