Skip to content

FP8 per-tensor / rowwise dequant under torch.compile below sm89: one opaque byte-decode op - #14913

Merged
danielhanchen merged 4 commits into
mainfrom
fp8-scale-dequant-compiled-pre-sm89
Oct 23, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
fp8-scale-dequant-compiled-pre-sm89

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Below sm89, compiling a per-tensor or rowwise FP8 dequant (weight_dequant on an e4m3fn weight inside torch.compile) crashed. Inductor fused x.to(dtype) * s into a Triton kernel that loads fp8e4nv, which Triton does not support before sm89:

torch._inductor.exc.InductorError: CompilationError: ... ValueError("type fp8e4nv not supported in this architecture. The supported fp8 dtypes are ('fp8e4b15', 'fp8e5')")

On a real T4, test_compiled_dequant_keeps_the_fused_expression failed this way on torch 2.10.0, 2.11.0 and 2.12.1. This affects any compiled region that dequantizes a per-tensor or rowwise FP8 weight on T4, V100, A100 or A10.

Change

unsloth/kernels/fp8.py:

Tests

tests/test_fp8_scale_dequant_one_pass.py:

  • test_compiled_dequant_below_sm89_is_one_opaque_op: fp16 / bf16 output with rowwise, transposed rowwise and per-tensor scales. The test forces the pre-sm89 gate, then checks that the compiled graph holds the opaque op, that strides match, and that the bits match the torch expression.
  • test_compiled_dequant_keeps_the_fused_expression: on sm89 and newer, checks that the graph has no opaque op and that Inductor gives the same bits. It is skipped where the new test applies.

Real T4 (Colab), with a fresh venv per torch version. The FP8 files run were the new test file, test_fp8_pre_sm89_byte_dequant.py and test_fp8_pre_sm89_dequant_fallback.py.

torch main this PR
2.10.0 8 failed, 203 passed 1 failed, 174 passed
2.11.0 8 failed, 203 passed 1 failed, 174 passed
2.12.1 8 failed, 203 passed 1 failed, 174 passed
2.13.0 7 failed, 204 passed 1 failed, 174 passed

How to read the main column:

  • 6 of main's failures are the new opaque-op test, which main cannot pass.
  • One is the compile crash above (it does not reproduce on 2.13.0).
  • The remaining one is test_fp8_pre_sm89_byte_dequant.py::test_under_torch_compile, which fails the same way on both sides; see below.
  • Main also runs a copy of the PR's test file under a different name, so its pass count is not comparable.

Compiled with the default Inductor backend, fullgraph=True, comparing bit patterns and strides against the torch expression:

GPU torch fp16 rowwise / transposed / per-tensor bf16 rowwise / transposed / per-tensor
T4 2.10.0, 2.13.0 identical fails with torch.* op returned non-Tensor (device_count)
A100 2.10.0, 2.13.0 identical identical

B200 (sm100, torch 2.10 and 2.14): 43 passed, with the pre-sm89 gate simulated for the new tests. test_compiled_dequant_reads_no_device_capability checks that the compiled gate queries no device capability while tracing (it reads a per-device tuple built at import).

Not changed / not covered

  • bf16 under compile on T4. It still fails with torch.* op returned non-Tensor (device_count), for this op and for the existing block op (test_under_torch_compile). A100 compiles both. T4 has no native bf16, and Unsloth trains in fp16 there, so this PR does not address it.
  • Main on A100. The A100 rows above were run on this branch only, not on main.
  • ROCm keeps its current path (torch.version.hip).

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7c309c982e

ℹ️ 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".

Comment thread unsloth/kernels/fp8.py Outdated
# NVIDIA e4m3fn only. Compiled regions fuse the torch expression, except where Inductor cannot emit
# fp8e4nv (below sm89): there the expression fails to compile, so the byte kernel runs as one op.
if torch.compiler.is_compiling():
if not _fp8_kernel_unsupported(x):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Move the capability check out of the compiled path

When a pre-sm89 process reaches this path for the first time through torch.compile, the cache for _fp8_device_lacks_kernel is normally empty, so this call executes torch.cuda.get_device_capability while Dynamo is tracing. That device query graph-breaks (the file already precomputes similar device properties specifically to keep them out of compiled regions), and with fullgraph=True the new fallback can fail before reaching the opaque operation. The tests conceal this cold-cache case by either monkeypatching _fp8_kernel_unsupported or checking it before compilation, which warms the cache; cache the capability outside tracing or use precomputed module-level properties here.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 430a91d: the compiled gate now reads _FP8_DEVICE_LACKS_KERNEL, a per-device tuple built at import (as _SM_COUNTS is), so no capability is queried while tracing. test_compiled_dequant_reads_no_device_capability makes _fp8_device_lacks_kernel raise and compiles with fullgraph=True: the previous head raises inside the trace, this head passes (43 passed on torch 2.10 and 2.14).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 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-23T05:20:32.836426Z 430a91d 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 430a91d053

ℹ️ 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".

[skip ci]
@danielhanchen
danielhanchen merged commit da109fd into main Oct 23, 2026
@danielhanchen
danielhanchen deleted the fp8-scale-dequant-compiled-pre-sm89 branch October 23, 2026 05:27
danielhanchen added a commit that referenced this pull request Oct 23, 2026
 (#14930)

Files merged without the repo formatter, so pre-commit.ci's
`ruff-format-with-kwargs` hook (which runs on all files) rewrites them
on every PR queued after:

- `tests/test_fp8_scale_dequant_one_pass.py`: formatted in #14885, then
changed again by #14913 (05:27 UTC)
- `unsloth/models/fsdp2_save.py`, `tests/test_fsdp2_save_gloo.py`,
`tests/test_fsdp2_merged_save_memory_cuda.py`: from #14823 (05:46 UTC),
folded in while this was in review

Same class as #14817, #14852, #14878, #14885 and #14908. `python
scripts/run_ruff_format.py` (the hook's entry point, `ruff==0.6.9`)
applied to those four files. Each parses to an identical AST before and
after, `ruff check` passes, and a full run of the hook over every
tracked Python file on this branch changes nothing else.
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Oct 24, 2026
…out native bf16 (unslothai#15071)

On T4 (sm75), Inductor refuses any graph with a bf16 input or output
("does not support bfloat16 compilation natively, skipping"). Dynamo
then compiles each nested Python frame on its own.

Since unslothai#14913, a compiled bf16 per-tensor / rowwise FP8 dequant below
sm89 routes to the opaque `unsloth::fp8_scale_dequant` op. On T4 in bf16
that op's body gets traced as its own frame. `fullgraph = True` then
fails at the first untraceable call in it (`torch.cuda.device_count()`);
without fullgraph it takes 7 to 13 graph breaks. fp16 on T4, and every
dtype on sm80+, are unaffected.

### Change
- **`unsloth/kernels/fp8.py`:** under compile, bf16 output on a CUDA
card without native bf16 keeps the torch expression instead of the
opaque op. This is the path that compiled before unslothai#14913, and its output
is bit-identical.
- The capability check reads a per-device tuple built at import, the
same pattern as `_FP8_DEVICE_LACKS_KERNEL`, so nothing queries the GPU
while tracing.
  - Unchanged: fp16, eager calls, sm80+ cards and ROCm.
- **Two fullgraph tests use fp16 on cards without native bf16:**
- which tests:
`test_fp8_pre_sm89_byte_dequant.py::test_under_torch_compile` and
`test_moe_dw_accumulate.py::test_compiled_fullgraph_zero_breaks_and_matches_eager`;
- why: fp16 is what Unsloth trains in on T4, and a bf16 graph there is
skipped by Inductor whatever the code does. A100 and L4 keep bf16.
- The unslothai#14913 description already listed `test_under_torch_compile`
failing on T4 in bf16.
- **`test_compiled_dequant_below_sm89_is_one_opaque_op`:** now expects
the torch expression for bf16 on T4.

### Tests
- **New test:** `test_inductor_fullgraph_dequant_matches_eager` (fp16 /
bf16, rowwise / per-tensor) compiles `weight_dequant` with real Inductor
and `fullgraph = True`, then compares bits with eager.

| T4, rowwise dequant under real Inductor | fp16 fullgraph | bf16
fullgraph |
|---|---|---|
| main | passes, 0 breaks | fails at `device_count` |
| this PR | passes, 0 breaks | passes, bits equal to eager |

- **Full batch:** 8 test files (FP8 dequant and MoE dW) on Colab,
failing test IDs main vs this PR:

| GPU | torch | main | this PR |
|---|---|---|---|
| T4 | 2.10.0 | 2 failed, 311 passed | 317 passed |
| T4 | 2.14.1 | 2 failed, 311 passed | 317 passed |
| A100 | 2.10.0 / 2.14.1 | 0 failed | 0 failed |

- **The 2 failures on main:** `test_under_torch_compile` and
`test_compiled_fullgraph_zero_breaks_and_matches_eager`.
- **A100 run:** made on the previous commit of this branch. The last
commit only drops a block-dequant gate that never ran on A100 and
adjusts the T4 expectation in one test.

### Not covered
- **bf16 block-FP8 dequant on T4 under fullgraph:** not changed here. It
goes through the byte decoder, and a matching gate did not make its
fullgraph test pass, so it was left out.
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