# Qwen4-35B-A3B with experts in RAM, CPU-side cache misses and the built-in MTP drafter

Qwen's 35B-A3B hybrid MoE (Unsloth UD-Q4_K_M, 23.7 GB plus an 11.5 GB n-gram table) on stock llama.cpp at 131k: experts in RAM, a 3800 MiB MoE cache with misses computed on the CPU, q8_0 KV and the built-in MTP drafter. 61.4 tok/s decode (stock 37.2), 54.9 at 32k, needles exact at 124.5k.

- Recipe id: `qwen4-35b-a3b`  ·  status: measured  ·  updated 2026-12-04
- Model: [Qwen4-35B-A3B](https://huggingface.co/Qwen/Qwen4-35B-A3B) (35B-A3B MoE hybrid (30 Gated DeltaNet + 10 Qwen Sparse Attention blocks, 256 experts top-8 + shared, n-gram embedding table, MTP head))
- Weights: GGUF UD-Q4_K_M (imatrix), 35.18 GB, from https://huggingface.co/unsloth/Qwen4-35B-A3B-GGUF, file `Qwen4-35B-A3B-UD-Q4_K_M-00001-of-00002.gguf`, revision `3ef64303abd0e81681d257437fc35d1ab0d49519`
- Engine: [llama.cpp](https://github.com/ggml-org/llama.cpp) master at 59b716523; CUDA sm_86 build
- Hardware: NVIDIA GeForce RTX 3060 12 GB, 62 GB RAM, Intel Core i5-12500, NVMe SSD
- Context: 131,072 tokens
- Placement: Every routed expert in RAM with llama.cpp's MoE cache holding hot experts in 3800 MiB of VRAM and 95% of cache misses computed on the CPU; attention, DeltaNet, shared experts, LM head, the q8_0 KV cache (10 sparse-attention blocks) and the MTP context on the GPU; the n-gram table read from the NVMe on demand.
- Contributor: [David Hatley](https://github.com/davidghatley)

## Run it

From `qwen4-35b-a3b/` in a clone of https://github.com/davidghatley/3060-recipes:

```sh
./serve.sh
```

API: OpenAI-compatible /v1 (llama-server) on port 8112

Key flags: `QW_PROFILE=default|nospec|stock`, `--cpu-moe --moe-cache-mib 3800 --moe-cache-cpu-share 0.95`, `--spec-type draft-mtp --spec-draft-n-max 2 --spec-draft-n-ubatch 64`, `-ot per_layer_token_embd=CPU --load-mode mmap --lazy-mode on`, `-ctk q8_0 -ctv q8_0 -ub 1024 -c 131072`, `chat_template_kwargs enable_thinking false per request`, `QW_OMP_PASSIVE=1 (OpenMP threads sleep: about half the CPU time, 4-9% less decode)`

The script itself (`qwen4-35b-a3b/serve.sh`, machine paths shown as `$MODELS`, `$SCRATCH`, `~`; set them to where your weights and builds live):

```sh
#!/usr/bin/env bash
# Qwen4-35B-A3B (35B MoE, 3B active: 30 Gated DeltaNet + 10 Qwen Sparse Attention layers, a 20B-value n-gram table,
# built-in MTP head, 1M native window) as Unsloth's UD-Q4_K_M GGUF on stock llama.cpp master. Routed experts in RAM,
# hot ones cached on the card, the n-gram table read from the NVMe on demand. See PLAN.md / RESULTS.md for how each
# default was chosen. Every knob has a QW_* env override; extra args go to llama-server. The server runs inside
# gpu_run and holds the shared GPU lock for its whole life.
#   QW_PROFILE   default  (PLAN rows 4, 6, 11): every routed expert in RAM, 3800 MiB MoE cache with 95% of the misses
#                         computed on the CPU, q8_0 KV, the file's MTP head as drafter (2 tokens), ub 1024, 131k window
#                nospec   (row 6): no drafter, cache 4300: +34% prefill, -19% decode against the default
#                stock    Unsloth's guide line: --load-mode mmap --lazy-mode on -ot per_layer_token_embd=CPU and the
#                         card's samplers; llama.cpp's own --fit picks the split and the window (the baseline)
#   QW_PLACEMENT cpu   (every routed expert in RAM + the MoE cache)
#                ncmoe (experts of the first QW_NCMOE layers in RAM, the rest fixed on the GPU)
#                fit   (stock: no -ngl, --fit on picks the split)
#   QW_OT        extra -ot pattern (appended to per_layer_token_embd=CPU)
#   QW_MOE_CACHE_MIB / QW_MOE_CACHE_SHARE   llama.cpp's MoE cache of hot RAM-resident experts (0 = off)
#   QW_SPEC      none | mtp (the file's own MTP head);  QW_DRAFT_N (draft length), QW_DRAFT_SAMPLING greedy|probabilistic, QW_DRAFT_UB (MTP context ubatch, default 64)
#   QW_BS=1      backend sampling (-bs)
#   QW_CONTEXT QW_KV QW_UBATCH QW_BATCH QW_THREADS QW_LOAD_MODE QW_LAZY QW_OP_OFFLOAD(0|1)
#   QW_THINK=0   thinking off by default (requests can still send chat_template_kwargs); QW_EFFORT default effort level
#   QW_MMPROJ=1  load the vision projector (mmproj-F16)
#   QW_OMP_PASSIVE=1  OpenMP worker threads sleep instead of spinning (OMP_WAIT_POLICY=passive): about half the server
#                CPU time for 4-9% less decode (PLAN rows 14, 15; lead from Rui Carmo). Off by default
#   QW_MODEL QW_PLE (another n-gram table file) QW_BIN QW_PORT QW_NEED QW_WAIT QW_NO_GPU_RUN=1 (already inside gpu_run)
set -euo pipefail
BIN=${QW_BIN:-$SCRATCH/llama.cpp/master-59b716523/bin}   # ggml-org/llama.cpp master @ 59b716523, no patches
D=$MODELS/qwen4-35b-a3b/unsloth
MODEL=${QW_MODEL:-$D/Qwen4-35B-A3B-UD-Q4_K_M-00001-of-00002.gguf}   # the table is file 00002, found next to it
export LD_LIBRARY_PATH="$SCRATCH/envs/llama-build-cuda/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
# experts are read from RAM (page cache): gpu_run's memcap counts page cache, 20 GB would bind
export GPU_RUN_MEM=${GPU_RUN_MEM:-44G}
[ "${QW_OMP_PASSIVE:-0}" = 1 ] && export OMP_WAIT_POLICY=passive

# p_place p_cache p_kv p_ub p_spec p_n p_ctx
case "${QW_PROFILE:-default}" in
  default) p_place=cpu; p_cache=3800; p_kv=q8_0; p_ub=1024; p_spec=mtp;  p_n=2; p_ctx=131072 ;;
  nospec)  p_place=cpu; p_cache=4300; p_kv=q8_0; p_ub=1024; p_spec=none; p_n=2; p_ctx=131072 ;;
  stock)   p_place=fit; p_cache=0;    p_kv=;    p_ub=;     p_spec=none; p_n=2; p_ctx= ;;
  *) echo "QW_PROFILE must be default, nospec or stock" >&2; exit 2 ;;
esac
PLACEMENT=${QW_PLACEMENT:-$p_place}
OT="per_layer_token_embd=CPU${QW_OT:+,$QW_OT}"
case "$PLACEMENT" in
  ncmoe) PLACE=(-ngl 99 --n-cpu-moe "${QW_NCMOE:-30}" --fit off) ;;
  cpu)   PLACE=(-ngl 99 --cpu-moe --fit off) ;;
  fit)   PLACE=() ;;
  *) echo "QW_PLACEMENT must be ncmoe, cpu or fit" >&2; exit 2 ;;
esac

ARGS=(--load-mode "${QW_LOAD_MODE:-mmap}" --lazy-mode "${QW_LAZY:-on}")
# QW_TABLE_OT=0 drops the -ot for the table (any -ot switches llama.cpp's --fit off)
[ "${QW_TABLE_OT:-1}" = 1 ] && ARGS+=(-ot "$OT")
CACHE=${QW_MOE_CACHE_MIB-$p_cache}; [ "$PLACEMENT" = cpu ] || CACHE=${QW_MOE_CACHE_MIB:-0}
[ "$CACHE" != 0 ] && ARGS+=(--moe-cache-mib "$CACHE")
[ "$CACHE" != 0 ] && ARGS+=(--moe-cache-cpu-share "${QW_MOE_CACHE_SHARE:-0.95}")
[ "${QW_OP_OFFLOAD:-1}" = 0 ] && ARGS+=(--no-op-offload)
KV=${QW_KV-$p_kv}; [ -n "$KV" ] && ARGS+=(-ctk "$KV" -ctv "$KV")
UB=${QW_UBATCH-$p_ub}; [ -n "$UB" ] && ARGS+=(-ub "$UB")
[ -n "${QW_BATCH:-}" ] && ARGS+=(-b "$QW_BATCH")
[ -n "${QW_THREADS:-}" ] && ARGS+=(-t "$QW_THREADS")
[ "${QW_BS:-0}" = 1 ] && ARGS+=(-bs)
CTX=${QW_CONTEXT-$p_ctx}; [ -n "$CTX" ] && ARGS+=(-c "$CTX")
[ -n "${QW_PLE:-}" ] && ARGS+=(--ple "$QW_PLE")
[ "${QW_MMPROJ:-0}" = 1 ] && ARGS+=(--mmproj "$D/mmproj-F16.gguf")
case "${QW_SPEC:-$p_spec}" in
  none) ;;
  mtp) ARGS+=(--spec-type draft-mtp --spec-draft-n-max "${QW_DRAFT_N:-$p_n}" --spec-draft-n-ubatch "${QW_DRAFT_UB:-64}")
       [ -n "${QW_DRAFT_SAMPLING:-}" ] && ARGS+=(--spec-draft-sampling "$QW_DRAFT_SAMPLING") ;;
  *) echo "QW_SPEC must be none or mtp" >&2; exit 2 ;;
esac
[ -n "${QW_EFFORT:-}" ] && ARGS+=(--reasoning-effort "$QW_EFFORT")
[ "${QW_PROFILE:-default}" = stock ] || ARGS+=(-fa on)
[ "${QW_THINK:-1}" = 0 ] && ARGS+=(--reasoning off)

RUN=("$HOME/anime-pipeline/tools/gpu_run" --need "${QW_NEED:-9800}" --wait "${QW_WAIT:-7200}")
[ -n "${QW_NO_GPU_RUN:-}" ] && RUN=()
exec "${RUN[@]}" \
  "$BIN/llama-server" -m "$MODEL" ${PLACE[@]+"${PLACE[@]}"} --alias qwen4-35b-a3b -np 1 --jinja \
  --temp 1.0 --top-p 0.95 --top-k 20 --min-p 0.0 \
  --host 127.0.0.1 --port "${QW_PORT:-8112}" "${ARGS[@]}" "$@"
```

## Measured results

| Metric | Conditions | Value | Stock | Ceiling | Source |
|---|---|---|---|---|---|
| decode | default (MTP n 2, q8_0 KV, 3800 MiB MoE cache), empty context, thinking off, coding answers, median of three runs | 61.4 tok/s | 37.2 | 120 | [qwen4-35b-a3b/RESULTS.md:32](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L32) |
| decode | default at 32k depth | 54.9 tok/s | 35.7 |  | [qwen4-35b-a3b/RESULTS.md:33](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L33) |
| decode | default at 120k depth | 42.5 tok/s |  |  | [qwen4-35b-a3b/RESULTS.md:34](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L34) |
| decode | nospec profile, empty context | 49.9 tok/s |  |  | [qwen4-35b-a3b/RESULTS.md:32](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L32) |
| prefill | 8k prompt, default | 458.8 tok/s | 376.3 |  | [qwen4-35b-a3b/RESULTS.md:35](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L35) |
| prefill | 8k prompt, nospec profile | 617.0 tok/s | 376.3 |  | [qwen4-35b-a3b/RESULTS.md:35](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L35) |
| prefill | 32k prompt, default | 430.2 tok/s | 375.8 |  | [qwen4-35b-a3b/RESULTS.md:36](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L36) |
| prefill | 124.5k prompt, default | 431.7 tok/s |  |  | [qwen4-35b-a3b/RESULTS.md:37](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L37) |
| ttft | 124.5k prompt, default | 288 s |  |  | [qwen4-35b-a3b/RESULTS.md:37](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L37) |
| needles | 3/3 exact at 64.4k and 124.5k (49% / 95% of the window) | 3/3 found |  |  | [qwen4-35b-a3b/RESULTS.md:38](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L38) |
| quality | codeeval, thinking on (effort low) | 8/8 tasks |  |  | [qwen4-35b-a3b/RESULTS.md:74](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L74) |
| quality | codeeval, thinking off | 7/8 tasks |  |  | [qwen4-35b-a3b/RESULTS.md:75](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L75) |
| vram_peak | card total incl. desktop, default | 11774 MiB |  |  | [qwen4-35b-a3b/RESULTS.md:39](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L39) |
| ram_peak | RSS, default | 29374 MiB |  |  | [qwen4-35b-a3b/RESULTS.md:40](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L40) |
| startup | ready, warm page cache, default | 2-3 s |  |  | [qwen4-35b-a3b/RESULTS.md:41](https://github.com/davidghatley/3060-recipes/blob/main/qwen4-35b-a3b/RESULTS.md#L41) |

## Caveats

- The native 1M window does not fit: KV is 13.6 KB per token at q8_0. This recipe serves 131k.
- Unsloth's guide command does not load on 12 GB: its -ot switches --fit off and every layer goes to the card.
- The MTP drafter costs 25% of prefill; QW_PROFILE=nospec serves without it.
- Quant drift is not measured: no Q8_0 or BF16 reference fits on disk. Vision and tool calls are not measured.
- reasoning_effort off in chat_template_kwargs fails with a template error on this build; enable_thinking false works.
- Decode on the default read 59.4-63.3 tok/s over three runs; 61.4 is the median.

## Credits

- Qwen: The 15 mtp.* tensors grafted back into the 3D coder came from Qwen3.5-4B; Kintsugi-27B is built on Qwen3.8-27B. (https://huggingface.co/Qwen/Qwen3.5-4B)
- Unsloth: The Mainline_Support files are the max-speed options of the Inkling recipe (41.4 tok/s whole-on-GPU); UD-Q3_K_XL was the smaller-expert test on Flint. (https://huggingface.co/unsloth/Inkling-2-Mini-GGUF)
- llama.cpp (ggml-org and contributors): The engine for Flint, the 3D coder and Inkling-2-Mini, and the base of the Kintsugi fork. (https://github.com/ggml-org/llama.cpp)
- ggml-org: Flint-Q4_K_M.gguf and Inkling-2-Mini-IQ4_NL.gguf are the files the two recipes serve; the Q8_0 files are the KLD references. (https://huggingface.co/ggml-org/Flint-GGUF)
- Hugging Face: Every model, quant and drafter in these recipes is fetched from the Hub at a pinned revision and checked against the API's sha256; the base model's MTP tensors were fetched with HTTP range requests. (https://huggingface.co)
- 0xSero / Sybil Solutions: The tuning method behind the Flint and Inkling recipes; its q8_0 KV gotcha is one we confirmed on Flint. (https://github.com/sybil-solutions/inference-optimisation-skill)
- Rui Carmo: The lead for the QW_OMP_PASSIVE switch: measured here as about half the server CPU time for 4–9% less decode. (https://github.com/rcarmo)

## Reproductions

None yet. Ran it on your own 3060? Report your numbers: https://github.com/davidghatley/3060-recipes/issues/new?template=reproduction.yml

## Machine-readable

- https://davidghatley.github.io/3060-recipes/recipes/qwen4-35b-a3b/recipe.json
- Folder with scripts and raw runs: https://github.com/davidghatley/3060-recipes/tree/main/qwen4-35b-a3b
- Full results log: https://raw.githubusercontent.com/davidghatley/3060-recipes/main/qwen4-35b-a3b/RESULTS.md

## Full write-up (README)

Serving [Qwen/Qwen4-35B-A3B](https://huggingface.co/Qwen/Qwen4-35B-A3B) on the RTX 3060 12 GB with stock llama.cpp.
It is a 35B MoE with 3B active per token (256 routed experts, 8 active, plus one shared expert) in 40 blocks: 30
Gated DeltaNet blocks (linear attention with a fixed-size state) and 10 Qwen Sparse Attention blocks. It also carries
a 20.5B-value n-gram embedding table, a one-block MTP head for speculative decoding and a vision encoder, and its
native window is 1,048,576 tokens. The weights are Unsloth's UD-Q4_K_M GGUF: 23.7 GB of model plus an 11.5 GB table file.

The 23.7 GB file does not fit in 12 GB, so experts live in RAM. This recipe is about four things:
- **the guide's command does not load here.** Its `-ot per_layer_token_embd=CPU` switches llama.cpp's `--fit` off,
  and every layer then goes to the card. Without it `--fit` works but picks a 4096-token window.
- **where the experts go.** Every routed expert stays in RAM and 3.8 GB of the card holds llama.cpp's MoE cache of
  the hot ones.
- **what happens on a cache miss.** By default llama.cpp uploads the missed expert to the card. Computing 95% of
  the misses on the CPU instead (`--moe-cache-cpu-share 0.95`) is +24% decode.
- **the built-in MTP drafter.** Two draft tokens are +27% decode. It needs its own context on the card and costs
  25% of prefill, so there is a profile without it.

Numbers and quality are in [RESULTS.md](RESULTS.md). The experiment log is [PLAN.md](PLAN.md), the ceilings are in
[THEORY.md](THEORY.md), and the raw data is in `runs/`.

### Result

| Single user, 131k window | Stock llama.cpp `--fit` | **This recipe** |
|---|---:|---:|
| Decode, coding answer | 37.2 tok/s | **61.4 tok/s (1.65×)** |
| Decode at 32k context | 35.7 tok/s | **54.9 tok/s (1.54×)** |
| Decode at 120k context | not measured | **42.5 tok/s** |
| Prefill, 8k prompt | 376 tok/s (21 s to first token) | **459 tok/s (17.5 s, 1.22×)**; 617 with `QW_PROFILE=nospec` |
| Prefill, 32k / 124.5k prompt | 376 tok/s / not measured | **430 / 432 tok/s** (75 s / 4.8 min) |
| Needles at 64.4k / 124.5k (49% / 95% of the window) | not run | **3/3 / 3/3** |
| codeeval, thinking on / off | | **8/8** / 7/8 |
| Card / RAM | 11.05 GB / 24.8 GB | 11.8 GB / 29.4 GB |

- 61.4 tok/s is the median of three runs of the default (59.4, 61.4, 63.3).
- **The 1M window does not fit on this card.** KV is 13.6 KB per token at q8_0, so the full window needs 13.6 GB.
  This recipe serves 131,072 tokens.
- Speed holds at depth: prefill is 430–441 tok/s from 32k to 124.5k, because the sparse attention reads 2048
  selected tokens however long the prompt is.
- The card was shared with a desktop holding 1.9 GB. A card with less on it has room for a larger cache.
- Quant drift against a higher-precision file, vision and tool calls are not measured.

### Which config

| `QW_PROFILE=` | KV | MoE cache | ubatch | Drafter | Decode empty / at 32k | Prefill 8k / 32k | Use |
|---|---|---:|---:|---|---:|---:|---|
| **`default`** | q8_0 | 3800 MiB | 1024 | MTP, 2 tokens | 61.4 / 54.9 | 459 / 430 | everything |
| `nospec` | q8_0 | 4300 MiB | 1024 | none | 49.9 / not measured | 617 / 592 | long prompts with short answers |
| `stock` | f16 | none | 512 | none | 37.2 / 35.7 | 376 / 376 | the baseline (run with `QW_TABLE_OT=0 QW_CONTEXT=131072`) |

### Use it

```sh
scripts/hf_get.sh notes/qwen4-35b-a3b-weights.tsv   # from the repo root: both UD-Q4_K_M files and the mmproj, pinned and sha256-checked
./serve.sh                                          # OpenAI-compatible API on 127.0.0.1:8112
QW_PROFILE=nospec ./serve.sh                        # no drafter: faster prefill
QW_MMPROJ=1 ./serve.sh                              # with the vision projector (not measured)
QW_OMP_PASSIVE=1 ./serve.sh                         # OpenMP threads sleep: about half the CPU time, 4–9% less decode
```

The flags the default passes to `llama-server`:

```
-ngl 99 --cpu-moe --fit off -ot per_layer_token_embd=CPU --load-mode mmap --lazy-mode on
--moe-cache-mib 3800 --moe-cache-cpu-share 0.95 -ctk q8_0 -ctv q8_0 -ub 1024 -c 131072 -fa on
--spec-type draft-mtp --spec-draft-n-max 2 --spec-draft-n-ubatch 64
--jinja --temp 1.0 --top-p 0.95 --top-k 20 --min-p 0.0
```

- Engine: ggml-org/llama.cpp master at 59b716523 or later (Qwen4 support is PR #33626). No patches.
- `--lazy-mode on` reads the n-gram table from the NVMe on demand, so the 11.5 GB file does not have to sit in RAM.
- `--spec-draft-n-ubatch 64` is required here: at the main ubatch of 1024 the MTP context asks for a 571 MiB compute buffer and the server fails to start.
- Thinking is on by default (the card's sampler settings for thinking mode are the defaults here). To switch it off
  for a request send `"chat_template_kwargs": {"enable_thinking": false}`. `"reasoning_effort": "off"` in the same
  field fails with a template error on this build; `low`, `high` and `xhigh` work.
- The server holds the shared GPU lock for its whole life and needs `GPU_RUN_MEM=44G` (set by `serve.sh`): the
  experts are read from page cache, which the memory cap counts.

### Tools

- `tools/run.sh <label> <mode>`: one server start and one test mode (`speed`, `deep`, `pp`, `needles`, `code`, `codeoff`).
- `tools/batch.sh <script>`: several runs inside one GPU-lock hold; the batches that produced the numbers are in `tools/batches/`.
- `tools/srvtest.py`: prefill, decode and needle measurements from llama-server's own timings.
- `tools/gguf_info.py`: the header and per-tensor byte table in `runs/gguf-info-model.txt`.

### Credits

- Model: [Qwen team, Alibaba](https://huggingface.co/Qwen) (Apache-2.0).
- Weights: [Unsloth](https://huggingface.co/unsloth/Qwen4-35B-A3B-GGUF) Dynamic GGUFs, the split n-gram table files and the [run guide](https://unsloth.ai/docs/models/qwen4).
- Engine: [llama.cpp](https://github.com/ggml-org/llama.cpp) (Qwen4 support #33626, external table loading #31185, the MoE cache and `--moe-cache-cpu-share`).
- [Rui Carmo](https://github.com/rcarmo) ran the recipe on his own 3060 and found the OpenMP spinning cost; `QW_OMP_PASSIVE=1` is the result (58.3 tok/s against 61.2–64.4, server CPU time 120 s against 208–261 s over a test run).
- Method: [inference-optimisation-skill](https://github.com/sybil-solutions/inference-optimisation-skill).
