Mixture of Experts (MoEs) in Transformers
Hugging 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.
Related posts
- 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.
- 180B Parameters, 32GB of RAM: Pushing MoE Inference to the Limit with Transformers
Hugging Face hardMasterStreaming 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.
- Welcome Mixtral - a SOTA Mixture of Experts on Hugging Face
Hugging Face mediumAdeptMixtral 8x7B is Mistral's sparse Mixture-of-Experts LLM. It replaces feed-forward layers with MoE layers that route each token to 2 of 8 experts, so it has about 45B total parameters but decodes at roughly the speed of a 12B dense model. On release it beat Llama 2 70B on the Open LLM Leaderboard and matched GPT-3.5 on MT-Bench (8.30), under an Apache 2.0 license. Hugging Face shipped integrations for running it with Transformers and Text Generation Inference, plus QLoRA fine-tuning with TRL on one A100 (attention projections only, since sparse MLP layers interact poorly with PEFT). For quantization, VRAM needs are about 90 GB in fp16 and about 23 GB in 4-bit, and GPTQ performs better when the expert gating layers are left unquantized (perplexity 4.40 vs 4.25 in fp16).
- 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.
- SegMoE: Segmind Mixture of Diffusion Experts
Hugging Face mediumAdeptSegMoE is a framework for building Mixture-of-Experts diffusion models by merging pretrained Stable Diffusion or SDXL checkpoints. Selected feed-forward and/or attention layers are replaced with sparse MoE layers whose router gate weights are computed from per-expert positive and negative prompts. Three merges are released (SegMoE-2x1, 4x2 and SD 4x2), and a YAML config plus CLI or Python API builds custom merges from Hugging Face or CivitAI models. Qualitative comparisons suggest better prompt understanding than the RealVisXL base, at the cost of slower inference when more than one expert is active per token and high VRAM use: 24GB in half precision for SegMoE-4x2.