Repository navigation
Backlog
Description
Problem
Profiling Qwen4-35B-A3B-UD-IQ2_XS on Apple Silicon Metal (v0.2.20-poly.10 / df2c3ff, Apple M5 Ultra 256 GB, both full VRAM and simulated 16 GB partial-VRAM budgets) surfaced four independent bottlenecks:
- PLE 11.52 GB n-gram table on macOS / Metal:
per_layer_token_embd.weight(160M x 128IQ4_NL,72 B/row, 16 random rows/token) is whole-filemmap'd on Metal. With macOS 16 KiB VM pages, randommmaplookups pull at least 256 KiB/token into the unified buffer cache and thrash 16–24 GB Macs, whilestart_ple_warmer()has no macOS branch for 64–256 GB Macs. Related: [Engine] qwen4: read the PLE n-gram table row by row from disk through the Direct reader #440 and [Engine] qwen4: with --ple-direct, read PLE rows ahead: a decode window's while its guesses are drafted, a prompt's while its first layers run #465 (note [Engine] qwen4: read the PLE n-gram table row by row from disk through the Direct reader #440 currently falls back tommapon Metal). - Metal partial-VRAM host-GPU sync wall (
router wait~17–19 ms/tok):Engine::bell_on()returnsfalseon Metal, disablingwhole_rec_/gr_deferred_write_as soon as any expert is outside VRAM (!all_cached()) and forcing ~40 per-layer command buffer flushes + host waits per token. - Missing Metal Qwen4 decode fusions:
q4_gr_down_silu_quant(poly_mul_q8_0_k4+q4_gr_silu_1_1= 11.0% of full-VRAM decode GPU time) andq4_mul_sigmoid_q8_1are skipped on Metal (#if !defined(STRATA_GPU_METAL)), and routed-expertgate_upstill round-trips FP32g_beforeswiglu_rows_quantize_q8_1(poly_quantize= 4.0% GPU time). - 4K prefill QSA sparse attention hotspot:
q4_qsa_attend_batch_q8_8uses scalar per-element K/V reads and scalar FMAs, taking 515 ms (29.3% of total 4K prefill GPU time) at1.34 TFLOP/son M5 Ultra.
Split PRs (4 independent PRs, each against df2c3ff)
- [Engine] qwen4: extend --ple-direct to Metal (F_NOCACHE), async overlap, and macOS warmer #473 (Draft) — PLE Direct reader (
F_NOCACHE) + async prefetch + macOS warmer (coordinates with [Engine] qwen4: read the PLE n-gram table row by row from disk through the Direct reader #440 / [Engine] qwen4: with --ple-direct, read PLE rows ahead: a decode window's while its guesses are drafted, a prompt's while its first layers run #465). - [Engine] metal: add MTLSharedEvent doorbell with defer-rank and watchdog-safe CB chunking #474 (Draft) — Metal
MTLSharedEventpartial-VRAM MoE doorbell + zero-bandwidthid < 0skip. - [Engine] qwen4/metal: fuse decode kernels, QSA score MMA, and overhaul Gated Residual #475 (Draft) — Metal Qwen4 decode fusions (
260-TGq4_gr_down_silu_quant,q4_mul_sigmoid_q8_1, routed-expert SRAMgate_up + SwiGLU + Q8_1). - [Engine] qwen4/metal: rewrite q4_qsa_attend_batch with simdgroup_matrix #476 (Ready) — Metal
q4_qsa_attend_batchsimdgroup_matrixrewrite (Metal-only 3 files; counterpart to CUDA [Engine] qwen4: QSA prompt attention for eight query heads a KV head from registers, the same bits #470).
Combined same-day A/B results (Apple M5 Ultra 256 GB, Qwen4-35B-A3B-UD-IQ2_XS, --kv q8_0 --lookup 0, df2c3ff vs combined 4-in-1)
| Scenario | Baseline (df2c3ff) |
Combined 4-in-1 | Gain |
|---|---|---|---|
Full VRAM --no-draft (192 tok) |
92.27 tok/s | 111.96 tok/s | +21.3% |
| Full VRAM Embedded MTP (192 tok) | 153.22 tok/s | 154.52 tok/s | +0.8% |
Full VRAM 4K prefill (4096 tok) |
2,488.2 tok/s | 3,425.1 tok/s | +37.7% |
Sim 16 GB dynamic LRU --no-draft (5.3 GiB VRAM, 6 threads, --ple-direct) |
23.08 tok/s | 32.67 tok/s | +41.6% |
Sim 16 GB dynamic LRU MTP (5.2 GiB VRAM, 6 threads, --ple-direct) |
25.39 tok/s | 30.79 tok/s | +21.3% |
Sim 16 GB frozen 65% + --ram-experts-mib 0 --no-draft (6 threads, --ple-direct) |
29.70 tok/s | 46.97 tok/s | +58.1% |
Sim 16 GB frozen 62.4% + --ram-experts-mib 0 MTP (6 threads, --ple-direct) |
40.63 tok/s | 57.60 tok/s | +41.8% |
Sim 16 GB frozen 65% + --ram-experts-mib 0 4K prefill (4096 tok, --ple-direct) |
2,039.5 tok/s | 2,570.3 tok/s | +26.0% |
Limits / unverified
- Hardware measurements above are on Apple M5 Ultra 256 GB (macOS, Release build); CUDA, HIP, SYCL, Windows, and Linux builds are not yet run. Physical 16 GB Apple M4 measurements will be added here once run.
Metadata
Metadata
Assignees
Labels
No labels
Activity
NightRain233 commentedon Dec 6, 2026
Status Update — Rounds 1–4 Complete, Rebased onto
origin/main(0794bc5), Physical 16 GB M4 & Factorial Quality VerifiedSince opening this issue, #476 (QSA
simdgroup_matrixbatch attention, integrated inde0fe5b9f), #498 (high-BWshort4/TG-LUT decode GEMV), and #504 (CI fixes) have landed onmain, and upstream #464 added opt-in--ple-directfor CUDA/HIP.All 9 remaining Qwen4 / Metal PRs have been cleanly rebased onto
origin/main(0794bc5, including #498 and #464), with zero merge conflicts (MERGEABLE), verified end-to-end on physical 16 GB Apple M4 (Mac16,12) and 256 GB Apple M5 Ultra, and audited with a 10-config factorial quality suite separating lossless defaults from opt-in approximate knobs.1. PR Status & Recommended Review / Merge Order
0794bc5)mainQ4_K/Q5_K/Q6_K/Q8_0),_fast_r2..r8,moe_sum_v4b6ec657MERGEABLE, pureengine/metal/)<2048-outk4guarded51/51 ctest+ standalone M5 Ultra & M4 A/B vs0794bc5kGateSoftmaxLogitAdd = 4)d38abe8MERGEABLE)--cache-bias > 0); default unchanged51/51 ctest+ factorial quality (CUDA/HIPkernels.cuawaitsrun-backendsCI)<= 24 GiBbalanced Metal budget (XNU reclaimable VM +wired > 55%backoff)cc073b0MERGEABLE)> 24 GiBMacs & non-Metal unchanged51/51 ctest,docs6/6--ple-directto Metal (F_NOCACHE, auto tight-RAM, layer-0 overlap, macOS warmer)40478feMERGEABLE)0default51/51 ctest,settings_register,docs6/635a0e6aMERGEABLE)1.5e-5parity)51/51 ctest,docs6/6MTLSharedEventdoorbell, watchdog/AGX leak guard, CB chunking & opt-in--defer-rank5a3b357MERGEABLE)--defer-rank -1strictly lossless51/51 ctest,settings_register,docs6/6--basinkv, Quest/SphereIndex, WHT-INT4)24ff792MERGEABLE)--basinkv)52/52 ctest(poly_basinkv_parity0–10),test_setup_basinkv6/6,docs6/6--dense-requant <q4_k|...>[+head]with never-upsize guard3cc75cdMERGEABLE)--dense-requant)51/51 ctest,quantize_q4_K_refmemcmp == 0, factorial qualityQwen4::draft_step+ BasinKVblk.40truncationf5a04a5MERGEABLE)5a3b35751/51 ctest(incl.DraftBellEngine5/5)2. Verified Hardware Results (
Qwen4-35B-A3B-UD-IQ2_XS)A. Standalone #511 (
b6ec657) Same-Day Interleaved A/B vsorigin/main(0794bc5)--no-draft --window 1:106.82 / 107.22→108.37 / 108.65 tok/s(+1.4%)--window 2(MTP):140.62 / 140.53→147.26 / 146.67 tok/s(+4.5%)--window 4(MTP):134.68 / 135.69→139.30 / 139.70 tok/s(+3.2%)--window 2:10.28 / 9.85→10.72 / 10.25 tok/s(+4.2%)--window 4:10.35 / 11.72→11.67 / 12.34 tok/s(+8.8%)B. Integrated Stack on Physical 16 GB Apple M4 (
Mac16,12, fanless 13" MBA, 8-core Apple9, 120 GB/s)--metal-memory balanced(ZERO manual--vram-reserve-mibflags):~6–7 tok/s, swap thrashing onmmapPLE).3.8 GiB,~93%hit), peak memory7.56 GiB,gpu_inuse = 0.00–0.47 GiBon exit (zero AGX leak after 22 consecutive runs):--no-draft:19.12 tok/s(R3 balanced); with opt-in--dense-requant q4_k:21.48 tok/s(+429extra VRAM experts, +12.3%).--window 2) + opt-in--defer-rank 0:30.16 tok/sbalanced (30.70 tok/sfrozen 4k).--cache-bias 0.5:32.69 tok/sbalanced (97.4%hit) /33.52 tok/sfrozen 4k (98.0%hit; fixes CPU--cache-bias 0.5regression of26.46 tok/s).--metal-memory balanced --ple-direct --max-context 131072 --basinkv --basinkv-index sphere --basinkv-kv-quant wht_int4):131,072tokens): GPU BasinKV68.3 MiB(hot 44.0 + idx 23.4 + meta 1.0) + pinned RAM726.0 MiB, while keeping3,393routed experts in VRAM.119.1 tok/sprefill, peak8.39 GiB, 100% exact recall (AMBER-4921).8.82 GiB,612 MiBPLE SSD reads (58.1%hit), 100% exact recall (ORCHID-8814).65,536tokens) BasinKV NIAH (needle at ~50% depth):60.1 tok/sprefill,7.37 tok/sdecode (9/9MTP drafts accepted), peak memory footprint9.56 GiB(8.47 GBmax RSS,0 swaps), PLE Direct93.0%hit (246.30 MiBSSD reads,699.8 mscollect wait), AGXgpu_inuseon exit0.00 GiB, 100% exact recall (COBALT-7319).122,880tokens) BasinKV NIAH (needle at ~50% depth, token ~61,420):28.6 tok/sprefill,7.26 tok/sdecode (7/8MTP drafts accepted), peak memory footprint9.57 GiB(8.51 GBmax RSS,0 swaps; RAM pressure guard smoothly released host buffers 3× without OOM), PLE Direct95.1%hit (338.86 MiBSSD reads,1,061.0 mscollect wait), AGXgpu_inuseon exit0.00 GiB, 100% exact recall (QUARTZ-6042).3. Honest 10-Config Factorial Quality Evaluation (Apple M5 Ultra,
Qwen4-35B-A3B-UD-IQ2_XS)Evaluated under a strict cold/low-hit partial-VRAM stress test (
4,000fixed experts from an M4 profile,36–76%natural VRAM hit across tasks; 128-step teacher-forced Top-1/Top-5/GoldP vs full-VRAM lossless baseline + 6 end-to-end tasks with exact--eos-ids 248044,248046):LRUCacheedit_distance--defer-rank -1)--dense-requant q4_k(full VRAM)--dense-requant q4_k+head(full VRAM)--cache-bias 0.5(--defer-rank -1)--cache-bias 0.3(--defer-rank -1)--cache-bias 1.0(--defer-rank -1)--defer-rank 4(partial VRAM)--defer-rank 0(partial VRAM)--cache-bias 0.5+--defer-rank 0Key takeaway: Default Metal Doorbell (
--defer-rank -1) is strictly lossless (99.22%Top-1,100%Top-5,6/6PASS). All approximate speed knobs (--defer-rank >= 0,--cache-bias,--dense-requant) are kept strictly opt-in with honest trade-offs documented in each PR; in particular,--defer-rank 0should not be treated as lossless when VRAM hit rate is low (~51%), and stacking--cache-biaswith--defer-rank 0compounds math/logic drift.4. Next Steps Needed from Maintainers
Per
CONTRIBUTING.md, PRs modifying shared cross-backendengine/src/poly/files (#499, #473, #475, #474, #500, #501, #512) were built and tested on Apple Silicon macOS only (CUDA, HIP, SYCL, and MSVC builds were not run locally) and are therefore kept as Draft until maintainers confirm scope and indicate which PRs to flip Ready for Review forrun-backendsCI. Pure-Metal #511 is already Ready for Review (and #513 is Ready with a clear non-Metalkernels.cuCI caveat).(Companion
Inkling-2-Mini16 GB Apple Silicon optimization series is tracked in #456.)