Skip to content

FP8 block dequant on ROCm: round fp32 to fp16 with integer ops - #14992

Merged
danielhanchen merged 4 commits into
mainfrom
fp8-hip-exact-f16-round
Oct 23, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
fp8-hip-exact-f16-round

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

On gfx1151, block-FP8 dequant to fp16 is not bit-equal to the torch reference (_torch_blockwise_dequant). bf16 output was already exact.

Cause

  • Lowering: ROCm Triton 3.6 lowers "fp32 multiply, then cast to fp16" to 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).
  • Kernels affected: both block-dequant kernels, the e4m3fn byte decoder (_e4m3fn_bytes_dequant_kernel) and the fp8e4nv weight_dequant_kernel.
  • This is the gap FP8 block dequant on gfx11 ROCm: ragged / rectangular e4m3fn blocks take the byte decoder #14936 left: it moved the ragged / rectangular gfx11 cases to the byte decoder for bf16 / fp32 only, and kept fp16 on the torch loop for this reason.

Change

  • ROCm: on ROCm both kernels round the fp32 product to fp16 with an integer round-to-nearest-even (_f32_to_f16_rne). It keeps subnormals and the sign of zero, and no backend combine can fuse it.
  • NVIDIA: the rounding sits behind an EXACT_F16 constexpr, 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.
  • Override: UNSLOTH_FP8_EXACT_F16_ROUND=0 / 1 forces it off / on.
  • Shift clamp: lanes that take the normal path never shift by a negative count.

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.py and the new test_fp8_exact_f16_round.py.

main this PR
passed / failed / skipped 181 / 15 / 4 195 / 9 / 6
  • Newly passing on this branch:
    • test_bytes_kernel_matches_torch_fallback[out_dtype0-...] (3 shapes);
    • test_past_int32_offsets;
    • test_transposed_and_strided[shape0] and [shape2].
  • New test file: all of its tests pass or skip on gfx1151.
  • Pre-existing failures: the 9 remaining failures are the same tests on main and here:
    • test_fallback_matches_the_triton_kernel_bit_for_bit (6 cases), test_pre_sm89_forward_never_reaches_the_fp8_only_kernels (2 cases) and test_fp8_block_linear_forward_backward_unchanged.
    • They simulate an NVIDIA capability with a torch.cuda.get_device_capability monkeypatch, 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) and test_triton_fp_fusion.py: 14 passed.

Tests

tests/test_fp8_exact_f16_round.py:

  • Rounding helper: _f32_to_f16_rne is 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.
  • Both dequant kernels: they are bit-equal to the torch loop for fp16 output on every e4m3fn byte. Scales from 2^-40 to 2^10 cover fp16 subnormals and overflow. A non-vacuity check confirms the data contains double-rounding ties.
  • Defaults: bf16 / fp32 are untouched; ROCm defaults to the exact path, and the env switch works.
  • Codegen: an offline gfx1151 compile shows the fused mixed-precision instructions are gone with the exact path.

Not covered

  • AMD architectures: CDNA, RDNA2, gfx12 and gfx950 were not run. The exact path is on for ROCm in general, so those take it too.
  • Real checkpoint on AMD: none was run; the evidence is kernel-level plus tests.
  • Windows ROCm: not run.

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

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 🔄 Running since 2026-10-23T15:08:44.077672Z 5a49e5f 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.

@danielhanchen
danielhanchen merged commit 3922fa9 into main Oct 23, 2026
@danielhanchen
danielhanchen deleted the fp8-hip-exact-f16-round branch October 23, 2026 17:04
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]>
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