mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #2987] [CLOSED] metal: prompt chain without MetalPerformancePrimitives (macOS 15 SDK) #4382
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4382
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/2987
Author: @nullspeech
Created: 10/17/2026
Status: ❌ Closed
Base:
main← Head:msl-legacy-prompt-chain📝 Commits (1)
937dd70metal: let the prompt chain build on an SDK without MetalPerformancePrimitives📊 Changes
11 files changed (+399 additions, -28 deletions)
View changed files
📝
metal/kernels/dense_mm.metal(+70 -2)📝
metal/kernels/mm_bench.metal(+9 -0)📝
metal/kernels/moe_mm.metal(+135 -0)📝
metal/kernels/prompt.metal(+64 -1)📝
metal/kernels/qsa_prompt.metal(+17 -0)📝
metal/src/engine/prompt_chain.inc(+18 -7)📝
metal/src/engine/window.cpp(+27 -9)📝
metal/src/mtl_rt.h(+4 -0)📝
metal/src/mtl_rt.mm(+24 -2)📝
metal/tests/coop_probe.h(+4 -0)📝
metal/tests/prompt_chain.cpp(+27 -7)📄 Description
Why
On macOS 15.8 (Apple M2 Max, 96 GB) the engine cannot start the prompt chain at all: every
matmul2dkernel inmetal/kernels/is reached through an unguarded#include <MetalPerformancePrimitives/MetalPerformancePrimitives.h>, and that header only ships with a Metal 4 SDK.Context::compile()ofdense_mm.metal/moe_mm.metal/prompt.metal/qsa_prompt.metalfails, the failure is fatal at init (exit 3), and the config has to carrySTRATA_METAL_PROMPT=window— which caps prefill at ~58 tok/s on this box.The GPU was never the limit.
simdgroup_matrix<half,8,8>+simdgroup_multiply_accumulateis Apple7+ and has always worked here; the repo's own probe with the guards applied does 7.22 TFLOP/s at 4096³ (rel L2 2.75e-07 against the fp32 reference) and printsdevice Apple M2 Max, metal4 0. So this is a toolchain question, not an instruction-set question — and it is the same root cause as #2860, one level down.What changed
11 files under
metal/, +399/−28, no kernel semantics touched.#if __has_include(<MetalPerformancePrimitives/...>)around the MPP include, theusing namespace mpp::tensor_ops;, and eachmatmul2dkernel body. Four sites get an#elsetwin —gemm_f16(dense_mm),gemm_hc_up(prompt),mm_gu2/mm_down2(moe_mm) — each asimdgroup_matrix<half,8,8>kernel with the same signature and the same[[host_name]], so no host call site changes. The twins reuse the existing threadgroup staging (stage_w<F, TN, TK>) and the existing quant tables.qsa.metal's scalarqsa_score/qsa_attnwhencoop_k4is false, so 17 lines of bare#ifare enough there.mtl::Context::msl_has_mpp()asks the compiler (one 5-line source that#errors when the header is missing) instead ofsupports_metal4(). The GPU family answers "Metal 4" on an M2 running a macOS 26 beta, but the toolchain is the question the kernel's own#ifanswers — keying host and MSL off the same oracle is what keeps variant selection and the compiled library from ever disagreeing.STRATA_METAL_LEGACY=1forces#define STRATA_MSL_MPP 0, so a machine that has both can be measured arm against arm.coop_k4()(the M5 right-input layout probe, which compiles an MPP-only kernel) returns false without the header instead of aborting on a missing function.MTLGPUFamilyMetal4is guarded for toolchains that predate the enum.mm_gu2_*_16_32_32_2for gate/up, TN 32 for the densegemm_f16(see below).Correctness (all under
STRATA_METAL_LEGACY=1, M2 Max / macOS 15.8)dense_mm_parityprompt_paritygdn_paritymoe_mm_parity --v216_16_128_2under legacyNumbers — one binary, one environment variable apart
1023-token prompts, five alternating runs each:
STRATA_METAL_PROMPT=window3.02× on prefill (medians 177.0 vs 58.6), decode unchanged to slightly better. Server-level over HTTP (
/v1/chat/completions), a 4252-token prompt: 55.3 → 159.4 tok/s median. Short prompts (27–34 tokens) are unchanged because they stay under_PROMPT_GEMM_MIN= 64 and never reach the chain. The first prompt after boot is slower on both arms — one pass over the pack is cold storage: 84.3 tok/s cold, 177 once the file cache holds it.Attribution on one 1024-token chunk (
prompt_chain --breakdown), same session: upstream dense tile64_64= 58 tok/s (gdn proj alone 8.73 s) → dense twin at TN 32 = 140 (gdn proj 1.18 s, qsa proj 2.44 → 0.24, hc gemm 0.80 → 0.36) → expert gate/up16_32_32_2= 169 (gu 4.15 → 2.93). So the dense projections carry 2.4× and the expert tile 1.2× of the total 2.9×.What this does not claim
It does not overtake llama.cpp on the same weights. Same machine, same prompt texts, the identical GGUF file,
llama-serverb11408 (-ngl 999,--parallel 1,-c 65536, MTP draft):The branch recovers ~3× of this engine's own 4.9× deficit on the old SDK and still leaves ~1.6× on the table. The deficit is spread rather than concentrated — llama.cpp's whole 1024-token chunk costs ~3.6 s at that rate, while gate/up (2.93 s) plus gdn proj (1.21 s) here are already 4.14 s — so it points at how the chain schedules these kernels, not at raw
simdgroup_matrixthroughput (the dense twin runs at 6.89 of the 7.22 TFLOP/s the probe measures). Decode is a wash: llama.cpp's 1596 completed production requests median at 30.1 tok/s, this at 27–31.Merge state / what I would want reviewed
296b59d2(v0.1.44.1) and will conflict withmain. main is 264 commits ahead, 13 of them undermetal/;prompt_chain.inc:302is the hunk that moved, and themaxBufferLengthwrap (8b3ec44b, which bounds the MoE kernels per layer) touchesmoe_mm.metalwhere the twins live. I can rebase and re-run the gates — flagging it as a draft rather than pretending it merges clean. It is also worth noting that main currently does not build on a Mac at all (#2860, compile at477ac13/ link ata2de744; #2895 looks like the fix), so the rebase would need that too.msl_has_mpp()oracle + the__has_includeguards are the part I think is worth merging even if the tile choices stay a follow-up; happy to split it that way if you prefer two reviews.mm_gu2_*_16_32_32_2also become the MPP default? It is faster for the twins here, and the guard is only about which tile the legacy branch asks for.Environment: macOS 15.8, Xcode Command Line Tools' macOS 15 SDK, Apple M2 Max (96 GB), engine 0.1.44.1-metal.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.