Skip to content

FP8 block dequant on gfx11 ROCm: route fp16 output to the byte decoder too - #15224

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
lonexreb:fix/fp8-hip-bytes-dequant-fp16-route
Nov 14, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
lonexreb:fix/fp8-hip-bytes-dequant-fp16-route

Conversation

@lonexreb

Copy link
Copy Markdown
Contributor

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 #14936 moved the ragged / rectangular gfx11 cases to the byte decoder for bf16 / fp32 only. #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 #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 #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:

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 #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

danielhanchen added a commit that referenced this pull request Oct 25, 2026
…15269)

16 cases in `tests/test_fused_preference_loss.py` fail in `Repo tests
(CPU, rest)` on PRs based on current main, for example #15224 and
#15245: every `test_orpo_cpo_match_trl` case plus the DDP / FSDP1 / hook
fallback cases. The reference run trips the hidden-states counter
(`assert (1 >= 1) == False`) or the dtype check.

## Cause

The test, not the product. The test builds its reference from `from
trl.experimental.orpo import ORPOTrainer` (and the CPO / DPO
equivalents). Unsloth's RL patch rebinds those names, and the defining
module's attribute, to the generated `UnslothORPOTrainer`
(`unsloth/models/rl.py`, the `trl.{name} = created_module.Unsloth{name}`
lines). Since #14185, that generated class's `concatenated_forward`
routes through `unsloth_fused_orpo_concatenated_forward`. So once TRL
has been patched in the process, the "TRL" side of the comparison is the
code under test. The stack of the reference forward shows it:
`test_fused_preference_loss.py:437 ->
unsloth_compiled_cache/UnslothORPOTrainer.py:1988`.

It reproduces locally with unsloth-zoo main on the path (16 failed, 9
passed, run alone). With the older installed zoo 2026.10.7, TRL is not
rebound here, which is why it looked like an ordering leak at first.

## Fix

- `_trl_trainer(name)` loads TRL's own trainer module from its file
under a private module name in the same package, so TRL's relative
imports still resolve and nothing Unsloth rebinds is reused.
- It asserts that the class it returns is not an `Unsloth*` class.
- ORPO, CPO and DPO all take their reference from it.

## Verification

- `tests/test_fused_preference_loss.py` against unsloth-zoo main: 16
failed, 9 passed on main; 25 passed here.
- Against the installed zoo 2026.10.7: 25 passed on both main and this
PR.
- Sabotage on the product: scaling the fused ORPO path's `chosen_logps`
by 1.001 in `unsloth/models/dpo.py` fails all 10
`test_orpo_cpo_match_trl` cases.
…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

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T08:43:22.632570Z 07c0afb 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 07c0afb316

ℹ️ 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".

@danielhanchen
danielhanchen merged commit c662a4c into unslothai:main Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants