Repository navigation
fix(fp8): measure the row-wise _scaled_mm contract before using it (#5174) - #5178
Merged
Merged
Conversation
…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
Qubitium
marked this pull request as ready for review
November 18, 2026 08:28
This was referenced Nov 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
TorchFP8Lineartakes its per-token (W8A8) fast path on the strength of the_scaled_mmlayout 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
float16output — no exception, just a wrong result: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_mmnow 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_mmassertions 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_invariantkeeps skipping when the native path is unavailable, unchanged.Rebased onto
origin/mainafter #5173; that PR does not touch_can_use_scaled_mm,_forward_scaled_mmor the new helper, so there is no overlap.Fixes #5174