# Running Qwen4-35B-A3B Locally at 256K Context: llama.cpp vs LiteRT-LM

> How to run Qwen4-35B-A3B locally with a 256K context window: exact install steps and CLI commands for llama.cpp and Google LiteRT-LM, plus realistic expectations for speed, memory usage, and disk footprint on Apple Silicon and NVIDIA hardware.

- URL: https://zeljic.com/blog/running-qwen4-35b-a3b-locally-llama-cpp-vs-litert-lm/
- Author: Đorđe Zeljić
- Published: 2026-12-04
- Tags: llm, ai-engineering, ai-agents, machine-learning, performance, architecture

Opening [`Qwen/Qwen4-35B-A3B`](https://huggingface.co/Qwen/Qwen4-35B-A3B) on Hugging Face greets you with two numbers that look like a typo: a model named `35B-A3B` carries a **`57B` parameter badge**, yet ships with Day-0 local builds claiming up to a **`1M`-token context window** on a laptop.

If your actual goal is pragmatic—**text-only inference**, a **`256K` (`262,144`-token) context window**, **maximum token-generation speed**, and **minimum disk footprint**—only two runtimes matter today: [`llama.cpp`](https://github.com/ggml-org/llama.cpp) and Google's [`LiteRT-LM`](https://ai.google.dev/edge/litert-lm). This article gives you the exact steps to get either one running, and honest expectations for what you will see: how fast tokens come out, how much RAM and disk each setup needs, and where the two differ.

## The Two Numbers That Explain Everything About Speed

Before commands, one 60-second primer. The `57B` checkpoint is not a dense model—it is a **sparse Mixture-of-Experts (`MoE`) model with `256` routed experts** (`moe_intermediate_size = 512`, `num_local_experts = 256` in [`config.json`](https://huggingface.co/Qwen/Qwen4-35B-A3B/blob/main/config.json)):

- **`35B` MoE backbone, `3B` active:** every token activates only **`8` routed experts + `1` shared expert**, so single-stream decode speed tracks a `3B`–`4B` dense model rather than a `35B` dense model.
- **`20B` 3-gram Per-Layer Embedding (`PLE` / engram) table:** a pure row lookup (`ngram_vocab_size_base = 10,000,000`, `ple_embed_dim = 2048`) that adds virtually zero compute per token and can be quantized aggressively or memory-mapped lazily from SSD.
- **`2B` Multi-Token Prediction (`MTP`) head:** a single full-attention draft layer (`mtp_num_hidden_layers = 1`) trained natively for self-speculative decoding—roughly **`1.3x` faster decode** at draft depth `2`, no separate draft model needed.

### Why 256K Context Does Not Blow Up Memory

Downstream from the MoE anatomy, Qwen4 uses a `3:1` hybrid attention schedule (`full_attention_interval = 4`):

- **`30` of `40` layers use linear attention (Gated DeltaNet):** a fixed-size recurrent state around `1.1 MB` per layer, **`~33 MB` total across all `30` layers**—identical whether your prompt is `512` tokens or `262,144` tokens.
- **Only `10` of `40` layers allocate a KV cache:** Qwen Sparse Attention with just **`2` KV heads** (`num_key_value_heads = 2`) of dimension `256`, plus a compact `indexer_budget = 2048` sparse indexer.

Multiplying `262,144 * 10 * 2 * 256 * 2` bytes for K and V gives the full KV budget at every context length:

| Context Length | FP16 KV Cache | `q8_0` KV Cache (`-ctk q8_0 -ctv q8_0`) |
| --- | ---: | ---: |
| `32,768` (`32K`) | `0.67 GB` | `0.34 GB` |
| `131,072` (`128K`) | `2.68 GB` | `1.34 GB` |
| **`262,144` (`256K`)** | **`5.37 GB`** | **`2.68 GB`** |
| `1,048,576` (`1M`) | `21.47 GB` | `10.74 GB` |

This is why `256K` is dramatically cheaper than on a dense `35B` transformer: after weights, live KV cache plus runtime buffers add **well under `8 GB`** at `256K`.

## What You Can Realistically Expect

Measured numbers on Apple Silicon come from [Unsloth's mainline `llama.cpp` benchmarks](https://unsloth.ai/docs/models/qwen4) on an **M5 Max** (`91.2 → 118.8 tok/s` with MTP at draft depth `2`); every other row below is that measurement scaled by memory bandwidth (`GB/s`), not a benchmark on those machines:

| Hardware | Bandwidth | Decode (no MTP) | Decode (MTP depth 2) |
| --- | ---: | ---: | ---: |
| M2 Pro | `200 GB/s` | `~30–40 tok/s` | `~40–55 tok/s` |
| **M2 Max (e.g. `96 GB` unified)** | **`400 GB/s`** | **`~60–70 tok/s`** | **`~80–95 tok/s`** |
| M3 Max | `400 GB/s` | `~60–70 tok/s` | `~80–95 tok/s` |
| M5 Max | `600 GB/s` | `91.2 tok/s` (measured) | `118.8 tok/s` (measured) |
| NVIDIA RTX 4090 | `~1 TB/s` | `~150–180 tok/s` | `~200+ tok/s` |

> **Label every number you measure.** The table above is a bandwidth-based estimate, not a benchmark on your machine. Record four things: model/tokenizer load time, prompt-processing (prefill) speed in `tok/s`, decode speed in `tok/s`, and peak resident memory (`/usr/bin/time -l` or `htop`).

Three expectations you can rely on before benchmarking:

- **Decode is memory-bandwidth-bound.** On a `3B`-active MoE at 4-bit quantization you are streaming `~1.7 GB` of weights per generated token; this is why the M5 Max numbers are only `~1.5x` the M2 Pro numbers—not `3x`, despite `3x` the bandwidth.
- **Quant scales decode roughly linearly with bytes-per-token.** On the same machine, `UD-Q8_K_XL` streams ~2x the bytes of `UD-IQ4_XS` and runs about half as fast; `UD-IQ3_XXS` is ~10–15% faster than `UD-IQ4_XS`. A `96 GB` M2 Max fits **every** quant from `UD-IQ1_S` (`21.3 GB`) to `UD-Q8_K_XL` (`62.29 GB`) in RAM at `256K` context—only the `112.81 GB` BF16 build overflows.
- **MTP (`~1.3x`) is the single biggest free speedup.** Draft depth `2` is the sweet spot (`0.89` acceptance at depth `1`, `0.68` at `2`); depth `3` and `4` regress on Apple Silicon.
- **Thinking mode is a feature, not overhead.** Qwen4-35B-A3B defaults to thinking (`high`), which is worth [`61.8` on DeepSWE v1.1](https://huggingface.co/Qwen/Qwen4-35B-A3B) for coding/agent tasks but emits reasoning tokens before the final answer. Turn it off (`--reasoning off` / `--thinking=false`) only when you want minimal wall-clock latency for plain chat.

## Path 1: `llama.cpp` (Maximum Text Speed at 256K Context)

Mainline support landed in [`ggml-org/llama.cpp#33626`](https://github.com/ggml-org/llama.cpp/pull/33626). Metal is enabled automatically on Apple Silicon; on Linux with NVIDIA GPUs, add `-DGGML_CUDA=ON`.

### 1. Pick the Quant That Fits Your Disk

From [`unsloth/Qwen4-35B-A3B-GGUF`](https://huggingface.co/unsloth/Qwen4-35B-A3B-GGUF), every quantization folder—from aggressive 1-bit `UD-IQ1_S` up to `UD-Q8_K_XL`—contains two shards: shard `00001` (MoE model + MTP head) and shard `00002` (the `IQ4_NL` engram table). The `11.52 GB` engram shard is byte-identical across every quant, so the real size difference between options is the model shard alone. **Text-only users skip `mmproj-*.gguf` entirely.**

| Quantization | Model + MTP | Engram | Total Disk | Best For |
| --- | ---: | ---: | --- | --- |
| `UD-IQ1_S` | `9.8 GB` | `11.52 GB` | `21.3 GB` | Absolute minimum disk; heavy quality loss (~1.6 bpw) |
| `UD-IQ2_XXS` | `11.9 GB` | `11.52 GB` | `23.4 GB` | Practical quality floor on a tight disk |
| `UD-IQ2_M` | `15.1 GB` | `11.52 GB` | `26.6 GB` | 2-bit quality/speed balance |
| `UD-IQ3_XXS` | `17.21 GB` | `11.52 GB` | `28.73 GB` | Sub-`30 GB` disk / `18–24 GB` RAM (SSD engram offload) |
| **`UD-IQ4_XS`** | **`20.52 GB`** | **`11.52 GB`** | **`32.04 GB`** | **Best speed-per-GB** |
| `UD-Q4_K_XL` | `24.72 GB` | `11.52 GB` | `36.24 GB` | Highest 4-bit quality on `64+ GB` machines |

If every gigabyte counts, note that `litert-lm`'s single prebuilt `.litertlm` file is `29.40 GB`—so every GGUF quant from `UD-IQ1_S` (`21.3 GB`) through `UD-IQ3_S` (`29.8 GB`) undercuts it, while `UD-IQ4_XS` and above exceed it. On a RAM-constrained machine, pair `UD-IQ1_S` with lazy SSD engram offload (`--load-mode mmap --lazy-mode on -ot per_layer_token_embd=CPU`) to run `256K` context in **`~13–15 GB` resident RAM**. One caveat: the 1-bit and 2-bit quants (`UD-IQ1_S` through `UD-IQ2_XS`) measurably degrade output quality on small-expert MoE models—use them only when disk or RAM is the hard constraint, and treat `UD-IQ4_XS` as the recommended default.

### 2. Build Recent `llama.cpp`

```bash
git clone https://github.com/ggml-org/llama.cpp
cmake llama.cpp -B llama.cpp/build -DBUILD_SHARED_LIBS=OFF
cmake --build llama.cpp/build --config Release -j --target llama-server llama-cli
```

### 3. Run at 256K Context with MTP Speculative Decoding

#### Option A: `64–96+ GB` Unified Memory (e.g. Mac M2/M3/M4/M5 Max)

Keep the engram in RAM, dedicate one slot (`-np 1`) so a single stream gets `100%` of memory bandwidth, and enable MTP at depth `2`:

```bash
./llama.cpp/build/bin/llama-server \
  -hf unsloth/Qwen4-35B-A3B-GGUF:UD-IQ4_XS \
  --spec-type draft-mtp --spec-draft-n-max 2 \
  --flash-attn on -ctk q8_0 -ctv q8_0 \
  -c 262144 -b 2048 -ub 512 -np 1 \
  --jinja \
  --temp 1.0 --top-p 0.95 --top-k 20 --min-p 0.0
```

Flag-by-flag:

- **`--spec-type draft-mtp --spec-draft-n-max 2`:** enables Qwen4's embedded 1-layer MTP head for self-speculative decoding—the `~1.3x` decode speedup with no extra model file.
- **`--flash-attn on -ctk q8_0 -ctv q8_0`:** cuts the `256K` KV cache from `5.37 GB` to **`2.68 GB`**.
- **`-c 262144 -b 2048 -ub 512 -np 1`:** allocates the full `256K` window for one slot, not divided across idle parallel slots.

**Expected footprint:** `~35–40 GB` resident RAM at `256K` (`32.04 GB` weights with `UD-IQ4_XS` or `36.24 GB` with `UD-Q4_K_XL`, plus `2.68 GB` KV and ~`1.5–3 GB` Metal buffers).

#### Option B: `24–36 GB` Laptops, `24 GB` Discrete GPUs (Lazy SSD Engram)

Keep the `20.52 GB` model resident and let `llama.cpp` read only the active n-gram rows of the `11.52 GB` engram from SSD on demand (`~24–26 GB` RAM at `256K`):

```bash
./llama.cpp/build/bin/llama-server \
  -hf unsloth/Qwen4-35B-A3B-GGUF:UD-IQ4_XS \
  --load-mode mmap --lazy-mode on -ot per_layer_token_embd=CPU \
  --spec-type draft-mtp --spec-draft-n-max 2 \
  --flash-attn on -ctk q8_0 -ctv q8_0 \
  -c 262144 -b 2048 -ub 512 -np 1 \
  --jinja \
  --temp 1.0 --top-p 0.95 --top-k 20 --min-p 0.0
```

On a discrete `16–24 GB` GPU where even routed experts spill to system RAM, add `--moe-cache-mib 4096` to cache hot experts in VRAM.

#### Fast Non-Thinking Mode (Lowest Wall-Clock Latency)

```bash
./llama.cpp/build/bin/llama-server \
  -hf unsloth/Qwen4-35B-A3B-GGUF:UD-IQ4_XS \
  --spec-type draft-mtp --spec-draft-n-max 2 \
  --flash-attn on -ctk q8_0 -ctv q8_0 \
  -c 262144 -b 2048 -ub 512 -np 1 \
  --jinja --reasoning off \
  --temp 0.7 --top-p 0.80 --top-k 20 --min-p 0.0 --presence-penalty 1.5
```

`llama-server` exposes an OpenAI-compatible endpoint on `http://127.0.0.1:8080/v1` by default.

## Path 2: Google `LiteRT-LM` (`.litertlm` Zero-Build Runtime)

Google's [`LiteRT-LM`](https://ai.google.dev/edge/litert-lm) ([GitHub](https://github.com/google-ai-edge/LiteRT-LM)) is the opposite engineering trade-off: zero CMake compilation, a single `29.40 GB` `.litertlm` artifact ([`litert-community/Qwen4-35B-A3B`](https://huggingface.co/litert-community/Qwen4-35B-A3B)), **`256K` (`262,144`) context enabled by default**, native Metal via [ML Drift](https://developers.googleblog.com/ml-drift-next-gen-gpu-aiml-inference-at-the-edge/) (`--backend=gpu`) on Apple Silicon, and TensorRT-RTX on Windows/Linux NVIDIA GPUs (`--backend=npu`). Two caveats for a text-only speed setup: the `29.40 GB` file permanently bundles the vision encoder (even if you never pass an image), and this `.litertlm` build has **no MTP drafter subgraph**, so the `~1.3x` speculative speedup is unavailable here.

### 1. Install `litert-lm-nightly` (Python `>= 3.11`)

Day-0 Qwen4 support lives in the nightly package rather than `litert-lm` `0.20.1`:

```bash
# macOS (Apple Silicon Metal) / Linux / Windows (WebGPU)
uv tool install litert-lm-nightly

# Linux or Windows with an NVIDIA RTX GPU (adds TensorRT-RTX --backend=npu)
uv tool install litert-lm-nightly --with ai-edge-litert-sdk-nvidia-nightly
```

### 2. Interactive Text-Only CLI at 256K Context

Lock the context budget explicitly with `--max-num-tokens=262144`, omit `--vision-backend` for text-only chat, and choose `--thinking=false` (fastest replies) or `--thinking=true --filter-channel-content-from-kv-cache=true` (strips prior ` thinking` blocks from the KV cache on later turns):

```bash
# Fastest text-only replies on Apple Silicon (Metal via ML Drift)
litert-lm run \
  --from-huggingface-repo=litert-community/Qwen4-35B-A3B \
  qwen4-35b-a3b.litertlm \
  --backend=gpu \
  --max-num-tokens=262144 \
  --thinking=false \
  --temperature=0.7 --top-p=0.80 --top-k=20
```

Disk-vs-startup controls in the [`litert-lm` CLI](https://ai.google.dev/edge/litert-lm/cli/configuration):

- **`--cache=disk` (default) vs `--cache=no`:** `disk` persists compiled GPU/Metal artifacts next to the model so the second launch skips shader/weight compilation; `--cache=no` avoids writing any compiled cache files if disk space is at a premium.
- **NVIDIA RTX note:** replace `--backend=gpu` with `--backend=npu` when running with `ai-edge-litert-sdk-nvidia-nightly`.

### 3. OpenAI-Compatible Local Server (`http://127.0.0.1:9379/v1`)

[`litert-lm serve`](https://ai.google.dev/edge/litert-lm/cli/openai_server) exposes `POST /v1/chat/completions` and `GET /v1/models`. Standard OpenAI payloads cannot carry LiteRT backend or thinking flags, so `serve` resolves them from `~/.litert-lm/config.json`. Import the model first (a `litert-lm import` uses a POSIX hard link, so it costs `0` extra bytes on the same APFS/ext4 drive), then configure and serve:

```bash
litert-lm import \
  --from-huggingface-repo=litert-community/Qwen4-35B-A3B \
  qwen4-35b-a3b.litertlm \
  qwen4-35b-a3b

mkdir -p ~/.litert-lm
cat <<'EOF' > ~/.litert-lm/config.json
{
  "default": {
    "backend": "gpu"
  },
  "models": {
    "qwen4-35b-a3b": {
      "backend": "gpu",
      "max_num_tokens": 262144,
      "thinking": false,
      "temperature": 0.7,
      "top_p": 0.8,
      "top_k": 20
    }
  }
}
EOF

litert-lm serve --host 127.0.0.1 --port 9379
```

```bash
curl http://127.0.0.1:9379/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "qwen4-35b-a3b",
    "stream": true,
    "messages": [
      {"role": "user", "content": "Explain why Qwen4 only allocates KV cache on 10 of its 40 layers."}
    ]
  }'
```

### 4. Measure Real Prefill and Decode Throughput

The [Hugging Face LiteRT-LM announcement post](https://huggingface.co/blog/litert-community/qwen4-in-litertlm) does not publish quantitative `tokens/s` numbers. Measure it on your own machine before committing to a workflow:

```bash
litert-lm benchmark qwen4-35b-a3b \
  --backend=gpu \
  --prefill-tokens=1024 \
  --decode-tokens=256 \
  --runs=3
```

## Side-by-Side: `llama.cpp` vs `LiteRT-LM` for 256K Text

| Dimension | `llama.cpp` (`UD-IQ4_XS`) | `LiteRT-LM` (`qwen4-35b-a3b.litertlm`) |
| --- | --- | --- |
| **Disk (text-only)** | **`32.04 GB`** (`UD-IQ4_XS`; minimum `21.3 GB` with 1-bit `UD-IQ1_S`) | **`29.40 GB`** single file (vision subgraphs bundled) |
| **Install & setup friction** | Compile `llama.cpp` (`cmake --build`) or use prebuilt binaries | One command: `uv tool install litert-lm-nightly` |
| **MTP speculative decoding (`~1.3x` decode)** | **Yes** (`--spec-type draft-mtp --spec-draft-n-max 2`) | **No** (no drafter subgraph in current `.litertlm` bundle) |
| **KV cache quant at `256K`** | **Yes** (`-ctk q8_0 -ctv q8_0` → `2.68 GB`) | `DEFAULT` (`INT8` KV cache currently limited to Artisan GPU models) |
| **Lazy SSD engram offload** | **Yes** (`--load-mode mmap --lazy-mode on -ot per_layer_token_embd=CPU`) | Managed internally by LiteRT runtime |
| **OpenAI-compatible server** | `llama-server` (`:8080/v1`) | `litert-lm serve` (`:9379/v1` + `~/.litert-lm/config.json`) |
| **Measured tok/s reference** | [Unsloth M5 Max: `91.2 → 118.8 tok/s`](https://unsloth.ai/docs/models/qwen4) | None published yet; measure with `litert-lm benchmark` |

For **pure text speed at `256K` context**, start with **`llama.cpp` + `UD-IQ4_XS` (`32.04 GB`)** with `--spec-type draft-mtp --spec-draft-n-max 2` and `q8_0` KV caching: MTP speculative decoding (`~1.3x` decode) and quantized KV caching give it a clear throughput and memory-headroom lead. Reach for **`LiteRT-LM`** when you want a zero-compile Python CLI/server, multimodal image input in the same `29.40 GB` file, a single model artifact you can ship unchanged across macOS Metal, Linux/Windows WebGPU, NVIDIA TensorRT-RTX, and mobile/web targets, or compiled-artifact caching for fast warm startups.

## References

- **Hugging Face — Base Model & Architecture:** [`Qwen/Qwen4-35B-A3B` Model Card & `config.json`](https://huggingface.co/Qwen/Qwen4-35B-A3B)
- **Hugging Face — LiteRT-LM Model Artifact:** [`litert-community/Qwen4-35B-A3B` (`qwen4-35b-a3b.litertlm`)](https://huggingface.co/litert-community/Qwen4-35B-A3B)
- **Hugging Face & Google AI Edge Blog:** [Bringing Qwen 4 to Your Laptop: 1M Context Local Agentic Workflows with LiteRT-LM](https://huggingface.co/blog/litert-community/qwen4-in-litertlm)
- **Hugging Face — Quantized GGUF Shards & MTP:** [`unsloth/Qwen4-35B-A3B-GGUF`](https://huggingface.co/unsloth/Qwen4-35B-A3B-GGUF) and [Unsloth Qwen4 Local Guide](https://unsloth.ai/docs/models/qwen4)
- **Google for Developers — LiteRT-LM Documentation:** [LiteRT-LM Overview](https://ai.google.dev/edge/litert-lm), [CLI Usage](https://ai.google.dev/edge/litert-lm/cli/usage), [CLI Configuration (`config.json`)](https://ai.google.dev/edge/litert-lm/cli/configuration), [OpenAI-Compatible Server](https://ai.google.dev/edge/litert-lm/cli/openai_server), and [`.litertlm` File Builder](https://ai.google.dev/edge/litert-lm/file_builder)
- **Google Developers Blog:** [ML Drift: Next-Gen GPU AI/ML Inference at the Edge](https://developers.googleblog.com/ml-drift-next-gen-gpu-aiml-inference-at-the-edge/)
- **Source Repositories:** [`google-ai-edge/LiteRT-LM`](https://github.com/google-ai-edge/LiteRT-LM) and [`ggml-org/llama.cpp`](https://github.com/ggml-org/llama.cpp)
