Abstract
Shampoo is an online and stochastic optimization algorithm belonging to the AdaGrad family of methods for training neural networks. It constructs a block-diagonal preconditioner where each block consists of a coarse Kronecker product approximation to full-matrix AdaGrad for each parameter of the neural network. In this work, we provide a complete description of the algorithm as well as the performance optimizations that our implementation leverages to train deep networks at-scale in PyTorch. Our implementation enables fast multi-GPU distributed data-parallel training by distributing the memory and computation associated with blocks of each parameter via PyTorch's DTensor data structure and performing an AllGather primitive on the computed search directions at each iteration. This major performance enhancement enables us to achieve at most a 10% performance reduction in per-step wall-clock time compared against standard diagonal-scaling-based adaptive gradient methods. We validate our implementation by performing an ablation study on training ImageNet ResNet50, demonstrating Shampoo's superiority over standard training recipes with minimal hyperparameter tuning.
Introduction
Most deep learning practitioners rely on "diagonal" adaptive gradient methods like Adam or RMSProp. These methods scale well because they only track one value per parameter (the diagonal of the preconditioner matrix). However, they ignore correlations between different parameters, which limits their convergence speed. The "holy grail" of optimization is full-matrix preconditioning (like full-matrix AdaGrad), which captures these correlations. Unfortunately, for a model with $d$ parameters, full-matrix methods require $O(d^2)$ memory and $O(d^3)$ computation—costs that are prohibitive for modern models with millions or billions of variables.
Shampoo was proposed to bridge this gap. It uses a block-diagonal Kronecker product approximation to capture correlations within layers while keeping costs manageable. However, previous implementations were often tied to specific hardware or lacked the distributed plumbing needed for massive GPU clusters. This work presents a production-ready PyTorch implementation designed for distributed GPU environments.
Methodology
Kronecker Product Approximation
Instead of a giant $d \times d$ matrix, Shampoo represents the preconditioner for a layer as the product of two much smaller matrices ($L$ and $R$). If a layer has dimensions $m \times n$, Shampoo tracks an $m \times m$ and an $n \times n$ matrix. This captures the essential structure of the full-matrix preconditioner without the prohibitive computational cost.
Distributed Implementation and Memory Management
Preconditioner states can be massive. To handle this, our implementation uses PyTorch’s DTensor to shard these states across multiple GPUs. Each GPU only computes a subset of the search directions, and an AllGather operation combines them, preventing any single GPU from running out of memory.
To ensure efficiency, the work of computing preconditioners is distributed across GPUs using a greedy load-balancing algorithm. This ensures no single worker becomes a bottleneck during the computation-heavy phases of the optimization step.
Grafting and Amortized Computation
Two key heuristics make Shampoo practical for large-scale training:
- Grafting: The direction of the update comes from Shampoo, but the magnitude (norm) is "grafted" from a simpler optimizer like SGD or Adam. This allows Shampoo to inherit the stable learning rate schedules of well-known optimizers while benefiting from better search directions.
- Amortized Computation: Computing matrix root inverses—the most expensive part of Shampoo—is done periodically (e.g., every 50 steps) rather than every iteration. This "staleness" significantly reduces computational overhead without hurting final accuracy.
Key Results
The implementation was evaluated on ImageNet-1k using a ResNet50 architecture (25.5M parameters), comparing Distributed Shampoo against a well-tuned SGD with Nesterov momentum baseline.
- Faster Convergence: Shampoo achieved the target validation accuracy in 60 epochs, whereas the Nesterov SGD baseline required 90 epochs.
- Wall-Clock Efficiency: Despite the additional mathematical complexity, the 60-epoch Shampoo run resulted in a 1.35x reduction in total wall-clock time compared to the 90-epoch SGD run.
- Robustness: Shampoo showed significantly less variability in accuracy and loss across different random seeds, suggesting it is a more robust optimizer than standard first-order methods.
- Low Overhead: With periodic updates, the time overhead per step was kept under 10% relative to standard diagonal methods.
Conclusion
This PyTorch implementation demonstrates that second-order optimization is not just theoretically interesting but practically viable for large-scale training on homogeneous GPU clusters. By leveraging Kronecker product approximations, distributed state management via DTensor, and amortized computation, Shampoo provides a significant speedup in convergence with minimal wall-clock overhead. Future work will explore extending these benefits to Transformer architectures and further optimizing performance through PyTorch 2.0 integration.
Summary updated