Skip to content

fix(fp8): measure the row-wise _scaled_mm contract before using it (#5174) - #5178

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-5174-fp8-rowwise-scaled-mm
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-5174-fp8-rowwise-scaled-mm

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Problem

TorchFP8Linear takes its per-token (W8A8) fast path on the strength of the _scaled_mm layout alone: scale_a=(tokens, 1), scale_b=(1, out_features).

On sm_121 (GB10) with torch 2.11.0+cu130 that layout is accepted but the row-wise scaling is not applied for float16 output — no exception, just a wrong result:

float16 : max_rel_err = 80.699   allclose(rtol=2e-2, atol=1e-2) = False
bfloat16: max_rel_err = 0.003    allclose = True
float32 : raises ValueError ("Only bf16 and fp16 high precision output types are supported for row-wise scaling")

So fp16 checkpoints on Blackwell silently produced wrong logits, and the failure is shape-independent (measured 16x32x16 through 4096x4096x4096). The defect is invisible to the caller because nothing is raised.

Fix

Measure the contract instead of trusting it. _scaled_mm_row_scale_supported(device, out_dtype) runs a 16x32x16 fp8-e4m3 GEMM and compares it with (A @ B) * scale_a * scale_b; a raised error or a mismatch disables that (device index, output dtype) combination. Results are memoised per process, so the per-forward guard is one dict lookup.

_can_use_scaled_mm now requires the probe for the exact output dtype the forward would request (_scaled_mm_out_dtype, factored out of _forward_scaled_mm), and per-token layers fall back to the existing dequantize + matmul path when it fails. Behaviour on healthy hosts is unchanged: BF16 keeps the native kernel on CUDA, and the FP32 bias epilogue still applies.

The probe logs once when it disables a combination, so a user can tell why W8A8 is not on the fast path.

Tests

  • test_fp8_per_token_forward_matches_the_dense_contract (new, fp16/bf16 x tensor/row): forward output must equal the dequantized dense reference. Fails on the pre-fix math on this host.
  • test_fp8_w8a8_is_opt_in_and_uses_per_token_scales: the two _can_use_scaled_mm assertions now compare against the probe rather than assuming the device supports the kernel.
  • test_fp8_scaled_mm_matches_dense_reference[tensor|row-float8_e4m3fn] already skip via their own guard; unchanged.
  • test_fp8_w8a8_forward_is_batch_invariant keeps skipping when the native path is unavailable, unchanged.
tests/kernels/test_fp8_kernel.py tests/test_fp8_bias_range.py
90 passed, 17 skipped
ruff check gptqmodel tests -> All checks passed

Rebased onto origin/main after #5173; that PR does not touch _can_use_scaled_mm, _forward_scaled_mm or the new helper, so there is no overlap.

Fixes #5174

…5174)

On sm_121 with torch 2.11.0+cu130 `torch._scaled_mm` accepts the row-wise
scale layout (`scale_a=(tokens, 1)`, `scale_b=(1, out_features)`) and returns a
result ~1e3x away from `(A @ B) * scale_a * scale_b` when the output dtype is
`float16`; nothing is raised, so a caller cannot detect it. The same call with
`bfloat16` output is correct, and `float32` output raises outright.

`TorchFP8Linear` took the per-token (W8A8) fast path on the strength of the
layout alone, so Blackwell fp16 checkpoints silently produced wrong logits.
Probe the contract once per process, device index and output dtype against a
16x32x16 fp8-e4m3 GEMM and keep the dequantize + matmul fallback unless the
result matches. BF16 keeps the native kernel on CUDA; the guard costs one dict
lookup per forward and logs once when it disables a combination.

The two `test_fp8_scaled_mm_matches_dense_reference` cases that asserted the
native path for `float16` now skip on hosts without a correct kernel, and a new
`test_fp8_per_token_forward_matches_the_dense_contract` pins the forward output
to the dense reference for fp16/bf16 x tensor/row scales.

Fixes #5174
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant