Repository navigation
FP8 block dequant on ROCm: round fp32 to fp16 with integer ops - #14992
Merged
Merged
Conversation
On gfx11 ROCm Triton 3.6 lowers an fp32 multiply followed by an fp16 cast to v_fma_mixlo_f16(a, b, +0): -0.0 (byte 0x80) comes out +0.0, and the product may be rounded once from the exact value instead of fp32 then fp16 as the torch loop does. The e4m3fn byte decoder and the fp8e4nv weight_dequant_kernel were therefore not bit-equal to _torch_blockwise_dequant for fp16 output there (bf16 already was). On ROCm both kernels now round the fp32 product to fp16 with an integer round-to-nearest-even (EXACT_F16 constexpr, off on NVIDIA, so NVIDIA code is unchanged). UNSLOTH_FP8_EXACT_F16_ROUND=0 / 1 forces it.
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. |
lonexreb
added a commit
to lonexreb/unsloth
that referenced
this pull request
Oct 31, 2026
…r too _hip_bytes_dequant kept fp16 on the torch loop because the gfx1151 byte decoder rounded fp16 unlike that loop (unslothai#14936). unslothai#14992 made both dequant kernels round to fp16 with integer ops on ROCm, bit-equal to the loop and on by default there, but left this gate as it was, so ragged / rectangular block weights dequantised to fp16 on gfx11 still took the ~7x slower torch loop while bf16 / fp32 took the kernel. Gate fp16 on that rounding instead: with UNSLOTH_FP8_EXACT_F16_ROUND=0 the torch loop keeps the route. Co-Authored-By: Claude Fable 5.1 <[email protected]>
danielhanchen
added a commit
to NilayYadav/unsloth
that referenced
this pull request
Nov 14, 2026
…r too (unslothai#15224) ## What `_hip_bytes_dequant` in `unsloth/kernels/fp8.py` still refuses `out_dtype = torch.float16`, with the comment "on gfx1151 the byte decoder rounds fp16 differently from the torch loop". That was true when unslothai#14936 moved the ragged / rectangular gfx11 cases to the byte decoder for bf16 / fp32 only. unslothai#14992 then made both dequant kernels round to fp16 with integer ops on ROCm, bit-equal to `_torch_blockwise_dequant` (its `test_dequant_kernels_match_torch_loop_fp16[bytes-1]` pins that) and on by default there (`_FP8_EXACT_F16_ROUND = torch.version.hip is not None`), and described itself as closing the gap unslothai#14936 left. The gate was not updated, so on gfx11 a block-FP8 weight dequantised to fp16 still takes the torch loop (~7x slower per unslothai#14936) while bf16 / fp32 take the kernel. Same outputs either way; this is the speed path the rounding work was done for. ## How Gate fp16 on the rounding that makes it exact instead of excluding it outright: ```python if out_dtype == torch.float16 and not _fp8_exact_f16_round(out_dtype): return False return _HIP_FP8_BYTES_DEQUANT and _rocm_bytes_dequant_arch(device) ``` So the ROCm default and `UNSLOTH_FP8_EXACT_F16_ROUND=1` route fp16 through the byte decoder; `=0` (the plain cast ROCm Triton fuses into `v_fma_mixlo_f16`) keeps the torch loop. bf16 / fp32 are unchanged, and nothing changes on NVIDIA, where `torch.version.hip is None` short-circuits before this gate. ## Verification - `tests/test_fp8_pre_sm89_byte_dequant.py::test_hip_bytes_dequant_takes_fp16_once_its_rounding_is_exact` (new, CPU): with the arch probe and the HIP switch patched on, fp16 routes with bf16 under `=1` and falls back under `=0`. - `test_rocm_routes`: the fp16 row now asserts the byte decoder under `=1` and the torch loop under `=0` (it was pinned to the torch loop with the stale rationale). Lifted `_hip_bytes_dequant` + `_fp8_exact_f16_round` from main's file vs this branch, `_FP8_EXACT_F16_ROUND = True` as on ROCm: | `UNSLOTH_FP8_EXACT_F16_ROUND` | main, fp16 | this branch, fp16 | bf16 (both) | |---|---|---|---| | unset (ROCm default) | torch loop | byte decoder | byte decoder | | `1` | torch loop | byte decoder | byte decoder | | `0` | torch loop | torch loop | byte decoder | No ROCm hardware here: the gfx11 fp16 bit-equality rests on unslothai#14992's own test, and the GPU-gated route test ran only in CI. `ruff check`, `enforce_kwargs_spacing.py` and the `ruff-format-with-kwargs` hook pass; `fp8.py` parses under Python 3.9. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5.1 <[email protected]> Co-authored-by: Daniel Han <[email protected]>
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 gfx1151, block-FP8 dequant to fp16 is not bit-equal to the torch reference (
_torch_blockwise_dequant). bf16 output was already exact.Cause
v_fma_mixlo_f16(a, b, +0). That turns -0.0 into +0.0, and the product is not rounded the way the torch loop rounds it (fp32 first, then fp16)._e4m3fn_bytes_dequant_kernel) and the fp8e4nvweight_dequant_kernel.Change
_f32_to_f16_rne). It keeps subnormals and the sign of zero, and no backend combine can fuse it.EXACT_F16constexpr, which is off on NVIDIA. The generated PTX is identical to main for 48 configs: both kernels, fp16 / bf16 / fp32 output, a few tile shapes, sm75 / 80 / 89 / 90 / 100 / 120, Triton 3.8. Debug line info is excluded from the comparison.UNSLOTH_FP8_EXACT_F16_ROUND=0/1forces it off / on.Results
gfx1151 (Radeon 8060S), main (d954720) and this branch in the same job. Tests:
test_fp8_pre_sm89_byte_dequant.py,test_fp8_pre_sm89_dequant_fallback.py,test_fp8_ragged_block_dequant.py,test_fp8_fp16_scale_rounding.pyand the newtest_fp8_exact_f16_round.py.test_bytes_kernel_matches_torch_fallback[out_dtype0-...](3 shapes);test_past_int32_offsets;test_transposed_and_strided[shape0]and[shape2].test_fallback_matches_the_triton_kernel_bit_for_bit(6 cases),test_pre_sm89_forward_never_reaches_the_fp8_only_kernels(2 cases) andtest_fp8_block_linear_forward_backward_unchanged.torch.cuda.get_device_capabilitymonkeypatch, which ROCm routing does not read. That is a test gating issue for a follow-up, not touched here.NVIDIA (B200, torch 2.14):
test_fp8_exact_f16_round.py(both kernels, exact path forced on and off) andtest_triton_fp_fusion.py: 14 passed.Tests
tests/test_fp8_exact_f16_round.py:_f32_to_f16_rneis compared with torch's fp16 cast over every fp32 exponent. Inputs include exact halfway mantissas, values one above and below, subnormals, both zeros, infinities and NaNs.Not covered