← 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)
  1. TL;DR
  2. The question
  3. Setup
  4. Results
  5. Surprises and caveats
  6. How to reproduce
  7. Conclusion

TL;DR

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:

  1. How much TTFT and decode speed change as the prompt grows from 512 to 16,384 tokens.
  2. Whether MLX or ollama handles long context better for the same model.
  3. 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:

Surprises and caveats

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.