Skip to content

Add block scaled fp8 quantization mode - #5017

Open
tony2037 wants to merge 3 commits into
ml-explore:mainfrom
tony2037:fp8-block-qmm
Open

tony2037 wants to merge 3 commits into
ml-explore:mainfrom
tony2037:fp8-block-qmm

Conversation

@tony2037

Copy link
Copy Markdown

Follow-up to ml-explore/mlx-lm#2081. As discussed there, this PR extends gather_qmm
(and quantized_matmul) to block-scaled fp8
so that FP8 checkpoints such as
Qwen/Qwen3-0.6B-FP8 can run without dequantizing to bf16 at load time
(also see vllm-project/vllm-metal#1410, #1416).

Goals

  • New quantization mode "fp8": e4m3 weights with one float32 or bfloat16 scale per
    [128, 128] tile. Scales keep the checkpoint layout.
  • Supported in quantized_matmul, gather_qmm and dequantize, on CPU and Metal.

Design

  • Reuses the existing fp kernels. The scale row of output row r is r / 128;
    the other modes are unchanged since the new paths are behind if constexpr.
  • Scales are converted to float32 in the op so the kernels read one type. This is a
    no-op for float32 scales, so loaders can convert once at load.
  • The GPU shape check runs when the op is created, not at eval.

Limitations

  • GPU: the last dimension of the weight must be a multiple of 128 (But CPU has no such limit).
  • NAX and the specialized qmv/qmm kernels are not used for fp8. Not tested on M5. No CUDA.
  • quantize with mode="fp8" is not supported yet, only loading existing checkpoints.
  • MoE at 64 tokens is ~14% slower than mxfp8; I haven't looked into it yet.

Tests

  • python/tests/test_fp8_block.py (CPU and GPU): partial tiles, both transposes,
    gather (sorted, unsorted, lhs/rhs indices), many sorted tokens, vjp, validation.
  • Breaking the per-expert scale stride in gather_qmm_rhs makes the sorted tests fail.
  • Full suite passes except test_grouped_mm ("Metal grouped_mm NYI", skipped in CI).

Benchmarks (M3, 16 GB)

fp8 is on par with mxfp8 and about 2x faster than bf16 for decode matmuls (1.46x end to end
on Qwen3-0.6B-FP8), and halves the memory of the quantized weights.

Numbers
bf16 mxfp8 fp8
qmm 8192x4096, M=1 (us) 780 382 375
MoE layer, 1 token (us) 1140 588 589
MoE layer, 512 tokens (us) 32608 28583 27871
Qwen3-0.6B-FP8 decode (tok/s) 65.9 96.2
Qwen3-0.6B-FP8 params (MiB) 1137 717

Scripts: https://gist.github.com/tony2037/c99087ad291b6965dbf06eb500f69f0d

Questions

  1. Block scales are detected by group_size == 128. Is that OK, or would you
    prefer an explicit mode/layout template parameter on the fp kernels?
  2. Is "fp8" a good name for the mode?
  3. Is the multiple-of-128 limit on the GPU acceptable for a first version?

AI usage disclosure

  • Used Claude Code to write the code and tests, and to discuss the design, tests and benchmarks. Reviewed every file, made the design calls, and asked for the MoE benchmark, which found the missing gather_qmm_rhs path.

🤖 Generated with Claude Code

Add an "fp8" quantization mode for e4m3 weights with one float32 or
bfloat16 scale per [128, 128] tile, the layout of block FP8 checkpoints
such as Qwen/Qwen3-0.6B-FP8. quantized_matmul, gather_qmm and dequantize
support it on the CPU and on Metal, so these checkpoints can run without
dequantizing the weights to bf16 at load.

Metal reuses the fp kernels: a group size of 128 selects float block
scales and the scale row of an output row r is r / 128. The fp8 mode is
routed to qmv, qmv_wide, qvm, qmm_t/n, gather_qmv/qvm, gather_qmm_t/n and
gather_qmm_rhs. The NAX and other specialized kernels are not used for it
yet, and on the GPU the last dimension of the weight must be a multiple of
128. Quantizing to fp8 is not supported yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
// scales[r / group_size, c / group_size].
template <int group_size>
MLX_MTL_CONST bool is_block_scaled = (group_size == 128);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is implicitly check if this is block scaled by simply checking the group size. I'm not sure about this here

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine for now, we can worry about it when there are new quantization modes in future.

Comment thread mlx/backend/cpu/quantized.cpp Resolved
Comment thread mlx/backend/cpu/quantized.cpp Resolved
const size_t stride_w = transpose ? N * K_w : K * N_w;
const size_t stride_s = transpose ? N * K_g : K * N_g;
const size_t stride_s =
size_t(num_scale_rows<group_size>(transpose ? N : K)) *

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The # of scale per expert is ⌈rows/128⌉ x how many a row

Comment thread mlx/backend/metal/quantized.cpp Resolved
constexpr int pack_factor = get_pack_factor<32, bits>();
constexpr int bytes_per_pack = get_bytes_per_pack<32>();
constexpr int nf4 = group_size / 4; // float4 lanes per quant group
constexpr int chunk = is_block_scaled<group_size> ? 32 : group_size;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

128 / 4 = 32 ; if qmv_wide proecess 128 elements at once the register usage for each lane is going to four times more than mxfp8 does

Comment thread mlx/ops.cpp
astype(scales, dtype, s),
astype(*biases, dtype, s)};
} else if (qmode == QuantizationMode::Fp8) {
inputs = {x, w, astype(scales, float32, s)};

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's bf16 each call will cast one time. I'd suggest to cast once at the time mlx-lm load it

@tony2037 tony2037 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nastya236 can you help me to review it? Thank you. Happy Thaknsgiving

@kongxinjun

Copy link
Copy Markdown

Tested on an M5 Pro (macOS 27.0.1, Xcode 26.3) at 3d05126: test_fp8_block.py and test_quantized.py pass on GPU and CPU; the full suite only fails the known test_grouped_mm (Metal NYI). Results match an fp32 dequantize reference (max rel err 4.3e-3 bf16 / 3.3e-6 fp32, including partial row tiles and E=128 gathers). As expected, fp8 never takes a NAX path on M5. M=1 matches mxfp8 (88 vs 87 us on 8192x4096), but M>=8 is 2-5x slower than mxfp8 and about 4x slower than bf16 at M>=128 (20018 vs 4699 us at M=2048), since both of those use NAX. On Qwen3-0.6B-FP8 that means 1.30x faster decode but 0.38x prefill (2048 tokens) and 0.66-0.73x throughput at batch 16 to 64, relative to bf16.

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really sure about the name, it is probably too general but vllm also just uses "fp8" https://docs.vllm.ai/projects/vllm-omni/en/latest/user_guide/quantization/fp8/

Comment thread mlx/backend/cpu/quantized.cpp Resolved
Comment thread mlx/backend/cpu/quantized.cpp Resolved
Comment thread mlx/backend/cpu/quantized.cpp Resolved
Comment thread mlx/backend/metal/quantized.cpp Resolved
Comment thread mlx/backend/metal/quantized.cpp Resolved
Comment thread python/tests/test_fp8_block.py Resolved
// scales[r / group_size, c / group_size].
template <int group_size>
MLX_MTL_CONST bool is_block_scaled = (group_size == 128);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine for now, we can worry about it when there are new quantization modes in future.

- CPU: fp_qmm_dispatch and fp_bs_qmm_dispatch take the QuantizationMode and
  route fp8 inside the typed dispatch, instead of detecting it by scale dtype.
- Metal: drop the separate fp8 branch in dispatch_qmm_t and check the mode at
  each kernel that has no fp8 support (qmv_mma, qmm_splitk_nax, qmm_splitk,
  qmv_quad).
- Metal: fp8 has no NAX kernels, so on gen 17 it uses the gen 15 qmv/qmm
  crossover, which was tuned without NAX.
- Tests: move the fp8 tests into TestQuantized.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tony2037

Copy link
Copy Markdown
Author

Thanks @kongxinjun for the M5 numbers. Since fp8 has no NAX kernels, the later commit 6c6ef6c makes it use the gen 15 qmv/qmm crossover on gen 17. Could you please help me to rerun bench_fp8.py at M=8 to 32 when you have time? Thank you very much

@tony2037

Copy link
Copy Markdown
Author

I also tried falling back to dequantize + matmul for large M. On my M3 it is slower at every M (25.4 ms vs 27.8 ms at M=2048 on 4096x4096), mostly because the fp8 dequantize is built from ops and takes about 5 ms by itself. So I think the real fix for prefill on M5 is NAX support for fp8, plus maybe a Metal dequantize kernel.

@zcbenz would you like NAX support in this PR, or as a follow-up?

@tony2037

Copy link
Copy Markdown
Author

@zcbenz I kept "fp8" since it matches vLLM and the quant_method in the HF checkpoints. Happy to rename it (for exmplle "fp8_block") if you prefer.

@tony2037
tony2037 marked this pull request as ready for review November 29, 2026 09:27
@zcbenz

zcbenz commented Nov 29, 2026

Copy link
Copy Markdown
Member

would you like NAX support in this PR, or as a follow-up?

Adding nax support in follow-up PR would make review much easier for us.

@zcbenz
zcbenz requested a review from nastya236 November 29, 2026 10:09
@nastya236

Copy link
Copy Markdown
Collaborator

thank you! all cuda tests are failing, can you have a look?

@kongxinjun

Copy link
Copy Markdown

@tony2037 I reran it on the same M5 Pro (macOS 27.0.1, Xcode 26.3, on AC power), building 3d05126 and 6c6ef6c back to back. Medians of 3 alternating runs, bench_fp8.py timing, bf16 activations, us:

M 2048x2048 old new 4096x4096 old new 8192x4096 old new
8 34.0 33.8 116.9 116.8 233.6 233.0
12 47.8 47.8 177.1 177.2 358.4 358.2
16 62.1 45.5 240.5 163.1 318.2 319.0
20 72.8 45.7 291.9 164.0 319.9 321.1
24 90.3 45.9 373.5 163.8 319.6 320.7
28 108.2 46.0 164.0 164.1 321.3 322.4
32 126.6 45.7 163.5 163.1 320.0 319.6

The new crossover works as intended. lldb shows qmm from M=13 on 2048x2048 (was 33) and from M=15 on 4096x4096 and 3072x1024 (was 25). 8192x4096 stays at 13. That removes the M=16 > M=32 anomaly and makes those rows up to 2.3x faster on 4096x4096 (2.8x on 2048x2048), with no change at any other M from 1 to 512 and no NAX kernel hit. One small leftover: qmv_wide just below the new limit is still a bit slower than qmm (4096x4096: 177 us at M=12 and 209 at M=14 vs 163 at M=15; 8192x4096: 358 at M=12 vs 319 at M=13). The measured crossover is about M=11 on all five shapes I tried, so a limit around 11 for fp8 on gen 17 would make it fully monotonic, but that is minor. test_quantized.py passes on GPU and CPU.

For reference, mx.dequantize(mode="fp8") takes 1.7 ms on 4096x4096 here, so on M5 dequantize + matmul still beats fp8 qmm at M=2048 (3.9 vs 10.0 ms). Agreed that NAX in a follow-up is the real fix.

@nastya236 nastya236 added the await response This pull request is waiting for response from the author. label Nov 29, 2026
- The CUDA backend has no block scaled fp8 kernels and its fp kernels read
  the scales as one uint8 per group, so fp8 inputs produced NaNs and out of
  bounds reads. quantized_matmul and gather_qmm now raise for fp8 on CUDA,
  and the fp8 tests that need a GPU kernel are skipped there.
- On gen 17 without NAX kernels the measured qmv_wide/qmm crossover for fp8
  is about M=11 (M5 Pro), so use that instead of the gen 15 limits. Other
  modes and other generations are unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@tony2037

tony2037 commented Nov 29, 2026 •

Copy link
Copy Markdown
Author

Hi @nastya236 Fixed in bb6131c. The cuda backend has no fp8 kernel yet. Its kernel selection does not check the mode, so fp8 fell into the affine path (8-bit weights, no biases, a different scale layout) in which gave NaNs and out of bounds reads. Now quantized_matmul and gather_qmm raise for fp8 on CUDA, and the GPU tests skip there. Could you approve the CI run?

thanks for your help on this

@tony2037

Copy link
Copy Markdown
Author

Hi @kongxinjun Thanks for rerunning. bb6131c I changed the limited toooo 11 for fp8 on gen 17, based on your stats! thanks for your help on this

@tony2037

Copy link
Copy Markdown
Author

@nastya236 so we still not support running fp8 quantized on cuda yet (this should error out). should we follow up on this in a different PR to keep this simple?
Thank you

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

await response This pull request is waiting for response from the author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants