# Qwen3.8-27B speed vs context length on an M3 Max: MLX vs ollama, dense vs MoE A single tokens-per-second number measured on a short prompt hides what happens with the 4k–16k-token prompts that coding agents and RAG pipelines send. This benchmark measures **time to first token (prefill)**, **decode speed**, and **time to a 256-token answer** at prompt lengths of 512, 4096 and 16384 tokens for: | Config | Backend | Model | Weights | |---|---|---|---| | A | mlx_lm (Python API) | Qwen3.8-27B (dense) | `mlx-community/Qwen3.8-27B-4bit`: affine 4-bit, group size 64, ≈4.50 bpw on text weights, 16.05 GB | | B | ollama (`/api/generate`, raw prompt) | Qwen3.8-27B (dense) | `unsloth/Qwen3.8-27B-GGUF` **UD-Q4_K_XL**: mixed Q4_K/Q5_K/Q6_K/IQ4, ≈5.14 bpw, 17.56 GB | | C | mlx_lm | Qwen3.6-35B-A3B (MoE, about 3B active) | `mlx-community/Qwen3.6-35B-A3B-4bit`, ≈4.50 bpw on text weights, 20.40 GB | | D (optional) | mlx_lm | LFM2.5-24B-A2B (MoE, about 2B active) | `LiquidAI/LFM2.5-24B-A2B-MLX-4bit`, ≈4.53 bpw, 13.57 GB | The bpw and size numbers come from the file headers (`model_stats.py`) and are written to `results.json`. The only GGUF on disk is UD-Q4_K_XL, not Q4_K_M, so A vs B compares the **closest available 4-bit files**. It is not a pure engine-vs-engine result. ## What gets measured - `ttft_s`: wall-clock time from sending the request to the first streamed token (prefill plus first token) - `prefill_tps`: prompt tokens divided by TTFT for MLX; `prompt_eval_count / prompt_eval_duration` for ollama - `decode_tps`: `(tokens-1)/(t_last - t_first)` for MLX; `eval_count / eval_duration` for ollama - `e2e_256_s`: time to a 256-token answer (measured; projected only when ollama stopped early at EOS) - `decode_drop_pct_16k_vs_512`, `ttft_ratio_16k_vs_512`, and cross-config ratios (B/A, C/A, D/A) at each length - `peak_mem_gb` (from `mx.get_peak_memory()`, per run) for MLX; ollama `/api/ps` size, plus `vm_stat` snapshots in the raw files - file size, logical parameter count and effective bits per weight for each model - every value is the median of n=3 with its min–max and all samples; per-run `pmset -g therm` CPU speed limit ## Method - **Prompts** (`make_prompts.py`): the source of `click` 8.1.8 (BSD-3, downloaded from PyPI into `corpus/`), wrapped in each model's own chat template with thinking turned off, ending with "Summarize the above in 5 bullet points." Each prompt is cut so the **full templated prompt is exactly 512 / 4096 / 16384 tokens** for that model's tokenizer; the script asserts this. Each request begins with a 4-digit nonce (`Request 0001.`), so no backend can reuse a cached prefix between runs. - **Settings**: greedy decoding (temperature 0, ollama `top_k 1`), seed 42, 256 output tokens. MLX bans EOS through a logits processor, so it always decodes exactly 256 tokens. Ollama has no way to ignore EOS, so each run records its actual `eval_count`. - **Ollama** is started by `run.sh` on its own port, with its own models directory and these settings: `OLLAMA_FLASH_ATTENTION=1`, `OLLAMA_KV_CACHE_TYPE=f16`, `OLLAMA_NUM_PARALLEL=1`, `num_ctx=20480`. The GGUF is registered from the HF cache through `/api/create`, using its sha256. `run.sh` hard-links the blob into `ollama_models/blobs`, so nothing gets copied. Prompts are sent with `raw: true` and are the exact text config A tokenizes. `prompt_eval_count` is checked against the prompt length to catch cache hits. - **Order**: one discarded 512-token warm-up per config, then 3 reps in a Latin square (512→16k→4k, 4k→512→16k, 16k→4k→512), so thermal drift doesn't favour any one length. There is a 30 s cooldown between configs. Each config runs in its own process, and ollama is stopped before the MLX MoE runs. - **Sanity checks**: every completion is saved to `outputs/completions.jsonl` and checked for bullets and for degenerate repetition. A and B outputs at 512 tokens are compared with difflib. - **Time budget**: `run.sh` gives A and B about 14 minutes each and C about 6, and runs D only if time is left. A timed run is skipped (and listed in `results.json` caveats) if its estimated duration would overrun its config's slot. The whole script stays under 40 minutes. ## Reproduce Hardware used: MacBook Pro (Mac15,9), Apple M3 Max with 16 CPU / 40 GPU cores, 64 GB, macOS 27.0.1 (build 26A434). Plug into AC power, turn on High Power mode, and close other apps first. One-time setup (needs network; already done in this directory): ```zsh # models in the HF cache: mlx-community/Qwen3.8-27B-4bit, unsloth/Qwen3.8-27B-GGUF (UD-Q4_K_XL), # mlx-community/Qwen3.6-35B-A3B-4bit, LiquidAI/LFM2.5-24B-A2B-MLX-4bit /opt/homebrew/Caskroom/miniconda/base/bin/python -m venv --system-site-packages .venv .venv/bin/pip install matplotlib==3.10.7 gguf==0.17.1 .venv/bin/pip download --no-deps -d corpus/_wheels click==8.1.8 mkdir -p corpus/click_src && (cd corpus/click_src && unzip -o ../_wheels/click-8.1.8-py3-none-any.whl 'click/*') ``` Run (no network needed): ```zsh ./run.sh --dry-run # prompts, model stats, env, script checks, analysis self-test; no GPU ./run.sh # full benchmark, ~25–40 min -> results.json, plots/speed_vs_context.png ``` Software: Python 3.13.5 (miniconda), mlx 0.31.2, mlx-lm 0.31.3, transformers 5.12.1 (tokenizers only), numpy 2.4.2, ollama 0.40.5, matplotlib 3.10.7, gguf 0.17.1. `results.json` records the exact versions, the ollama server version, and the relevant ollama server log lines (runner/engine, flash attention, offload). ## Files | File | Purpose | |---|---| | `run.sh` | runs everything end to end (zsh) | | `make_prompts.py` | builds the exact-length prompts in `prompts/*.json` (text plus token ids, 4 nonce variants each) | | `bench_mlx.py` | configs A, C and D; writes `raw/.json` | | `bench_ollama.py` | config B; writes `raw/B.json` | | `model_stats.py` | file size, parameter count and bpw from safetensors/GGUF headers | | `collect_env.py` | machine, OS, power/thermal state and versions (serial numbers and UUIDs dropped) | | `analyze.py` | `results.json`, `plots/speed_vs_context.png`, `outputs/completions.jsonl` (`--selftest` uses synthetic data) | | `common.py` | schedule, thermal snapshot, time budget, output checks | | `tests/` | stand-ins for mlx/mlx_lm and ollama HTTP, used to smoke-test the control flow without a GPU | ## Caveats - These results come from **one M3 Max 64 GB laptop**. Other chips, RAM sizes and macOS builds will give different numbers, which is why the scripts are published. - The quantizations differ (≈4.5 vs ≈5.1 bpw), so llama.cpp reads about 14% more weight bytes per decoded token. Read A vs B as "what you get with the files people actually download". - Qwen3.8-27B and Qwen3.6-35B-A3B are **hybrid** models: 3 of every 4 layers are linear attention with a fixed-size state, and only 16 of 64 (27B) or 10 of 40 (35B) layers keep a growing KV cache. LFM2.5 has 10 attention layers out of 40. Their long-context slowdown will be milder than for pure full-attention models. - Ollama may run this architecture on its own ggml-based Go engine instead of the llama.cpp runner. Check `environment.ollama_server_log_lines` in `results.json` before calling it "llama.cpp" in the post. - n=3 per point with medians and min–max. There are no significance tests. - High Power mode can't be turned on from a script. The `pmset` power-mode lines record what was active. - LFM2.5's template always opens ``, so the prompts append an empty think block to skip reasoning. - MLX uses `prefill_step_size=2048` and ollama uses its default batch size. Neither was tuned.