Repository navigation
FP8 dequant under compile: keep the torch path for bf16 on cards without native bf16 - #15071
Merged
Merged
Conversation
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.
…torch path on T4 bf16
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. More of your lovely PRs please. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
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.
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.
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_dequantop. On T4 in bf16 that op's body gets traced as its own frame.fullgraph = Truethen 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._FP8_DEVICE_LACKS_KERNEL, so nothing queries the GPU while tracing.test_fp8_pre_sm89_byte_dequant.py::test_under_torch_compileandtest_moe_dw_accumulate.py::test_compiled_fullgraph_zero_breaks_and_matches_eager;test_under_torch_compilefailing on T4 in bf16.test_compiled_dequant_below_sm89_is_one_opaque_op: now expects the torch expression for bf16 on T4.Tests
test_inductor_fullgraph_dequant_matches_eager(fp16 / bf16, rowwise / per-tensor) compilesweight_dequantwith real Inductor andfullgraph = True, then compares bits with eager.device_counttest_under_torch_compileandtest_compiled_fullgraph_zero_breaks_and_matches_eager.Not covered