180B Parameters, 32GB of RAM: Pushing MoE Inference to the Limit with Transformers
Streaming MoE experts from SSD through a bounded device-side slot pool lets Transformers run Qwen3.8-Flash-Next (121 GB of experts) on a 32 GB MacBook M5 at ~13.9 tok/s, within 6% of the specialized Strata engine on decode and slightly faster on prefill. The key idea is a preallocated cache tensor shared across layers with a per-layer expert-id-to-slot table, so the stock grouped_mm kernel runs on cached experts unchanged; 3-bit experts, 8 I/O threads, router-based prefetch, and custom Metal kernels close most of the gap. A comparison with llama.cpp, vLLM and Accelerate offloading shows why existing approaches fail when experts don't fit in RAM, and the remaining gap is attributed to host-GPU round trips that PyTorch's MPS backend can't avoid today.
Related posts
- Mixture of Experts (MoEs) in Transformers
Hugging Face hardMasterHugging Face transformers v5 reworks its internals to treat Mixture of Experts models as first-class: a WeightConverter pipeline packs per-expert checkpoint tensors into contiguous runtime tensors with async, lazy materialization, cutting Qwen1.5-110B load time from ~66s to ~21s (10s with tensor parallelism) on an A100. A pluggable Experts Backend dispatches tokens via eager, batched_mm (torch.bmm) or grouped_mm (torch._grouped_mm) strategies, suited respectively to debugging, small-batch and large-batch/memory-constrained workloads. Expert parallelism shards experts across GPUs with a single config flag, and a collaboration with Unsloth using Triton grouped-GEMM + LoRA kernels yields ~12× faster MoE training with >35% less VRAM.
- Accelerating Transformers Fine-Tuning with NVIDIA NeMo AutoModel
Hugging Face hardMasterNeMo AutoModel subclasses Transformers v5's AutoModelForCausalLM and adds Expert Parallelism as a dedicated mesh orthogonal to data parallelism, DeepEP fused all-to-all dispatch, and TransformerEngine kernels. Users swap one import and get 3.4-3.7x higher MoE fine-tuning throughput and 29-32% lower peak memory on Qwen3-30B-A3B and Nemotron 3 Nano on 8 H100s. NeMo AutoModel's benchmarks used a balanced routing gate while v4/v5 ran their native routers. The same path enables full fine-tuning of the 550B Nemotron 3 Ultra across 128 GPUs, where v5 runs out of memory. Explanations cover why v4 deadlocks under FSDP (data-dependent per-expert loops cause mismatched collectives), v5's expert backends (eager, batched_mm, grouped_mm), and reversible dynamic weight loading that keeps checkpoints in standard HF format.
- Native-speed vLLM transformers modeling backend
Hugging Face hardMasterThe transformers modeling backend in vLLM now matches or beats vLLM's hand-written native implementations in throughput. Benchmarks covered Qwen3-4B on one GPU, Qwen3-32B with tensor parallelism, and Qwen3-235B FP8 MoE with data and expert parallelism on 8×H100. The backend uses torch.fx static analysis to find known patterns, then rewrites the model source via AST to swap in fused vLLM kernels such as QKVParallelLinear, MergedColumnParallelLinear and expert-parallel MoE ops. It also infers TP/PP plans while staying compatible with torch.compile and CUDA Graphs. Any compliant transformers model gets this with `--model-impl transformers`, except linear-attention models and custom Hub-code models.
- Tricks from OpenAI gpt-oss YOU 🫵 can use with transformers
Hugging Face hardMasterHugging Face upgraded transformers to support OpenAI's gpt-oss models, and most of the new features carry over to other models. These include pre-built kernels downloaded from the Hub (Liger RMSNorm, MegaBlocks MoE, Flash Attention 3 with attention sinks), native MXFP4 4-bit quantization via Triton kernels, tensor and expert parallelism, dynamic sliding window caching, continuous batching with paged attention, and faster loading. MXFP4 stores E2M1 values in 32-element blocks with shared scales. That lets gpt-oss-20B run in about 16 GB of VRAM and 120B in about 80 GB, versus roughly 4× the memory in the bf16 fallback. Benchmarks show the custom and MXFP4 kernels pay off mainly at larger batch sizes.
- Mixture of Experts Explained
Hugging Face hardMasterMixture of Experts (MoE) transformers replace dense feed-forward layers with sparse expert layers and a learned router that sends each token to only a few experts. Pretraining is much faster than for dense models, and inference is faster than a dense model with the same parameter count, but all experts must sit in memory: Mixtral 8x7B needs VRAM for about 47B parameters while computing like a 12B model. The history runs from the 1991 origins through Shazeer's noisy top-k gating, GShard's expert capacity and random routing, and Switch Transformers' single-expert routing and selective bfloat16 precision. It also covers load-balancing auxiliary losses, router z-loss, fine-tuning difficulties, expert parallelism and serving tradeoffs.