Skip to content

metal: add NVFP4 mul_mat, mul_mat_id and get_rows kernels (mirror of #330 by @Lyxot) - #348

Open
danielhanchen wants to merge 2 commits into
base/upstream-ee17f293bfrom
mirror/lyxot-metal-nvfp4
Open

danielhanchen wants to merge 2 commits into
base/upstream-ee17f293bfrom
mirror/lyxot-metal-nvfp4

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Mirror of #330 by @Lyxot, so the change can be kept mergeable from this repo. All credit for the kernels goes to @Lyxot.

The nightly pin moves from #330 to this PR at 56ec1e88a; #330 stays open.


Original description from #330:

Fixes the slow generation in unslothai/unsloth#15409 (MiniMax-M3.1 GGUFs on Apple Silicon run at ~8.5 tok/s with "61/61 layers offloaded to GPU").

Problem

The MiniMax-M3.1 exports keep the routed experts in native NVFP4 (171 tensors, ~218 GiB in UD-Q8_K_XL, also present in UD-Q4_K_XL). ggml_metal_supports_mul_mat_op returns false for GGML_TYPE_NVFP4, and GET_ROWS rejects it too, so the scheduler leaves every expert MUL_MAT_ID on the CPU. The layer count still reports full offload because the tensors' layers are assigned to Metal; only the expert ops fall back. The reporter's log shows 218.71 GiB CPU_Mapped against 22.57 GiB MTL0_Mapped.

Upstream has no Metal NVFP4 kernels yet. ggml-org#20456 adds them but is open and written against the old single ggml-metal.metal, so it no longer applies.

Fix

Metal kernels for NVFP4 in every path MUL_MAT / MUL_MAT_ID / GET_ROWS can take, following the MXFP4 wiring:

  • ue4m3_to_fp32 in kernels/common.h: places the 7 scale bits into an fp16 and rescales by 2^8, which covers UE4M3 subnormals exactly; 0x7F decodes to 0 as on the CPU. No 0.5 factor, because Metal's kvalues_mxfp4_f are the real E2M1 values while the CPU table is doubled.
  • dequantize_nvfp4 (16 weights per call, nl = 4) and dequantize_nvfp4_t4 in kernels/dequantize.h, matching the CPU layout: sub-block s uses qs[8s..8s+7], low nibble j -> element j, high nibble -> element j+8.
  • kernel_mul_mv_nvfp4_f32 (each lane pair covers one 64-weight block, each lane two contiguous sub-blocks) plus its mul_mv_id instantiation.
  • mul_mm, mul_mm_id, get_rows, mul_mv_ext instantiations, and registration in the few-row MMA kernels (mul_mv_mma, ggml_metal_mul_mv_mma_type_supported). Without MMA, 2..8-row batches went to the ext kernel at ~3x the MXFP4 time.
  • supports_op no longer rejects NVFP4 for MUL_MAT, MUL_MAT_ID and GET_ROWS. TQ1_0 is still rejected.

Evidence

M3 Max, base d854d98 (no Metal source differs from the b11887 sync point).

test-backend-ops test -b MTL0 -o MUL_MAT,MUL_MAT_ID,GET_ROWS -p nvfp4
126/126 tests passed   (base: all NVFP4 cases "not supported" on MTL0)

The cases cover mat-vec (n=1), MMA (n=2..16), mat-mat (n=16, 64), mul_mv_id, mul_mm_id and get_rows. Breaking the per-sub-block scale index, the nibble order, or the ext byte group each fails between 15 and 30 cases. The scale decoder was compared against the CPU formula for all 256 byte values (0 mismatches). That check was repeated on the compiled Metal kernel, because random test data never produces subnormal or 0x7F scales.

test-backend-ops perf, MUL_MAT m=4096 k=14336, src1 f32:

n NVFP4 CPU NVFP4 Metal MXFP4 Metal
1 267 us 100 us 92 us
8 1925 us 152 us 159 us
512 118 ms 5.9 ms 5.9 ms

NVFP4 is 4.5 bits/weight against 4.25 for MXFP4, so the n=1 gap is roughly the extra bytes.

End to end

I took Qwen3-0.6B (BF16) and quantized all 196 attention/MLP projections to NVFP4 in ModelOpt layout: per-tensor scale_2 = amax/(6*448), an FP8 E4M3 scale per 16 values, round-to-nearest E2M1. The checkpoint was converted with this tree's convert_hf_to_gguf.py, which wrote all 196 tensors as NVFP4. The "before" row restores the old NVFP4 rejection in supports_op on the same build.

llama-perplexity, wikitext-2 test, -c 512 --chunks 40, M3 Max:

run NVFP4 weights in PPL s/pass
BF16, Metal - 19.4502 0.28
NVFP4, -ngl 99, before (log says "offloaded 29/29 layers") CPU_Mapped 532.79 MiB 22.4646 1.57
NVFP4, -ngl 99, this PR MTL0_Mapped 533.60 MiB 22.4644 0.41
NVFP4, --device none CPU_Mapped 22.4650 2.99

The "before" row has the same signature as the issue: every layer reported offloaded, but the NVFP4 weights stay on the CPU. With this PR, the only CPU_Mapped buffer left is the BF16 token embedding (296.75 MiB). Greedy generation: 187 tok/s on Metal vs 66 tok/s CPU only. The NVFP4-vs-BF16 PPL gap comes from the naive round-to-nearest quantizer used to make the checkpoint; the three NVFP4 runs agree to 4 significant figures.

Not measured on MiniMax-M3.1 itself (259 GB). Its expert ops are the MUL_MAT_ID / GET_ROWS shapes covered by the op tests above.

Notes

The ext kernel (dequantize_nvfp4_t4) is not reached by test-backend-ops once MMA handles 2..8 rows. It still runs when MMA is ineligible (no simdgroup_mm, src1 strides not 16-byte aligned), the same as MXFP4. It passed the full suite before MMA was registered.

Pinning into the nightly needs a pr-set.json entry for this PR's commit.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 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-11-04T11:27:02.756649Z 56ec1e8 PR opened
🔒 Security Review ✅ Completed 2026-11-04T11:28:22.107114Z 56ec1e8 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.

NilayYadav pushed a commit to NilayYadav/llama.cpp that referenced this pull request Nov 9, 2026
NilayYadav pushed a commit to NilayYadav/llama.cpp that referenced this pull request Nov 9, 2026
pr-set : repin for b12122, pin unslothai#348 (mirror of unslothai#330), drop unslothai#336 and ggml-org#31360
@fitchmultz

Copy link
Copy Markdown

Tested on M5 Max with Inkling-2-Mini (NVFP4 experts, 40 tensors / 88 logical projections/tok). A few solid wins on top of #348:

  • Replace the kvalues_mxfp4 threadgroup LUT in dequantize.h / mul_mv.metal with pure ALU bit-manipulation (0 shmem) and hoist * 16384.0f out of the K-loop to the final simdgroup store.
  • Bump N_R0_NVFP4 from 2 -> 4 (nsg=2, nr0=4).
  • Fuse MUL_MAT_ID + REPEAT(scale) + GET_ROWS(ids) + MUL into kernel_mul_mv_id_nvfp4_f32 (folding 16384.0f * w_scale[i02] into the final store, zero RMW) and kernel_mul_mm_id (FC_mul_mm_id_scale).

On M5 Max (-fa 1 -ctk q8_0 -ctv q8_0 -ub 512), the fused expert scale + hoisted scale alone added +2.5 tok/s on tg128 (52.0 -> 54.5) and +120 tok/s on pp8192 (1596 -> 1717). Branch with these changes: https://github.com/fitchmultz/llama.cpp/tree/inkling-opt

mjwsolo added a commit to mjwsolo/localcode that referenced this pull request Nov 21, 2026
… on the GPU

Unsloth's Inkling 2 Mini quants keep the routed experts in NVFP4. The Metal
backend rejected NVFP4 in MUL_MAT, MUL_MAT_ID and GET_ROWS, so the scheduler
left every expert op on the CPU (about 11 GB, 90 graph splits) while the load
log still reported full offload. Prompts were read about 5x slower.

Patch 0014 ports unslothai/llama.cpp#348 (a mirror of #330 by Lyxot; the open
upstream equivalent is ggml-org #20456). After it: 0.5 GB on the CPU, 2 splits,
prompt reading 307-357 tok/s on Inkling UD-Q4_K_XL and UD-Q8_K_XL, correct
output. All 11 patches replay on 6792507 and the binary is self-contained.
The precompiled Metal bundles follow from CI.
@danielhanchen

Copy link
Copy Markdown
Member Author

Dropping this pin from pr-set.json in the next pin-set PR.

  • From b12668 the merge is refused in ggml/src/ggml-metal/kernels/mul_mm.metal: llama, metal : support BF16/F32 src1 for MUL_MAT and MUL_MAT_ID ggml-org/llama.cpp#32677 (BF16/F32 src1 for Metal MUL_MAT and MUL_MAT_ID) rewrote the instantiation lines this PR adds to.
  • Upstream metal : add NVFP4 support ggml-org/llama.cpp#32825 (metal : add NVFP4 support) covers the same kernels and landed in the b12672 tag. b12672 was never published as a release, so the first release that carries it is b12674 (published 08:25Z on 2026-11-24). Once the aged base reaches b12674, Metal NVFP4 comes from the base tag.
  • A release built on b12670 or b12671 would have no Metal NVFP4 kernels, so NVFP4 matmuls fall back to the CPU on Apple GPUs, as they did before this PR. b12674 ages in at 14:25Z (6 h after publishing), so the 20:13Z nightly picks b12674 or newer and keeps Metal NVFP4. Only a run dispatched before 14:25Z would miss it.

This PR stays open for reference. Thanks again to Lyxot for the kernels.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants