Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Hugging Face 21 min read Deep dive hardMaster

Mixture of Experts Explained

Mixture 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.

Related posts

  • Mixture of Experts (MoEs) in Transformers

    Hugging Face hardMaster

    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.

  • Welcome Mixtral - a SOTA Mixture of Experts on Hugging Face

    Hugging Face mediumAdept

    Mixtral 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).

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

    Hugging Face hardMaster

    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.

  • SegMoE: Segmind Mixture of Diffusion Experts

    Hugging Face mediumAdept

    SegMoE 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.

  • Accelerating Transformers Fine-Tuning with NVIDIA NeMo AutoModel

    Hugging Face hardMaster

    NeMo 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.

The quest for software engineering knowledge.

Look:
Layout: