Repository navigation
Fix fp8 block dequant fallback on pre-sm89 GPUs - #10790
Conversation
fp8e4nv (torch.float8_e4m3fn) only exists from sm89 on, so weight_dequant_kernel cannot be compiled for older architectures. The dequant fallback in _blockwise_weight_dequant_any_shape sent evenly tiled square blocks to that triton kernel anyway, so on A100 the fallback raised a triton CompilationError instead of falling back. Take the torch-native scale expansion there as the other two branches already do.
|
Confirmed the even-tiling branch in unsloth/kernels/fp8.py still hands float8_e4m3fn to the triton kernel, which cannot compile fp8e4nv below sm89. The new guard tests only the major capability below 9, which is also true for sm89 cards like the 4090 and L40S, so they would drop off the triton path too; could you switch it to a full (major, minor) at least (8, 9) comparison? |
triton adds fp8e4nv to its supported dtypes at capability >= 89, not >= 90 (third_party/nvidia/backend/compiler.py). Testing only the major version also diverts sm89 (4090, L40S, L4), which compiles the kernel fine, so all of Ada dropped off the triton path. Compare (major, minor) against (8, 9) instead, and skip the check on ROCm, where the capability is gfx-derived rather than an SM number and AMD's triton backend lists fp8e4nv unconditionally. Also chunk the torch expansion. It built two m*n float32 temporaries, ~6x the peak of the kernel it stands in for, and the guard newly routes full-size evenly tiled weights into it, so a flattened MoE expert stack could trade the CompilationError for an OOM on the 40GB A100s this is meant to fix. Walking whole block-rows holds the peak at 1.2-1.8x the triton path; measured on 8192x28672, 2688MB -> 532MB, values bit-identical. Verified on a B200 with TRITON_OVERRIDE_ARCH plus a matching device-capability patch, so the compiler takes the same capability-gated path the real card would: sm80/sm86 reproduce the reported CompilationError on main and pass here, and sm89/sm90/sm100 are byte-identical to main. 495 routing cells (capability x dtype x scale dtype x shape x block) are bit-exact against a float64 reference, forward and backward.
|
Thanks for tracking this down, the underlying bug is real. I pushed a commit to the branch rather than leaving it on you. The guard boundary was off by one architecture. triton adds # third_party/nvidia/backend/compiler.py
if capability >= 89:
supported_fp8_dtypes.add("fp8e4nv")so The fallback also needed to get leaner. It built two
How it was checked. No A100 here, so
Added One thing worth saying plainly: this does not make block-fp8 training work end to end on A100. The dequant helper is fixed, but the main |
|
Addendum on the upgrade path, since that is the part a kernel change usually gets away without checking. Took three real released versions, installed each, then pip-upgraded in place to this branch exactly as
The dequant checksum is identical ( Version skew is fine too: each upgrade deliberately kept the old Stale Installs older than 2026-07-06 predate On the Desktop/Studio side the diff is two files, |
…m89 GPUs The routing cases wrapped the real weight_dequant_block, so asserting "this capability selects triton" actually compiled and launched the kernel. On the very cards this file is about that raises, and a real A100 run failed 7 of them: the four (8,9)/(9,0)/(10,0)/(12,0) boundary cases and all three ROCm cases, each with "type fp8e4nv not supported in this architecture" rather than a routing mismatch. The file claimed to pin routing on any CUDA GPU and did not. These cases only need to observe which branch was chosen, so stub the kernel instead of wrapping it. Values are already covered by test_fallback_matches_the_triton_kernel_bit_for_bit, which skips when the hardware cannot run both sides, and did skip correctly on the A100. Verified on a real Colab A100-40GB (sm80) and locally under TRITON_OVERRIDE_ARCH=sm80, where any surviving real compile would still fail. Still discriminates: main fails the four pre-sm89 cases, and the previous commit on this branch fails sm89 and ROCm.
|
Ran this on a real Colab A100-SXM4-40GB (sm80, torch 2.11.0+cu128, triton 3.6.0) rather than leaving it at the simulation. Your report reproduces exactly, on the actual card and on the actual shipped code:
Memory on the 39.5 GiB card matches the B200 numbers exactly, and the chunked result is bit-identical to the old one:
The run also found a bug in my own test, which I have fixed in Those cases only need to observe which branch was chosen, so the kernel is now stubbed rather than wrapped. Values stay covered by Worth stating plainly, since it was nearly missed: the notebook runner reported PASS on the first run because every cell executed. The 7 failures were inside a cell, in |
|
Correction to my last comment: Colab does offer an sm89 card. The L4 is Ada, compute capability 8.9. So the sm89 claim no longer rests on simulation - here it is on real silicon. NVIDIA L4, capability (8, 9), torch 2.11.0+cu128, triton 3.6.0: That is the regression reproduced end to end: the originally submitted What that misroute costs, measured on the L4 itself:
One thing I should flag about my own reporting: the The regression test also gets stronger on this card: 22 passed, 0 skipped. Coverage now, all on rented hardware: A100-40GB (sm80) for the reported bug, L4 (sm89) for the boundary, B200 (sm100) locally for the untouched side. The remaining gaps are ROCm and sm120, which stay argued from triton's backend source rather than measured. |
…t the dequant Fixing _blockwise_weight_dequant_any_shape was necessary but not sufficient. On a simulated sm80, four of six fp8 entry points still raised with that fix in place: act_quant, fp8_block_matmul, and FP8BlockQuantLinear forward and backward, all with the same "type fp8e4nv not supported in this architecture". act_quant emits an fp8e4nv pointer and the w8a8 gemm consumes two, so a card that cannot compile the dtype cannot run the pair either, and the public path kept crashing on the exact GPUs this branch is meant to fix. Lift the predicate into _fp8_kernel_unsupported and apply it at the two other places that reach those kernels: FP8BlockQuantLinear.forward now takes the dequant-plus-matmul branch it already had for an indivisible hidden dim, and the fbgemm path folds it into kernel_supported, since triton_quantize_fp8_block emits fp8e4nv too. All three reachable call sites are now behind the guard, so sm80 and sm86 go from 4/6 failing to 0/6, while sm89 and sm90 stay at 0/6 unchanged. The fallback is a different computation, not a bit-for-bit substitute, so it was measured rather than assumed: against a float64 reference it is about 8x MORE accurate than the kernel it replaces (relative error 0.0032 vs 0.0254), because it keeps activations in bf16 instead of quantizing them, with gradients finite and of matching magnitude. Verified on real hardware: A100-40GB and L4 via Colab, gfx1151 via the AMD CI runner (NO_REGRESSION on both jobs, with ROCm correctly never diverting even at spoofed pre-sm89 capabilities).
|
Extended this so pre-sm89 cards actually RUN fp8, not just survive the dequant. Pushed as Fixing the dequant helper was necessary but not sufficient. On a simulated sm80, with the helper already fixed, four of six fp8 entry points still raised:
The predicate is now On a real A100-40GB and a real L4, a full forward+backward through the package now runs: The fallback is a different computation, so I measured it rather than assuming. Against a float64 reference it is about 8x MORE accurate than the kernel it replaces, relative error 0.0032 vs 0.0254, because it keeps activations in bf16 instead of quantizing them. Gradients finite and of matching magnitude. So pre-sm89 gets: works, slower, slightly more accurate. Hardware coverage, all rented or self-hosted, none simulated:
The AMD run used the gfx1151 self-hosted runner in regression mode rather than differential, deliberately: the defect is an NVIDIA capability gate, so the base cannot exhibit it there and a differential run would be VOID rather than a pass. It reports identical routing and identical checksums at all five capabilities including spoofed pre-sm89 ones, with Remaining gap, stated plainly: |
|
@codex security 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. Nice work! 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". |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
|
Pushed
Also shortened the comments this branch added, since several had grown to four and five lines for a one-line point. Checks on the final state:
Cross-platform, via staging CI on Linux x64, Windows x64 and macOS arm64: all three green, with the file collecting 23 of 23 tests on each. To be precise about what that shows, those runners have no CUDA, so all 23 skip on the One note on approach, since it came up. This is first-party code in |
|
Went looking for the commit that broke this, on the assumption it was a regression. It is not one. The archaeology says fp8 has never worked on a pre-sm89 GPU, since the day it landed. Bisect. I wrote a probe that extracts each commit's The first bad commit is the commit that introduced fp8. There is no good commit after it. I also ran the probe linearly over all 24 commits that have ever touched Blame. So why did nobody hit it for eleven months. Three things lined up:
Worth noting that Practical upshot for the changelog: this is a gap being closed, not a regression being reverted. Anyone who has ever tried a block-fp8 checkpoint on an A100, A10, V100 or T4 hit it, and there was no version where it worked. |
|
Pushed lru_cache on the guard: done, and it is worth very little. A device's capability cannot change, so the lookup is now cached per device with no_grad: worth more than the cache, for a reason that is not speed. Timing is unchanged, 0.527 ms either way. But the fallback was building an autograd graph the triton kernel it replaces never builds: So the two paths now agree, and the fallback can no longer quietly retain a graph in a grad-enabled caller such as torch.compile: measured, and deliberately not used. Compiled, the simple three-line expansion is excellent, 17x faster at 4096x14336 and holding the peak at the triton kernel's own 112 MB. But:
A conditional guarantee with a flaky test is worse than a slightly slower unconditional one, so the chunked eager version stays. Happy to revisit if you would rather take the compiled version and drop the memory assertion. On the two GRPO notebooks, an honest account. Colab would not allocate an H100 to this account on three attempts, and both notebooks need one: I ran it locally on a B200 instead, which clears the same gate. Results, with what they do and do not establish:
The evidence for the kernels themselves remains the unit level: 495 routing cells bit-exact against a float64 reference, and real hardware on A100, L4, T4, RTX PRO 6000 Blackwell, B200 and gfx1151. A full FP8 GRPO run on an H100 is still the one thing outstanding, and it needs an H100 that Colab is not currently handing out. |
|
Follow-up on the questions raised since the last round: caching the guard, whether the fallback is optimal, whether The guard is now cached
@functools.lru_cache(maxsize = None)
def _fp8_device_lacks_kernel(device):
return torch.version.hip is None and torch.cuda.get_device_capability(device) < (8, 9)Measured on a B200, 200k calls each: 1.19 us per call before, 0.53 us after, so 0.66 us saved per call. That is 0.66 ms per thousand linear layers, which is noise next to the kernel itself, but it costs nothing and the capability of a device cannot change. One hazard came with it: anything simulating another card must call The fallback is under no_grad, and is faster eagerIt was not under
The workload is a memory-bound scale expansion, so there is nothing for inductor to fuse away, and compiling the chunk loop unrolls one graph per chunk. The 34 s first call is per shape, and a Llama-sized model hits four distinct weight shapes. The callers are already inside compiled regions in the normal path, so this would be paying the warmup twice. Left eager, with a comment saying so. Peak memory is unchanged from the earlier chunking fix: 532 MB against 2688 MB for the whole-weight expansion, on a 4096x8192 weight. Full GRPO notebook
The guard correctly stands aside on sm120, so this is the unchanged path and it behaves as before. One cell raised Real A100The rest of this needed real sm80 silicon. Reporting sm80 through On an A100-SXM4-40GB: and a genuine block-FP8 checkpoint, The same prompt on a B200 gives " Paris" and loss 4.35188, so the pre-sm89 path lands in the same place. Suites on that A100: |
…quantizes below sm89 anyway
|
Correction to my earlier comment, since it changes a conclusion rather than just wording. I claimed the A100 run showed "the pre-sm89 path is landing in the same place". The numbers were real, but the mechanism was not what I said. That load had been dequantized to bf16 by The check that settles it: a B200 at capability (10, 0), where that transformers gate does not fire, loads I have reverted that loader change in 85467b6. What still stands, measured on a real A100-SXM4-40GB rather than inferred: That is the PR doing its job, and it matches the reproduction in the description, which is a kernel-level test and not a model load. The remaining commits are the sm89 boundary, the chunked fallback, the cached capability lookup, Making the fallback reachable from a pre-sm89 Suites after the revert: 42 passed, 5 skipped across the three FP8 files. |
|
Codex Review: Didn't find any major issues. 🎉 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". |
|
Thanks for taking this over. The guard I wrote was wrong: |
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
What
_blockwise_weight_dequant_any_shapesent evenly-tiled square fp8 blocks to the tritonweight_dequant_kerneleven when the GPU cannot compile that kernel's fp8 dtype.torch.float8_e4m3fn(fp8e4nv) only exists from sm89 on, so on A100 the fallback raisedinstead of falling back — the opposite of what the helper is for. The other two branches of the same
helper already expand the scales in torch; this makes the third one do the same when the device cannot
compile the kernel.
Reproduction
A100-PCIE-40GB (sm80), torch 2.8.0+cu128, triton 3.4.0:
Before:
1 failed, 3 passed— only thebf16_scaleparameter fails, because it is the one thatreaches the block dequant path with a real 128x128 scale grid (the other three take the per-tensor or
uneven-tiling branches, which are already torch-native).
After:
4 passed.No regressions in the surrounding fp8 coverage:
tests/test_fp8_fbgemm_block_shapes.py— 11 passed, 1 skippedtests/test_fp8_device_context.py— 6 passed, 3 skippedNotes
sm89+ is unaffected: the guard only diverts when the device cannot compile fp8e4nv, so those GPUs
still take the triton path and produce the same values.
I searched issues and PRs for this and did not find an existing report.
Maintainer notes
Reviewed and extended in place. The original diagnosis holds, and the fix now covers the
rest of the surface that shared the same defect.
Changes on top of the original commit
< (8, 9), not< 9. A major-only test drops all of Ada (4090, L40S, L4)onto the fallback even though those cards do compile fp8e4nv. The guard also stands down
under ROCm, where the capability is gfx-derived and AMD's triton lists fp8e4nv regardless.
m*n float32 temporaries, roughly 6x the triton kernel's peak, which traded a
CompilationErrorfor an OOM on a 40GB A100. Measured 2688 MB to 532 MB on a 4096x8192weight, bit-identical output.
FP8BlockQuantLinear.forwardandFP8_fbgemm_block_linear.forward, plusweight_dequant's block branch, which now routesthrough the same helper so
fast_dequantize's callers inherit the fallback.act_quantemits e4m3fnwhatever the weight is, so e5m2 block weights reached an uncompilable kernel. The dequant
site deliberately keeps the weight's own dtype, since fp8e5 compiles everywhere.
its own fp8e4nv kernels.
no_gradto match the kernel it stands in for, which builds no graph either.torch.compilewas measured and rejected: 1.83 ms against 1.61 ms eager at 4096x14336, witha 34 s first call per distinct shape. It is a memory-bound scale expansion with nothing to
fuse, and compiling the chunk loop unrolls one graph per chunk.
Not a regression.
git bisectputs the first bad commit at the one that introducedfp8.py; all 24 commits to that file fail on sm80. It went unnoticed becausevalidate_fp8gates
load_in_fp8at>= (9, 0), which looks like full coverage, and the fp8 suites skiprather than fail on hardware that cannot run them.
Verified on a real A100-SXM4-40GB, not a simulation, since reporting sm80 through
get_device_capabilityon a newer card retargets every triton kernel in unsloth and the rundies in rms layernorm before reaching fp8:
tests/test_fp8_pre_sm89_dequant_fallback.py23 passed 6 skipped,tests/test_fp8_fbgemm_block_shapes.py11 passed 1 skipped,tests/test_fp8_device_context.py6 passed 3 skipped, all on that A100.Llama_FP8_GRPO.ipynbas published, install pointed here, ran 5 GRPO steps on an RTX PRO 6000with 112 fp8 params and finite losses throughout, confirming sm89+ is untouched. AMD gfx1151
returned NO_REGRESSION.
Scope deliberately not taken. A pre-sm89
from_pretrainedstill will not reach thisfallback, because transformers' own
FineGrainedFP8HfQuantizer.validate_environmentsetsdequantize = Truebelow capability 8.9 and loads the model as bf16. Keeping the weights infp8 and dequantizing per forward means patching that validator, and belongs in its own change
with memory and throughput numbers behind it. This PR stays on the kernels.