Efficiently Scaling Transformer Inference

Reiner Pope, Sholto Douglas, Aakanksha Chowdhery, Jacob Devlin, James Bradbury, Jonathan Heek, Kefan Xiao, Shivani Agrawal, Jeff Dean

TL;DR

Deploying 500B+ parameter language models for low-latency, interactive inference requires rethinking traditional distributed parallelization schemes. This paper presents an analytical partitioning framework—combining 2D weight-stationary partitioning, weight-gathered layouts for prefill, and batch-sharded multiquery attention—enabling PaLM 540B to generate tokens in just 29 ms with up to 76% Model FLOPS Utilization (MFU) across 64 TPU v4 chips.

What Problem Does This Paper Address?

While training massive language models can leverage parallelization over thousands of tokens simultaneously, autoregressive generation produces tokens one by one. This introduces two distinct bottlenecks:

  1. Memory bandwidth saturation: Each decoding step must stream hundreds of billions of weights and gigabytes of dynamic Key-Value (KV) cache from High-Bandwidth Memory (HBM) to compute cores for just a single token.
  2. Communication limits: Standard 1D tensor parallelism (Megatron-style) incurs constant communication overhead regardless of how many chips are added, hitting a scalability wall beyond 8–16 accelerators.

Addressing tight latency targets (e.g., interactive chatbots) or high-throughput batch serving for 500B+ models therefore requires partitioning strategies tailored specifically to the differing demands of prompt processing (prefill) and autoregressive token generation (decode).

Methods

The authors introduce an analytical framework and low-level optimizations optimized for TPU v4 3D torus interconnects:

  • 2D Weight-Stationary Partitioning (Decode): Instead of slicing weight matrices along only one dimension (1D), weight matrices are sharded across two dimensions ($E \times F$). Alternating activation gathers and reductions reduces communication time from $O(1)$ to $O(1/\sqrt{n_{\text{chips}}})$, allowing efficient scaling past 64 accelerators.
  • Weight-Gathered Partitioning (Prefill): In large-batch prompt processing, activations dwarf model weights. The system switches to gathering weights dynamically while keeping activations partitioned across chips, reducing communication scaling to $O(\sqrt{BL})$.
  • Batch-Sharded Multiquery Attention (MQA): Standard multi-head attention shards KV caches across heads. However, multiquery attention uses a single shared key/value head across all query heads; sharding by head forces full replication of keys and values on every chip. The authors instead partition MQA over the batch dimension ($B$), reducing per-chip KV memory by a factor of $n_{\text{chips}}$ via an all-to-all collective.
  • Low-Level Scheduling & Quantization: Computation and communication are overlapped using Looped CollectiveEinsum kernels. Model weights are quantized to int8 via AQT to cut weight-loading time in half.

Datasets

This is an empirical systems paper focused on inference serving performance. It does not train new models or introduce NLP evaluation datasets. Performance benchmarks evaluated:

  • Synthetic / Standard Serving Benchmarks: Standard token configurations mirroring the FasterTransformer benchmark suite (e.g., 20-in/8-out, 60-in/20-out, and 128-in/8-out sequences).
  • Long Context Regimes: Prompt and generation sequence lengths ranging from 1,024 to 32,768 tokens.
  • Evaluated Architectures: The PaLM model family (8B, 62B, and 540B) and Megatron-Turing NLG 530B.

Key Insights and Ideas

  • Prefill and Decode Demand Different Topologies: Prefill is compute-bound and activation-dominated, favoring weight-gathered layouts. Decode is memory-bandwidth-bound and weight-dominated, favoring 2D weight-stationary layouts. The optimal deployment dynamically transitions between layouts between phases.
  • MQA Unlocks Long Contexts Only When Partitioned Correctly: Naive head-partitioning negates MQA's memory savings because the shared KV head is duplicated everywhere. Partitioning across the batch axis avoids replication entirely, unlocking context windows up to 32× longer than multi-head models.
  • Parallel Architecture Fuses Operations: Using PaLM’s parallel attention/MLP formulation allows fusing projection matrix multiplications and eliminates half of the inter-chip all-reduce communication steps compared to standard sequential blocks.

Key Results

  • Record Latency on PaLM 540B: On 64 TPU v4 chips with int8 weights, generation latency reached 29 ms per token at batch size 64.
  • Interactive End-to-End Latency: For an interactive chatbot task (1,920 cached tokens, 64 input tokens, 64 generated tokens), total turn-around time was 1.9 seconds.
  • High-Throughput Efficiency: Large-batch prefill reached 76% MFU, and large-batch end-to-end serving achieved 73% FLOPS efficiency.
  • Beating FasterTransformer: Scaled 64-way tensor parallelism to 44% MFU on 500B-class models, outperforming FasterTransformer baselines which capped out at 33% MFU on 32-way parallelism due to communication bottlenecks.
  • Context Scaling: Allowed PaLM 540B to handle context lengths up to 43,000 tokens (at batch size 128) by fitting within 30% reserved HBM for KV caching.

Limitations and Future Work

  • Minimum Batch Constraints: Batch-partitioned multiquery attention requires a batch size at least equal to the torus sub-dimension size (e.g., $\ge 4$ on TPU v4), offering no speedup for strict batch-1 decoding.
  • Weights-Only Quantization: While weights were quantized to int8, activations remained in bfloat16; 8-bit activation quantization could further lower compute times and communication volume.
  • Dense Scaling Ceilings: The authors note that interconnect bandwidth and dense FLOP scaling ultimately remain fundamental ceilings. Future work points to integrating these partitioning schemes with sparsity methods (e.g., Mixture-of-Experts) and adaptive compute.

Summary updated

BibTeX
@inproceedings{pope2023efficiently,
  author = {Reiner Pope and Sholto Douglas and Aakanksha Chowdhery and Jacob Devlin and James Bradbury and Jonathan Heek and Kefan Xiao and Shivani Agrawal and Jeff Dean},
  title = {{Efficiently Scaling Transformer Inference}},
  booktitle = {MLSys},
  year = {2023},
  url = {https://openreview.net/forum?id=Wxu5JhBt3S},
}

Loading related content...

Figure