[PR #4807] [CLOSED] [Engine] Mac: opt-in fp16 GEMVs and Q2_0 experts in decode windows (STRATA_METAL_F16=1) #5193

Closed
opened 2026-11-07 16:33:22 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4807
Author: @rasa04
Created: 10/31/2026
Status: ❌ Closed

Base: main ← Head: metal-f16-decode


📝 Commits (2)

  • 46dc3f6 [Engine] Mac: STRATA_METAL_F16=1 - the decode windows' GEMVs and routed experts in fp16 (opt-in)
  • 28c2a7f [Engine] Mac: STRATA_METAL_F16 - only the decode windows of one resident sequence; the tests' fp16 buffers only in fp16 mode

📊 Changes

12 files changed (+611 additions, -27 deletions)

View changed files

📝 docs/MAC.md (+1 -0)
📝 metal/kernels/gdn.metal (+16 -0)
📝 metal/kernels/gemv.metal (+63 -0)
📝 metal/kernels/glue.metal (+1 -0)
📝 metal/kernels/gr.metal (+1 -0)
📝 metal/kernels/moe.metal (+132 -0)
📝 metal/kernels/qsa.metal (+16 -0)
📝 metal/kernels/quant.metal (+15 -0)
📝 metal/kernels/units.metal (+226 -0)
📝 metal/src/engine/window.cpp (+68 -17)
📝 metal/tests/expert_parity.cpp (+23 -6)
📝 metal/tests/gemv_parity.cpp (+49 -4)

📄 Description

Problem and result. On an M1 Max (24-core GPU) the decode window's low-bit GEMVs are not bound by memory: a Q3_K 2560→10240 2-token GEMV reads 11.3 MB in 177 µs (64 GB/s; rt_bench bw reads 327 GB/s), and alu_bench gives fp16 FMA 2.24-2.39 Tops against 1.20-1.43 for fp32. With STRATA_METAL_F16=1 a 2-token window's GPU time drops from 56-60 ms to 40-41 ms. Unset, the output is byte-identical to c62d5e3a.

Related discussion: #4025 (the same M1 Max decode gap; no maintainer reply yet). Overlaps #4019 in window.cpp: its calibration must skip the fp16 dispatches; I will rebase whichever lands second.

Change (opt-in; unset, the fp16 kernels are not compiled and the buffers and dispatches are unchanged):

  • In the decode windows of one resident sequence (not the prompt's windows, not SSD streaming, not batch slots): gemv16 for Q2_0, Q3_K, Q4_K, Q5_K, Q6_K, IQ4_XS, and moe_gu16 / moe_down16 for the Q2_0 routed experts without the Hadamard rotation. Weights decoded into half; each half unit's products (16, or 8 for Q2_0) summed in half; the scales and the sum over units in float.
  • The writers of the window's x (xprep, gr_up, gdn_step, qsa_merge, swiglu_prep) also write three half planes after the float ones (function constant 25).
  • Everything else stays fp32: other formats, other routed experts, the MTP layer, the non-GEMV kernels.

Result (Qwen3.8-Flash-Next Q2_0 resident, M1 Max 24-core GPU, 64 GB, macOS 27, MTP --spec 2, default tiles; 28c2a7ff on c62d5e3a; runs in the order unset, set, set, unset; the first unset run was the engine's first start after the build):

unset STRATA_METAL_F16=1
2-token window, GPU time (STRATA_METAL_KPROF=2) 56.5 / 60.3 ms 40.3 / 40.8 ms
decode, 256 tokens after a 400-token prompt 25.6 / 23.9 tok/s 34.7 / 35.6 tok/s

fp16 changes 2.6% of the top-1 tokens, so the text and the drafts' acceptance differ: the window's GPU time is the like-for-like number.

GEMVs alone, 2 tokens, each side's best tile (gemv_parity --one, GEMV_F16=1; the same kernels, measured on 7dd5cf18): Q3_K 2560→10240 177.0 → 112.7 µs, Q2_0 2560→10240 96.9 → 63.1, Q6_K 6144→2560 99.6 → 67.6, IQ4_XS 2560→10240 90.2 → 68.8, Q5_K 6144→2560 98.6 → 77.4, Q4_K 2560→10240 125.7 → 104.8, the Q5_K LM head 2881 → 2371.

Quality (tools/quant_quality.py, 4995 positions, against c62d5e3a): same top token 97.38%, mean KL 0.0091 (p99 0.126), perplexity 4.439 → 4.415. For scale: changing only the GEMV tiles (the order of a sum, #4019) gives KL 0.003.

Validation (28c2a7ff on c62d5e3a, M1 Max, macOS 27; commands below):

  • Passed, unset: build; gemv_parity ALL PASS; gr_parity ALL PASS; expert_parity 507/507; output byte-identical to c62d5e3a (quant_quality, cmp: 4995/4995 rows); a cold compile of the kernels 9.6-10.4 s against 9.7-10.1 s (on the previous base 5e8a4cca, another GPU job running).
  • Passed, set: GEMV_F16=1 gemv_parity --one 12/12 (6 formats × ntok 1, 2; worst rel L2 against fp32 1.06e-3, limit 4e-3); EXPERT_F16=1 expert_parity 405/405 (worst rel L2 against double: h 1.31e-3, y 3.9e-4; limit 4e-3).
  • Failed: fuse_parity router_route (8 cases, unset), the same with c62d5e3a's kernels (STRATA_METAL_ROUTE on an M1 Max, docs/MAC.md).
  • Not run: other Macs (M1 Pro / Ultra, M2-M5); SSD streaming (fp16 off there); the IQ3 / Unsloth / Gyro packs; decode without MTP; a unit test of the half planes written by gr_up, gdn_step, qsa_merge, swiglu_prep (covered end to end by quant_quality; a throwaway kernel test found them bit-equal to half(x · scale) at ntok 1, 3, 8; not in the PR); CUDA / HIP / SYCL (not touched).

Limits.

  • Measured on an M1 Max only; other Apple GPUs were not measured, so it stays opt-in.
  • The half range is not guarded: a product or a half unit's sum above 65504 is inf - IQ4_XS from |x| ≈ 516, Q6_K ≈ 1040, Q5_K ≈ 2113, Q2_0 ≈ 21835, up to 8× lower when a run has one sign. This model's normed inputs stay below that (|x| ≤ 386 after the HC read, ≤ 677 into the head); the attention output and the experts' h have no such bound. |x| below ~2^-6 loses precision (subnormal half).
  • One-token GEMVs (gemv_parity on 7dd5cf18, same kernels): 5-29% faster, a Q2_0 one 3% slower; one-token decode end to end not measured.
  • With the variable a cold compile took 10.67 / 10.66 s against 9.6-10.4 s unset (on 5e8a4cca, another GPU job running); a later start reuses Metal's cache (0.01 s, measured on 7dd5cf18).
Commands
ninja -C build-metal strata gemv_parity expert_parity gr_parity fuse_parity
./gemv_parity; ./gr_parity; ./fuse_parity
GEMV_F16=1 ./gemv_parity --one {Q2_0,Q3_K,Q4_K,IQ4_XS} 2560 10240 --ntok {1,2}
GEMV_F16=1 ./gemv_parity --one {Q5_K,Q6_K} 6144 2560 --ntok {1,2}
./expert_parity --gu F --dn Q2_0 --gucfg T --dncfg 8,1,4 --ntok {1,2,4}     F: Q2_0 IQ3_XXS IQ2_XXS IQ2_XS IQ2_S IQ3_S IQ4_XS Q4_K Q5_K Q6_K Q8_0; T: 11 gate/up tiles
./expert_parity --gu Q2_0 --dn F --gucfg 32,1,4 --dncfg T --ntok {1,2,4}    F: Q2_0 Q5_1 Q8_0 IQ4_NL; T: 12 down tiles
EXPERT_F16=1 ./expert_parity --gu Q2_0 --dn Q2_0 --gucfg T --dncfg D --ntok {1,2,4}   11 x 12 tiles
EXPERT_F16=1 ./expert_parity --gguf <shard 1> --layer {0,20,47} --ntok {1,2,4}
python tools/quant_quality.py run --config CFG --out RUN.tsv    CFG: the config with exe = the build, env STRATA_METAL_STREAM=0 [STRATA_METAL_F16=1]
python tools/quant_quality.py compare MAIN.tsv UNSET.tsv SET.tsv; cmp MAIN.tsv UNSET.tsv
STRATA_METAL_KPROF=2 strata --serve <the config args> --max-context 4096 --prompt-cache 0; GEN 256 over the first 400 ids of data/quant_quality/v1
KPROF
# unset, run 4: the 8 largest classes
window T2, 1142 dispatches, GPU 60.313 ms; classes alone (serialized) sum 61.543 ms:
   8.209 ms  13.3%  moe_gu_42_32_1_4 x48
   5.594 ms   9.1%  gemv_13_16_1_4_1 output.weight x1
   5.133 ms   8.3%  moe_down_142_8_1_4 x48
   3.823 ms   6.2%  gdn_step x72
   3.689 ms   6.0%  gemv_11_16_1_4_1 attn_qkv.weight x18
   3.166 ms   5.1%  gemv_11_16_1_4_1 attn_gate.weight x25
   2.343 ms   3.8%  gemv_23_32_1_8_2 ssm_out.weight x17
   2.082 ms   3.4%  gr_down_q8_8_8 hc_attn_down.weight x48
# STRATA_METAL_F16=1, run 2: the 8 largest classes
window T2, 1142 dispatches, GPU 40.271 ms; classes alone (serialized) sum 43.546 ms:
   4.960 ms  11.4%  moe_gu16_42_32_1_4 x48
   3.548 ms   8.1%  moe_down16_142_8_1_4 x48
   3.098 ms   7.1%  gdn_step x72
   2.431 ms   5.6%  gemv16_13_16_1_4_1 output.weight x1
   2.397 ms   5.5%  gemv16_11_16_1_4_1 attn_qkv.weight x18
   2.193 ms   5.0%  gr_down_q8_8_8 hc_attn_down.weight x48
   2.152 ms   4.9%  gr_down_q8_8_8 hc_ffn_down.weight x48
   2.098 ms   4.8%  gemv16_11_16_1_4_1 attn_gate.weight x25

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/4807 **Author:** [@rasa04](https://github.com/rasa04) **Created:** 10/31/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `metal-f16-decode` --- ### 📝 Commits (2) - [`46dc3f6`](https://github.com/Niko1221/Strata/commit/46dc3f67dc094acf264db918a6afb26e4bd9b775) [Engine] Mac: STRATA_METAL_F16=1 - the decode windows' GEMVs and routed experts in fp16 (opt-in) - [`28c2a7f`](https://github.com/Niko1221/Strata/commit/28c2a7ffc8704faccc181cfa48a55e786221d846) [Engine] Mac: STRATA_METAL_F16 - only the decode windows of one resident sequence; the tests' fp16 buffers only in fp16 mode ### 📊 Changes **12 files changed** (+611 additions, -27 deletions) <details> <summary>View changed files</summary> 📝 `docs/MAC.md` (+1 -0) 📝 `metal/kernels/gdn.metal` (+16 -0) 📝 `metal/kernels/gemv.metal` (+63 -0) 📝 `metal/kernels/glue.metal` (+1 -0) 📝 `metal/kernels/gr.metal` (+1 -0) 📝 `metal/kernels/moe.metal` (+132 -0) 📝 `metal/kernels/qsa.metal` (+16 -0) 📝 `metal/kernels/quant.metal` (+15 -0) 📝 `metal/kernels/units.metal` (+226 -0) 📝 `metal/src/engine/window.cpp` (+68 -17) 📝 `metal/tests/expert_parity.cpp` (+23 -6) 📝 `metal/tests/gemv_parity.cpp` (+49 -4) </details> ### 📄 Description **Problem and result.** On an M1 Max (24-core GPU) the decode window's low-bit GEMVs are not bound by memory: a Q3_K 2560→10240 2-token GEMV reads 11.3 MB in 177 µs (64 GB/s; `rt_bench bw` reads 327 GB/s), and `alu_bench` gives fp16 FMA 2.24-2.39 Tops against 1.20-1.43 for fp32. With `STRATA_METAL_F16=1` a 2-token window's GPU time drops from 56-60 ms to 40-41 ms. Unset, the output is byte-identical to `c62d5e3a`. **Related discussion:** #4025 (the same M1 Max decode gap; no maintainer reply yet). Overlaps #4019 in `window.cpp`: its calibration must skip the fp16 dispatches; I will rebase whichever lands second. **Change** (opt-in; unset, the fp16 kernels are not compiled and the buffers and dispatches are unchanged): - In the decode windows of one resident sequence (not the prompt's windows, not SSD streaming, not batch slots): `gemv16` for Q2_0, Q3_K, Q4_K, Q5_K, Q6_K, IQ4_XS, and `moe_gu16` / `moe_down16` for the Q2_0 routed experts without the Hadamard rotation. Weights decoded into half; each half unit's products (16, or 8 for Q2_0) summed in half; the scales and the sum over units in float. - The writers of the window's x (`xprep`, `gr_up`, `gdn_step`, `qsa_merge`, `swiglu_prep`) also write three half planes after the float ones (function constant 25). - Everything else stays fp32: other formats, other routed experts, the MTP layer, the non-GEMV kernels. **Result** (Qwen3.8-Flash-Next Q2_0 resident, M1 Max 24-core GPU, 64 GB, macOS 27, MTP `--spec 2`, default tiles; `28c2a7ff` on `c62d5e3a`; runs in the order unset, set, set, unset; the first unset run was the engine's first start after the build): | | unset | `STRATA_METAL_F16=1` | |---|---:|---:| | 2-token window, GPU time (`STRATA_METAL_KPROF=2`) | 56.5 / 60.3 ms | 40.3 / 40.8 ms | | decode, 256 tokens after a 400-token prompt | 25.6 / 23.9 tok/s | 34.7 / 35.6 tok/s | fp16 changes 2.6% of the top-1 tokens, so the text and the drafts' acceptance differ: the window's GPU time is the like-for-like number. GEMVs alone, 2 tokens, each side's best tile (`gemv_parity --one`, `GEMV_F16=1`; the same kernels, measured on `7dd5cf18`): Q3_K 2560→10240 177.0 → 112.7 µs, Q2_0 2560→10240 96.9 → 63.1, Q6_K 6144→2560 99.6 → 67.6, IQ4_XS 2560→10240 90.2 → 68.8, Q5_K 6144→2560 98.6 → 77.4, Q4_K 2560→10240 125.7 → 104.8, the Q5_K LM head 2881 → 2371. Quality (`tools/quant_quality.py`, 4995 positions, against `c62d5e3a`): same top token 97.38%, mean KL 0.0091 (p99 0.126), perplexity 4.439 → 4.415. For scale: changing only the GEMV tiles (the order of a sum, #4019) gives KL 0.003. **Validation** (`28c2a7ff` on `c62d5e3a`, M1 Max, macOS 27; commands below): - Passed, unset: build; `gemv_parity` ALL PASS; `gr_parity` ALL PASS; `expert_parity` 507/507; output byte-identical to `c62d5e3a` (`quant_quality`, `cmp`: 4995/4995 rows); a cold compile of the kernels 9.6-10.4 s against 9.7-10.1 s (on the previous base `5e8a4cca`, another GPU job running). - Passed, set: `GEMV_F16=1 gemv_parity --one` 12/12 (6 formats × ntok 1, 2; worst rel L2 against fp32 1.06e-3, limit 4e-3); `EXPERT_F16=1 expert_parity` 405/405 (worst rel L2 against double: h 1.31e-3, y 3.9e-4; limit 4e-3). - Failed: `fuse_parity` `router_route` (8 cases, unset), the same with `c62d5e3a`'s kernels (`STRATA_METAL_ROUTE` on an M1 Max, docs/MAC.md). - Not run: other Macs (M1 Pro / Ultra, M2-M5); SSD streaming (fp16 off there); the IQ3 / Unsloth / Gyro packs; decode without MTP; a unit test of the half planes written by `gr_up`, `gdn_step`, `qsa_merge`, `swiglu_prep` (covered end to end by `quant_quality`; a throwaway kernel test found them bit-equal to half(x · scale) at ntok 1, 3, 8; not in the PR); CUDA / HIP / SYCL (not touched). **Limits.** - Measured on an M1 Max only; other Apple GPUs were not measured, so it stays opt-in. - The half range is not guarded: a product or a half unit's sum above 65504 is inf - IQ4_XS from |x| ≈ 516, Q6_K ≈ 1040, Q5_K ≈ 2113, Q2_0 ≈ 21835, up to 8× lower when a run has one sign. This model's normed inputs stay below that (|x| ≤ 386 after the HC read, ≤ 677 into the head); the attention output and the experts' h have no such bound. |x| below ~2^-6 loses precision (subnormal half). - One-token GEMVs (`gemv_parity` on `7dd5cf18`, same kernels): 5-29% faster, a Q2_0 one 3% slower; one-token decode end to end not measured. - With the variable a cold compile took 10.67 / 10.66 s against 9.6-10.4 s unset (on `5e8a4cca`, another GPU job running); a later start reuses Metal's cache (0.01 s, measured on `7dd5cf18`). <details><summary>Commands</summary> ```text ninja -C build-metal strata gemv_parity expert_parity gr_parity fuse_parity ./gemv_parity; ./gr_parity; ./fuse_parity GEMV_F16=1 ./gemv_parity --one {Q2_0,Q3_K,Q4_K,IQ4_XS} 2560 10240 --ntok {1,2} GEMV_F16=1 ./gemv_parity --one {Q5_K,Q6_K} 6144 2560 --ntok {1,2} ./expert_parity --gu F --dn Q2_0 --gucfg T --dncfg 8,1,4 --ntok {1,2,4} F: Q2_0 IQ3_XXS IQ2_XXS IQ2_XS IQ2_S IQ3_S IQ4_XS Q4_K Q5_K Q6_K Q8_0; T: 11 gate/up tiles ./expert_parity --gu Q2_0 --dn F --gucfg 32,1,4 --dncfg T --ntok {1,2,4} F: Q2_0 Q5_1 Q8_0 IQ4_NL; T: 12 down tiles EXPERT_F16=1 ./expert_parity --gu Q2_0 --dn Q2_0 --gucfg T --dncfg D --ntok {1,2,4} 11 x 12 tiles EXPERT_F16=1 ./expert_parity --gguf <shard 1> --layer {0,20,47} --ntok {1,2,4} python tools/quant_quality.py run --config CFG --out RUN.tsv CFG: the config with exe = the build, env STRATA_METAL_STREAM=0 [STRATA_METAL_F16=1] python tools/quant_quality.py compare MAIN.tsv UNSET.tsv SET.tsv; cmp MAIN.tsv UNSET.tsv STRATA_METAL_KPROF=2 strata --serve <the config args> --max-context 4096 --prompt-cache 0; GEN 256 over the first 400 ids of data/quant_quality/v1 ``` </details> <details><summary>KPROF</summary> ```text # unset, run 4: the 8 largest classes window T2, 1142 dispatches, GPU 60.313 ms; classes alone (serialized) sum 61.543 ms: 8.209 ms 13.3% moe_gu_42_32_1_4 x48 5.594 ms 9.1% gemv_13_16_1_4_1 output.weight x1 5.133 ms 8.3% moe_down_142_8_1_4 x48 3.823 ms 6.2% gdn_step x72 3.689 ms 6.0% gemv_11_16_1_4_1 attn_qkv.weight x18 3.166 ms 5.1% gemv_11_16_1_4_1 attn_gate.weight x25 2.343 ms 3.8% gemv_23_32_1_8_2 ssm_out.weight x17 2.082 ms 3.4% gr_down_q8_8_8 hc_attn_down.weight x48 # STRATA_METAL_F16=1, run 2: the 8 largest classes window T2, 1142 dispatches, GPU 40.271 ms; classes alone (serialized) sum 43.546 ms: 4.960 ms 11.4% moe_gu16_42_32_1_4 x48 3.548 ms 8.1% moe_down16_142_8_1_4 x48 3.098 ms 7.1% gdn_step x72 2.431 ms 5.6% gemv16_13_16_1_4_1 output.weight x1 2.397 ms 5.5% gemv16_11_16_1_4_1 attn_qkv.weight x18 2.193 ms 5.0% gr_down_q8_8_8 hc_attn_down.weight x48 2.152 ms 4.9% gr_down_q8_8_8 hc_ffn_down.weight x48 2.098 ms 4.8% gemv16_11_16_1_4_1 attn_gate.weight x25 ``` </details> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 16:33:22 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#5193
No description provided.