Skip to content
AI360Xpert
Paper Breakdowns
Paper breakdown

GShard

GShard introduces conditional computation and automatic sharding, enabling the training of a 600-billion parameter Transformer using sparsely-gated MoEs.

Paper: GShard: Scaling Giant Models with Conditional Computation and Automatic Sharding

Authors: Dmitry Lepikhin, HyoukJoong Lee, Yuanzhong Xu, Dehao Chen, Orhan Firat, Yanping Huang, Maxim Krikun, Noam Shazeer, Zhifeng Chen · 2020

Read the paper
GShard partitions MoE layers across accelerators, distributing expert networks while keeping non-MoE layers replicated, enabling massive model capacity with sub-linear compute.
GShard partitions MoE layers across accelerators, distributing expert networks while keeping non-MoE layers replicated, enabling massive model capacity with sub-linear compute.

The Problem

Scaling neural networks consistently yields better performance, but training giant models across hundreds of accelerators is notoriously difficult. Data parallelism, where every device holds a full copy of the model, hits a hard wall when the model's memory footprint exceeds the capacity of a single chip. Standard model parallelism requires manual, brittle partitioning of the computation graph across devices, making code difficult to write, debug, and port. At the same time, scaling up dense models means scaling up computation linearly with parameter count, hitting a compute ceiling long before hitting the memory limit. The field needed a way to vastly increase model capacity without exploding the compute budget, and a compiler that could automatically map such a giant model onto a cluster without heroic manual engineering.

The Idea

GShard solves this by combining two major innovations: an algorithmic shift to sparsely-gated Mixture-of-Experts (MoE) Transformers, and a systems shift to compiler-driven automatic sharding. Instead of building a dense 600-billion parameter model that activates every weight for every token, the authors replaced the feed-forward networks in the Transformer with MoE layers. Each token is routed to only the top two "expert" networks out of thousands. To train this efficiently, they extended the XLA compiler with an annotation API. By adding just a few sharding annotations to the frontend code, the compiler automatically partitions the tensors and inserts the necessary cross-device communication primitives (like all-to-all operations).

How It Works

GShard's mechanism bridges the model architecture and the compiler backend.

Position-Based Routing In the MoE layers, a gating network calculates a probability distribution over all available experts for each token. To ensure load balancing and avoid all tokens piling onto a few popular experts, GShard introduces a specialized capacity limit and routing mechanism. Each expert is assigned a strict maximum number of tokens it can process in a batch. If an expert reaches capacity, overflow tokens are routed to the next best expert or dropped (passed through via residual connections).

Automatic Sharding via XLA Instead of manually splitting tensors in the Python code, developers use lightweight annotations (e.g., spt.shard) to declare how a tensor should be distributed across a logical mesh of devices. The XLA compiler reads these annotations and automatically transforms the computation graph. It handles the low-level MPI-style communications, emitting efficient All-Reduce and All-to-All instructions.

Expert Parallelism GShard implements "expert parallelism." While the non-MoE layers (like self-attention) are replicated across all devices (standard data parallelism), the MoE layers are sharded. Each accelerator in the cluster hosts only a small subset of the experts. When a batch of tokens reaches an MoE layer, the devices perform an All-to-All communication: tokens are sent over the network to the specific device holding their chosen expert, processed, and then sent back.

Why It Mattered

GShard provided a blueprint for training models that were previously thought impossible to fit or compute. By successfully training a 600-billion parameter multilingual translation model on a 2048-TPU v3 cluster, it proved that sub-linear compute scaling (via MoE) and automatic compiler sharding could work together in production. It drastically lowered the engineering barrier for distributed training, as developers no longer needed to manually write complex MPI routines or custom partitioning logic.

What Came After

GShard set the foundation for the MoE renaissance in large language models. Its compiler techniques directly influenced subsequent distributed training frameworks (like Mesh TensorFlow, JAX's pjit, and Alpa). The architectural findings paved the way for models like Google's Switch Transformer, which pushed the parameter count into the trillions, and later open-source MoE models like Mixtral 8x7B. The concept of separating the logical model definition from the physical device mapping is now a standard design pattern in modern AI compilers.