Train Mixture of Experts (MoE) models using DeepSpeed or HuggingFace. Use when training large-scale models with limited compute (5× cost reduction vs dense models), implementing sparse architectures like Mixtral 8x7B or DeepSeek-V3, or scaling model capacity without proportional compute increase. Covers MoE architectures, routing mechanisms, load balancing, expert parallelism, and inference optimization.
Instrucciones de origen · Vista previa de solo lectura
name
moe-training
description
Train Mixture of Experts (MoE) models using DeepSpeed or HuggingFace. Use when training large-scale models with limited compute (5× cost reduction vs dense models), implementing sparse architectures like Mixtral 8x7B or DeepSeek-V3, or scaling model capacity without proportional compute increase. Covers MoE architectures, routing mechanisms, load balancing, expert parallelism, and inference optimization.
category
ml-training
version
1.0.0
author
Synthetic Sciences
license
MIT
tags
["Emerging Techniques","MoE","Mixture Of Experts","Sparse Models","DeepSpeed","Expert Parallelism","Mixtral","DeepSeek","Routing","Load Balancing","Efficient Training"]
dependencies
["deepspeed","transformers","torch","accelerate"]
MoE Training: Mixture of Experts
When to Use This Skill
Use MoE Training when you need to:
Train larger models with limited compute (5× cost reduction vs dense models)
Scale model capacity without proportional compute increase
Achieve better performance per compute budget than dense models
Specialize experts for different domains/tasks/languages
Reduce inference latency with sparse activation (only 13B/47B params active in Mixtral)
Implement SOTA models like Mixtral 8x7B, DeepSeek-V3, Switch Transformers
# MoE models need lower LR than dense models# - Dense model: lr = 6e-4# - MoE model: lr = 1e-4 (3-6× lower)# Also extend decay schedule
dense_lr_decay_iters = 300000
moe_lr_decay_iters = 500000# 1.5-2× longer
4. Loss Coefficient Tuning
# Start with standard values
moe_loss_coeff = 0.01# Auxiliary loss (load balancing)
router_z_loss_coeff = 0.001# Router entropy (stability)# If load imbalance persists, increase aux lossif max_expert_usage / min_expert_usage > 2.0:
moe_loss_coeff = 0.1# Stronger load balancing# If training unstable, increase z-lossif grad_norm > 10.0:
router_z_loss_coeff = 0.01
5. Avoid Common Pitfalls
# ❌ Bad: Using same LR as dense model
optimizer = Adam(model.parameters(), lr=6e-4)
# ✅ Good: Lower LR for MoE
optimizer = Adam([
{'params': model.non_moe_params, 'lr': 6e-4},
{'params': model.moe_params, 'lr': 1e-4}
])
# ❌ Bad: No load balancing
loss = lm_loss
# ✅ Good: Add auxiliary loss
loss = lm_loss + 0.01 * aux_loss + 0.001 * z_loss
# ❌ Bad: Too many experts for small dataset
num_experts = 128# Overfitting risk# ✅ Good: Match experts to data diversity
num_experts = 8# Better for small datasets
Inference Optimization
Sparse Inference
# Only activate top-k experts (huge memory savings)@torch.no_grad()defmoe_inference(x, model, top_k=2):
"""Sparse MoE inference: only load k experts."""# Router
gate_logits = model.gate(x)
topk_scores, topk_indices = torch.topk(
torch.softmax(gate_logits, dim=-1),
k=top_k,
dim=-1
)
# Load and run only top-k experts
output = torch.zeros_like(x)
for i inrange(top_k):
expert_idx = topk_indices[:, i]
# Load expert from disk/offload if needed
expert = model.load_expert(expert_idx)
output += topk_scores[:, i:i+1] * expert(x)
return output