Metal SSD-streaming benchmark m4 max 128GB results and improvement proposals
Hi antirez, Thanks for implementing ssd-streaming! I've been having a lot of fun playing around with it.
Overview
I benchmarked DSV4-Flash SSD streaming on an M4 Max / 128 GiB machine, and in addition to the existing baseline path, prototyped and compared two alternative strategies (residency-lru / nommap). This issue is a short report plus improvement proposals. Full numbers, plots, and implementation notes are in the fork's README (linked at the end).
- Environment: Apple M4 Max 128 GiB / model: deepseek-v4-flash
- Quantization: q2 (actual weights 80.76 GiB, fits in RAM) / q4 (153.33 GiB, does not fit)
- Budgets: 8 / 16 / 32 / 64 / 80 GB (--ssd-streaming-cache-experts)
- Strategies compared (only the routed-expert supply path differs).
baseline / full are upstream as-is; residency-lru / nommap are added in this work:
- Upstream (unchanged):
- baseline (
cafc134) … the default SSD-streaming path (mmap + pread copy + evolved LRU) - full … non-streaming (whole model resident). Measured for q2 only.
- baseline (
- Added (prototypes in this work):
- residency-lru (
11f20b1) … gen uses a zero-copy mmap view of selected experts + residency LRU; prefill uses an F_NOCACHE pread double-buffer + MADV_DONTNEED trailing - nommap (
cae5e95) … no mmap; preads weights from an F_NOCACHE fd into owned buffers (no page cache)
- residency-lru (
- Upstream (unchanged):
TL;DR
Framed as characteristics:
-
The fastest case is "memory is sufficient and the GPU runs at 100%" (the GPU never stalls waiting on weights). Since q2's full weights of 80.76 GiB fit in 128 GiB, non-streaming
fullis closest to this state and is fastest. The cost: a ~28s residency build at startup, and the whole model is wired, so it competes with other processes for RAM. -
SSD-streaming trades speed to reduce OOM risk for long context / concurrent execution. By not making the whole model resident it saves memory, but supplying weights on demand costs speed. For models that don't fit in RAM (e.g. q4 at 153.33 GiB),
fullis infeasible, so streaming is required. -
There are two memory-management families:
- mmap-based (baseline / residency-lru) … rides on the OS page cache. Fast when RAM is sufficient, but behavior depends on RAM size (won't reproduce on smaller machines).
- nommap … manages memory entirely by itself. Uses no page cache, so it's RAM-size independent and the most robust against OOM (details below in "nommap characteristics").
Numbers for representative configs
Because per-expert size is q2 = 6.75 MiB / q4 = 13.50 MiB (2×), the optimal budget is asymmetric for mmap-based strategies.
- q2 hits enough experts at b32 (in part because the whole model can sit in the page cache). Raising budget further makes the cache slab eat into the page cache and degrades prefill (→ for mmap-based strategies, b32 is the practical sweet spot).
- q4's experts are 2× larger, so at the same budget only half as many experts stay resident as q2, and since the model exceeds RAM, low budget isn't enough. Raising budget (more resident experts) is faster (→ b80 is best).
Note that nommap uses no page cache, so this asymmetry does not apply: for both q2 and q4, gen scales with budget (= number of resident experts), and higher budget is faster as far as you can afford it (see "nommap characteristics" below).
Memory columns: wired = mandatory pinned (cannot be released) / used = total in use (partly releasable) / file-backed = OS cache portion (mmap-derived, releasable).
q2 / budget 32GB (fits in RAM. full is the ceiling, b32 the practical best)
| config | prefill | gen | wired | used | file-backed |
|---|---|---|---|---|---|
| full (non-stream) | 261.0 | 22.80 | — | 103 | — |
| baseline b32 | 249.3 | 19.29 | 48 | 124 | 83 |
| residency-lru b32 | 243.9 | 16.58 | 51 | 103 | 82 |
| nommap b32 | 214.6 | 12.16 | 45 | 61 | 2 |
q4 / budget 80GB (exceeds RAM, streaming required. full N/A)
| config | prefill | gen | wired | used | file-backed |
|---|---|---|---|---|---|
| baseline b80 | 113.2 | 11.00 | 88 | 127 | 116 |
| residency-lru b80 | 129.2 | 11.81 | 101 | 127 | 113 |
| nommap b80 | 126.7 | 7.59 | 88 | 106 | 2 |
(t/s = prefill: cumulative avg at the 100% point of the largest prefill / gen: avg of the longest gen sequence. Values in GiB.)
nommap characteristics (fixed memory footprint, real-machine-representative perf)
Because nommap uses no page cache, its memory footprint is predictable and the numbers reproduce independent of RAM size.
| budget(GB) | prefill t/s | gen t/s | wired | used | file-backed |
|---|---|---|---|---|---|
| 8 | 206.5 | 4.52 | 21 | 36 | 2.8 |
| 16 | 210.1 | 6.54 | 31 | 44 | 2.3 |
| 32 | 214.6 | 12.16 | 45 | 61 | 2.1 |
| 64 | 215.9 | 19.57 | 77 | 92 | 1.5 |
| 80 | 215.2 | 19.92 | 85 | 100 | 1.9 |
(q2. wired/used/file-backed in GiB. compressor = 0 / swapout = 0 across all budgets.)
- file-backed stays flat at ≈2 GiB regardless of budget … no mmap, F_NOCACHE pread, so the OS page cache never grows. This is the substance of the "fixed memory footprint".
- used ≈ fixed base (≈11 GiB) + budget … the budget you set becomes the actual wired RAM. → Independent of RAM size, so these numbers reproduce on other machines (given the same budget) = real-machine-representative performance.
- gen scales with budget (= number of resident experts), reaching near full (22.80 t/s) at b64–b80. prefill is roughly flat at ≈206–215.
- Slow gen at low budget is not a nommap-specific weakness — it directly reflects "few resident experts." mmap-based strategies look fast at low budget because they use the remaining RAM of a 128 GiB box as free page cache covering all experts; that speed depends on the 128 GiB premise. On a machine with less RAM, mmap can't hold the model in page cache either and thrashes via re-pagein (concretely: for q4 where model > RAM, baseline's DRAM BW drops to 56.8 GB/s and GPU active to 59%). → nommap's per-budget numbers are "the honest performance for that amount of RAM," and being RAM-size independent gives it the best portability.
Detailed report
Full per-budget tables (prefill/gen/memory), vm_stat / mactop analysis, and per-strategy implementation notes are here: https://github.com/tkhr-sait/ds4/blob/ssd-streaming-brushup/ssd-streaming-memo/README.md
Improvement proposals
baseline is fast enough with no tuning (the default path). There are essentially 2 areas to improve, and I prototyped one approach for each. Shared purely as reference (mechanism details in "prefill speedup notes" and "implementation notes" below).
-
Addressing prefill degradation at high budget (→ residency-lru — a rough, not-yet-successful prototype) At b64/b80, baseline's cache slab and the page cache fight over RAM → re-pagein → prefill drops. residency-lru avoids page-cache pollution on the prefill side, giving prefill +15 to +31 vs baseline at high budget. That said, this is the least polished of the three, and I wouldn't call it a clean success:
- The prefill gain comes entirely from the prefill-side handling (F_NOCACHE pread double-buffer + MADV_DONTNEED trailing), not from its namesake gen residency LRU.
- The gen residency LRU itself actually drags q2 gen below baseline (16.58 vs 19.29 at b32).
- It's the most complex to implement, and it saves memory mainly for q2 — q4 stays pinned at used ≈ 127 GiB. So the reusable takeaway here is the prefill-side technique; the gen residency LRU is not working well yet and needs rework.
-
Memory management for models larger than RAM / portability to smaller machines (→ nommap) By dropping mmap and preading via F_NOCACHE so the page cache never grows: file-backed ≈2 GiB, RAM-size independent, most robust against OOM. Cost: gen at low budget is the "honest performance" scaled to the number of resident experts (see "nommap characteristics" above).
prefill speedup notes
- The reason residency-lru's prefill beats baseline is the prefill-side processing, not the gen residency LRU. An F_NOCACHE pread double-buffer + MADV_DONTNEED trailing (2 layers behind the encode front) avoids polluting the page cache. baseline preads experts on the regular fd, so the prefill sweep pulls the model's pread-source pages into the page cache; at high budget this competes with the cache slab and thrashes.
- Place MADV_DONTNEED 2 layers behind the encode front — this is the key (too close re-faults, too far grows the footprint). It keeps the prefill sweep's file-backed footprint to ~2–3 layers.
- Async-prefetch cold ranges with F_RDADVISE (on LRU miss). The prefill cache slab is returned to the OS on gen resume.
- nommap prefill overlap: background-prefetch the entire gate/up/down expert tensors of layer il+1 (per-parity double-buffer), overlapping the F_NOCACHE SSD read with layer il's GEMM.
Implementation notes (non-obvious OS / Metal behavior)
These are macOS / Metal behaviors that are hard to discover without actually trying or digging into the docs.
- F_NOCACHE requires page-aligned I/O. Interleaving short unaligned reads makes the kernel implicitly re-enable caching and disables F_NOCACHE. Widen the request window to page boundaries and read in a single pread (page-aligned bounce buffer, 32 MiB ceiling).
- F_NOCACHE alone does not reduce file-backed. The kernel's automatic read-ahead ignores F_NOCACHE and puts prefetched pages into file-backed, so you must turn off F_RDAHEAD on the model fd (this cuts gen-time file-backed growth to about 1/3).
- stdio-based reads (KV / staged reads) also land in file-backed, so they need to be bypassed via a separate path. Whatever the prefill cold-load burst brings in remains as reclaimable cache, since macOS provides no way to drop an fd's cache (diminishing returns).
- A Metal noCopy MTLBuffer is counted as wired for its full length (a gotcha when wrapping mmap pages directly).
Shared as reference for the direction of SSD streaming (take speed by depending on the page cache, or take portability / low memory by dropping the page cache).
Small related note: while validating a Metal SSD-streaming fix I hit a separate --tool-call-quality failure in the exact path:
Metal model range ... is not covered by mapped model views
It reproduces on clean upstream/main too, so I opened a dedicated issue instead of mixing it into this benchmark thread: #455
Might be relevant to the SSD-streaming mapping/cache discussion here, but keeping details over there so this issue stays focused.
SSD streaming / prefill layer-major benchmark summary
Measurement results for reordering prefill processing and raising wire_limit + specifying experts to bypass the cap check in ds4's Metal SSD streaming.
Implementation branches:
- no-mmap: https://github.com/tkhr-sait/ds4/tree/ssd-streaming-metal-no-mmap
- mmap layer-major: https://github.com/tkhr-sait/ds4/tree/ssd-streaming-metal-layer-major
Setup (common)
- Model: Q4 (opencode task)
- Benchmark: generate minesweeper.html via opencode (THINKING → TOOLS prefill 10,658 tok → gen)
- The wire_limit increase is applied to ALL conditions (including baseline):
sudo sysctl iogpu.wired_limit_mb=122880 # Metal alloc ~120GiB
sudo sysctl vm.global_user_wire_limit=130996502528 # 122 GiB
sudo sysctl vm.user_wire_limit=130996502528
Conditions
| Condition | env / setting | prefill order | cache experts |
|---|---|---|---|
| baseline | (none) | chunk-major (default) | 7964 |
| mmap-layerd | DS4_METAL_ENABLE_MMAP_LAYER_MAJOR=1 |
layer-major (mmap) | 7000 |
| nommap-wired | DS4_METAL_ENABLE_STREAMING_NO_MMAP=1 |
layer-major (no-mmap) | 7964 |
Launch command examples
Common variable (Q4 model):
MODEL=gguf/DeepSeek-V4-Flash-Q4KExperts-F16HC-F16Compressor-F16Indexer-Q8Attn-Q8Shared-Q8Out-chat-v2-imatrix.gguf
--ssd-streaming-cache-experts is given in the count form (number of experts) to
bypass ds4's 0.7x working-set cap (q4 ≈ 13.50 MiB/expert, 105 GiB ≈ 7964). The
wire-limit increase from "Setup" is assumed to be done for all conditions.
baseline (chunk-major / mmap, e7964)
ds4-server --model $MODEL --ssd-streaming --ssd-streaming-cache-experts 7964 --ctx 100000
mmap-layerd (layer-major / mmap, e7000)
DS4_METAL_ENABLE_MMAP_LAYER_MAJOR=1 \
ds4-server --model $MODEL --ssd-streaming --ssd-streaming-cache-experts 7000 --ctx 100000
- Extra buffers shrink the expert budget, so the count is 7000 (7964 swaps).
nommap-wired (layer-major / no-mmap, e7964)
DS4_METAL_ENABLE_STREAMING_NO_MMAP=1 \
ds4-server --model $MODEL --ssd-streaming --ssd-streaming-cache-experts 7964 --ctx 100000
- no-mmap uses owned buffers, so without the wire_limit increase from Setup the cache is capped to the budget and stays below baseline.
Results
Measured TOOLS prefill (10,658 tok) and gen (final avg):
| Condition | prefill avg (t/s) | gen final avg (t/s) | wired max | compressor max | swapout |
|---|---|---|---|---|---|
| baseline | 117.88 | 14.75 | 118G | 5451M | 0 |
| mmap-layerd e7000 | 205.85 | 13.72 | 106G | 4922M | 0 |
| nommap-wired e7964 | 239.19 | 15.58 | 118G | 315M | 0 |
- The output (minesweeper.html) was generated correctly in all 3 conditions (7.1–7.6 KB).
- No actual swap occurred during any of the runs.
Analysis
1. Reordering prefill from chunk-major to layer-major speeds it up
Reorganizing the prefill loop from chunk-major to layer-major lets each layer's experts be read once instead of being re-read per chunk. This reduces SSD reads, lets the page cache work effectively, and speeds up prefill.
- baseline (chunk-major) 117.88 t/s → layer-major gives +75% to +103%
- mmap-layerd: 205.85 t/s
- nommap-wired: 239.19 t/s
no-mmap (nommap-wired) is the fastest. The mmap version (mmap-layerd) also improves greatly but does not reach no-mmap.
Hiding prefetch latency with double buffering
Since layer-major processes layers in order, prefetching the next layer's experts into a separate buffer (double buffering) lets the current layer's compute overlap with the next layer's load (SSD/prefetch), hiding prefetch latency for further speedup.
However, releasing memory is hard with mmap:
- mmap depends on the OS page-cache management, so it is hard to explicitly release the pages of a finished layer, and physical memory keeps being pressured.
- no-mmap manages owned buffers itself, so releasing/reusing the double buffer is straightforward. For this reason too, no-mmap is better suited to layer-major + prefetch hiding.
2. gen speed with raised wire_limit + cap-bypass expert specification
When measuring with the wire limit raised and experts specified to bypass the cap check, no-mmap (nommap-wired) sustains gen speed at or above baseline (15.58 vs 14.75 t/s). Memory pressure is also the lowest, with compressor at 315M — the healthiest.
The wire_limit dependency differs by architecture
- baseline (mmap): experts ride in the OS file-backed page cache, which is automatically allocated up to the physical memory limit. → gen speed holds even without raising wire_limit (independent of wire_limit).
- no-mmap: experts ride in wired owned buffers, so without raising wire_limit the cache is limited to "just the specified memory (budget)", and the number of cacheable experts hits a ceiling. → Only after raising wire_limit is the budget constraint lifted, allowing it to exceed baseline.
In other words, no-mmap's advantage (gen 15.58 > baseline 14.75) is predicated on raising wire_limit. Without it, the budget limit keeps it below baseline.
3. mmap-layerd implementation is not yet complete
mmap-layerd requires extra buffers, so fewer experts fit in the same memory budget (7964 → 7000).
- At e7000 it keeps swap=0, but increasing experts beyond this causes swap and degradation, so it tops out at 7000.
- As a result gen is 13.72 t/s, below baseline / nommap-wired, and compressor is higher at 4922M (= higher memory pressure).
- Improving this requires revisiting mmap-layerd's buffer allocation logic.
Conclusion
- Layer-major prefill is effective (up to +103% for no-mmap, +75% even for mmap). There is further room via double-buffered prefetch hiding.
- Under a raised wire_limit, no-mmap gen is at or above baseline (15.58 vs
14.75 t/s).
- baseline (mmap) is wire_limit-independent since the page cache fills to the physical limit.
- no-mmap is predicated on raising wire_limit (without it, the budget limit keeps it below baseline).