Decode performance optimizations for MLX inference
Summary
During work on fusion-mlx, a mixed-bit quantization inference engine built on MLX, we implemented several decode-path optimizations that yielded significant speedups on Apple Silicon. These are general techniques that could benefit the upstream MLX framework.
Proposed Optimizations
1. Skip-logsumexp in decode path
During autoregressive decode, only one token is sampled from the logits distribution. The full logsumexp normalization across the vocabulary is unnecessary — we only need the relative ordering (for argmax) or the unnormalized probabilities (for top-k/top-p sampling). Skipping logsumexp in the decode path saves ~15% of the per-layer compute.
2. Fused sampler kernel
Currently, sampling involves a GPU→CPU round-trip per token: logits are computed on GPU, transferred to CPU for argmax or random sampling, then the result is sent back. A fused Metal kernel that combines argmax/random sampling into a single GPU operation eliminates this round-trip, reducing latency per decode step.
3. FP8 KV cache
Store the KV cache in FP8 (E4M3) format instead of float16, halving memory bandwidth for attention operations. A lightweight Metal dequant kernel converts back to float16 on load. We measured +8-12% decode speedup for 7B models, with the benefit growing for larger models where KV cache bandwidth dominates.
4. mx.compile for per-layer decode
Apply mx.compile to the single-token decode step. Because the decode graph is fixed (sequence length = 1, no dynamic shapes), compilation is highly effective and reduces Python dispatch overhead by ~20%. This is a relatively easy win since it requires no custom Metal code.
5. Metal command buffer reuse
Reuse Metal command buffers across decode steps instead of creating new ones each step. This reduces Metal framework overhead from buffer allocation and submission, which becomes noticeable in the tight decode loop where each step is very fast.
Benchmark Results
Tested on Qwen3.6-27B on M2 Ultra 137GB:
| Configuration | bpw | tok/s | vs baseline |
|---|---|---|---|
| mxfp8 baseline | — | 18.5 | — |
| mixed_3_4 | 3.68 | 36.2 | +96% |
| quant2-all | 2.37 | 48.5 | +162% |
The mixed-bit quantization accounts for a large portion of the speedup (reduced compute + memory), but the decode-path optimizations above contributed measurably on top of that.
Implementation Notes
- Optimizations 1, 4, and 5 are purely framework-level changes that would benefit all MLX models.
- Optimization 2 requires a new Metal kernel but follows existing patterns in MLX.
- Optimization 3 is most impactful for long-context / large-model scenarios where KV cache bandwidth is the bottleneck.
Happy to contribute PRs for any of these if there is interest.
1. Skip-logsumexp in decode path
I'm not sure what this means actually.
2. Fused sampler kernel
Sounds like a bug if it really is the case though.
3. FP8 KV cache
At the moment we suggest using custom extension rather than adding new kernels to core.
4. mx.compile for per-layer decode
I don't know what this means but feel free sending a PR to mlx-lm with some benchmarks on real models.
5. Metal command buffer reuse
Sounds a plausible optimization but would need some real world benchmarks.
Thanks for your reply.
I will submit a PR for these items and then proceed to implement them. The final decision on whether to merge them into main will rest with your team
---Original--- From: @.> Date: Fri, Jul 3, 2026 07:51 AM To: @.>; Cc: @.@.>; Subject: Re: [ml-explore/mlx] Decode performance optimizations for MLX inference (Issue #3789)
zcbenz left a comment (ml-explore/mlx#3789)
- Skip-logsumexp in decode path
I'm not sure what this means actually.
- Fused sampler kernel
Sounds like a bug if it really is the case though.
- FP8 KV cache
At the moment we suggest using custom extension rather than adding new kernels to core.
- mx.compile for per-layer decode
I don't know what this means but feel free sending a PR to mlx-lm with some benchmarks on real models.
- Metal command buffer reuse
Sounds a plausible optimization but would need some real world benchmarks.
— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today! You are receiving this because you authored the thread.Message ID: @.***>