Repository navigation
FP8 per-tensor / rowwise dequant under torch.compile below sm89: one opaque byte-decode op - #14913
Conversation
|
@codex review |
There was a problem hiding this comment.
💡 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".
| # 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): |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
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).
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. |
…ile tracing [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
[skip ci]
(#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.
…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.
Below sm89, compiling a per-tensor or rowwise FP8 dequant (
weight_dequanton an e4m3fn weight insidetorch.compile) crashed. Inductor fusedx.to(dtype) * sinto a Triton kernel that loads fp8e4nv, which Triton does not support before sm89:On a real T4,
test_compiled_dequant_keeps_the_fused_expressionfailed 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:_fp8_kernel_unsupported, the gate the block path already uses),_scale_mulnow calls the byte-decode kernel from FP8 per-tensor / rowwise dequant: one Triton pass over the e4m3fn bytes #14886. The kernel is registered as one opaque op,unsloth::fp8_scale_dequant, via_opaque_under_compile, so Inductor never generates fp8 code for it.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.pyandtest_fp8_pre_sm89_dequant_fallback.py.How to read the main column:
test_fp8_pre_sm89_byte_dequant.py::test_under_torch_compile, which fails the same way on both sides; see below.Compiled with the default Inductor backend,
fullgraph=True, comparing bit patterns and strides against the torch expression:torch.* op returned non-Tensor(device_count)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_capabilitychecks that the compiled gate queries no device capability while tracing (it reads a per-device tuple built at import).Not changed / not covered
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.torch.version.hip).