Metadata-Version: 2.5
Name: nvspool
Version: 0.1.0
Summary: NVMe-spooled MoE inference for Apple Silicon — run 26B–56B language models in 6–10 GB RAM.
Project-URL: Homepage, https://github.com/jhdcho/nvspool
Project-URL: Repository, https://github.com/jhdcho/nvspool
Project-URL: Issues, https://github.com/jhdcho/nvspool/issues
Author: Jason Cho
License-Expression: Apache-2.0
License-File: LICENSE
License-File: NOTICE
Keywords: apple-silicon,inference,mlx,moe,nvme
Classifier: Operating System :: MacOS
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: <3.15,>=3.12
Requires-Dist: mlx-lm==0.31.3
Requires-Dist: mlx==0.32.2
Requires-Dist: numpy>=2.0
Provides-Extra: serve
Requires-Dist: fastapi>=0.141; extra == 'serve'
Requires-Dist: uvicorn>=0.52; extra == 'serve'
Description-Content-Type: text/markdown

# nvspool

NVSpool is an NVMe-spooled mixture-of-experts (MoE) runtime for Apple silicon Macs — run 26B–56B
language models in 6–10 GB RAM without holding the whole model in memory. The weights every token
uses stay in RAM; routed experts (and Qwen4's n-gram lookup tables) stay on the SSD behind a
fixed-size per-layer pool. Qwen4-35B-A3B (32.2 GB / 56B params) peaks at 7.7 GB on short prompts
and 9.2 GB at 34.5k context; Qwen3.6-35B-A3B (20.4 GB) peaks at 7.6–8.0 GB and 8.3 GB at 20k.

You can use it three ways: as an OpenAI-compatible server on `127.0.0.1:11500` (for OpenCode, Cline,
Continue, Open WebUI or any OpenAI SDK), as an offline browser chat app served from the same port,
or as a terminal chat.

Status: 0.1, in development. Apple silicon only. Built on MLX 0.32.2 and mlx-lm 0.31.3 (pinned to
match the benchmark runs). Tested on an M4 Max with 36 GB; 16 GB is the intended minimum and hasn't
been tested yet.

## Background

A 32 GB or 36 GB Mac has enough unified memory to load a 20–35B model by itself, but not enough to
keep it resident alongside containers, an IDE, browsers and build tools. On a 16 GB or 24 GB Mac,
models in the 20–40B range either won't fit at all or push the machine into OOM / aggressive
paging.

Sparse MoE models only use a small fraction of their parameters on any given token (for example,
Qwen3.6-35B-A3B activates 8 of 256 experts per layer). Keeping the always-used weights plus a
capped pool of hot experts in RAM — and streaming the rest from SSD with the OS page cache bypassed
— lets a 26B–35B model run in 6–10 GB while leaving the rest of the Mac usable.

## Benchmarks

All numbers measured on an M4 Max (36 GB), 4-bit MLX checkpoints, thinking off, greedy decoding
(Qwen4 on external USB-C NVMe SSD; Qwen3.6, Gemma 4 and GLM-4.7-Flash on internal SSD).

### Qwen4-35B-A3B (preview, 10 GB tier — cap 71)

A 56B-parameter (32.2 GB on disk) checkpoint with 11.5 GB of n-gram lookup tables (PLE) plus
18.1 GB of routed experts — too large to leave usable headroom in 36 GB RAM resident, and impossible
on 16–24 GB Macs. NVSpool streams both experts and PLE (bit-exact) from SSD, keeping only 1.51 GiB
of core weights always in RAM:

| Context / strategy (`cap = 71`) | Prefill | Decode | Peak OS RAM |
|---|---:|---:|---:|
| Short prompt — `exact` (zero skipped picks) | — | 16.4 tok/s | **7.7 GB** |
| Short prompt — `top6` | — | 19.5 tok/s | **7.7 GB** |
| Short prompt — `top4` *(default)* | — | **23.5 tok/s** | **7.7 GB** |
| Short prompt — `top2` | — | **29.4 tok/s** | **7.7 GB** |
| 4.3k tokens (`top4`) | 506 tok/s (8.4 s) | 21.8 tok/s | **8.5 GB** |
| 17.2k tokens (`top4`) | 701 tok/s (24.6 s) | 19.5 tok/s | **8.5 GB** |
| 34.5k tokens (`top4`) | **765 tok/s (45.2 s)** | **19.2 tok/s** | **9.2 GB** |

*Full 50-item quality benchmark scores for Qwen4-35B-A3B (HumanEval+, IFEval, MATH-500, MMLU-Pro)
and 65k/131k scaling are coming soon.*

### Qwen3.6-35B-A3B: short-context accuracy & speed (10 GB tier)

50-item subsets (`top4` with decode prefetch `k=4`):

| | Whole model in RAM (mlx-lm) | NVSpool, 10 GB tier (`top4`) |
|---|---:|---:|
| Peak memory | 20.3–20.8 GB | 7.6–8.0 GB |
| Decode | ~110 tok/s | 24–31 tok/s |
| Time to first token, short prompt | 0.1–0.2 s | 2.1–3.4 s |
| HumanEval+ | 45 / 50 | 47 / 50 |
| IFEval (strict) | 40 / 50 | 42 / 50 |
| MATH-500 | 48 / 50 | 48 / 50 |
| MMLU-Pro | 44 / 50 | 39 / 50 |

With 50 items, one item is 2 points; two runs of the whole model already differ by 2 items on
MMLU-Pro. On the full benchmark sets (pool simulated at cap 71), `top4` is at most 1.7 points below
the whole model on any of the four benchmarks (MMLU-Pro: 84.0% vs 85.7%; HumanEval+: 91.5% vs
91.5%; MATH-500: 97.0% vs 96.0%; IFEval: 84.5% vs 81.5%).

### Context-length scaling (Qwen3.6-35B-A3B, 10 GB tier vs whole model)

| Prompt length | Whole model prefill | NVSpool prefill | Whole model decode | NVSpool decode | Whole model peak RAM | NVSpool peak RAM |
|---|---:|---:|---:|---:|---:|---:|
| Short (~170–500 tok) | 550–994 tok/s (0.1–0.2 s) | 27–70 tok/s (2.1–3.4 s) | ~110 tok/s | 24–31 tok/s | 20.3–20.8 GB | **7.6–8.0 GB** |
| Medium (20k tokens) | 618 tok/s (32.4 s) | **800 tok/s (25.0 s)** | 67.3 tok/s | 21.0 tok/s¹ | 30.2 GB | **8.3 GB** |
| Long (65k tokens) | —² | **500 tok/s (131.3 s)** | —² | 22.4 tok/s¹ | —² | **9.8 GB** |
| Very long (131k tokens) | —² | **300 tok/s (442.1 s)** | —² | 16.9 tok/s¹ | —² | **12.2 GB** |

¹ Measured prior to decode prefetch `k=4`. Medium prompts hit higher prefill tok/s than short
prompts because layer-major prefill spreads one fixed SSD pass over more tokens.
² Stock `mlx-lm` already reached 30.2 GB OS peak at 20k on the 36 GB Mac, leaving no headroom to
run 65k/131k resident without heavy swap.

### All three models at 20k context (NVSpool 10 GB tier vs whole model)

| Model (20k prompt) | Whole model prefill | NVSpool prefill | Whole model decode | NVSpool decode | Whole model peak RAM | NVSpool peak RAM |
|---|---:|---:|---:|---:|---:|---:|
| Qwen3.6-35B-A3B (`top4`) | 618 tok/s (32.4 s) | **800 tok/s (25.0 s)** | 67.3 tok/s | 21.0 tok/s | 30.2 GB | **8.3 GB** |
| Gemma 4 26B-A4B (`top4`) | 1,070 tok/s (19.0 s) | 717 tok/s (28.4 s) | 77.7 tok/s | 32.3 tok/s | 19.3 GB | **9.4 GB** |
| GLM-4.7-Flash (`top2`) | 457 tok/s (44.0 s) | 340 tok/s (59.2 s) | 43.0 tok/s | 15.0 tok/s | 26.0 GB | **8.9 GB** |

On short prompts, **Gemma 4 26B-A4B (`top4`)** also offers smaller tiers: 36–38 tok/s at 10 GB,
23–26 tok/s at 8 GB, and 17 tok/s at the **6 GB tier** (useful on 16 GB Macs). **GLM-4.7-Flash**
decodes at 14–20 tok/s (`top2`) or 9–13 tok/s (`exact`, identical output to the whole model).

Full tables (including fast-vs-shared storage speedups) are in [docs/BENCHMARKS.md](https://github.com/jhdcho/nvspool/blob/main/docs/BENCHMARKS.md).

If you're dedicating the whole machine to inference and the model fits in RAM with room to spare,
plain mlx-lm is about 4× faster. NVSpool is for when it doesn't fit — or when you need RAM left over
for containers, IDEs and the rest of your dev stack.

## Models & strategies

| Profile | Checkpoint | Size | Tiers (GB) | Strategies (default first) |
|---|---|---:|---|---|
| `qwen36-35b` | `mlx-community/Qwen3.6-35B-A3B-4bit` | 20.4 GB | 10, 15 | `top4`, `top6`, `exact`, `fill71` |
| `qwen4-35b` *(preview)*¹ | `mlx-community/Qwen4-35B-A3B-4bit-text` | 31.3 GB | 10, 15 | `top4`, `top6`, `top2`, `exact`, `fill71` |
| `gemma4-26b` | `mlx-community/gemma-4-26b-a4b-it-4bit` | 15.3 GB | 6, 8, 10 | `top4`, `exact` |
| `glm47-flash` | `mlx-community/GLM-4.7-Flash-4bit` | 16.9 GB | 10 | `top2`, `exact` |

¹ **Qwen4-35B-A3B (`qwen4-35b`)** streams both its routed experts and its 11.5 GB n-gram lookup
tables from SSD (1.51 GiB stays in RAM), running the 31–32 GB (56B-parameter) model in **7.7 GB
peak RAM** (`23.5 tok/s` `top4`, `29.4 tok/s` `top2`, `16.4 tok/s` `exact`; `765 tok/s` prefill and
`9.2 GB` peak RAM at 34.5k context on an external NVMe SSD). Because Qwen4 MoE is not in PyPI
`mlx-lm` yet, see [Try Qwen4-35B-A3B (preview)](#try-qwen4-35b-a3b-preview) below for exact setup
commands.

The default tier is 10 GB and the default context limit is 65,536 (64K). All profiles are
**text-only** (vision towers are skipped; image inputs return HTTP 400). Profiles live in
[src/nvspool/profiles.toml](https://github.com/jhdcho/nvspool/blob/main/src/nvspool/profiles.toml). Each model keeps its own licence (Qwen3.6 /
Qwen4: Apache-2.0; GLM-4.7-Flash: MIT; Gemma 4: see its model card).

### What the strategies (`top4`, `top2`, `top6`, `exact`, `fill71`) mean

You pick a strategy by appending a tag to the model name (e.g. `qwen36-35b:exact`,
`glm47-flash:top2`). Prompt prefill always runs `exact`; strategies only control what happens
during reply token generation when a router-picked expert isn't in the RAM pool:

- **`exact`**: Reads every missing router pick from SSD. Output matches stock mlx-lm token for
  token, at slower decode speed (e.g. ~15–18 tok/s on Qwen3.6 10 GB; 9–13 tok/s on GLM-4.7-Flash).
- **`top4` / `top6` / `top2` (`topN`, default)**: Only reads missing experts among the router's
  highest-scoring *N* picks (default reads half: 4 of 8 for Qwen3.6 / Qwen4 / Gemma 4, 2 of 4 for
  GLM-4.7-Flash). Any lower-ranked miss is substituted with the best-scoring expert already in the
  RAM pool, and routing weights are computed over the experts actually used. Cuts SSD reads roughly
  in half for ~1.5–1.7× faster decode with minimal accuracy delta (see Benchmarks).
- **`fill71`** *(Qwen3.6 / Qwen4)*: Reads missing picks until the pool has been claimed within a
  32-token window, then decodes strictly from resident experts.

## Install & quick start

**Requirements:** Apple silicon Mac (macOS), Python 3.12–3.14, [uv](https://docs.astral.sh/uv/),
and an NVMe SSD (internal or fast external USB-C; slow drives bottleneck decode).

```bash
git clone https://github.com/jhdcho/nvspool && cd nvspool
uv sync --extra serve
uv run nvspool pull qwen36-35b
uv run nvspool app qwen36-35b
```

`--extra serve` installs FastAPI and uvicorn, which `serve` and `app` need. `run` works without
them.

### Try Qwen4-35B-A3B (preview)

Qwen4-35B-A3B needs a newer `mlx-lm` build than the pinned PyPI release (`ml-explore/mlx-lm#2140` /
`mlx-lm >= 0.33`). Until `mlx-lm` ships on PyPI, install it into the repo's venv and pass
`--no-sync` to `uv run` (otherwise `uv run` reverts `mlx-lm` back to `uv.lock`):

```bash
# 1. Install NVSpool (skip --extra serve if you only want terminal `nvspool run`)
git clone https://github.com/jhdcho/nvspool && cd nvspool
uv sync --extra serve

# 2. Install MLX + mlx-lm with Qwen4 MoE support (once mlx-lm >= 0.33 is on PyPI, just upgrade it)
uv pip install "mlx>=0.32.4" \
  "git+https://github.com/jundot/mlx-lm.git@7b35ab57159442082a64f0a8282ea61b4940cd17"

# 3. Download a compatible 4-bit MLX checkpoint (~31.3 GB; skip if you already have one)
#    Also works with mlx-community/Qwen4-35B-A3B-4bit or unsloth/Qwen4-35B-A3B-MLX-4bit.
#    (GGUF and mlx-vlm oQ4e-mtp shard builds are not supported.)
uv run --no-sync hf download mlx-community/Qwen4-35B-A3B-4bit-text \
  --local-dir ~/models/Qwen4-35B-A3B-4bit

# 4. Run in terminal (`nvspool run`) or browser UI (`nvspool app`) — pick ONE storage mode:
# Option A: Shared copy (recommended first try — ~1.5 GB extra disk, 1 s setup, keeps your folder)
uv run --no-sync nvspool run qwen4-35b --storage shared --from ~/models/Qwen4-35B-A3B-4bit

# Option B: Fast copy (needs ~31 GB extra disk during setup, ~30 s; delete original folder after)
uv run --no-sync nvspool run qwen4-35b --from ~/models/Qwen4-35B-A3B-4bit
```

- Swap `nvspool run` for `nvspool app` to open the browser UI at `http://127.0.0.1:11500/`, or
  `nvspool serve` for OpenCode / OpenAI clients.
- To store NVSpool's copy on an external SSD, add `--store-dir /Volumes/YourSSD/nvspool-models/qwen4-35b`
  (or set `NVSPOOL_HOME=/Volumes/YourSSD/nvspool`).
- After the first setup, later runs don't need `--from`:
  `uv run --no-sync nvspool run qwen4-35b --storage shared` (or without `--storage shared` for fast).

## Get a model

### Download

```bash
uv run nvspool pull qwen36-35b
```

`pull` first looks for a copy you already have, in LM Studio's model folders or the Hugging Face
cache. To be reused, that copy must have the same model type and checkpoint name as the profile,
4-bit quantization, and every shard present. Otherwise `pull` downloads from Hugging Face; it checks
free disk space first, and an interrupted download resumes. It then builds the fast copy under
`~/.nvspool/models/` (set `NVSPOOL_HOME` to move it), checks 40 random expert records against the
source, and deletes the download.

| Flag | Effect |
|---|---|
| `--storage shared` | Keep the checkpoint and set up a shared copy (about 1.3 GB) instead of a fast copy |
| `--keep-download` | With the fast copy, keep the downloaded checkpoint |
| `--download-only` | Download only; set up no copy |
| `--force-download` | Ignore local copies |
| `--find` | List the local copies it would reuse, then exit |
| `--repo`, `--revision`, `--download-dir`, `--store-dir` | Override where it downloads from or writes to |

### Use a checkpoint you already have

Download any `mlx-lm`-formatted checkpoint (from `mlx-community`, `unsloth`, LM Studio's MLX
downloads, or your own `mlx_lm.convert` run) and pass `--from <dir>` once to `run`, `app` or
`serve`. NVSpool packs or indexes it automatically and verifies the copy; later runs find it without
`--from`.

```bash
# Fast copy (packs experts.bin — plus ple.bin on Qwen4 — then you can delete the original download)
uv run nvspool app qwen36-35b --from ~/path/to/Qwen3.6-35B-A3B-4bit
uv run --no-sync nvspool app qwen4-35b --from ~/path/to/Qwen4-35B-A3B-4bit   # after Qwen4 mlx-lm step above

# Shared copy (~1.3–1.5 GB extra disk; streams experts and Qwen4 PLE straight from your download)
uv run nvspool app qwen36-35b --storage shared --from ~/path/to/Qwen3.6-35B-A3B-4bit
uv run --no-sync nvspool app qwen4-35b --storage shared --from ~/path/to/Qwen4-35B-A3B-4bit
```

Any `run`, `app` or `serve` command accepts `--from` (plus `--store-dir <dir>` to place the NVSpool
copy on another drive). The checkpoint must be in `mlx-lm` format (`mlx_lm.convert` layout):

- `config.json` plus sharded `*.safetensors` with `model.safetensors.index.json`.
- Routed experts stacked into one `[experts, …]` tensor per projection (`switch_mlp` layout). For
  Qwen4, per-head PLE tables (`ngram_embedding.{0..15}.*`, as in `mlx-community/*-4bit`,
  `mlx-community/*-4bit-text`, and `unsloth/Qwen4-35B-A3B-MLX-4bit`; `mlx-vlm`'s 128-shard
  `shards.{0..127}` layout is not supported yet).
- Quantized experts: MLX affine quantization in `config.json`'s `quantization` field (including
  mixed-bit per-module overrides such as Unsloth's 8-bit core + 4-bit experts), or
  `nvfp4`/`mxfp4`/`mxfp8`. The tiers are sized for 4-bit checkpoints.
- A `model_type` that the installed `mlx-lm` can build (`qwen4-35b` currently needs an `mlx-lm`
  build with `ml-explore/mlx-lm#2140` / `mlx-lm >= 0.33`).

GGUF files, the original Hugging Face BF16/FP8 releases (one tensor per expert), and GPTQ/AWQ
checkpoints don't work directly — convert them with `mlx_lm.convert --hf-path <repo> -q --q-bits 4`
first. `--from` doesn't check that the checkpoint matches the profile you name, so pass the right
one.

| | Fast copy (default) | Shared copy (`--storage shared`) |
|---|---|---|
| What it writes | `experts.bin` (one page-aligned record per expert; plus `ple.bin` on Qwen4) and `core.safetensors` | Only `core.safetensors` and an index; experts (and Qwen4 PLE) stream from your checkpoint |
| Extra disk | About the model's text size (~18–31 GB) | ~1.3–1.5 GB |
| Speed | Full (Qwen4 cold PLE ~2.8 ms/tok) | Gemma 4 about the same; Qwen3.6 0.85× short / 0.92× at 20k; Qwen4 cold PLE ~8–11 ms/tok, ~0.1 ms/tok warm |
| Your checkpoint | Can be deleted afterwards | Must stay where it is, unchanged (checked on each open) |

`--side` (shared only) also copies the scales and biases, about 6% of the expert bytes, so each
expert takes 4 reads instead of 9. `--store-dir` picks where the copy goes. NVSpool never
overwrites a directory that holds something else. [docs/STORAGE.md](https://github.com/jhdcho/nvspool/blob/main/docs/STORAGE.md) covers the
lower-level `pack`, `index`, `verify` and `inspect` commands. `nvspool inspect <checkpoint>` reads
only the headers and reports whether a model suits this approach.

## Run

### Terminal chat

```bash
uv run nvspool run qwen36-35b
```

In-chat commands: `/set strategy <name>`, `/set context <N>`, `/stats`, `/clear`, `/bye`.
`--max-tokens` and `--temp` (default 0) are also available.

### Browser app

```bash
uv run nvspool app               # opens the browser with no model loaded; pick one there
uv run nvspool app qwen36-35b    # loads qwen36-35b first
```

This starts the server and opens `http://127.0.0.1:11500/`. The page has:

- Model & strategy switcher in the header, plus live context-length control in Settings. Switching
  unloads the current model before loading the next, so only one is ever in RAM; Unload frees it
  entirely. Chat context is kept when switching models.
- A Think toggle (`Off` by default), streaming reasoning into a collapsible block.
- File attachments: text, code, CSV, JSON, Markdown, HTML, DOCX/RTF and PDF (extracted locally
  using macOS `textutil` / `PDFKit`, or optional `pypdf` / `pdftotext`, and never saved to disk).
- Optional web search, off by default. Bring your own Brave, Tavily or Exa key, or a SearXNG URL,
  or use "Open" (Wikipedia and arXiv). Set it in Settings (saved to `$NVSPOOL_HOME/web.toml`, default
  `~/.nvspool/web.toml`, mode 0600) or with `NVSPOOL_WEB_PROVIDER`, `NVSPOOL_WEB_API_KEY` and
  `NVSPOOL_SEARXNG_URL`. Attached files are never sent to the search provider. Nothing else leaves
  the machine.
- Local chat history sidebar (stored in browser `localStorage`) and per-reply prefill and decode
  tok/s stats.

### Server

```bash
uv run nvspool serve qwen36-35b
uv run nvspool serve --lazy --models-dir /Volumes/External/nvspool-models
```

The server listens on `127.0.0.1:11500` by default; set `--host` and `--port` to change it. It
handles one request at a time with one model in RAM. To connect OpenCode, Cline, Continue, Open
WebUI or any OpenAI SDK:

- Base URL: `http://127.0.0.1:11500/v1`
- API key: any string
- Model: `<profile>:<strategy>`, e.g. `qwen36-35b:top4`, `qwen4-35b:top4`, `gemma4-26b:top4`,
  `glm47-flash:top2`

```bash
curl http://127.0.0.1:11500/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model":"qwen36-35b:top4","messages":[{"role":"user","content":"Hello"}]}'
```

Endpoints:

- `POST /v1/chat/completions`: streaming SSE, tool calls, and optional `thinking` (`false` by
  default), `reasoning_effort`, `web` (`false` by default) and `web_mode` (`"quick"` | `"deep"`).
- `GET /v1/models`: every `model:strategy` ready on disk.
- `POST /v1/models/load`: takes `{"model": "...", "ram_gb": 8, "context": 65536}`; `ram_gb` and
  `context` are optional, and `{"context": N}` alone changes the context limit without reloading
  weights.
- `POST /v1/models/unload`.
- `POST /v1/files/extract` (offline local text extraction) and `GET`/`POST /v1/web/config`.
- `GET /stats`.

With no model argument, or with `--lazy`, the server starts with nothing loaded (~0 GB MLX RAM) and
loads a model on the first `/v1/models/load` or chat request that names one. `--models-dir DIR`
(repeatable) adds a directory to search for ready copies, named `<profile>` (fast) or
`<profile>-shared`. To see what a running server holds and what its last request did, run
`uv run nvspool ps`.

### Flags for `run`, `app` and `serve`

| Flag | Effect |
|---|---|
| `<profile>:<strategy>` | Choose the strategy (see [Models & strategies](#models--strategies)) |
| `--ram-gb N` | Memory tier; it must be one the model has |
| `--cap N` | Experts per layer in the pool, overriding the tier |
| `--context N` | Prompt plus reply limit in tokens (default: 65536; KV cache grows dynamically with prompt length, so larger active contexts use more RAM) |
| `--storage fast\|shared`, `--from`, `--store-dir`, `--side` | See [Get a model](#get-a-model) |

`uv run nvspool --help` lists every command.

## How it works

NVSpool loads the model with mlx-lm, then swaps each MoE layer's experts module for a pooled one
(and on Qwen4, also swaps layer 1's 11.5 GB n-gram lookup tables for a disk-backed, bit-exact row
cache in `ple.bin`). The remaining weights (attention, embeddings, norms, routers, shared experts
and dense layers) are loaded from `core.safetensors` and stay in RAM: about 1.4–1.5 GB on Qwen3.6,
Qwen4 and Gemma 4, 1.1 GB on GLM-4.7-Flash. Vision towers and MTP heads are skipped.

**Experts on disk (why not `mmap`, and SSD wear).** Standard `mmap` lets macOS pull the whole 20 GB
checkpoint into page cache, starving other apps and causing swap thrashing. Instead, NVSpool opens
the expert store with `F_NOCACHE` (page cache bypassed) and manages its own fixed-size pool. The
fast copy stores each expert as one page-aligned record in `experts.bin` (one `pread` per expert);
the shared copy reads the same bytes directly from the checkpoint's stacked tensors (9 ranges per
expert in parallel, or 4 with `--side`). Inference only issues **reads** — zero SSD writes per
token (SSD endurance / TBW is a write-wear limit, and `--storage shared` avoids even the one-time
pack write).

**The pool.** Each MoE layer holds at most `cap` experts in RAM, evicting the least recently used.
The tier sets the cap: Qwen3.6 71 at 10 GB and 136 at 15 GB (of 256 experts); Gemma 4 50, 35 and 20
at 10, 8 and 6 GB; GLM-4.7-Flash 20 at 10 GB.

**Prompt processing (layer-major prefill).** The whole prompt goes through layer 0, then layer 1,
and so on, so each expert is read about once per prompt rather than once per chunk. Within a layer,
attention runs chunk by chunk, then the experts are read in groups of at most cap/2. A background
thread reads the next group while the GPU computes the current one. The last groups read are the
experts the prompt's final tokens wanted most, and they become the pool, so the reply starts warm.

**Reply tokens (decode prefetch).** While layer *l* computes, NVSpool runs layer *l+1*'s router on
layer *l*'s input and reads its predicted top *k* early: k = 4 on Qwen3.6, 2 on GLM, off on Gemma.

**Long contexts.** At head dims 256 and 512, MLX's fused attention builds the full score matrix,
which grows with chunk × context. With `attn_budget_mb` (256 MB by default) NVSpool splits prefill
attention into blocks of query rows to bound it. Each row still sees the same keys, so only
rounding changes.

**Prefix cache.** Up to 1 GB of KV snapshots (6 entries) lets a follow-up request reuse the prompt
it shares with an earlier one.

Adding a model family takes a router adapter, a layer-major `Steps` class and a profile; see
[docs/ADDING-A-MODEL.md](https://github.com/jhdcho/nvspool/blob/main/docs/ADDING-A-MODEL.md).

## Licence

Apache-2.0. See [LICENSE](https://github.com/jhdcho/nvspool/blob/main/LICENSE) and [NOTICE](https://github.com/jhdcho/nvspool/blob/main/NOTICE).
