Skip to content

FP8: float32 dequant before torch 2.14, no direct path under mismatched autocast - #15927

Merged
danielhanchen merged 5 commits into
mainfrom
fp8-autocast-and-fp16-out
Nov 1, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
fp8-autocast-and-fp16-out

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #15584 (direct rowwise FP8 path) and #15791 (sm89 large-weight row threshold, fp16 _scaled_mm workaround). A recheck found three correctness gaps. Two independent reviewers reproduced each one on a B200. The first one, fp16 output rounded through bf16 on torch 2.14, is fixed on main by #16063. This branch now merges main and uses its _SCALED_MM_FP16_OUT_BROKEN check (torch < 2.14, pre-releases of 2.14 included). This PR fixes the other two.

1. float32 activations sent to rowwise _scaled_mm before torch 2.14 (#15791)

Rowwise _scaled_mm accepts only bf16 / fp16 outputs on torch 2.10 - 2.13. On sm89 the new threshold sends 17 - 64 row calls with an over-L2 weight to _scaled_mm, so float32 activations there fail with "out.dtype must be BFloat16 or Half". Before torch 2.14 they now keep the dequant path, gated on the same _SCALED_MM_FP16_OUT_BROKEN check as the fp16 workaround. That also covers the same failure above 64 rows, which predates #15791. torch 2.14 can write float32, so it is unchanged.

2. Direct path under autocast in another dtype (#15584)

The direct path dequantizes into a buffer of X's dtype and then calls matmul, which autocast may run in another dtype. The compiled path fuses the dequant into autocast's GEMM. With fp16 X under bf16 autocast, a finite weight times scale above 65504 overflows the fp16 buffer first:

fp16 X = 1e-4, W = 1, row scale 1e5, bf16 autocast before #15584 main
outputs 5120 (finite) inf / NaN

Random inputs also differ from the compiled result, by up to 0.25 abs. The direct route now returns None when CUDA autocast is enabled with a dtype different from X's, so those calls keep the compiled path. Same-dtype autocast and no autocast are unchanged.

Tests

The CPU tests in tests/test_fp8_dequant_max_rows.py route through FakeTensorMode. They sit beside the fp16 out dtype tests from #16063:

  • float32 keeps dequant while the fp16 output is broken (before 2.14) and keeps _scaled_mm after it;
  • mismatched autocast keeps the compiled path, while matching autocast still takes the direct path.

Results after merging main (9a1ea09):

  • With main's fp8.py, 2 of the new cases fail (float32 gate, fp16 X under bf16 autocast).
  • With this PR, test_fp8_dequant_max_rows.py and test_fp8_rowwise_direct_route.py give 29 passed, 2 skipped (CUDA only).
  • All tests/test_fp8_*.py, tests/test_fp8linear_*.py and tests/test_compressed_tensors_fp8_kernel_route.py on torch 2.13 CPU: 328 passed, 470 skipped (CUDA only).

GPU results (before merging main)

These runs predate the merge with main. Base = merge base fa7ad0d, which still had the fp16 bf16 round trip on every torch version. The fp16 rows of the earlier runs are now main's behaviour (#16063), so only the rows below belong to this PR.

Colab L4, sm89, scaled_mm backend. One VM, each torch version in its own uv venv, deleted between versions.

case 2.10 base 2.10 head 2.14 base 2.14 head
bf16 autocast, fp16 X = 1e-4, scale 1e5 (ref 5120.85) inf 5120 inf 5120
float32 X, 32 rows, 14336 x 4096 ValueError dequant, rel err 4.4e-7 scaled_mm, rel err 0.026 bitwise equal to base

Colab H100 80GB, sm90, same scripts and arms, torch 2.10.0 and 2.14.1:

case (public fbgemm_fp8_linear) 2.10 base 2.10 head 2.14 base 2.14 head
bf16 autocast, 16 rows, fp16 X = 1e-4, scale 1e5 (ref 5120.85) inf 5120 inf 5120
bf16 autocast, 128 rows (scaled_mm) 4896 4896 (same) 4896 4912
float32 X, 32 rows, 14336 x 4096 dequant, rel err 8.1e-7 same same same
  • bf16 outputs are bitwise equal base vs head on both torch versions and both GPUs.
  • On sm90 the 32-row float32 call already took the dequant path on base, so the L4's ValueError does not occur there.
  • The 2.14 head runs above used a final release (2.14.1). Main's check also keeps the bf16 detour on 2.14 pre-releases. On those, float32 activations take the dequant path as well.

Unchanged by this PR: calling FbgemmFp8Linear_matmul.apply directly in eager mode under a mismatched autocast still overflows, the same as main. The guard covers the public fbgemm_fp8_linear direct route.

…re it, direct path off under mismatched autocast
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T07:20:38.696347Z e16b40a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 94f61950e2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# Conflicts:
#	tests/test_fp8_dequant_max_rows.py
#	unsloth/kernels/fp8.py

[skip ci]
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 9a6ac0d9e7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Only calls that could take the direct path pay the autocast lookup, and the no-argument is_autocast_enabled (CUDA) is about 40% cheaper than passing the device.

[skip ci]
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: e16b40a476

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen danielhanchen changed the title FP8: native fp16 scaled_mm output on torch 2.14, float32 dequant before it, no direct path under mismatched autocast FP8: float32 dequant before torch 2.14, no direct path under mismatched autocast Nov 1, 2026
@danielhanchen
danielhanchen merged commit b068981 into main Nov 1, 2026
@danielhanchen
danielhanchen deleted the fp8-autocast-and-fp16-out branch November 1, 2026 09:41
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 3, 2026
…rch < 2.14) (unslothai#16393)

Follow-up to unslothai#15927.

Before torch 2.14, the rowwise `torch._scaled_mm` float32 restriction
belongs to the CUTLASS fallback. Hopper with CUDA/cuBLAS 12.9+ can use
cuBLAS with float32 output, so the blanket fallback unnecessarily
dequantizes the full weight on that configuration.

This change probes float32 rowwise output support once per device and
keeps the existing dequant fallback when unsupported. Shape and
row-count eligibility are checked first, so decode-sized and unaligned
calls do not run an irrelevant capability probe.

Validation:
- Focused local suites: 36 passed, 2 CUDA-only skips.
- Three dequant-only regression cases fail on the submitted PR and pass
after the eligibility-order fix; differential result: 3 FIXED, 25
UNCHANGED, no head failures.
- Real Colab L4, torch 2.11/cu130: eight fresh-process comparisons
confirm one probe on the first decode call in each fresh process before
the fix and zero afterward, with identical outputs. No timing speedup is
claimed from these runs.
- Scoped staging tests: 28 passed each on Linux, macOS, and Windows.
- The 864-cell routing matrix changes only the two intended
supported-float32 cells, with no narrowed routes.
- Current-head switchboard: NO_REGRESSION (40 SAME, 2 DOM_ONLY_DIFF with
no visible pixel changes). Remote L4 regression smoke: 0 regressions, 0
metric moves, 0 missing cases; 66 passed, 13 xpass, 24 skipped per arm.
Standard SFT and inference comparisons also report NO_REGRESSION, with
identical SFT losses and gradient norms.
- Additional exclusive Colab G4 torch 2.11 check: all eight
fresh-process arms match the torch reference; the first-call probe count
drops from one to zero. No steady-state speedup is claimed.

Native Hopper execution and its performance benefit were not measured.
The runtime capability distinction is supported by [PyTorch's dispatch
source](https://github.com/pytorch/pytorch/blob/v2.13.0/aten/src/ATen/native/cuda/ScaledBlas.cpp)
and [rowwise
tests](https://github.com/pytorch/pytorch/blob/v2.13.0/test/test_scaled_matmul_cuda.py).
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.

1 participant