Skip to content

FP8 block dequant: ragged / rectangular e4m3fn blocks take the byte decoder on every NVIDIA GPU (4-16x) - #14450

Merged
danielhanchen merged 2 commits into
mainfrom
fp8-ragged-bytes-dequant
Oct 21, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fp8-ragged-bytes-dequant

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Block-FP8 weights whose shape does not tile into whole blocks (2880 x 2880 for gpt-oss-sized layers, 14400 x 4160, ...) or that use rectangular blocks are dequantized by the chunked torch loop _torch_blockwise_dequant on sm89+ GPUs (L4, H100, B200, RTX PRO 6000). That is 4 to 16x slower than one pass over the weight. #14408 added a bit-identical e4m3fn byte decoder (_e4m3fn_bytes_dequant), but routed only pre-sm89 GPUs to it.

Change

  • In _blockwise_weight_dequant_any_shape, every case that took the torch loop on NVIDIA now takes the byte decoder for e4m3fn weights with a 2-D scale: ragged shapes and rectangular blocks on any arch, plus pre-sm89 as before.
  • Unchanged:
    • even square tiles on sm89+ keep the native fp8 Triton kernel;
    • e5m2 weights, per-tensor scales, CPU and ROCm keep their routes.

The byte decoder's output is bit-identical to the torch loop: same arithmetic, one rounding, every byte value incl. subnormals, -0.0 and NaN, as tested in #14408.

Validation

tests/test_fp8_pre_sm89_byte_dequant.py:

  • Routing: sm89 / sm90 / sm100 send ragged and rectangular-block e4m3fn weights to the byte decoder, keep even tiles on the fp8 kernel, and keep ragged e5m2 on the torch loop.
  • New unmocked test: on the real device, ragged (130 x 256, 300 x 520) and rectangular-block (128 x 64) weights take the byte decoder and are bit-equal to the torch loop.
  • Results: with the other FP8 tests and test_triton_fp_fusion.py on B200, 330 passed, 25 skipped; on Colab L4 (torch 2.11, Triton 3.6) the two pre-sm89 FP8 files gave 140 passed.

_blockwise_weight_dequant_any_shape per call, bf16 output, ms. Main and this PR in fresh processes; main's route is the torch loop (main / torch loop = 1.00 in every row):

GPU weight block main this PR speedup
B200 14400 x 4160 128 x 128 1.348 0.121 11.1x
B200 2880 x 2880 128 x 128 0.201 0.012 16.1x
B200 4096 x 14336 128 x 64 1.106 0.113 9.8x
B200 1000 x 4096 128 x 128 0.041 0.008 5.0x
L4 14400 x 4160 128 x 128 5.32 0.81 6.6x
L4 2880 x 2880 128 x 128 0.742 0.059 / 0.080 9.3 to 12.5x
L4 4096 x 14336 128 x 64 5.12 0.755 6.8x
L4 1000 x 4096 128 x 128 0.256 0.060 4.2x

L4 rows: two rounds each, main within 0.1% of itself; the 2880 x 2880 row gave 0.080 and 0.059 ms on this PR.

Not covered:

  • H100 (no Colab capacity on either account): same route as L4 / B200 (sm89+).
  • End-to-end training or decode on a ragged block-FP8 checkpoint.
  • ROCm is untouched here (separate change).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

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 ✅ Completed 2026-10-21T05:30:32.285446Z 4cdf79c Manual request
🔒 Security Review 🔄 Running since 2026-10-21T05:05:08.557825Z e2fbbd4 PR opened
ℹ️ 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

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 4cdf79c16e

ℹ️ 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 1c299c5 into main Oct 21, 2026
@danielhanchen
danielhanchen deleted the fp8-ragged-bytes-dequant branch October 21, 2026 11:23
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 23, 2026
…ake the byte decoder (unslothai#14936)

On ROCm, block-FP8 weights whose tiles are ragged, or whose blocks are
rectangular, still went through the chunked torch loop. unslothai#14450 moved
these cases to the e4m3fn byte decoder on NVIDIA only. On gfx1151 (RDNA
3.5) that loop is 7.5x slower than the byte decoder, which is
bit-identical to it for bf16 / fp32 output.

### Change
- **gfx11 (RDNA3 / 3.5):** the cases the torch loop took run
`_e4m3fn_bytes_dequant` for bf16 / fp32 output. Those cases are ragged
blocks, rectangular blocks, and even tiles when Triton has no fp8e4nv.
- **fp16 output** keeps the torch loop. On gfx1151 both Triton kernels
round fp16 differently from it.
- **Even tiles** keep the fp8e4nv kernel.
- **CDNA, RDNA2, gfx12, gfx950 and unknown archs** keep their current
routes.
- **Kill switch:** `UNSLOTH_FP8_HIP_BYTES_DEQUANT=0` restores the old
routes.
- **NVIDIA:** routing is unchanged.

### Results
gfx1151 (Radeon 8060S, torch 2.11 ROCm 7.13, Triton 3.6), main and this
branch in the same job on the same runner:

| shape | main | this PR |
|---|---|---|
| ragged 14336 x 4096, bf16 | 7.64 ms (torch loop) | 1.02 ms (byte
decoder) |
| q / up / down (even tiles) | fp8e4nv kernel | unchanged |

- **Correctness:** output is bit-equal to the torch loop.
- **Kill switch:** restores main's routes. A/A on the unchanged `up`
shape is within 0.6%.
- **FP8 tests on gfx1151:** main has 121 passed / 15 failed, this branch
155 passed / 15 failed. The same 15 fail on main: fp16 parity on
gfx1151.
- **NVIDIA, all FP8 test files (B200):**

| torch | main | this PR |
|---|---|---|
| 2.10 | 707 passed | 733 passed |
| 2.14 | 659 passed | 685 passed |

The same 3 tests fail on main and here:
`test_explicit_and_automatic_dequantize_reload[False]` and
`test_ragged_rows_match_block_reference[glm5_3|ling3]`.

### Tests
- `test_rocm_routes` / `test_rocm_kill_switch_restores_old_routes`: the
routing table per gfx arch and Triton version.
- `test_real_rocm_device_takes_bytes_kernel_bit_exact`: runs on real
ROCm hardware.
- The NVIDIA routing assertions are kept.

### Not covered
- **AMD architectures:** CDNA, RDNA2, gfx12 and gfx950 were not run.
They keep their current route by construction.
- **Real checkpoint on AMD:** none was run; the evidence is kernel-level
plus tests.
- **Windows ROCm** was not run.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

1 participant