37 · GShard — MoE + Transformer 首次结合

MoE Distributed Training Lepikhin et al. · 2020
arXiv:2006.16668

上一代的问题:Sparse MoE (Shazeer 2017) used learned gating with RNNs, but no one had made MoE work with Transformers at scale. Dense Transformer scaling hit the wall — going from 2.3B to 600B was impractical due to O(L2) attention cost and memory bandwidth limits.

这代改了什么:First practical MoE Transformer. Replace every other FFN with a Top-2 gated expert layer. Use distributed sharding across TPU devices with All-to-All communication. Add auxiliary load balancing loss to ensure uniform expert utilization.

效果:600B MoE trained in 4 days on 2048 TPU v3 cores (22.4 TPU core-years). Quality far exceeded dense baseline. Direct predecessor of Switch Transformer and all modern MoE LLMs.

一、核心架构

1.1 MoE Transformer Layer

GShard takes a standard Transformer and replaces every other FFN with a Mixture-of-Experts layer. The core idea: instead of one dense FFN that processes every token with the same parameters, we have E expert FFNs (FFN1 ... FFNE) and a routing mechanism that sends each token to the most relevant experts.

G(x) = Softmax(KeepTop2(x · Wg, 2))
Gating function: project input to E-dimensional logits, keep only Top-2, apply softmax

Each token is routed to exactly 2 experts (Top-2 routing), and the outputs are weighted by the gate values:

y = Σi=1..E G(x)i · FFNi(x)
MoE output: weighted sum of the chosen experts' outputs

1.2 Expert Capacity

To ensure balanced computation across devices, each expert has a fixed capacity:

C = ceil(2 · N / E)
Expert capacity: each expert processes at most 2N/E tokens per batch

where N is the total number of tokens per batch and E is the number of experts. Tokens that overflow (routed to a full expert) skip the MoE layer via a residual connection, passing through unchanged.

Key design choice: Top-2 routing (not Top-1) provides a natural load balancing mechanism: if the top expert is full, the second expert can absorb the overflow. The residual skip ensures no token is lost, at the cost of some tokens not receiving expert computation.

1.3 Gating Variants

GShard explores two gating strategies:

二、分布式工程

2.1 Expert Sharding

GShard's key engineering contribution: how to distribute experts across a TPU pod (2048 cores) while keeping communication efficient.

Each expert is sharded across multiple devices. The standard configuration places one expert replica per group of devices. The communication pattern is All-to-All: tokens must be sent from their original device to the device hosting their chosen expert(s).

# GShard distributed MoE: token routing
# D = total devices, E = total experts
# Phase 1: Gate (local computation)
for each token x on device d:
    scores = softmax(top2(x @ W_g))        # E-dimensional gate vector
    expert_ids = argsort(scores)[:2]        # pick top 2 experts

# Phase 2: All-to-All communication
send(token_data, expert_ids)               # token → expert's device
receive(tokens, other_devices)             # expert receives its tokens

# Phase 3: Expert computation (local FFN)
for each assigned token:
    output += weight * FFN_expert(token)

# Phase 4: All-to-All return
send(output, original_device)              # result → token's device

2.2 Sub-linear Communication

GShard achieves sub-linear communication scaling: O(√D) for All-to-All on a 2D TPU mesh. This is crucial — if communication scaled linearly with D, adding more devices would not help.

2.3 SPMD Programming Model

GShard uses the Single Program, Multiple Data (SPMD) compiler strategy — one program runs on all devices, with three annotation APIs to control distribution:

三、Auxiliary Loss

A critical innovation: without explicit load balancing, the gate network would quickly learn to route all tokens to the same few experts, defeating the purpose of MoE. GShard introduces an auxiliary loss:

Laux = Σe=1..E (ce / S) · me
Auxiliary load balancing loss: penalizes imbalanced expert utilization

where:

The auxiliary loss is added to the main training loss with a small coefficient (typically 0.01). The term me makes the loss differentiable — without it, the routing decision (ce) is a discrete choice that blocks gradient flow.

四、实验与结果

4.1 Machine Translation

GShard trained a 600B parameter MoE model (dense: 6.7B parameters per layer, 64 experts) for multilingual translation (100 languages to English):

MetricDense BaselineGShard MoE (600B)Improvement
Average BLEU (100-En)36.944.3+13.5 (36.6%)
Training Cost235.5 TPU core-years22.4 TPU core-years10.5x more efficient
Training Timeestimated months4 dayson 2048 TPU v3 cores
Total Parameters~6.7B per layer~600B total~90x more capacity
Active Parameters~6.7B per layer~7.8B per layer~1.2x compute per token

4.2 Ablations

4.3 1T Parameter Attempt

GShard attempted a 1 trillion parameter configuration but encountered numerical stability issues. The model would diverge during training, suggesting scaling laws for MoE training are more complex than increasing parameters alone. This foreshadowed the need for careful initialization and training stability techniques used in later MoE models.

五、工程影响

5.1 Why GShard Matters

GShard demonstrated three critical things simultaneously:

  1. MoE + Transformer works at scale — the combination is not just theoretically appealing but practically trainable
  2. Compute efficiency breakthrough — 10x less training compute for better quality than dense scaling
  3. Distributed engineering blueprint — the All-to-All + SPMD pattern became the standard for all subsequent MoE training systems

5.2 The Active vs Total Parameters Gap

Total: 600B → Active per token: ~7.8B (1.16x dense baseline)
GShard's key efficiency: 90x more total capacity, only 1.16x more compute per token

This is the fundamental MoE trade-off: you get the representational capacity of a 600B model (because each expert specializes in different domains/patterns), but you only pay the compute cost of a 7.8B model per forward pass.

六、上下游关联

上一篇:Outrageously Large Neural Networks: The Sparsely-Gated MoE Layer (Shazeer et al., 2017) — introduced learned gating for MoE with RNNs. GShard adapts this to Transformers and solves the distributed training problem.

下一篇:Switch Transformer (Fedus et al., 2021) — simplifies Top-2 to Top-1 routing, simplifies the distributed communication, and scales to 1.6T parameters. GShard's auxiliary loss and expert capacity mechanisms are inherited.

引用/采用:Every modern MoE LLM builds on GShard's foundation:

七、个人思考

GShard is a systems paper disguised as a modeling paper. Its most impactful contribution is not the gating mechanism or the auxiliary loss (both adapted from prior work), but the engineering blueprint for distributed MoE: how to shard experts across devices, how to communicate tokens efficiently, and how to keep load balanced at 600B+ scale.

The numbers tell a clear story: training a 600B dense model would have cost 235+ TPU core-years and likely been impossible in practice. GShard's MoE achieved better quality with 10x less compute. This efficiency lesson is why every major LLM provider now has an MoE variant.

The 1T parameter failure is also instructive: scaling MoE isn't just about adding experts. The interaction between the routing mechanism and the optimizer creates new failure modes that don't exist in dense training. This is a recurring theme in deep learning — every new architecture class brings its own unique training pathologies.