← Blog · · 10 min read
Qwen3.8 27B on an M3 Max: speed from 512 to 16k tokens, MLX vs ollama, dense vs MoE
On this page (7 sections)
TL;DR
- On our 64 GB M3 Max, the time to first token (TTFT) for Qwen3.8-27B (dense, 4-bit, MLX) went from 3.29 s at a 512-token prompt to 116.3 s at 16,384 tokens. That is 35.35x longer for a prompt that is 32x longer. Decode speed dropped less, from 14.8 to 11.9 tok/s (about 19%).
- For the same model, MLX decoded 1.76x faster than ollama at 512 tokens and 1.61x faster at 16k. Prefill (TTFT) was close: ollama was 1.17x slower at 512 tokens and 0.95x of MLX's time at 16k. At 16k the run-to-run spreads of the two backends overlap.
- The 3B-active MoE (Qwen3.6-35B-A3B, MLX) kept its lead at every length. At 16k its TTFT was 14.7 s against 116.3 s for the dense model, and it decoded at 81.9 tok/s against 11.9.
- Decode slowed by 11–19% from 512 to 16k in all four configs. At long prompts, prefill dominates the time to a 256-token answer: at 16k it was 136.8 s for dense MLX and 17.8 s for the MoE.
The question
Most local-LLM hardware discussions report one number: decode tokens per second on a short prompt. Coding agents and RAG pipelines send 4k–16k-token prompts. At those lengths the model has to read the whole prompt (prefill) before it writes anything, and each decoded token attends over a larger cache.
We already had the same Qwen3.8-27B model on disk as an MLX 4-bit file and as a GGUF. So we measured three things on one machine:
- How much TTFT and decode speed change as the prompt grows from 512 to 16,384 tokens.
- Whether MLX or ollama handles long context better for the same model.
- Whether a sparse MoE (Qwen3.6-35B-A3B, about 3B active parameters) keeps its speed lead once the prompt is long. We also added LFM2.5-24B-A2B as a second MoE data point.
Setup
Machine: one M3 Max laptop with 64 GB, on AC power, running macOS 27.0.1 (build 26A434). The kernel string (T6031) confirms the M3 Max. sysctl was unavailable to our logging script, so memory size and GPU core count were not recorded in the logs. pmset -g therm reported no thermal or performance warnings before or after any config. pmset reported powermode 0, which is Automatic (1 is Low Power, 2 is High Power). High Power mode was therefore off for this run.
Software: Python 3.13.5, mlx 0.31.2, mlx-lm 0.31.3, transformers 5.12.1 (tokenizers only), numpy 2.4.2, ollama 0.40.5. The server log shows that ollama launched its bundled llama-server binary with these flags: -c 20480 --cache-type-k f16 --cache-type-v f16 --flash-attn on -b 1024 -ub 1024 --context-shift --keep 4, with 66/66 layers offloaded to the GPU. llama.cpp then disabled context shifting for this model. We did not record the llama.cpp commit.
| Config | Backend | Model | File | Size | bpw |
|---|---|---|---|---|---|
| A | mlx_lm | Qwen3.8-27B dense | mlx-community 4-bit, group size 64 | 16.06 GB | 4.50 (text weights) |
| B | ollama (llama-server) | Qwen3.8-27B dense | unsloth UD-Q4_K_XL GGUF | 17.56 GB | 5.14 |
| C | mlx_lm | Qwen3.6-35B-A3B MoE | mlx-community 4-bit | 20.40 GB | 4.50 (text weights) |
| D | mlx_lm | LFM2.5-24B-A2B MoE | LiquidAI MLX 4-bit | 13.57 GB | 4.53 |
The only GGUF we had locally was UD-Q4_K_XL, not Q4_K_M. A vs B therefore compares the closest available 4-bit files, not two engines on identical weights.
Prompts: the source of click 8.1.8 wrapped in each model's chat template, with thinking disabled. LFM2.5 (config D) still emitted reasoning text in some runs; see the caveats. Each prompt ends with "Summarize the above in 5 bullet points." It is cut so the full templated prompt is exactly 512, 4096 or 16384 tokens for that model's tokenizer, and the script asserts this. Every request starts with a unique, sequential 4-digit request number ("Request 0001"), so no prefix cache can be reused. In every run, the backend-reported prompt token count matched the full length. The ollama log also shows forcing full prompt re-processing on every request.
Decoding: greedy (temperature 0; for ollama, top_k 1, seed 42) with 256 output tokens. On MLX we ban EOS with a logits processor, so every run decodes exactly 256 tokens (excerpt):
def eos_ban(eos_ids):
bias = {}
def proc(tokens, logits):
if "b" not in bias:
b = np.zeros(logits.shape[-1], dtype=np.float32)
b[list(eos_ids)] = -np.inf
bias["b"] = mx.array(b)
return logits + bias["b"].astype(logits.dtype)
return proc
proc = eos_ban(tok.eos_token_ids)
stamps = []
mx.clear_cache(); mx.reset_peak_memory()
t0 = time.perf_counter()
for r in stream_generate(model, tok, ids, max_tokens=256,
logits_processors=[proc], prefill_step_size=2048):
stamps.append(time.perf_counter())
n = len(stamps)
ttft = stamps[0] - t0
decode_tps = (n - 1) / (stamps[-1] - stamps[0])
Ollama's API has no option to ignore EOS, so we recorded the actual eval_count. We called /api/generate with raw: true so that ollama received exactly the templated text config A tokenizes:
OPTIONS = {"temperature": 0, "top_k": 1, "seed": 42, "num_predict": 256, "num_ctx": 20480}
body = {"model": MODEL_NAME, "prompt": text, "raw": True, "stream": True, "options": OPTIONS}
# TTFT = wall clock to first streamed chunk
# prefill tok/s = prompt_eval_count / prompt_eval_duration
# decode tok/s = eval_count / eval_duration
The two prefill tok/s columns use different definitions. For MLX it is prompt tokens / wall-clock TTFT. For ollama it is measured inside the server and excludes HTTP and tokenization time. TTFT is the like-for-like comparison.
Order: each config got one discarded 512-token warm-up, then 3 timed reps in a Latin square (512→16k→4k, 4k→512→16k, 16k→4k→512), so no length always runs first or last. We left a 30 s cooldown between configs. The whole run took 1728 s.
Results
Medians of n=3. The ranges show min–max.
| Config | Prompt | TTFT (s) | Prefill (tok/s) | Decode (tok/s) | 256-token answer (s) | Peak mem (GB) |
|---|---|---|---|---|---|---|
| A MLX dense | 512 | 3.29 (2.58–3.52) | 156 | 14.8 (14.2–22.7) | 20.6 | 16.4 |
| A MLX dense | 4096 | 28.7 (25.8–43.3) | 143 | 13.4 (11.7–14.4) | 47.7 | 18.5 |
| A MLX dense | 16384 | 116.3 (110.9–171.1) | 141 | 11.9 (8.5–12.5) | 136.8 | 20.6 |
| B ollama dense | 512 | 3.84 (3.20–3.95) | 135 | 8.4 (8.2–11.0) | 34.2* | — |
| B ollama dense | 4096 | 31.0 (31.0–31.1) | 133 | 7.7 (7.6–7.9) | 64.1 | — |
| B ollama dense | 16384 | 111.0 (104.6–133.6) | 148 | 7.4 (7.1–7.7) | 144.1 | — |
| C MLX MoE 35B-A3B | 512 | 0.58 (0.58–0.61) | 876 | 96.5 | 3.24 | 20.2 |
| C MLX MoE 35B-A3B | 4096 | 3.36 (3.34–3.37) | 1219 | 93.3 | 6.10 | 21.5 |
| C MLX MoE 35B-A3B | 16384 | 14.7 (14.6–15.5) | 1111 | 81.9 | 17.8 | 22.6 |
| D MLX LFM2.5-24B-A2B | 512 | 0.44 | 1169 | 132.0 | 2.38 | 14.0 |
| D MLX LFM2.5-24B-A2B | 4096 | 2.46 | 1665 | 128.0 | 4.45 | 14.7 |
| D MLX LFM2.5-24B-A2B | 16384 | 10.4 | 1582 | 110.9 | 12.7 | 14.9 |
* At 512 tokens all three ollama runs stopped at EOS after 193–197 tokens. For those runs, the 256-token time is projected as TTFT + 255 / decode tok/s. Their decode rate is averaged over those 193–197 tokens rather than 256, so over a slightly shorter context. For B memory, ollama ps reported 17.28 GB, which is ollama's allocation estimate at num_ctx 20480, not a measured peak.
Decode slowdown from 512 to 16k: A 19%, B 11%, C 15%, D 16%.
TTFT growth from 512 to 16k (32x more tokens): A 35.35x, B 28.93x, C 25.23x, D 23.65x.
What this means on our machine:
- Prefill sets the cost of long prompts. The dense 27B processed prompts at about 133–156 tok/s on both backends at every length, so TTFT grew roughly in proportion to prompt length. At 16k, almost two minutes passed before the first token. Decode added about 20 s more for 256 tokens. A short-prompt decode number tells you very little about that wait.
- MLX vs ollama: decode differs, prefill does not. MLX decoded 1.76x, 1.74x and 1.61x faster at 512, 4k and 16k. Prefill throughput was within about 15% at every length, and at 16k ollama's median was slightly higher (ratio 1.049, on ollama's server-internal metric). The end-to-end gap narrows as prompts grow: ollama took 1.66x as long as MLX for a 256-token answer at 512 tokens and 1.05x at 16k. Part of the decode gap may come from the GGUF storing 1.14x more bits per weight (5.14 vs 4.50). That is a hypothesis: we did not isolate it, and it would not account for the whole gap.
- The MoE keeps its lead. Qwen3.6-35B-A3B decoded 6.5–7x faster than the dense 27B at all three lengths (6.5x, 7.0x, 6.9x). Its prefill was also 5.6–8.5x faster. At 16k its TTFT was 7.9x shorter, and a 256-token answer came 7.7x sooner. We expected the lead to shrink in prefill. It didn't.
- Memory grew modestly. For dense MLX, peak memory rose 4.2 GB from 512 to 16k tokens. LFM2.5 grew by less than 1 GB.
Surprises and caveats
- The first timed dense runs were noisy. For config A, rep 1 had the fastest 512-token decode (22.7 tok/s) and then the slowest 16k run (171.1 s TTFT, 8.5 tok/s decode). Reps 2 and 3 were consistent with each other. Ollama also showed a faster first 512-token decode (11.0 vs 8.2–8.4 tok/s). One possible factor: the machine was in Automatic power mode, not High Power, and under sustained load Automatic mode can lower GPU clocks.
pmset -g thermwould not show this, because it does not report GPU frequency throttling on Apple Silicon. We did not log GPU frequency (for example withpowermetrics), so this is a hypothesis we can't confirm. A second hypothesis: the warm-up only covered 512 tokens, and in A, B and C the slowest 16k run was the first one (A 171.1 s, B 133.6 s, C 15.5 s), which would fit one-time costs of the first long prompt such as allocation or kernel setup. A 16k warm-up would separate the two explanations. We report medians, but the ranges matter: at 16k, the TTFT spreads for A and B overlap. The "ollama is 5% faster at 16k" median is within noise. - Fixed config order. The configs always ran in the same order (A, B, C, D), so the A-vs-B ratios could carry some drift in power or thermal state between configs. The decode ranking does not depend on it: MLX was faster in every pair of runs (MLX min 14.2 vs ollama max 11.0 tok/s at 512; 8.5 vs 7.7 at 16k).
- These are hybrid models. According to the model architecture, Qwen3.8-27B and Qwen3.6-35B-A3B use linear-attention layers with fixed-size state in 3 of every 4 layers, and only every 4th layer has a growing KV cache. LFM2.5 has 10 attention layers out of 40, according to the
layer_typesfield in its config.json. Our hypothesis is that this is why decode dropped only 11–19% at 16k. Pure full-attention models may slow down more. - LFM2.5 did not fully disable thinking. Although we applied the template with thinking disabled, some config D completions (512 and 4096 tokens, rep 1) contain reasoning text followed by
</think>. The 512-token one then rambles about "Request 0001". Timings are unaffected because MLX decoding was forced to 256 tokens either way, but the D outputs are not clean non-thinking answers. - Quantization differs. A uses affine 4-bit (about 4.50 bpw on text weights) and B uses UD-Q4_K_XL (mixed K-quants, 5.14 bpw). The greedy outputs at 512 tokens had a difflib similarity of only 0.23. Both produced bullet summaries, but they are not the same computation.
- Defaults were not tuned. mlx_lm used prefill_step_size 2048 and ollama used batch 1024. Different settings could change prefill numbers.
- Output checks: 97.2% of completions contained at least 3 bullet lines. One completion (D, rep 2, 16k) failed the bullet/degeneracy check.
- One machine, n=3. No significance testing. Other chips, RAM sizes and macOS builds will give different numbers.
How to reproduce
Scripts, prompts, raw per-run JSON, all completions and the plot are at https://hackbig.com/blog-data/m3-max-long-context-speed-curve/. In the published env.json, results.json and ollama_build_lines.txt, absolute local paths are replaced with placeholders such as <run_dir>.
# models must already be in the HF cache; the run itself needs no network
python3 -m pip install mlx==0.31.2 mlx-lm==0.31.3 transformers==5.12.1 numpy==2.4.2
python3 -m venv --system-site-packages .venv
.venv/bin/pip install matplotlib==3.10.7 gguf==0.17.1
./run.sh --dry-run # builds prompts, reads model stats, checks scripts; no GPU
./run.sh # full benchmark -> results.json, plots/speed_vs_context.png
run.sh runs MLX with the system python3 and the helper scripts with the venv. It starts a private ollama server on its own port with OLLAMA_FLASH_ATTENTION=1, OLLAMA_KV_CACHE_TYPE=f16, OLLAMA_NUM_PARALLEL=1 and OLLAMA_CONTEXT_LENGTH=20480, and registers the GGUF from the HF cache through a hard link. We'd like to see the same curve on M4 and M5 chips and on full-attention models.
Conclusion
On our M3 Max, a 16k-token prompt to the dense Qwen3.8-27B waited about two minutes before the first token on either backend; MLX decoded faster than ollama. The 3B-active MoE decoded 6.5–7x faster at every length and, at 16k, reached the first token 7.9x sooner.
Code and raw data for this post
How this was made: an AI agent (Claude) designed and ran this experiment on our own MacBook in a sandbox; every number comes from the run's results file (linked below), checked by script before publishing.