Skip to content

[Engine] Optimize Metal strata-poly on 16 GB Apple Silicon (Apple9 M4): decode fusion, memory guard, SME2 prefill, and opt-in pruning #456

Backlog

Description

@NightRain233

Area

[Engine] (speed, kernels, models, the server and API)

What do you want to do?

Improve strata-poly Metal performance, memory safety, and out-of-the-box ergonomics for 16 GB Apple Silicon Macs (Apple9 / M4) running MoE models (specifically Inkling-2-Mini), while keeping Apple10 (M5 Ultra) and non-Metal backends regression-free.

Following CONTRIBUTING.md, I want to align scope with maintainers before splitting this work into 4 focused PRs (rebased onto main / v0.2.20-poly.10, df2c3ff; opened as drafts where shared cross-backend engine files need CUDA/HIP/SYCL CI):

  1. PR 1 ([Engine] optimize Metal Inkling decode fusion and Apple9 prefill GEMM #458) — Lossless Metal decode fusion & Apple9 prefill GEMM tuning ([Engine])

    • On-chip threadgroup SRAM fused MoE gate_up + SiTU + Q8_1 (poly_mul_k1_gu_situ_q8), low-VGPR Apple9 decode kernels (poly_mul_k0/k1/k9), Q2_0 LUT / custom quantized dots, smaller Apple9 poly_mm tiles + kTile={16,32} split, vectorized ink_control3_gate_v4 / poly_gemv_v4, and UMA zero-blit output_rows_q8 / d_picks_ readback.
    • 100% mathematically lossless; benefits both Apple9 (M4) and Apple10 (M5 Ultra).
  2. PR 2 ([Engine] guard 16 GB Apple Silicon Metal memory headroom and tune Inkling setup defaults #457) — 16 GB Apple Silicon Metal memory headroom guard & Inkling setup presets ([Engine])

    • Guard Metal metal_cover / balanced VRAM reservation with an OS headroom cap on 16 GB Macs to prevent memory-pressure thrashing and OOMs.
    • Tune 16 GB Apple Silicon Inkling setup presets (--no-draft --prompt-batch 512 --attn-requant q5_k --kv q8_0) in engine/metal/setup_mac.py and terminal/tools/metal_poly_setup.py, with unit tests in terminal/tools/test_metal_poly_setup.py and engine/metal/poly_tests/memory_growth.cpp.
  3. PR 3 ([Engine] add opt-in Apple Silicon CPU SME2 (Accelerate BNNS) concurrent MoE prefill #459) — Opt-in Apple Silicon CPU SME2 (Accelerate BNNS) concurrent MoE prefill ([Engine])

    • Zero-copy unified-memory heterogeneous MoE prefill: concurrently execute ~25% of prefill expert rows (prioritizing uncached cold experts) on Apple Silicon CPU SME2 via Accelerate BNNS (FP16 x FP16 -> FP32 to avoid NVFP4 raw-accumulator overflow, plus FP16 activation clamp and 25-type NEON FP16 dequantizers), while the Metal GPU processes the remaining experts in parallel.
    • Kept strictly opt-in by default (STRATA_POLY_SME_PREFILL=1 / STRATA_POLY_SME_SHARE=0.25) so the default path stays byte-identical unless maintainers prefer auto-enabling it on Apple9 FEAT_SME=1 Macs (8–10 GPU cores).
  4. PR 4 ([Engine] add opt-in Metal GPU-native dynamic expert pruning (--gpu-skip-weight, --gpu-route-top, --tail-renorm) #460) — Opt-in Metal GPU-native dynamic expert pruning ([Engine])

    • Zero-CPU-sync pruning inside Metal poly_route (--gpu-skip-weight, --gpu-route-top, --gpu-skip-min, --tail-renorm) with id < 0 early exits and NaN/OOB guards across downstream kernels, plus host moe()/moe_defer() parity (including Gemma 4 pre-escale ordering) and fail-fast error on non-Metal builds.
    • Strictly opt-in (off by default, byte-identical default path).

Prototype measurements before rebasing (built from 4106d1a -> local 03340da)

  • Local 16 GB M4 (Mac16,12, fanless 13" MacBook Air, 8-core Apple9 GPU, 16 GiB UMA, FEAT_SME=1, FEAT_SME2=1, macOS 26.6.2):
    • Inkling-2-Mini-UD-Q2_K_XL.gguf (9.1 GB, 3072/3072 experts in VRAM):
      • Baseline before changes: 16.8 tok/s decode (default draft) / 18.6 tok/s (no-draft); 4K prefill ~81 tok/s (~51s); 8K prefill ~78 tok/s (~105s).
      • PR 1 + PR 2 (100% lossless, GPU-only): short decode 22.1–22.5 tok/s (+20% lossless), 4K prefill 160.1 tok/s (25.1s).
        • PR 3 (SME2 share=0.25, 100% lossless): 4K prefill 195.5 tok/s (21.0s), 7,680-token (~8K) prefill 192.7 tok/s (39.9s), post-8K decode 18.88 tok/s.
        • PR 4 (opt-in pruning + --tail-renorm): 23.52 tok/s (--gpu-skip-weight 0.08), 23.90 tok/s (--gpu-route-top 6, 99.2% top-1 agreement on 128 teacher-forced steps), 24.45 tok/s (--gpu-route-top 5), 26.10 tok/s (--gpu-skip-weight 0.10). At --gpu-route-top 4 (27.47 tok/s), multi-step logic degrades; omitting --tail-renorm at 0.08 causes a logic reasoning failure while keeping --tail-renorm passes all math/logic/coding checks.
    • Inkling-2-Mini-GSQ-RCO-IQ3_S.gguf (11.4 GB, ~2,563/3,072 experts in VRAM):
      • Warm 4K prefill: 64.9 tok/s (61.8s, SME off) -> 88.6 tok/s (45.3s, +36.5%, SME share=0.25 with cold-expert priority); post-4K decode 5.8 -> 9.5 tok/s (steady-state 13.58 tok/s).
      • Cold-start short decode with PR 4 (--gpu-skip-weight 0.08 --tail-renorm): 9.66 -> 12.03 tok/s (+24.5%, VRAM swaps 254 -> 104).
  • Apple M5 Ultra (Apple10 GPU + Metal 4, regression check):
    • ctest -L poly: 50 / 50 passed, q36_metal_parity: 0 mismatches.
    • Inkling-2-Mini-UD-Q2_K_XL no-draft decode improved from 83.3–85.9 to 87.7–90.5 tok/s, 4K/8K prefill steady at 2,176–2,215 tok/s (SME2 stays off on Apple10).

(Unrelated Linux CI test_setup_qwen4 case-sensitivity fix submitted separately in #461.)

Scope questions for maintainers

  1. Does splitting this work into the 4 PRs above match what you prefer to review?
  2. For PR 2, are the 16 GB Apple Silicon Inkling setup defaults (--no-draft, --prompt-batch 512, --attn-requant q5_k, --kv q8_0) and the OS headroom cap acceptable as defaults on <= 16 GB Macs?
  3. For PR 3, should CPU SME2 concurrent prefill land strictly opt-in first (proposed), or auto-enable on Apple9 FEAT_SME=1 Macs with <= 10 GPU cores?

What do you do today instead?

Today on a 16 GB M4 Mac running Inkling-2-Mini-UD-Q2_K_XL or IQ3_S, users hit:

  • Slow long-context prefill (~81 tok/s at 4K, ~105s wait at 8K) because large Apple10 poly_mm tiles underperform on 8-core Apple9 GPUs and M4's CPU SME2 unit sits idle.
  • Sub-optimal decode (16.8 tok/s with draft enabled, ~35% bandwidth lost to unfused intermediate memory round-trips).
  • Manual trial-and-error to avoid 16 GB macOS unified-memory thrashing (tuning --vram-reserve-mib, --no-draft, --attn-requant q5_k, --kv q8_0, --prompt-batch 512).
  • No GPU-native way on Metal to trade tail-expert compute for higher decode throughput without CPU-GPU sync stalls.

Activity

  1. added
    enhancement
    on Dec 5, 2026
  2. NightRain233 commented on Dec 6, 2026

    @NightRain233
    ContributorAuthor

    Status Update — Rebased onto origin/main (0794bc5) & Full Metal Validation Complete

    All 4 split PRs for Inkling-2-Mini on 16 GB Apple Silicon (Apple9 M4) have been cleanly rebased onto origin/main (0794bc5, including #498 and #464), with zero merge conflicts (MERGEABLE) and full Metal Release build + ctest -L poly verification on Apple M5 Ultra (Apple10) on every exact commit.

    PR Status & Recommended Review Order

    Order PR Head Status Default Path Verified Checks (exact commit on 0794bc5)
    1 #457 — 16 GB Metal headroom guard & Inkling setup defaults 4de93c2 Draft (MERGEABLE) > 20 GiB Macs & non-Metal unchanged M5 Ultra 51/51 ctest, test_metal_poly_setup 22/22, docs 6/6
    2 #458 — Lossless Metal decode fusion, low-VGPR kernels & Apple9 GEMM 9953d89 Draft (MERGEABLE) 100% lossless; preserves #498 _fast/_fast4/_k1/_k1g M5 Ultra 51/51 ctest (incl. mv_parity, inkling_parity, qwen4_parity), settings_register, docs 6/6
    3 #459 — Opt-in Apple Silicon CPU SME2 (Accelerate BNNS) concurrent prefill 1298d88 Draft (MERGEABLE) Strictly opt-in (STRATA_POLY_SME_PREFILL=1); default byte-identical M5 Ultra 52/52 ctest (incl. poly_sme_prefill_parity 25/25 quant types + NVFP4 >65k + FP16 clamp), docs 6/6
    4 #460 — Opt-in Metal GPU-native dynamic expert pruning 262f6a6 Draft (MERGEABLE) Strictly opt-in (--gpu-skip-weight / --gpu-route-top); default byte-identical M5 Ultra 51/51 ctest (incl. basic_parity, quantized_parity, inkling_parity), settings_register, docs 6/6

    (Note: both #458 and #460 add a virtual hook to Engine and bump kPolyEngineAbiRev 2 -> 3; whichever merges second only needs a trivial rebase on engine.hpp.)

    Summary of Verified Hardware Results

    Physical 16 GB Apple M4 (Mac16,12, fanless 13" MacBook Air, 8-core Apple9 GPU, 16 GiB UMA, macOS 26.6.2) & Apple M5 Ultra (64-core Apple10, 256 GiB UMA):

    Model & Workload Baseline (before changes) After (#457–#460) Gain / Notes
    Inkling-2-Mini-UD-Q2_K_XL decode (100% lossless, M4) 16.8 (draft) / 18.6 tok/s (--no-draft) 22.13–22.5 tok/s +20% vs --no-draft (+32–34% vs default)
    UD-Q2_K_XL decode (opt-in --gpu-route-top 6 --tail-renorm, M4) 16.8 tok/s 23.90 tok/s +42% (99.2% 128-step Top-1; 4/4 math/logic/coding PASS)
    UD-Q2_K_XL decode (opt-in --gpu-skip-weight 0.10 --tail-renorm, M4) 16.8 tok/s 26.10 tok/s +55% (96.9% Top-1; 4/4 PASS; --tail-renorm required)
    UD-Q2_K_XL 4K prefill (100% lossless, GPU + opt-in SME2 0.25, M4) ~81 tok/s (~51s) 195.5 tok/s (21.0s) 2.41× (160.1 tok/s GPU-only; +10.1% single-var SME2 A/B)
    UD-Q2_K_XL ~8K prefill (7,680 tok, lossless, GPU + SME2, M4) ~78 tok/s (~105s) 192.7 tok/s (39.9s) 2.47× (18.88 tok/s post-8K decode)
    Inkling-2-Mini-IQ3_S 4K prefill (warm A/B, SME2 off vs 0.25, M4) 64.9 tok/s (61.8s) 88.6 tok/s (45.3s) +36.5% (cold-expert priority; post-4K decode 5.8 -> 9.5 tok/s)
    IQ3_S cold decode (opt-in --gpu-skip-weight 0.08 --tail-renorm, M4) 9.66 tok/s (254 swaps) 12.03 tok/s (104 swaps) +24.5% (steady-state 13.58 tok/s)
    UD-Q2_K_XL regression check (Apple M5 Ultra, --no-draft) 83.3–85.9 tok/s 87.7–90.5 tok/s Zero regression (4K/8K prefill steady 2,176–2,215 tok/s; SME2 off on Apple10)

    Why Drafts & Next Steps Needed from Maintainers

    Per CONTRIBUTING.md, all 4 PRs modify shared cross-backend engine files (engine/src/poly/) and were built/tested on Apple Silicon macOS only (CUDA, HIP, SYCL, and MSVC builds were not run locally). They are therefore kept as Draft until maintainers confirm scope and let me know which PRs to flip Ready for Review for run-backends CI.

    (Companion Qwen4-35B-A3B Metal optimization series is tracked in #472, with pure-Metal #511 already Ready for Review.)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions