180B Parameters, 32GB of RAM: Pushing MoE Inference to the Limit with Transformers
A contributor to the Hugging Face ecosystem has published a project that teaches Transformers to stream mixture-of-experts weights from disk, and used it to run Qwen3.8-Flash-Next — a 180B-class model — on a 32GB Apple M5 laptop. The work is packaged as a pull request against Transformers (huggingface/transformers#50717) plus a small model-specific package called moe-expert-streaming, with the code and benchmark results collected in the Space tarekziade/moe-expert-streaming. Against Strata, the inference engine built by Niko Veit for MoE models, the final Transformers-based setup lands roughly 6% behind on short decode while slightly ahead on prefill, according to the author's own measurements on identical prompts.
The motivation is concrete. The author runs a Windows 11 desktop with two RTX 4090s for heavier work, but wanted large models running locally on the 32GB unified-memory MacBook. Strata keeps only hot experts in VRAM and spills the rest to RAM or disk — experts make up more than 90% of a MoE model's weights — so a 180B model can fit on a much smaller GPU. A prior blog post ran Qwen3.8-Flash-Next on a 32GB M4 at 13.5 tok/s, faster than llama.cpp. That is below what coding agents need, but it set a target to chase.
Before building, the author surveyed how the existing engines handle a model too large for memory, judging each on three questions: where experts live, where the expert matmul executes, and what moves per token. llama.cpp memory-maps the GGUF file and can keep experts on the CPU (via --cpu-moe, --n-cpu-moe N, -ot "exps=CPU", or --fit on). Because the matmul also runs on CPU, only activations cross PCIe, never weights — excellent when experts fit in RAM, but the page cache thrashes when they don't. A GPU expert cache (--moe-cache-mib) recently landed, but fragments the graph from 2 splits per token to 50 and loses around 10% even in the all-fits case. vLLM's --cpu-offload-gb N pins N GB of weights in RAM and reads them in place over PCIe, with no caching and no disk tier; --cpu-offload-params experts limits offloading to experts, which roughly doubled vLLM's speed in a Qwen3.6-35B-A3B test on one 24GB A10G, with FP8 helping further. Transformers' Accelerate path (device_map="auto", offload_folder) copies one module's weights to the GPU before forward — for a MoE layer that means the entire expert bank moves to use eight or ten experts, with no cache and no routing awareness. On a Mac, CPU and GPU share memory, so "offload to CPU" frees nothing.
The engineering path started small with OLMoE-1B-7B, which fits in memory and allows token-by-token comparison with vanilla Transformers. A first version — experts on disk, an LRU cache on device, a Python loop running one expert at a time — managed 10.1 tok/s versus roughly 32 for vanilla. The I/O was fine; the Python loop was the problem, since vanilla uses grouped_mm to run all of a layer's experts in a single kernel. The key fix was turning the cache into a slot pool: one preallocated device tensor shared across layers, plus a per-layer table mapping expert id to slot. Remapping router expert ids to slots makes the pool look like an ordinary expert bank, so grouped_mm runs unmodified. That reached 34.6 tok/s against 38.1 for vanilla.
On the real target, the numbers are large. Qwen3.8-Flash-Next has 48 layers of 512 experts with 10 active, 121GB in FP8, plus a 51GB n-gram embedding table that needs its own disk path. The setup keeps experts quantized to 3 bits (49GB on disk, 12GiB cached on device), dense layers in 8 bits (about 5.4GB, adding 0.2% perplexity versus BF16), and the n-gram table on disk with rows read on demand. Conversion takes about five hours. After loading, the process uses roughly 18GiB, and a 6K prompt peaks at 25.4GiB — near the machine's limit, to the point where a browser has to be closed to avoid macOS swapping.
Closing the gap from the first 10 tok/s involved work outside the expert path. Forcing a GPU commit after each block gave +10% because PyTorch on MPS otherwise holds small kernels until the next sync while Python runs. Custom Metal kernels cover Gated DeltaNet and hyper-connections, which lack fast paths on macOS. Switching to Strata's GGUF weights cut dense traffic from 5.4GB to 3.9GB per token and per-expert size from 2.15MB to 1.75MB, with separate cache pools per expert size. Bigger prefill chunks plus reading the next layer's experts during the current layer's compute took prefill from 68 to about 228 tok/s. Long documents got attention over only the indexer's selected blocks and a KV cache that grows in place rather than being copied each token.
What remains is host round-trip overhead, around 10ms per token; waking after a sync accounts for only about 1ms. The rest is the GPU going idle while Python queues the next layer. Strata queues an entire token's work up front and has the GPU wait on a Metal shared event per layer while the host reads experts — control PyTorch's MPS stream doesn't expose. An attempted workaround, a kernel spinning on a CPU-written flag, saw the GPU notice the write anywhere from a second to never, so it wasn't shipped.
The API is still under discussion with maintainers. The current form maps experts to "disk" in device_map, which routes them through the streaming path instead of Accelerate; the loader skips those tensors and MoE layers switch to an offloaded implementation. Misses use 8 I/O threads with one contiguous read per expert, and the next two layers' routers run early to guess prefetches. Quantized experts will likely move to the per-layer quantization plan in PR #50245 rather than a dedicated option. The code is Python only — Metal kernels compiled at runtime with torch.mps.compile_shader or loaded prebuilt from the Hub — and the kernels are now published there. According to the author, the Transformers PR, the model package and the kernels were all built with AI assistance, with the human role being choosing experiments, reading profiles and deciding what to keep.
Why it matters: developers who want large sparse models on consumer or single-GPU hardware now have an upstreamable path in Transformers, and the slot-pool trick shows that expert streaming can reuse existing fused kernels instead of requiring custom matmuls. The measured ~14 tok/s is still too slow for agentic coding in the author's own use, so the near-term value is experimentation and profiling rather than production serving; inference is also that the final gap to Strata will stay open until PyTorch exposes finer control over per-layer synchronization on the MPS backend.
Based on reporting from the original publisher. Visit the source for full context and later updates.