Skip to content

FP8 dequant under compile: keep the torch path for bf16 on cards without native bf16 - #15071

Merged
danielhanchen merged 3 commits into
mainfrom
t4-compile-device-count
Oct 24, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
t4-compile-device-count

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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 #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 FP8 per-tensor / rowwise dequant under torch.compile below sm89: one opaque byte-decode op #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:
  • 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.

On sm_75 Inductor raises SkipFrame for any graph with bf16 inputs or outputs
("does not support bfloat16 compilation natively"). Dynamo then compiles each
nested Python frame on its own, including the opaque op bodies, and fullgraph
fails on the first untraceable call (torch.cuda.device_count()). fp16, the
dtype Unsloth uses on these cards, compiles as one graph with no breaks.
…r has no native bf16

#14913 routes compiled per-tensor / rowwise dequant below sm89 through the
opaque op. On sm_75 Inductor skips any bf16 graph, so Dynamo traced the op
body as its own frame and fullgraph failed on torch.cuda.device_count(); the
torch expression it replaced compiled fine there. bf16 on such a card now
keeps the torch expression (and the block path its torch fallback), both
bit-identical. Eager calls and every other dtype / card are unchanged.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 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-24T03:50:48.595159Z ea66f04 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. More of your lovely PRs please.

Reviewed commit: ea66f04e68

ℹ️ 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 merged commit ea73d37 into main Oct 24, 2026
10 of 30 checks passed
@danielhanchen
danielhanchen deleted the t4-compile-device-count branch October 24, 2026 03:56
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 24, 2026
…ak between files (unslothai#15078)

Three FP8 tests fail on main depending on the GPU and on which other
test files run in the same pytest session. These are test bugs, not
library bugs. This PR changes tests only.

### Failures
**1.
`test_fp8_dequant_save_policy.py::test_explicit_and_automatic_dequantize_reload[False]`**
- **What the test assumes:** transformers dequantizes a `device_map =
"cpu"` FP8 load by itself.
- **What transformers does:** in 5.x,
`FineGrainedFP8HfQuantizer.validate_environment` only does that when
there is no CUDA, or when the GPU is below sm89. On L4, H100 and B200
the weights stay FP8 and the first assert fails.
- **Where it fails:** run alone, it fails on L4 and B200 and passes on
A100, T4 and CPU.
- **Fix:** when CUDA is present, the automatic case now reports a
pre-sm89 capability, so every GPU takes the same route.

**2.
`test_fp8_ragged_block_dequant.py::test_ragged_rows_match_block_reference[glm5_3]`
and `[ling3]`**
- **Cause:** shared state from an earlier file.
`test_fp8_16bit_text_loader_dequant.py` does real in-process FP8 loads,
and Unsloth's quantizer wrapper then patches transformers'
`Fp8Dequantize` with the ragged-block fix for the rest of the session.
After that, the unpatched case in the ragged test no longer raises
`ValueError`.
- **Where it fails:** on every GPU, but only when the two files share a
session. Both IDs pass when their file runs alone.
- **Fix:**
- the loader file restores `Fp8Dequantize` after each test, with the
same fixture `test_fp8_dequant_save_policy.py` already uses;
- the ragged test unwraps any inherited ragged-block wrapper before
building its unpatched class.

Either change alone made the IDs pass. Both are kept so neither file
depends on the other.

### Results
Colab, transformers 5.17.0, main (09e30cf) and this branch in the same
VM per GPU. "Batch" means all `tests/test_fp8_*` files in one pytest
call.

| GPU | torch | main, batch failures | this PR, batch |
|---|---|---|---|
| L4 (sm89) | 2.10.0 / 2.14.1 | `save_policy[False]`, `ragged[glm5_3]`,
`ragged[ling3]` | 718 passed, 0 failed |
| A100 (sm80) | 2.10.0 / 2.14.1 | `ragged[glm5_3]`, `ragged[ling3]` |
479 passed, 0 failed |
| T4 (sm75) | 2.10.0 / 2.14.1 | the 2 ragged IDs + 5 more | the same 5 |
| B200 (sm100) | 2.10.0 | the same 3 as L4 | 712 passed, 0 failed |

The 5 other T4 failures are the same IDs on both sides and are not
touched here:
- `test_fp8_pre_sm89_byte_dequant.py::test_under_torch_compile`, which
unslothai#15071 fixes; that commit is not in this run's base;
- 4 cases in `test_fp8_to_nf4_load.py`.
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 24, 2026
…es the dequant (unslothai#15107)

`test_fp8_scale_dequant_one_pass.py::test_inductor_fullgraph_dequant_matches_eager`,
added in unslothai#15071, fails on B200 for bf16:
- `[row-dtype1]`
- `[tensor-dtype1]`

### Cause
The test compares bit patterns against the eager reference on every GPU.
- **sm89+:** Inductor can emit fp8, so it fuses the torch dequant
expression itself. Its bf16 rounding can land one ulp away from eager.
- **Below sm89 (T4, the case the test was written for):** the dequant
goes to the opaque op, so the result is bit-exact.

### Change
- **Below sm89:** where `_fp8_e4m3fn_unsupported_in_trace` holds, the
test keeps the bit-exact check.
- **sm89+:** it checks closeness within one ulp of the output dtype
instead (`rtol` 2^-7 for bf16, 2^-10 for fp16, `atol` 0).

The `fullgraph = True` requirement is unchanged on every GPU.

### Results
`tests/test_fp8_scale_dequant_one_pass.py` on B200:

| torch | main | this PR |
|---|---|---|
| 2.14.1 | 2 failed, 45 passed | 47 passed |
| 2.10.0 | 2 failed, 45 passed (also seen on 2.11.0, 2.12.1, 2.13.0) |
47 passed |

T4 keeps the bit-exact branch, so its results are unchanged.
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