Pushing MoE Inference to the Limit with Transformers

The author, who prefers running large models locally, describes an experiment to push MoE inference through Transformers on a 32 GB MacBook M5. At home they have Orthrus, a dual-RTX 4090 Windows PC, but their working style moves between rooms, so a fixed desktop is not enough. An earlier setup ran a 27B Qwen model with llama.app on Orthrus and used it remotely from the laptop, with an online model doing orchestration and the second RTX running a FIM model for Neovim autocompletion. After social media feedback, they tried Strata, an inference engine for MoE models that keeps hot experts in VRAM and places the rest in RAM or disk. Since experts are 90%+ of a MoE model’s weights, a 180B model like Qwen3.8-Flash-Next can run on a single RTX 4090, and a related blog post showed the same model running on a 32 GB Mac M4 at 13.5 tok/s, faster than llama.cpp. That is too slow for coding agents, but it motivated a deeper look at how other engines offload MoE experts.

The article compares llama.cpp, vLLM, and Transformers on three questions: where experts live, where expert matmuls run, and what moves on each token. llama.cpp can mmap a GGUF file and keep experts on the CPU, computing them there so only activations cross PCIe. That works well when experts fit in RAM, but the page cache thrashes when they do not; a new GPU expert cache loses roughly 10% and fragments the graph. vLLM‘s –cpu-offload-gb places weights in pinned RAM and kernels read them in place over PCIe, but there is no caching or disk tier, so a hot expert crosses PCIe on every token. The flag –cpu-offload-params experts limits offloading to routed experts, which roughly doubles speed and suits large batches more than a single user. Transformers uses Accelerate’s device_map offloading one module at a time; for MoE, that module is the whole expert bank per layer, so every token copies every expert even though only 8 or 10 are routed. On Macs, CPU and GPU share memory, so offloading to CPU frees nothing. Benchmarks on Qwen3.6-35B-A3B on one A10G show Strata at 82-88 tok/s, llama.cpp at 63-64 with Q8_0 –fit on, vLLM FP8 with expert-only CPU offload at 20.1 tok/s, and Transformers + Accelerate at 0.16 tok/s.

The author decided to fix Transformers offloading itself, with all code built using AI, and published the work in a Space: tarekziade/moe-expert-streaming. Starting with OLMoE-1B-7B, a first version using a disk-backed LRU cache and a Python loop over one expert at a time reached 10.1 tok/s versus 32 for vanilla, because vanilla uses grouped_mm to run all experts of a layer in one kernel. The key idea became a slot pool: one preallocated device tensor shared by all layers plus a per-layer mapping from expert id to slot. Remapping the router’s expert ids makes the pool look like a regular expert bank, so grouped_mm works without a custom expert matmul. That reached 34.6 tok/s against 38.1 for vanilla. The loader then became an option of from_pretrained via huggingface/transformers#50717, using device_map with experts mapped to “disk”, a MetalConfig for 8-bit quantization, cache_bytes set to 12 GiB, and a path to 3-bit quantized experts. Misses are read by 8 I/O threads as one contiguous read per expert, and routers for the next two layers run early on current hidden states to guess which experts to prefetch.

For the real target, Qwen3.8-Flash-Next, the model has 48 layers of 512 experts with 10 active, totaling 121 GB in FP8, plus a 51 GB n-gram embedding table that also needs a disk path. The conversion script outputs dense weights, 3-bit quantized experts, and the n-gram table. With experts quantized to 3 bits, 49 GB stay on disk while 12 GiB are cached on the device; dense layers use 8 bits at about 5.4 GB with +0.2% perplexity versus BF16; and n-gram rows are read from disk when needed. Conversion takes roughly 5 hours, loading uses about 18 GiB, and a 6K prompt peaks at 25.4 GiB, close enough to the Mac’s limit that swapping becomes a risk. Model-specific parts live in the moe-expert-streaming package. Initial results on the same M5 compared to Strata 0.1.60 were 10.0 tok/s decode versus 14.8, 68 tok/s prefill versus 220, and 7.3 tok/s decode after a 6K document versus 13.7. The author also tried vllm-metal through MLX, which streams routed experts through a bounded cache, but it cannot run Qwen3.8-Flash-Next on 32 GB because only experts are streamed; the n-gram tables stay in memory.

Most of the remaining optimization time was not spent on expert I/O. Forcing PyTorch to commit GPU work early after each block added about 10%, because MPS otherwise holds small kernels until the next sync. Metal kernels were added for Gated DeltaNet and hyper-connections. Switching to Strata‘s GGUF weights reduced dense weight traffic from 5.4 GB to 3.9 GB per token and expert size from 2.15 MB to 1.75 MB, with separate cache pools per expert size. Prefill improved from 68 to about 228 tok/s by using bigger chunks and reading the next layer’s experts during computation. Long-document handling changed to attention over indexed blocks only and a KV cache that grows in place. Misses are not hidden, but reading one guessed expert per layer while the SSD would otherwise be idle helped. Final numbers on the same M5 are Transformers at 13.9 tok/s decode versus Strata‘s 14.8, 228 tok/s prefill versus 220, and 12.4 tok/s decode after a 6K document versus 13.7, roughly 6% behind on short decode with slightly faster prefill. The remaining gap is mostly host round trips of about 10 ms per token; Strata queues a whole token’s work up front and uses a Metal shared event, which PyTorch does not expose. The author’s conclusion is that the project reaches about 14 tok/s, still too slow for coding agents, but the interesting result is how close it got using only Python, runtime-compiled Metal kernels, and a bounded cache that grouped_mm can run on directly, with no C++ extension. The generic implementation is in the Transformers PR and the benchmarks are in the Space, with Strata setting the bar for expert streaming on consumer hardware.

180B Parameters, 32GB of RAM: Pushing MoE Inference to the Limit with Transformers

View Original