Skip to content
qoder3Public
forked from Niko1221/Strata

About

Apple Silicon Metal 4 fork of Strata for Looped v4 (T=3, 72L) Qwen4-35B-A3B UD-IQ4_XS: 325 tok/s prefill (+82%), 21-25 tok/s decode (+26%), 256K K8V4 & 37x subagent SSD cache on 24 GB Mac

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

Strata — Apple Silicon Metal 4 Fork (Qwen4-35B-A3B 4-bit + Looped v4 T=3)

72-logical-layer 4-bit Qwen4-35B-A3B (UD-IQ4_XS + Open-Channel Looped v4 T=3) with native 256K (262 144) K8V4 context, single-read NVMe SSD expert streaming, and persistent subagent prefix caching — running comfortably on a 24 GB Apple Silicon Mac.

This is a performance-focused Metal 4 fork of Niko1221/Strata (v0.2.28, original README preserved in README.upstream.md). Every optimization in this fork is 100% lossless (verified bit-exact on decode and numerically lossless on prefill) and tailored for everyday 24 GB+ Apple Silicon Macs (tested on MacBook Pro Apple M4 Pro, 16-core GPU, 24 GB Unified Memory, macOS 26+/27).


Before vs. After (Apple M4 Pro 16-core, 24 GB RAM)

All measurements on Qwen4-35B-A3B UD-IQ4_XS + Looped v4 T=3 (72 logical layers, core=[12,28)), native 262 144 K8V4 context, --spec 2 suffix drafting, inside the 18.8 GB macOS working-set cap (15.6 GB footprint, zero swap):

Metric Baseline (6.5 GB LRU) Wave 1 (STRX + Core-LFU) Wave 2 (Split-MoE + F16) Wave 3 (Current) Total Gain
Long Prefill (9 790 tok, 72L) 178.9 tok/s (54.7 s) 213.5 tok/s (45.9 s) 254.0 tok/s (38.5 s) 325.3 tok/s (30.1 s) +81.8% (-24.6 s)
Equivalent 40-layer prefill 322 tok/s 384 tok/s 457 tok/s ~586 tok/s (peak 635) +82%
Short Prefill (247 tok) 68.7 tok/s 79.6 tok/s 70.7 tok/s 90.1 tok/s +31.1%
Decode: RU Prose 19.2 tok/s 21.0 tok/s 23.6 tok/s 23.1 tok/s +20.3%
Decode: Python Code 15.9 tok/s 18.3 tok/s 21.0 tok/s 20.8 tok/s +30.8%
Decode: Math (CoT) 17.6 tok/s 19.8 tok/s 22.2 tok/s 22.4 tok/s +27.3%
Decode: Logic / Rust 19.2 tok/s 21.8 tok/s 24.6 tok/s 24.7 tok/s +28.6%
Decode @ 10K ctx (cold) 17.7 tok/s 20.3 tok/s 22.8 tok/s 22.8 tok/s +28.8%
Decode @ 10K ctx (cached turn) 18.7 tok/s (1.70 s TTFT) 20.8 tok/s (1.69 s) 21.8 tok/s (1.50 s) 22.7 tok/s (1.59 s) +21.4%
Mean Decode (6 benchmarks) 18.05 tok/s 20.33 tok/s 22.67 tok/s 22.75 tok/s +26.0%
Subagent Prefix Reuse (9.8K sys) 44.5 s TTFT 1.18 s RAM / 1.40 s SSD 1.18 s / 1.40 s 1.18 s RAM / 1.40 s SSD 31–37× faster
Decode SSD Traffic / Step ~114.8 MB/win 57.0 MB/win 48.3 MB/win 48.7 MB/win (95.5% hit) -58% SSD I/O
Total SSD Moved (7-prompt suite) — 328.5 GB 191.8 GB 149.9 GB -54.4% vs W1

What Was Built in This Fork (Diff vs. Upstream Strata v0.2.28)

1. Open-Channel Looped v4 (T=3, 72 Logical Layers) on Metal + 256K K8V4 on 24 GB RAM

  • Files: engine/metal/src/engine/loop.inc, engine/metal/src/engine/window.cpp, engine/metal/src/gpu/runtime.cpp
  • Full v4 open-channel recurrence (core=[12,28), 3 passes = 72 logical layers) in both decode windows and streaming prompt_chunk prefill, plus T=3 K8V4 KV cache, core-wrap prefetch, and early purge of unused host-side Metal allocations (8.59 GB freed before window init) so 256K context fits cleanly on 24 GB unified memory.

2. Bit-Exact Split-MoE SSD Overlap + Metal 4 Dual-MTLSharedEvent Race Fix + F16 Streaming Decode

  • Files: engine/metal/src/engine/window.cpp, engine/metal/src/mtl_rt.mm
  • Split-MoE overlap (STRATA_METAL_SPLIT_MOE=1): runs the entire shared expert across [Cut 1, Wait 1) while begin_layer() submits SSD misses, runs all resident routed experts across [Wait 1, Wait 2) while finish_layer() completes SSD reads, and dispatches only the newly arrived experts after Wait 2 (hardware no-op when 0 misses). Verified BIT-EXACT PARITY: True against non-split decode.
  • Dual unidirectional MTLSharedEvents + multi-CB chunking (kMaxCutsPerCb = 48): eliminates an Apple Silicon Metal 4 bidirectional event race on fast 0-miss cuts (kIOGPUCommandBufferCallbackErrorTimeout on 144-cut T=3 graphs).
  • F16 dense GEMV + Q6_K LM head (STRATA_METAL_F16=1) unlocked for streaming decode + STRATA_METAL_SEG_FILL=cut to keep GPU clocks up across 72 cuts.

3. M4 Pro Prefill Turbo (178.9 → 325.3 tok/s on 72L = ~586 tok/s 40L Equivalent)

  • Files: engine/metal/kernels/moe_mm.metal, engine/metal/kernels/dense_mm.metal, engine/metal/kernels/qsa_prompt.metal, engine/metal/src/engine/loop.inc, engine/metal/src/engine/window.cpp
  • FP32 matmul2d Looped v4 Mixers (gemm_f32_64_64_4, gemm_f32_ws_64_64_4): kernel profiling (PG-KPROF) revealed that scalar decode-style mixer kernels burned 616 ms/chunk (~3.1 s on 9.8K prefill) despite representing only 1.2% of FLOPs. Moving them to FP32 matmul2d for n_tok >= 32 cut them to ~18 ms (34× faster) with full FP32 accuracy.
  • M4-Native Ragged Multi-Subtile MoE GEMMs (v2-r4): mm_gu2_r (IQ4_XS 64×32×4, parallel 64+64 gate/up decode) and mm_down2_r (Q4_K 64×64×2) reuse mm_scan6 (MS = 4) so each expert weight is dequantized 1× per 64 tokens instead of 4×, with a fused 64×64 fast path when nsub == 4 and zero coop_k4 (M5-only) dependency — cutting Q4_K down GEMM from 1 028 ms → 525 ms (1.96× faster, 4.7 TFLOPS incl. dequant).
  • TG-Staged qsa_flash + 32-Lane SIMD Parallel Online Softmax: replaces scalar per-element !coop_k4 K/V loads with coalesced char4 K (2 cells/lane) + uchar4 int4 V staged into idle sp[sg] (0 extra TG memory) and parallelizes online softmax across all 4 simdgroups (-1.0 s/chunk at full QSA budget, -140 MB scratch RAM).
  • Fused gdn_scanf_8 (fixes "Apple M4 Pro" chip match + fuses GDN scan & post), prompt at_cut() overlap with MoE planning + shared expert, and full-chunk wrap layer 0 prefetch (missed rate 1.47% → 0.32%).

4. STRX Auto-Shape, Core-Weighted LFU, Core-Pinned Prompt Eviction & macOS Memory Hygiene

  • Files: engine/metal/tools/stream_experts.py, engine/metal/src/engine/stream.h, engine/metal/src/engine/stream.cpp
  • Auto-shape STRX (40×256): packs each expert's (gate, up, down) into a single contiguous F_NOCACHE NVMe read (4.38 GB/s peak on M4 Pro).
  • Core-Weighted LFU + STRP Profile Collector (STRATA_METAL_DUMP_PROFILE): protects the [12,28) core experts executed 3× per token; 7.2 GB wired (mlock) arena holds 4 225 slots (> all 4 096 core experts).
  • Core-Pinned Prompt Eviction: keeps core experts at LRU head during prefill (44.5% → 64.6% prompt arena hit rate, -22% total SSD wear).
  • Standalone Prefill Scratch + Darwin MADV_FREE_REUSABLE / MADV_FREE_REUSE + 64 MiB Stripe mlock Retry: steals 0 arena slots during 2048-token prefill (2.12 GB), immediately reclaims scratch from macOS phys_footprint after prefill, and reliably wires all 7.2 GB even right after restart.

5. Direct-to-SSD Persistent Conversation & Subagent Prefix Cache

  • Files: engine/src/program/generate.cpp, engine/src/program/conversation_state.cpp, engine/src/program/conversation_spill.cpp
  • ConversationSpill::put_streamed + spill_direct() writes large checkpoints straight to SSD across server restarts (6 144 MiB persistent tier).
  • Fixed Qwen4 root_at boundary detection past the 15-token Reasoning effort header and preserved the root system-prompt checkpoint alongside the deepest turn — giving subagents 37.6× faster TTFT in RAM (1.18 s) and 31.5× faster TTFT from SSD even after a full restart (1.40 s).

Quick Start (One Command on Apple Silicon, macOS 26+)

git clone https://github.com/qoder3/Strata.git
cd Strata

# 1. Build Metal engine (q4_lite), download UD-IQ4_XS + PLE-Q4 + Looped v4 T=3,
#    build trunk40.gguf + pack + STRX + RAM-tuned config.json
./presets/qwen4-looped-metal/setup.sh

# 2. Start OpenAI-compatible server on http://127.0.0.1:10101/v1
./presets/qwen4-looped-metal/run.sh start

# 3. Check status & memory pressure
./presets/qwen4-looped-metal/run.sh status

Already have the GGUF / PLE / loop adapter on disk? Symlink them instead of re-downloading, or inspect every planned command with --dry-run:

./presets/qwen4-looped-metal/setup.sh --dry-run

For full requirements, custom STRP routing profiles, environment knobs, and Z-Code / OpenAI client setup, see presets/qwen4-looped-metal/README.md.


Engineering Notes: What We Tested and Intentionally Left Off

  • LoopSpec (T=1/T=2 → T=3 self-speculative drafting): despite 97.4% (T=1→3) / 100.0% (T=2→3) teacher-forced agreement, drafting is 0.67×–0.78× slower on SSD-streamed MoE because each draft step still pays SSD misses while multi-token verification scales linearly with misses. Free suffix drafting (--spec 2) stays ON.
  • Adaptive Tail-Skip (STRATA_METAL_TAIL_SKIP): implemented in Split-MoE with exact renormalization, left 0.0 (lossless) by default because Qwen4's top-8 router weights are flat (~0.08–0.18), so safe thresholds (0.03–0.05) skip < 1.6% of misses with no latency gain.
  • Chunked WY/UT GDN (gdn_cprep2 + gdn_cstate2): wired (STRATA_METAL_GDN_CHUNK=1) and benchmarked at 461.6 ms vs 212.5 ms for gdn_scanr_4_8 / gdn_scanf_8 on 16-core M4 Pro (2.17× slower), so fused gdn_scanf_8 is default.
  • PROMPT_CHUNK=4096 on 24 GB Mac: 4.0 GB standalone scratch pushes total footprint over the 18.8 GB cap into macOS swap; 2048 (2.12 GB) is the swap-free sweet spot on 24 GB (render_config.py automatically enables 4096 on 48+ GB Macs).

Upstream Credits & License

Built on top of Niko1221/Strata (MIT License — see LICENSE and README.upstream.md). Model weights by Unsloth, Strata Engine, and sliderforthewin.

About

Apple Silicon Metal 4 fork of Strata for Looped v4 (T=3, 72L) Qwen4-35B-A3B UD-IQ4_XS: 325 tok/s prefill (+82%), 21-25 tok/s decode (+26%), 256K K8V4 & 37x subagent SSD cache on 24 GB Mac

Topics

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages