Training Costs and Engineering Practices of Mixture of Experts Large Models
This article explores the Mixture of Experts (MoE) architecture in large language models, analyzing its training costs, parameter dynamics, and engineering practices. It covers the distinction between total and active parameters, provides a cost estimation framework using real-world metrics, and details key engineering challenges such as parallel strategies, load balancing, numerical precision, and fault tolerance. Finally, it discusses inference and serving considerations, alongside a practical implementation checklist.
1. Why Large Models Are Adopting the Mixture of Experts Architecture
As the parameter scale of large models continues to grow, training and inference costs rise rapidly. Dense models activate all parameters during every forward pass, meaning more parameters lead to higher computational costs per token. The Mixture of Experts (MoE) architecture splits the feedforward network into multiple expert subnetworks, and a router selects only a few experts to process each token. This allows the model to maintain a massive total parameter count while keeping the computational overhead of a single pass manageable.
Understanding this mechanism is the starting point for evaluating the cost and performance of any MoE model.
2. Distinguishing Total Parameters from Active Parameters
When evaluating MoE models, two metrics require attention:
- Total parameters: The entire scale of the model weights, which sets the lower bound for storage and memory requirements.
- Active parameters: The parameters actually involved in computation for each token, directly impacting the computational load and inference speed per token.
The ratio between these two reflects the degree of sparsity. A lower ratio indicates a more favorable trade-off between knowledge capacity and computational cost. However, if the routing design is inadequate, some experts may become overloaded while others remain idle, offsetting the performance gains.
The following example calculates the activation ratio for a typical configuration:
total_params = 671e9 # Total parametersactive_params = 37e9 # Active parameters per tokenratio = active_params / total_paramsprint(f"Activation ratio: {ratio:.2%}") # Outputs about 5.51%
The calculation shows that a single forward pass uses only about 5% of the weights, yet the model retains its full parameter capacity.
3. Training Compute Power and Cost Estimation
The core method for calculating training costs is as follows:
- GPU hours = Number of training GPUs × Training hours per GPU
- Total training cost ≈ GPU hours × Cost per GPU hour
Taking a publicly disclosed case as an example: 2,048 GPUs participated in the training, consuming a total of approximately 2,788,000 GPU hours, of which the pre-training phase accounted for about 2,664,000 GPU hours. On average, each GPU ran for about 1,361 hours, or roughly 56.7 days. If the publicly stated R&D cost was $5.576 million, the cost per GPU hour was approximately $2.
gpus = 2048gpu_hours_total = 2788e3 # Total GPU hourscost_total_usd = 557.6e4 # Public total cost (USD)hours_per_gpu = gpu_hours_total / gpusdays_per_gpu = hours_per_gpu / 24cost_per_gpu_hour = cost_total_usd / gpu_hours_totalprint(f"Training hours per GPU: {hours_per_gpu:.1f}") # About 1361.3print(f"Equivalent days: {days_per_gpu:.1f}") # About 56.7print(f"Cost per GPU hour: {cost_per_gpu_hour:.2f}") # About 2.00 USD
When making estimates, note the following three points:
- Publicly disclosed costs typically only cover compute expenditure and do not include labor, data collection and cleaning, failed experiments, or infrastructure depreciation.
- Actual training duration is affected by parallel efficiency. Multi-GPU scaling rarely achieves ideal linear acceleration, and communication overhead increases with scale.
- The price per GPU hour varies significantly based on procurement methods, geographic location, and contract terms. Budgeting should rely on your own quotes rather than directly applying figures from others.
4. Key Engineering Aspects of Training
Large-scale MoE training involves multiple coupled engineering issues, typically requiring simultaneous management of the following four areas:
1. Parallel Strategies
Data parallelism, tensor parallelism, pipeline parallelism, and expert parallelism are often used in combination. Expert parallelism places different experts on different devices, necessitating cross-card all-to-all communication at each layer to distribute and retrieve tokens. When communication bandwidth is insufficient, this step often becomes the training bottleneck.
2. Load Balancing
The router might allocate most tokens to a few experts, causing some devices to idle while others overload. Common practices include:
- Adding auxiliary balancing terms to the loss function.
- Setting an upper limit on expert capacity, where excess tokens are dropped or transferred based on strategy.
- Adopting bias adjustment methods without auxiliary loss to dynamically correct the routing distribution without interfering with the main loss.
3. Numerical Precision
Low-precision formats (such as BF16, FP8) can reduce memory usage and bandwidth pressure. However, low-precision training is prone to gradient overflow or precision loss. Therefore, critical operators (e.g., accumulation, normalization) usually retain high-precision computation, and gradient norms and loss curves are continuously monitored.
4. Fault Tolerance and Checkpoints
Hardware failures are not uncommon in large-scale clusters. The training process should save checkpoints at fixed step intervals and ensure recovery from the most recent checkpoint. The checkpoint saving frequency requires a trade-off between storage overhead and lost training progress.
CHECKPOINT_INTERVAL = 1000 # Save every 1000 stepsdef train_loop(model, optimizer, data_loader, start_step=0):for step, batch in enumerate(data_loader, start=start_step):loss = model.forward_backward(batch)optimizer.step()if step % CHECKPOINT_INTERVAL == 0:save_checkpoint(model, optimizer, step)
5. Inference and Serving
After training, the model enters the inference phase, shifting the focus to:
- Long Context: As the context lengthens, the memory footprint of the KV cache grows. Common practices involve using paged management for the cache and combining it with quantization compression to reduce usage.
- Throughput and Latency: MoE inference requires distributing experts across multiple GPUs, where cross-card communication latency affects the time to first token. Batching strategies should dynamically adjust based on request length and concurrency.
- Deployment Methods: Options include private deployment or calling cloud-based APIs. When selecting, compare the cost per token, latency percentiles (e.g., P95, P99), and data compliance requirements.
6. Implementation Checklist
- Clarify the relationship between total parameters, active parameters, and memory requirements to select the appropriate hardware scale.
- Use the GPU hours formula to build a training budget, leaving ample margin for failed reruns.
- Continuously monitor expert load distribution and adjust routing and balancing strategies promptly if long-term imbalance is detected.
- Establish unified version management for checkpoints, training data versions, and evaluation sets.
- Use independent evaluation sets to validate performance before launch, and record inference latency and cost metrics.
