Skip to content

[Engine] Qwen4-35B-A3B Apple Silicon Metal optimizations (16 GB partial-VRAM & full-VRAM) #472

Backlog

Description

@NightRain233

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:

  1. PLE 11.52 GB n-gram table on macOS / Metal: per_layer_token_embd.weight (160M x 128 IQ4_NL, 72 B/row, 16 random rows/token) is whole-file mmap'd on Metal. With macOS 16 KiB VM pages, random mmap lookups pull at least 256 KiB/token into the unified buffer cache and thrash 16–24 GB Macs, while start_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: 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 to mmap on Metal).
  2. Metal partial-VRAM host-GPU sync wall (router wait ~17–19 ms/tok): Engine::bell_on() returns false on Metal, disabling whole_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.
  3. 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) and q4_mul_sigmoid_q8_1 are skipped on Metal (#if !defined(STRATA_GPU_METAL)), and routed-expert gate_up still round-trips FP32 g_ before swiglu_rows_quantize_q8_1 (poly_quantize = 4.0% GPU time).
  4. 4K prefill QSA sparse attention hotspot: q4_qsa_attend_batch_q8_8 uses scalar per-element K/V reads and scalar FMAs, taking 515 ms (29.3% of total 4K prefill GPU time) at 1.34 TFLOP/s on M5 Ultra.

Split PRs (4 independent PRs, each against df2c3ff)

  1. [Engine] qwen4: add memory-aware PLE Direct reader (F_NOCACHE) 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: read PLE rows ahead: a decode window's while its guesses are drafted, a prompt's while its first layers run #465).
  2. [Engine] metal: add MTLSharedEvent doorbell with defer-rank and watchdog-safe CB chunking #474 (Draft) — Metal MTLSharedEvent partial-VRAM MoE doorbell + zero-bandwidth id < 0 skip.
  3. [Engine] qwen4/metal: fuse decode kernels, QSA score MMA, and overhaul Gated Residual #475 (Draft) — Metal Qwen4 decode fusions (260-TG q4_gr_down_silu_quant, q4_mul_sigmoid_q8_1, routed-expert SRAM gate_up + SwiGLU + Q8_1).
  4. [Engine] qwen4/metal: rewrite q4_qsa_attend_batch with simdgroup_matrix #476 (Ready) — Metal q4_qsa_attend_batch simdgroup_matrix rewrite (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.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions