[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-114536":3},{"id":4,"name":5,"fullName":6,"owner":7,"repo":5,"description":8,"homepage":9,"htmlUrl":10,"language":11,"languages":10,"totalLinesOfCode":10,"stars":12,"forks":13,"watchers":14,"openIssues":15,"contributorsCount":16,"subscribersCount":16,"size":16,"stars1d":17,"stars7d":18,"stars30d":19,"stars90d":16,"forks30d":16,"starsTrendScore":20,"compositeScore":21,"rankGlobal":10,"rankLanguage":10,"license":22,"archived":23,"fork":23,"defaultBranch":24,"hasWiki":25,"hasPages":23,"topics":26,"createdAt":10,"pushedAt":29,"updatedAt":30,"readmeContent":31,"aiSummary":32,"trendingCount":16,"starSnapshotCount":16,"syncStatus":33,"lastSyncTime":34,"discoverSource":35},114536,"tandem.cpp","ewhacc\u002Ftandem.cpp","ewhacc","llama.cpp patch: faster MoE offload for models that don't fit in VRAM","",null,"Python",129,21,3,1,0,9,45,53,35,87.33,"Other",false,"main",true,[27,28],"local-llm","moe","2026-11-23 11:07:06","2026-11-23 18:13:43","# tandem.cpp\n\ntandem.cpp is a patch set for [llama.cpp](https:\u002F\u002Fgithub.com\u002Fggml-org\u002Fllama.cpp). It makes MoE models that don't fit in VRAM run faster in llama.cpp. It keeps the most-used experts on the GPU and lets the CPU help with the rest, for any MoE model llama.cpp supports. Single-user decode is the target.\n\nIt is an independent project, not affiliated with ggml-org. Base: llama.cpp `f5222f25c` (2026-10-28, see `BASE_COMMIT`). Version **v0.3.0** (2026-11-16).\n\nDecode tok\u002Fs, one user, GSM8K questions. Each row is one model on one PC. Two numbers are greedy \u002F the model's recommended sampling, one number is greedy.\n\n| Model | GPU | RAM | CPU | llama.cpp | tandem.cpp | Strata |\n|---|---|---|---|---|---|---|\n| **Qwen3.8-Flash-Next** IQ3_S + MTP (78 GiB) | RTX 4090 24 GB | 64 GB | i5-12400F | 29.8 \u002F 29.4 | **97.3 \u002F 96.1** (3.3×) | 103.6 \u002F 98.1 |\n| **Qwen3.8-Flash-Next** IQ3_S + MTP (78 GiB) | RTX 5070 Ti 16 GB | **32 GB** | i5-12400F | 14.9 | **34.3** (2.3×) | 30.3 |\n| **gpt-oss-120b** MXFP4 | RTX 4090 24 GB | 64 GB | i5-12400F | 27.8 \u002F 27.7 | **51.5 \u002F 48.0** (1.85×) | — |\n| **DeepSeek-V4.1-Flash** IQ2_XS (139 GiB of experts) | RTX 5070 Ti 16 GB | 64 GB | i5-14600KF | 7.4 | **14.2** (1.9×) | 10.4 |\n| **DeepSeek-V4.1-Flash** IQ2_XS | RTX 5070 Ti + 5060 Ti (32 GB) | 64 GB | i5-14600KF | — | **16.9** | — |\n| **DeepSeek-V4.1-Flash** IQ2_XS | RTX 5090 32 GB | 128 GB | i5-14600KF | 11.5 | **33.1** (2.9×) | 28.0 |\n\nThe 32 GB row is a cold start from a DRAM-less PCIe 4.0 SSD; at 64 GB DeepSeek-V4.1 is about twice the RAM and is read from a PCIe 4.0 SSD.\n\nWe tuned for Qwen3.8 only, but other MoE models that do not fit in VRAM gain too: typically 1.4–2.1× (gpt-oss-120b, GLM-4.5-Air, Qwen3.6-35B-A3B, DeepSeek-V4.1), less when the model nearly fits. Prompts are also 4–13× faster than llama.cpp's on a 16 GB card with the experts in RAM or on the SSD ([v0.3.0](#whats-new-in-v030-2026-11-16)).\n\n[Strata](https:\u002F\u002Fgithub.com\u002FNiko1221\u002FStrata) is a dedicated MoE offload engine for a few models. Its default skips low-ranked experts (`STRATA_ROUTE_TAIL_SKIP`), which is faster but approximate (127.1 tok\u002Fs in the RTX 4090 Qwen3.8 row); the tables show its exact mode (`STRATA_ROUTE_TAIL_SKIP=0`, every routed expert computed, as llama.cpp and tandem.cpp do). Full tables per machine are [below](#results).\n\nFor a big card serving many concurrent streams, use [Tandem-vLLM](https:\u002F\u002Fgithub.com\u002Fewhacc\u002Ftandem) instead; tandem.cpp is for one user on a 16–32 GB card with the experts in RAM.\n\n## What's new in v0.3.0 (2026-11-16)\n\n- **Prompts 2–6× faster.** With an expert cache, prompts now run in 4096-token batches instead of 512. Each batch has to\n  bring every expert it uses into VRAM, so a 16k-token prompt used to load the experts 31 times; now 4 times. The larger\n  batch needs more VRAM for its buffers: it **borrows** that from the expert cache while a long prompt runs and gives it back\n  for decode, so decode keeps its full cache (the VRAM is remapped in place: nothing is rebuilt, ~10–100 ms per prompt). Exact.\n  Time to first token, RTX 5070 Ti 16 GB + 64 GB (i5-12400F), the same prompts for every engine:\n\n  | Prompt | llama.cpp | tandem.cpp before | **tandem.cpp v0.3.0** | Strata v0.2.9 |\n  |---|---|---|---|---|\n  | Qwen3.8 IQ3_S + MTP, 5.5k tokens | 28.9 s | 12.4 s | **4.1–7.0 s** | 6.6 s |\n  | Qwen3.8 IQ3_S + MTP, 15.8k tokens | 81.9 s | 35.1 s | **14.7–15.5 s** | 11.7 s |\n  | DeepSeek-V4.1 IQ2_XS, 5.6k tokens | 722 s | 324 s | **53–103 s** | 58 s |\n  | DeepSeek-V4.1 IQ2_XS, 15.7k tokens | 2012 s | 885 s | **189–201 s** | 57 s |\n\n  On an RTX 5090 + 128 GB, Qwen3.8 at 15.8k tokens: 19.4 → 7.4 s (Strata 2.2 s). Decode after the prompt is unchanged on the\n  5090; on the 16 GB card it is 72.1 vs 73.6 tok\u002Fs (four interleaved cells each, ~2%, near the noise). The borrowing needs\n  CUDA's virtual memory management; on AMD (ROCm builds have it off) the cache is simply sized for the larger buffers.\n  `LLAMA_MOE_UBATCH=0` restores 512-token batches.\n- **Project Maya's DeepSeek-V4.1 GGUFs run on tandem.cpp** after a one-time conversion ([`tools\u002Fmaya_ds41_convert.py`](tools\u002Fmaya_ds41_convert.py),\n  [below](#deepseek-v41-maya-xs-the-same-weights-on-both-engines)): the same Maya-XS files on both engines, RTX 5090 + 128 GB,\n  **tandem.cpp 60.3 \u002F 59.4 tok\u002Fs vs Maya's 39.0 \u002F 39.6** (1.5×; 54.9 with Maya's DSpark drafter, which tandem.cpp does not have).\n- Store mode: when the RAM store is a few slots short of holding every expert next to the VRAM cache, it now holds all but\n  those few instead of none (`LLAMA_MOE_ARENA_FILL_PART=0`: the old behaviour).\n\n## Earlier (2026-11-15, second update)\n\n- **Fixed: 128 GB of RAM with a model bigger than RAM (e.g. DeepSeek-V4.1) was killed at load or hung the PC.** On the Linux 7.0\n  kernels we tested, part of RAM is a region that pinned memory cannot use (9 GiB on a 128 GB PC), but the kernel counts it as free, and\n  the RAM store took it. The store now leaves it out and keeps 2 GiB + 1\u002F64 of RAM free. It also switches on only where it\n  helps: when it can hold every expert, or when the experts are larger than RAM. DeepSeek-V4.1 on 128 GB + RTX 5090 runs in\n  mmap mode again (31.4 tok\u002Fs in today's check; the table row is 33.1, Strata 28.0). `LLAMA_MOE_STORE_AUTO=0` is no longer needed.\n- **A big card in a small PC: VRAM and RAM hold every expert.** When the VRAM cache plus the RAM store can hold all the\n  experts, they are placed at start (as many as fit in VRAM, the rest in RAM) and nothing is read from the SSD after that. Qwen3.8\n  IQ3_S + MTP (78 GiB) on a 32 GB PC with an RTX 5090: **147 tok\u002Fs, up from 75**, first token 0.45 s;\n  Strata 153–158 on the same PC (0.95×).\n- **Faster first token in store mode.** Each expert upload's padding was read from the file, an SSD read per expert.\n\n## Earlier (2026-11-15)\n\n- **Faster GPU work between the routing steps, on NVIDIA and AMD.** Three kernel changes in the shared CUDA\u002FHIP code: the\n  routing results come back to the CPU in one copy instead of three, a faster hyper-connection mixing kernel, and fused\n  state copies. Qwen3.8 with one user, A\u002FB on the same PC: RX 7900 XTX +2.5%, RTX 5070 Ti +1.3%. The 7900 XTX same-day table below now\n  reads 68.6 tok\u002Fs (65.6 on 11-12).\n  Exact; no setting needed (`LLAMA_MOE_RB_PACK=0` \u002F `GGML_CUDA_CPY_RUN=0` turn two of them off).\n\n## Earlier (2026-11-13)\n\n- **Big models on small PCs are much faster.** When a model is far larger than your RAM, tandem.cpp now uses VRAM and RAM\n  together as one large expert cache, so far less has to come from the SSD. It switches on by itself; nothing to set.\n  - Qwen3.8 (78 GiB) on a 32 GB PC with a 16 GB card: **34 tok\u002Fs, up from 29**. That's 2.3× llama.cpp and 1.13× Strata,\n    with half Strata's SSD reads and a faster first token.\n  - DeepSeek-V4.1 (139 GiB of experts) on a 64 GB PC: +7–10%, and **1.8× Strata** on a budget DRAM-less SSD.\n  - Models that fit in RAM run as before. 2 GiB of RAM stays free for your other programs.\n  - Details in [Results](#results); `LLAMA_MOE_STORE_AUTO=0` turns it off ([Defaults](#defaults)).\n\n## Earlier (2026-11-11)\n\n- Models larger than RAM: prompt batches read their missed experts ahead with several threads, and misses read from the SSD\n  keep the drive busy. DeepSeek-V4.1 on a 64 GB PC with a 16 GB card is now 1.9× the speed of llama.cpp and 1.4× Strata's (the earlier build was slower than llama.cpp).\n- A launcher, `tandem-run.py`, that picks the flags for your machine ([Quick start](#quick-start)).\n- More GPU memory for the expert cache: the default reserve is 512 MiB (was 1024), and with `LLAMA_MTP_DRAFT_VOCAB` the MTP\n  head's full output layer stays in host memory (only the vocabulary subset is on the GPU).\n- The CPU path times its i-quant kernels once at startup and keeps the fastest for your CPU (Qwen3.8 IQ3_S: +4.5% on an i7-8700, +1.5% on an i5-14600KF).\n- The CPU\u002Fupload split leaves a little more to the CPU (factor 2.25 instead of Strata's 1.85: +2.7% on a 14600K, level elsewhere).\n- Pinned copies of the experts use huge pages when the RAM allows (a fragmented host lost up to 30% before).\n- Fixes from [issue #2](https:\u002F\u002Fgithub.com\u002Fewhacc\u002Ftandem.cpp\u002Fissues\u002F2) (2026-11-12): the CPU-expert threads sleep when idle\n  (3.3 cores busy at idle before, 0.03 now), and the auto cache size leaves room for the pooled graphs, which grow with the\n  context (long prompts on a 12 GB card ran out of memory).\n- The CPU threads default (`LLAMA_MOE_CPU_THREADS`) now counts physical cores within the process's CPU set: CPUs without\n  SMT or with E-cores got about half before (14600K: 13 threads instead of 9, level on speed; a 6-core\u002F12-thread CPU is unchanged).\n- The RAM rules (pinned experts, the per-layer embedding table, read-ahead) also see a container's memory limit (Docker or\n  Podman with cgroup v2, or cgroup v1), not only a systemd scope's.\n- Qwen3.8 with MTP keeps one recurrent state and replays the accepted tokens instead of a snapshot per draft position:\n  315 MiB more for the expert cache, +2.8% on a 16 GB card (exact; `LLAMA_GDN_REPLAY=0` restores the snapshots).\n- Fix (Qwen3.8 with MTP): after a speculative step that rejected draft tokens, a following batch of 64 or more tokens (the\n  next turn's prompt) on Ampere, Ada or Blackwell GPUs started from the recurrent state that still included the rejected tokens.\n\n## Build\n\n```\nbash build.sh            # clones llama.cpp into .\u002Ftandem, checks out the base commit, applies tandem.patch, builds llama-server\n```\n\nor by hand:\n\n```\ngit clone https:\u002F\u002Fgithub.com\u002Fggml-org\u002Fllama.cpp tandem && cd tandem && git checkout f5222f25c\ngit apply ..\u002Ftandem.patch\ncmake -B build -DGGML_CUDA=ON -DCMAKE_BUILD_TYPE=Release\ncmake --build build -j --target llama-server\n```\n\nTested with CUDA 13.0–13.4 on Ubuntu 22.04 and 24.04, sm_89 (RTX 4090, RTX 6000 Ada) and sm_120 (RTX PRO 6000 Blackwell).\n\n### AMD GPUs (ROCm)\n\nThe patch builds with HIP. We use the same code as on CUDA; kernel work is done once in that shared code and measured on an RX 7900 XTX too:\n\n```\ngit clone https:\u002F\u002Fgithub.com\u002Fggml-org\u002Fllama.cpp tandem && cd tandem && git checkout f5222f25c\ngit apply ..\u002Ftandem.patch\ncmake -B build -DGGML_HIP=ON -DGGML_HIP_GRAPHS=ON -DGPU_TARGETS=gfx1100 -DCMAKE_BUILD_TYPE=Release\ncmake --build build -j --target llama-server\n```\n\nSet `GPU_TARGETS` to your card (gfx1100 = RX 7900 series). `-DGGML_HIP_GRAPHS=ON` matters: without graphs every layer's kernels are\nlaunched one by one. For `-ot`, use the ROCm device name, `ROCm0`, where the examples say `CUDA0`. `tandem-run.py` supports NVIDIA only.\n\nTested on one PC: i7-8700 (6 cores), 64 GB DDR4, RX 7900 XTX 24 GB on PCIe 3.0, Ubuntu 26.04 with its ROCm 7.1 packages.\nQwen3.8-Flash-Next + MTP, greedy, the first 40 GSM8K questions, same day (2026-11-15), decode tok\u002Fs:\n\n| Engine, quant | tok\u002Fs |\n|---|---|\n| tandem.cpp IQ3_S | 68.6 |\n| Strata v0.2.9 IQ3_S | 69.7 |\n| Strata's RDNA3 fork IQ3_S | 80.1 |\n| Avalon IQ3_KS (its own quant) | 83.6 |\n\nStock Strata and tandem.cpp are level; on this 6-core CPU the AMD-specific engines are ahead. Most of that gap is the CPU's work on missed experts: IQ3_S is costly to\ndecode on a CPU, and Avalon's own quant types decode cheaply.\n\n## Quick start\n\n```\nbash build.sh                                  # builds llama-server (see Build above)\npython3 tandem-run.py --model qwen3.8          # or: gpt-oss-120b, or --model \u002Fpath\u002Fto\u002Fany-moe.gguf\n```\n\n`tandem-run.py` is one file (Python 3 standard library; Linux + NVIDIA only). It looks at your machine, picks the flags from the\n[Run](#run) section for it, prints each flag with a one-line reason and the exact command, then runs `llama-server` in the\nforeground. When it prints `URL: http:\u002F\u002F127.0.0.1:8080\u002F` the OpenAI-compatible API is ready at `\u002Fv1`.\n\nIt finds `llama-server` on your `PATH`, in `$TANDEM_SERVER`, or at `--server-bin`. `qwen3.8` and `gpt-oss-120b` are built in\nwith their MTP head \u002F EAGLE3 draft: if the files are missing it shows the `hf download` commands and sizes and asks before\ndownloading (`--yes` accepts; files go to `--models-dir`, default `~\u002Ftandem-models`). Any other GGUF gets the generic MoE rules\nand the plan says so. If the model fits in VRAM the launcher leaves the expert cache out, because plain llama.cpp is faster there.\n\n### Reading the plan\n\n```\n== Hardware ==   GPU, VRAM, PCIe, physical cores, RAM (the lower of MemTotal and the container limit), free disk, kernel\n== Model ==      which file, which draft\n== Plan ==       \u003Cflag>   \u003Cwhy>          one line per choice; \"(no -lm none)\" means a flag was left out on purpose\n                 note: \u002F WARNING:        things to know about this machine (below)\n== Calibration == cached, or \"will run on first start\"\n== Command ==    copy this to run it by hand\n```\n\n### Dry run\n\n`--dry-run` prints the plan and starts nothing, downloads nothing. Use it to see what the launcher would do, or to test other\nhardware (`--assume-vram-gib`, `--assume-ram-gib`, `--assume-cores` apply the rules for a smaller machine; they do not limit the\nreal hardware).\n\n### Calibration\n\nThe first start for a machine, model and server build loads the model once per candidate draft count (the server cannot change it\nper request), measures decode speed on three short prompts, keeps the fastest and saves it to\n`~\u002F.cache\u002Ftandem\u002F\u003Ckey>.json` (`$TANDEM_CACHE_DIR` moves it). It adds about 1-2 minutes after the first model load. Later starts reuse it.\n`--calibrate` redoes it and also tries one fewer draft token; `--no-calibrate` skips it and uses the rules.\n\n### Notes the plan may print\n\n- **The model is larger than RAM**: the experts are read from the SSD, so speed depends on the SSD, and the first requests are slower\n  while the cache warms. Below 0.6x the model size in RAM the drive sets the speed (DeepSeek-V4.1 on 64 GB: ~12 tok\u002Fs from a\n  PCIe 4.0 drive, ~8 from a DRAM-less one).\n- **Older kernels (before 6.1)**: they reclaim mapped model pages poorly. Expect slow loads when RAM is below the model size + 8 GiB.\n- **\"This build predates the low-RAM fixes\"**: your tandem.cpp is older than the RAM fixes. Update it; until then the launcher sets\n  `LLAMA_MOE_PIN_EXPS=0` and `LLAMA_PLE_GPU_COPY=0` when RAM is limited (a container limit is not seen by old builds).\n- In a container the launcher uses the memory limit (`memory.max`), not the host's RAM.\n\n### Other options\n\n`--port`, `--ctx` (default 16384), `--model-path` \u002F `--draft` (your own files, e.g. another quant), `--no-draft`, `--gpu N`,\n`--extra \"\u003Cllama-server flags>\"` (appended last).\n`--fast` adds `LLAMA_MOE_TAIL_SKIP=7`: faster but **approximate** (quality: TODO).\n\n## Run\n\nHow it works: the experts live in host RAM, and the GPU memory left over holds a cache of recently used experts. The experts a token needs that are not in the cache are split between the CPU, which computes its share while the GPU runs the cached ones, and an upload into the cache. The split follows the measured CPU and PCIe speeds, using Strata's rule.\n\nAny MoE model whose experts do not fit in VRAM:\n\n```\nllama-server -m model.gguf -ngl 99 -cmoe --moe-cache-mib auto -lm none -np 1 -c 16384\n```\n\n- `-cmoe` keeps every expert in host RAM. `--moe-cache-mib auto` gives the GPU memory left after the model, the KV cache and the compute buffers to the expert cache, minus `--moe-cache-margin-mib` (default 512 MiB; a draft model's GPU size is added automatically). A fixed size in MiB also works.\n- `-lm none` reads the weights into RAM instead of mmapping the file.\n- `-np 1 -c 16384`: one conversation with a 16k-token context. Without them llama-server opens 4 slots that share a small context, too short for a thinking model. `-c` is the total context; the KV cache comes out of the GPU memory the expert cache would get, so set it to what you need (on Qwen3.8, about 0.7 GiB per extra 16k tokens).\n- The startup line `failed to fit params ... n_gpu_layers already set by user to 99, abort` is expected: `-ngl 99` turns llama.cpp's automatic placement off, and the expert cache does the placement instead.\n- **Less RAM than the model** (e.g. Qwen3.8 IQ3_S, 78 GiB, on a 64 GB PC): leave out `-lm none`. The file stays mapped; the experts are copied once into pinned memory when the RAM holds them plus 10 GiB, and the rest is read from the file.\n- **Much less RAM (e.g. a 16 GB PC):** also pass `--cache-ram 0`. llama-server's prompt cache otherwise takes up to 8 GiB of RAM and pushes the experts out of the page cache.\n- Supported expert types for the CPU path: SwiGLU experts (fused or separate gate\u002Fup, optional clamp) and gpt-oss's SwiGLU-OAI with biases. For other types the server logs a warning and uploads every miss instead (correct, slower).\n\nQwen3.8-Flash-Next with its MTP head (the head as a separate GGUF). Download the model (IQ3_S, 78 GiB, two files) and the head (Q4_K):\n\n```\nhf download ISTA-DASLab\u002FQwen3.8-Flash-Next-GSQ-RCO-GGUF --include 'IQ3_S\u002F*' --local-dir models\nhf download ewhacc\u002Fqwen38-mtp-outq4_K-expsq4_K --include '*.gguf' --local-dir models\n```\n\nthen run:\n\n```\nLLAMA_MTP_DRAFT_VOCAB=100000 llama-server -m models\u002FIQ3_S\u002FQwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00001-of-00002.gguf \\\n  -ngl 99 -cmoe --moe-cache-mib auto -lm none -np 1 -c 16384 \\\n  --spec-type draft-mtp -md models\u002Fqwen38-mtp-outq4_K-expsq4_K.gguf --spec-draft-n-max 3 --spec-draft-sampling probabilistic \\\n  -bs -ot '^token_embd\\.weight=CUDA0'\n```\n\n- The head ([ewhacc\u002Fqwen38-mtp-outq4_K-expsq4_K](https:\u002F\u002Fhuggingface.co\u002Fewhacc\u002Fqwen38-mtp-outq4_K-expsq4_K), Qwen Community License 1.0) was made with `convert_hf_to_gguf.py --mtp` on the original checkpoint (Q8_0), then its output and expert tensors to Q4_K: `llama-quantize --allow-requantize --tensor-type output=q4_K --tensor-type _exps=q4_K qwen38-mtp-q8_0.gguf qwen38-mtp.gguf Q8_0`. That takes it from 3.6 to 1.8 GiB of VRAM with the same acceptance; the freed memory goes to the expert cache.\n- `--spec-draft-n-max 3` is best on 24 GB cards and 6–10 core CPUs; 4 on a 48 GB card.\n- `--spec-draft-sampling probabilistic` keeps sampled output exact (rejection sampling) and acceptance high at the model's default temperature.\n\ngpt-oss-120b with its EAGLE3 draft (`ggml-org\u002Fgpt-oss-120b-GGUF`):\n\n```\nllama-server -m gpt-oss-120b-MXFP4.gguf -ngl 99 -cmoe --moe-cache-mib auto -lm none -np 1 -c 16384 \\\n  --spec-type draft-eagle3 -md eagle3-gpt-oss-120b-Q8_0.gguf --spec-draft-n-max 4\n```\n\nEAGLE3 helps on 10+ core CPUs and slows things down on a 6-core one (see the consumer PC table).\n\n## Defaults\n\nThe server applies these when the variable is unset (logged at startup); `VAR=0` turns one off, `LLAMA_MOE_NO_DEFAULTS=1` turns all off. All are exact or differ only in floating-point rounding (kernel choice); the Qwen3.8-specific ones act only on that architecture.\n\n| Variable | Default | What |\n|---|---|---|\n| `LLAMA_MOE_CPU` | `auto` | CPU\u002FGPU split for missed experts (`0..1` = fixed share uploaded) |\n| `LLAMA_MOE_PREFILL_CPU` | `auto` | the split for prompt batches |\n| `LLAMA_MOE_CPU_THREADS` | cores − 1 | CPU threads for missed experts |\n| `LLAMA_MOE_CPU_RB`, `LLAMA_MOE_CPU_DOWN_PE` | `64`, `64` | CPU expert job work-item layout |\n| `LLAMA_MOE_STORE_AUTO` | `1` | store mode (below) when `LLAMA_MOE_ARENA_MIB` is unset and either the experts outside VRAM exceed the RAM, or all experts exceed 85% of the RAM and VRAM + the block can hold every one |\n| `LLAMA_MOE_ARENA_MIB` | unset | `auto` forces store mode: the experts outside VRAM live in one pinned block of RAM (the whole RAM less the reserve), read with direct I\u002FO |\n| `LLAMA_MOE_ARENA_RESERVE_MIB` | 2048 + RAM\u002F64 | store mode: RAM left free (on top of the kernel's CMA region, which pinned memory cannot use) |\n| `LLAMA_MOE_ARENA_FILL` | `1` | store mode: when VRAM + the block hold every expert, place them all at start (then every miss is uploaded, no CPU share) |\n| `LLAMA_MOE_ARENA_EXCL` | `1` | store mode: an expert is in VRAM or in the block, not both; the VRAM victim is copied back to RAM |\n| `LLAMA_MOE_ARENA_ASYNC` | `1` | store mode: decode reads from the SSD overlap the compute |\n| `LLAMA_MOE_PIN_EXPS` | auto | mmap mode: copy the mapped experts to pinned memory when the RAM holds them + 10 GiB |\n| `LLAMA_MOE_UBATCH` | `4096` | with an expert cache and `-b`\u002F`-ub` left at their defaults, prompts run in batches of this many tokens (`0`: `-ub`, 512). Draft contexts keep 512 |\n| `LLAMA_MOE_FLEX` | `1` | one GPU with CUDA VMM: the larger prompt buffers borrow expert-cache VRAM while a prompt runs (`0`: the cache is sized for them) |\n| `LLAMA_MOE_ARENA_FILL_PART` | `1` | store mode: fill the RAM store even when it is up to 3% short (the rest stays on the SSD) |\n| `LLAMA_MOE_GATHER` | `0` | `1`: a prompt batch's expert copies in one GPU kernel (slower than the copy engines on the GPUs we tried) |\n| `GGML_SCHED_BATCH_INPUTS` | `3` | a graph's host inputs in one packed upload |\n| `GGML_CUDA_GRAPH_KEY_SHAPE` | `1` | one CUDA graph per batch shape |\n| `GGML_CUDA_MMVF_MAX` | `8` | vector kernel for float weights up to 8 columns |\n| `GGML_CUDA_Q8_CACHE` | `1` | reuse quantized activations across matvecs |\n| `GGML_CUDA_CPY_RUN` | `1` | consecutive same-shape copies in one kernel |\n| `LLAMA_MOE_RB_PACK` | `1` | the routing results read back to the CPU in one copy |\n| `LLAMA_GRAPH_POOL` | `6` | keep allocated graphs for alternating small batch shapes; with `--moe-cache-mib auto` fewer (or none) at long contexts |\n| `LLAMA_GRAPH_POOL_MIB` | `256` | VRAM those pooled graphs may take, measured at the full context and left out of the auto cache size |\n| `LLAMA_SRV_USER_BREAK` | `0` | no prompt-cache checkpoint at every user message |\n| `LLAMA_MTP_DEFER` | `1` | MTP catch-up decode deferred to the next draft |\n| `LLAMA_HC_FUSE`, `GGML_CUDA_HC_MIX_FUSE` | `1` | hyper-connection fusions (Qwen3.8) |\n| `LLAMA_GDN_DIRECT`, `LLAMA_GDN_GATES` | `1` | Qwen3.8 gated delta net |\n| `LLAMA_GDN_REPLAY` | `1` | Qwen3.8 with speculative decoding, one sequence: one recurrent state plus the last step's inputs, replayed after a rollback, instead of a snapshot per draft position (450 → 135 MiB with 3 drafts) |\n| `LLAMA_CONV_CONT`, `LLAMA_CONV_TAIL` | `1` | Qwen3.8 conv state |\n| `LLAMA_PLE_GPU` | `1` | per-layer embeddings read by the GPU from pinned host memory |\n| `LLAMA_PLE_GPU_COPY` | auto | Linux 6.8+ (file mmaps cannot be pinned): copy the table to pinned memory, only when the RAM holds the model + 8 GiB |\n\n## Results\n\nSingle user, the first 200 GSM8K test questions (zero-shot chat, max 2048 tokens), decode tok\u002Fs. Every row gives both engines the same GPU memory and the same draft model, if any. llama.cpp is `f5222f25c` with its default placement (`-fit on`: dense layers on the GPU, then as many layers' experts as fit), with `-fitt` set to tandem.cpp's margin when a draft model is loaded. Each cell is \"greedy \u002F sampled\", sampled being the model's recommended sampling (Qwen: T 1.0, top-k 20, top-p 0.95; gpt-oss: T 1.0, top-p 1.0; GLM: the server's defaults; DeepSeek: T 1.0, top-p 0.95). Accuracy is within run-to-run noise between the engines in every row. The scripts are in [`bench\u002F`](bench\u002F).\n\nModels: Qwen3.8-Flash-Next [ISTA GSQ-RCO](https:\u002F\u002Fhuggingface.co\u002FISTA-DASLab\u002FQwen3.8-Flash-Next-GSQ-RCO-GGUF) IQ3_S (78 GiB) with the Q4_K MTP head above; gpt-oss-120b MXFP4 (`ggml-org`); Qwen3.6-35B-A3B UD-Q4_K_XL and GLM-4.5-Air Q4_K_M (unsloth); DeepSeek-V4.1-Flash-1017 [ISTA GSQ-RCO](https:\u002F\u002Fhuggingface.co\u002FISTA-DASLab\u002FDeepSeek-V4.1-Flash-1017-GSQ-RCO-GGUF) IQ2_XS (139 GiB of routed experts). We tuned for Qwen3.8 only.\n\n### Gaming PC: i5-14600KF (6P + 8E cores), DDR4-3200 (2 channels), Samsung PM9A1 (PCIe 4.0 NVMe)\n\nDeepSeek-V4.1-Flash-1017 IQ2_XS, no draft, greedy, the first 40 GSM8K questions (this build, 2026-11-11). At 64 GB the model is\nabout twice the RAM, so most experts are read from the SSD; at 128 GB it fits.\n\n| GPU, RAM | llama.cpp | tandem.cpp | | Strata v0.2.9 exact | time to first token (llama.cpp \u002F tandem.cpp \u002F Strata) |\n|---|---|---|---|---|---|\n| RTX 5070 Ti 16 GB, 64 GB | 7.4 | 14.2 \u002F 13.3 | 1.9× | 10.4 | 14.4 \u002F 6.3 \u002F 6.8 s |\n| RTX 5070 Ti + RTX 5060 Ti (32 GB, `-sm layer`), 64 GB | — | 16.9 | | — | — \u002F 4.3 \u002F — s |\n| RTX 5090 32 GB, 128 GB | 11.5 | 33.1 \u002F 32.2 | 2.9× | 28.0 | 3.4 \u002F 1.7 \u002F 4.0 s |\n\nAll cells answered 39–40 of 40 correctly. Two numbers for tandem.cpp are two runs (the ratio uses the first); the 32 GB row\nsplits the layers over both cards (the RTX 5060 Ti sits in an x4 slot).\n\n### DeepSeek-V4.1 Maya-XS: the same weights on both engines\n\n[Project Maya](https:\u002F\u002Fgithub.com\u002Fmw00\u002Fproject-maya) v1.1.64 (`d93d149`) is a MoE engine built for big models such as\nDeepSeek-V4.1, with its own quants. Its Maya-XS v2 GGUF (routed experts IQ1_S\u002FIQ2_XXS, 1.63 bpw, 117 GB; Engram tables Q6_K,\n161 GB) runs on tandem.cpp after `tools\u002Fmaya_ds41_convert.py` (it renames nothing in the model: it copies the three main\nshards with their split count patched and joins the two Engram tables into the one tensor llama.cpp reads). Same files,\nsame PC (i5-14600KF, 128 GB DDR4-3200, RTX 5090, GPU 0 only), cold, greedy, the first 16 GSM8K questions twice, then two\nprompts at 5.6k and 15.7k tokens (the same text for both):\n\n| | decode tok\u002Fs | GSM8K | first token, 5.6k \u002F 15.7k | decode after the long prompts |\n|---|---|---|---|---|\n| Maya, no drafting (as tandem.cpp) | 39.0 \u002F 39.6 | 16, 15 of 16 | 7.8 \u002F 9.5 s | 26–43 |\n| Maya with its DSpark drafter (its default here) | 54.9 | 15 of 16 | 7.9 \u002F 9.7 s | 28–38 |\n| **tandem.cpp v0.3.0** | **60.3 \u002F 59.4** | 15, 15 of 16 | **6.5** \u002F 16.5 s | **63–68** |\n\nMaya's own installer and settings (reasoning effort high), DSpark off for the first row. The whole model fits in this PC's\nVRAM + RAM, so neither engine reads the SSD after loading. Maya is still faster on the 15.7k-token prompt; overlapping the\nexpert uploads with the compute is next. To run it: download `peasantsmith\u002FDeepSeek-V4.1-Flash-1017-Maya-GGUF` (folder\n`Maya-XS\u002F`, MIT), then\n\n```\npython3 tools\u002Fmaya_ds41_convert.py --maya-dir models\u002FMaya-XS --out models\u002FMaya-XS-llama\nllama-server -m models\u002FMaya-XS-llama\u002FDeepSeek-V4.1-Flash-1017-Maya-XS-v2-IQ1_S-00001-of-00004.gguf \\\n  -ngl 99 -cmoe --moe-cache-mib auto -np 1 -c 32768\n```\n\nThe converted copy needs as much disk again as the download (278 GB). Maya-S has the same layout (not tested here).\n\n### Consumer PC: i5-12400F (6 cores), 64 GB DDR4 (43 GB\u002Fs), RTX 4090 24 GB, PCIe 4.0 x16\n\nThe models are larger than RAM with the OS, so both engines read them from the mapped file.\n\n| Model, setup | llama.cpp | tandem.cpp | | Strata exact |\n|---|---|---|---|---|\n| Qwen3.8 IQ3_S, MTP | 29.8 \u002F 29.4 | 97.3 \u002F 96.1 | 3.3× \u002F 3.3× | 103.6 \u002F 98.1 |\n| gpt-oss-120b, no draft | 27.8 \u002F 27.7 | 51.5 \u002F 48.0 | 1.85× \u002F 1.73× | |\n| gpt-oss-120b, EAGLE3 | 25.2 \u002F 24.2 | 41.1 \u002F 42.0 | 1.63× \u002F 1.74× | |\n| DeepSeek-V4.1 IQ2_XS (3× RAM, from the SSD), no draft, earlier build (2026-11-01) | 6.86 \u002F – | 5.99 \u002F – | 0.87× | 8.41 \u002F – |\n\n- Strata exact is 6% ahead greedy and 2% sampled on Qwen3.8; its default mode, which skips low-ranked experts, gives 127.1. tandem.cpp's equivalent (`LLAMA_MOE_TAIL_SKIP=7`, approximate, off by default) gives 123.6 \u002F 124.3.\n- On 6 cores EAGLE3 slows both engines down: verifying several tokens multiplies the experts the CPU must run.\n- DeepSeek-V4.1 is three times this PC's RAM. That row is from the earlier build, which was slower than llama.cpp when misses had to come from the SSD; the current build reads them ahead with several threads (see the gaming-PC rows). This PC's GPU has since changed, so the row was not re-measured.\n\n\nThe same PC with an RTX 5070 Ti 16 GB instead of the 4090, on an MSI M450 drive (DRAM-less, PCIe 4.0), models much larger than\nRAM, from a cold start (page cache dropped), greedy, two runs per engine in mirrored order (2026-11-13).\n\nQwen3.8-Flash-Next IQ3_S + MTP (78 GiB) at 32 GB of RAM (`mem=` boot), 8K context, the first 16 GSM8K questions:\n\n| | decode tok\u002Fs | read from the SSD | time to first token (median) |\n|---|---|---|---|\n| llama.cpp `-fit on`, the same MTP head | 14.9 \u002F 15.1 | 294 \u002F 290 GB | 5.1 \u002F 5.3 s |\n| tandem.cpp (defaults: store mode) | **34.3 \u002F 35.0** | 231 \u002F 232 GB | 2.9 \u002F 4.1 s |\n| Strata v0.2.9 exact, its own low-RAM setup and MTP settings | 30.3 \u002F 32.0 | 492 \u002F 418 GB | 4.3 \u002F 4.2 s |\n\nAll cells 15 of 16 correct. Strata's draft settings are its own (not the same speculative decoding as the other two rows).\n\nDeepSeek-V4.1-Flash IQ2_XS at 64 GB, no draft, the first 8 GSM8K questions: tandem.cpp\n**10.4 \u002F 10.2** tok\u002Fs (374 \u002F 385 GB read, first token 13.4 \u002F 14.4 s), Strata v0.2.9 exact 5.7 \u002F 5.7 (746 \u002F 708 GB, 15.0 \u002F 13.7 s):\n**1.8×**. All 8\u002F8 correct. On the gaming PC's faster drive above the gap is smaller (1.4×).\n\n### Older workstation: i9-10900X (10 cores), 8× DDR4-2666 (78 GB\u002Fs), RTX 4090 24 GB, PCIe 3.0 x16\n\n| Model, setup | llama.cpp | tandem.cpp | |\n|---|---|---|---|\n| Qwen3.8 IQ3_S, MTP | 31.1 \u002F 31.9 | 100.9 \u002F 100.9 | 3.2× \u002F 3.2× |\n| gpt-oss-120b, no draft | 31.2 \u002F 30.1 | 49.6 \u002F 49.6 | 1.59× \u002F 1.65× |\n| gpt-oss-120b, EAGLE3 | 30.3 \u002F 28.8 | 62.5 \u002F 61.0 | 2.06× \u002F 2.12× |\n\nDrafts do not speed up llama.cpp on this host (Qwen3.8 29.8 without MTP): most of its experts run from system RAM, so verifying several tokens costs nearly as much as it saves.\n\n### Workstation: Threadripper PRO 5955WX (16 cores), DDR4 (~80 GB\u002Fs read), RTX 6000 Ada 48 GB, PCIe 4.0 x16\n\n| Model, setup | llama.cpp | tandem.cpp | | Strata exact |\n|---|---|---|---|---|\n| Qwen3.8 IQ3_S, MTP, whole card | 98.6 \u002F 97.1 | 182.5 \u002F 180.9 | 1.85× \u002F 1.86× | 185.9 \u002F 169.9 |\n| Qwen3.8 IQ3_S, MTP, 12 GiB expert cache | | 131.1 \u002F 131.9 | | 129.2 \u002F 119.4 |\n| Qwen3.8 IQ3_S, MTP, 4 GiB expert cache | | 91.3 \u002F 89.5 | | 89.3 \u002F 83.7 |\n| gpt-oss-120b, whole card | 87.9 \u002F 86.3 | 95.3 \u002F 90.7 | 1.08× \u002F 1.05× | |\n| gpt-oss-120b, 24 GB budget | 47.1 \u002F 47.3 | 70.7 \u002F 68.5 | 1.50× \u002F 1.45× | |\n| gpt-oss-120b, EAGLE3, whole card | 88.7 \u002F 84.4 | 136.7 \u002F 126.8 | 1.54× \u002F 1.50× | |\n| Qwen3.6-35B-A3B (22.4 GB), fits | 173.1 \u002F 173.1 | 179.2 \u002F 179.2 | 1.04× \u002F 1.04× | |\n| Qwen3.6-35B-A3B, 16 GB budget | 117.8 \u002F 117.8 | 148.6 \u002F 148.6 | 1.26× \u002F 1.26× | |\n| Qwen3.6-35B-A3B, 12 GB budget | 97.2 \u002F 96.5 | 136.5 \u002F 135.4 | 1.40× \u002F 1.40× | |\n| Qwen3.6-35B-A3B, 8 GB budget | 79.8 \u002F 80.0 | 115.0 \u002F 114.5 | 1.44× \u002F 1.43× | |\n| GLM-4.5-Air (73 GB), whole card | 31.9 \u002F 31.9 | 54.6 \u002F 55.1 | 1.71× \u002F 1.73× | |\n| GLM-4.5-Air, 24 GB budget | 20.1 \u002F 20.0 | 34.7 \u002F 34.6 | 1.73× \u002F 1.73× | |\n\nThe cache-size rows compare against Strata at the same expert cache; a \"budget\" is the GPU memory both engines may use. Strata's cache rows are its exact mode as above.\n\nLong context: a 64k-token prompt of real text in a 72k context, two planted code words found in every cell, greedy. Prefill is the prompt's tokens over the time to its first token:\n\n| Model, setup | prefill llama.cpp | prefill tandem.cpp | | decode llama.cpp | decode tandem.cpp | |\n|---|---|---|---|---|---|---|\n| Qwen3.8 IQ3_S, MTP | 600 tok\u002Fs (109 s) | 860 tok\u002Fs (76 s) | 1.44× | 88.0 | 158.4 | 1.80× |\n| gpt-oss-120b, no draft | 660 tok\u002Fs (100 s) | 1150 tok\u002Fs (57 s) | 1.75× | 64.9 | 75.4 | 1.16× |\n| gpt-oss-120b, EAGLE3 | 560 tok\u002Fs (117 s) | 1030 tok\u002Fs (63 s) | 1.84× | 85.0 | 125.5 | 1.48× |\n\nOn the GSM8K questions (short prompts) the time to first token is 1.6–2.7× lower with tandem.cpp in the rows we checked, except the consumer-PC DeepSeek row (22 s against 12 s, the SSD issue below).\n\n### Server: EPYC 9534 (64 cores), DDR5 (~350 GB\u002Fs), RTX PRO 6000 Blackwell 96 GB, PCIe 5.0 x16\n\nDeepSeek-V4.1-Flash-1017 IQ2_XS (139 GiB of experts), whole card, reasoning on:\n\n| Setup | llama.cpp | tandem.cpp | | Strata v0.2.9 |\n|---|---|---|---|---|\n| no draft | 51.7 \u002F 46.0 | 89.6 \u002F 81.0 | 1.73× \u002F 1.76× | 84.4 \u002F 84.0 (6 experts, no DSpark) |\n| DSpark draft (3 tokens) | 63.0 \u002F – | 132.4 \u002F 107.2 | 2.10× | 148.9 \u002F – (its default: DSpark, up to 5 tokens) |\n\ntandem.cpp caches 87 GiB of the experts here (98.7% hit rate). Five draft tokens with a 0.65 confidence cut-off, Strata's default, was slower for tandem.cpp (122.3).\n\n### Quality\n\nQwen3.8 Q2_0, teacher-forced KL divergence of the next-token distribution against plain llama.cpp `f5222f25c` with the whole model on the GPU, 8-token batches:\n\n| Build | mean KLD | same top token | PPL ratio |\n|---|---|---|---|\n| tandem.cpp, `LLAMA_MOE_NO_DEFAULTS=1` | 0.000000 | 100% | 1.000 |\n| defaults, whole model on the GPU | 0.0091 | 96.1% | 0.999 ± 0.003 |\n| defaults, 4 GiB cache + CPU path | 0.0093 | 95.5% | 1.000 ± 0.003 |\n\nWith the defaults off the patch computes exactly what llama.cpp does; the defaults change only rounding (fused kernels, kernel choice), and the cache and CPU path add ≤ 0.0002.\n\n## Limits\n\n- Greedy output is not repeatable run to run with an expert cache: which experts are cached, and which misses the CPU computes, depends on timing, and the CPU and GPU round differently. Accuracy is unchanged. A fixed share (e.g. `LLAMA_MOE_CPU=0.2`) makes it repeatable.\n- Built for one user at a time. Concurrent requests work (tested at 2 and 4, accuracy unchanged) but gain less, and with a small cache several requests together can be slower in total than one.\n- Multi-GPU: only `-sm layer` (the default) works with the cache; checked for correctness on 2 GPUs, not tuned for speed.\n- Linux only. Tuned and tested on NVIDIA (CUDA). AMD (ROCm) works and gets the shared kernel work, but it has had less tuning and was tested on one RX 7900 XTX ([above](#amd-gpus-rocm)); it does not borrow cache VRAM for prompts (no VMM in ROCm builds), so its cache is a little smaller.\n- Qwen3.8 GGUFs converted with a current llama.cpp may be a little slower than the ISTA files we measured (one fusion does not apply to their layout).\n- Known issues, to be fixed:\n  - models much larger than RAM (read from the SSD) depend on the drive (see the gaming-PC rows for a PCIe 4.0 drive);\n  - DeepSeek-V4.1 with `-lm none` loads very slowly (its 103 GiB Engram tables are copied page by page); run it without `-lm none`;\n  - a model whose experts are a little larger than RAM (DeepSeek-V4.1 IQ2_XS, 139 GiB, on 128 GB) runs from the page cache, and\n    long prompts read the SSD again on every batch (15.7k tokens: ~57 s on the RTX 5090 PC; Maya-XS, which fits, 16.5 s).\n\n## Licence\n\nMIT, see [LICENSE](LICENSE). tandem.patch modifies llama.cpp, which is MIT-licensed by the ggml authors.\n","tandem.cpp 是一个针对 llama.cpp 的补丁集，专为加速显存（VRAM）不足时的 MoE（Mixture of Experts）大模型推理而设计。其核心是动态专家卸载策略：将高频使用的专家常驻 GPU，低频专家按需从 CPU 或 SSD 加载，显著提升单用户场景下的 token 生成速度（实测提速 1.4–3.3 倍）。项目不修改 llama.cpp 架构，兼容所有 llama.cpp 支持的 MoE 模型，适用于消费级显卡（如 RTX 4090\u002F5070 Ti）运行超大 MoE 模型（如 Qwen3.8、DeepSeek-V4.1）的本地部署场景。",2,"2026-11-24 04:48:55","CREATED_QUERY"]