Hugging FaceTarek ZiadéTutorial
180B Parameters, 32GB of RAM: Pushing MoE Inference to the Limit with Transformers
Summary
The author modified Transformers to stream MoE experts from disk, enabling a 180B-parameter Qwen model to run on a 32 GB MacBook M5 at ~14 tok/s. Existing engines like llama.cpp and vLLM either thrash the page cache or copy all experts on every token. The key innovation is a slot-pool cache: one preallocated device tensor shared across layers, with a per-layer mapping from expert ID to slot, so that grouped_mm runs on the pool as if it were a regular expert bank. Experts are quantized to 3 bits, read by 8 I/O threads, and prefetched by running the next two layers’ routers early. The implementation closes the gap with Strata to about 6% on short decode, with slightly faster prefill. The author notes that closing the remaining gap would require deeper changes in PyTorch’s MPS stream control.
Context
The author wanted to run large MoE models (e.g., Qwen3.8-Flash-Next, 180B parameters) locally on a 32 GB MacBook M5, where the full model does not fit in unified memory. Existing inference engines (llama.cpp, vLLM, Transformers with Accelerate) either thrash the page cache, copy all experts on every token, or lack a disk tier. The goal was to stream only the routed experts from SSD through a bounded cache, reusing the model’s fast expert kernels, and to close the performance gap with Strata, a dedicated MoE inference engine.
Approach / What changed
The author modified Transformers to stream MoE experts from disk by replacing Accelerate’s per-module offload with a slot-pool cache: one preallocated device tensor shared across layers, with a per-layer mapping from expert ID to slot. The router’s expert IDs are remapped to slots so that grouped_mm runs on the pool as if it were a regular expert bank. Experts are quantized offline to 3 bits, read by 8 I/O threads, and prefetched by running the next two layers’ routers early on the current hidden states. The implementation is in huggingface/transformers#50717.
Takeaways
Streaming only the routed experts from SSD through a bounded cache, rather than copying all experts on every token, is essential for running large MoE models on memory-constrained hardware.
A slot-pool cache (one preallocated device tensor shared across layers with a per-layer expert-to-slot mapping) allows grouped_mm to run on the cache as if it were a regular expert bank, avoiding custom matmul kernels.
Closing the remaining ~6% performance gap with Strata would require a native per-layer sync primitive in PyTorch’s MPS stream, which the author could not implement due to risk of hangs.
FAQ
How does the expert cache avoid custom matmul kernels?
The slot-pool approach: one preallocated device tensor shared by all layers, plus a per-layer table from expert ID to slot. The router's expert IDs are remapped to slots so that grouped_mm runs on the pool as if it were a regular expert bank. This avoids custom expert matmul kernels and keeps the cache small.
How close did Transformers get to Strata's performance?
The author achieved ~14 tok/s decode, about 6% behind Strata (14.8 tok/s), with slightly faster prefill (228 vs 220 tok/s). The gap is attributed to host round trips (~10 ms per token) and the inability to queue all token work upfront because PyTorch does not expose a per-layer sync primitive like Strata's Metal shared event.
Why can't MLX run Qwen3.8-Flash-Next on 32 GB?
The n-gram embedding table (51 GB in FP8) stays in memory; only the experts are streamed. In MLX checkpoints, the n-gram tables alone are 32 GB, before the ~5 GB of dense layers and any expert cache, so the full model does not fit in 32 GB unified memory.
Source and freshness
- Source
- huggingface.co
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.