Repository navigation
Conversation
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); | ||
|
|
There was a problem hiding this comment.
This is implicitly check if this is block scaled by simply checking the group size. I'm not sure about this here
There was a problem hiding this comment.
I think it is fine for now, we can worry about it when there are new quantization modes in future.
| 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)) * |
There was a problem hiding this comment.
The # of scale per expert is ⌈rows/128⌉ x how many a row
| 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; |
There was a problem hiding this comment.
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
| astype(scales, dtype, s), | ||
| astype(*biases, dtype, s)}; | ||
| } else if (qmode == QuantizationMode::Fp8) { | ||
| inputs = {x, w, astype(scales, float32, s)}; |
There was a problem hiding this comment.
if it's bf16 each call will cast one time. I'd suggest to cast once at the time mlx-lm load it
tony2037
left a comment
There was a problem hiding this comment.
@nastya236 can you help me to review it? Thank you. Happy Thaknsgiving
|
Tested on an M5 Pro (macOS 27.0.1, Xcode 26.3) at |
zcbenz
left a comment
There was a problem hiding this comment.
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/
| // scales[r / group_size, c / group_size]. | ||
| template <int group_size> | ||
| MLX_MTL_CONST bool is_block_scaled = (group_size == 128); | ||
|
|
There was a problem hiding this comment.
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>
|
Thanks @kongxinjun for the M5 numbers. Since fp8 has no NAX kernels, the later commit |
|
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? |
|
@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. |
Adding nax support in follow-up PR would make review much easier for us. |
|
thank you! all cuda tests are failing, can you have a look? |
|
@tony2037 I reran it on the same M5 Pro (macOS 27.0.1, Xcode 26.3, on AC power), building
The new crossover works as intended. lldb shows For reference, |
- 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>
|
Hi @nastya236 Fixed in thanks for your help on this |
|
Hi @kongxinjun Thanks for rerunning. |
|
@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? |
Follow-up to ml-explore/mlx-lm#2081. As discussed there, this PR extends
gather_qmm(and
quantized_matmul) to block-scaled fp8so that FP8 checkpoints such as
Qwen/Qwen3-0.6B-FP8can run without dequantizing to bf16 at load time(also see vllm-project/vllm-metal#1410, #1416).
Goals
"fp8": e4m3 weights with one float32 or bfloat16 scale per[128, 128] tile. Scales keep the checkpoint layout.
quantized_matmul,gather_qmmanddequantize, on CPU and Metal.Design
risr / 128;the other modes are unchanged since the new paths are behind
if constexpr.no-op for float32 scales, so loaders can convert once at load.
Limitations
quantizewithmode="fp8"is not supported yet, only loading existing checkpoints.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.
gather_qmm_rhsmakes the sorted tests fail.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
Scripts: https://gist.github.com/tony2037/c99087ad291b6965dbf06eb500f69f0d
Questions
group_size == 128. Is that OK, or would youprefer an explicit mode/layout template parameter on the fp kernels?
"fp8"a good name for the mode?AI usage disclosure
gather_qmm_rhspath.🤖 Generated with Claude Code