# Batched decode throughput: MoE vs dense in MLX on an M3 Max **Question.** When several local agents hit one M3 Max (64 GB) at once, does the MoE models' speed lead over a dense 27B hold up, or shrink as the batch grows? We measure total and per-request decode throughput at batch 1, 2, 4, 8, 12 and 16 for: | name | HF repo | type | weights on disk | |---|---|---|---| | Qwen3.8-27B | `mlx-community/Qwen3.8-27B-4bit` | dense, 4-bit | 15 GB | | Qwen3.6-35B-A3B | `mlx-community/Qwen3.6-35B-A3B-4bit` | MoE (~3B active), 4-bit, router gates 8-bit | 19 GB | | LFM2.5-24B-A2B | `LiquidAI/LFM2.5-24B-A2B-MLX-4bit` | MoE (~2B active), 4-bit, embeddings 6-bit | 13 GB | ## What it does - `prompts/prompts.json`: 16 prompts we wrote (code explanation, summarising, planning), 259–329 tokens after each model's own chat template (`enable_thinking=False` where the template supports it). - `bench.py` (one fresh process per model): uses mlx_lm's `BatchGenerator`, the engine behind `mlx_lm.batch_generate`, directly. That lets us **disable EOS** so every request generates exactly 256 tokens, and timestamp every token. Greedy (argmax). All B prompts are prefilled together, then decoded together. - warm-up at every batch size (short prompts) so kernel compilation isn't timed - 3 repeats; each repeat runs all batch sizes in random order - prefill time = submit → every request has its first token (= TTFT in a static batch); also mlx_lm's own prompt-only timer - decode throughput = (generated tokens − B) / (last token time − first token time); per-request = total / B - peak memory via `mx.get_peak_memory()`, reset before each run; `vm_stat` swapouts before and after each run (runs that swap are left out of the throughput numbers and listed in the caveats) - correctness: batch-1 greedy output for all 16 prompts; every batched output is compared with it and we record the first token index where they differ - stops starting new runs when the model's time budget is used up, and records which runs it skipped - `orchestrate.py`: random model order (seed recorded), logs `pmset -g therm` and `sysctl vm.swapusage` before and after each model, 60 s pause between models, splits the time budget (dense 50%, each MoE 25%). Optional last step (`server_test.py`): starts `mlx_lm.server` for the model that was fastest at batch 1 and sends 1, 4 and 8 concurrent streaming requests (EOS banned via `logit_bias`, unique first line per request so the prompt cache can't skip prefill). Reports end-to-end tokens/s and TTFT and compares them with the batch run. Skipped if fewer than 3 minutes are left. - `analyze.py` → `results.json` (every number for the post: medians with min/max samples, speedup vs batch 1, MoE/dense ratios, crossovers, knee, memory, divergence, thermal). `plot.py` → `throughput.png`. **Knee** = first tested batch size where going to the next tested size adds < 10% total throughput. **Crossover** = the pair of adjacent batch sizes where the MoE/dense total-throughput ratio crosses 1. ## Reproduce Hardware: MacBook Pro M3 Max, 64 GB, on mains power, other apps closed. ```zsh ./run.sh # ≈35 min, no network; writes results.json, throughput.png, run.log, raw/ BUDGET_MIN=30 ./run.sh # smaller time budget ./run.sh --no-server # skip the optional mlx_lm.server test ./run.sh --seed 42 # fixed model order ``` Software: macOS 27.0.1 (26A434), Python 3.13.5, mlx 0.31.2, mlx-metal 0.31.2, mlx-lm 0.31.3 (`/opt/homebrew/Caskroom/miniconda/base/bin/python`). Plotting uses matplotlib 3.10.7, installed into `./vendor` (see `requirements-plot.txt`; recreate with `python -m pip install --target vendor matplotlib==3.10.7`). Models must already be in the Hugging Face cache (`HF_HUB_OFFLINE=1` is set). ## Caveats - The quantisations differ between models (see table). Compare scaling (speedup vs batch 1) as well as absolute speed. This is not a quality ranking. - Static batches with equal lengths. Real agents arrive at different times and stop early, which lowers the effective batch. - Short contexts (~290-token prompts + 256 new tokens). Long agent contexts add KV and attention cost per step. - All three models are hybrids (Qwen3.5-family: gated linear attention + attention; LFM2.5: short conv + attention), so only some layers keep a growing KV cache. In this post "dense" means the weights and FFN are dense. - After EOS the outputs are forced continuations. They are used only for timing and the divergence check. - One machine, one session. Thermal state is logged but not controlled. - mlx_lm batching code changes quickly. The results hold for the exact versions above.