Shampoo: Preconditioned Stochastic Tensor Optimization

Vineet Gupta, Tomer Koren, Yoram Singer

Shampoo is a structure-aware preconditioning algorithm that bridges the gap between simple diagonal optimizers (like Adam) and computationally expensive second-order methods. By treating model parameters as tensors and maintaining separate preconditioners for each dimension, it achieves significantly faster convergence with a runtime per step that remains competitive with standard first-order methods.

The Optimization Dilemma

In deep learning, practitioners usually have to choose between two extremes for optimization:

  1. First-order methods (SGD, Adam, AdaGrad): These are computationally "cheap" because they treat parameters as a flat vector and only use diagonal information. However, they ignore the correlations between different parameters, which can lead to slow convergence.
  2. Second-order methods (Newton's Method): These use a "preconditioner" (like the Hessian) to capture parameter correlations. While powerful, a full preconditioner for a model with millions of parameters ($d$) requires $O(d^2)$ space and $O(d^3)$ time, making it impossible to use in practice.

The authors address this bottleneck by exploiting the fact that neural network parameters aren't just flat vectors—they are tensors (e.g., a weight matrix in a dense layer or a 4D kernel in a convolutional layer). Shampoo uses this structure to create a "structured" preconditioner that is much smaller than the full matrix but far more informative than a simple diagonal.

The Shampoo Algorithm

Shampoo maintains a set of small preconditioning matrices, one for each dimension of a parameter tensor. Most optimizers "flatten" the world, losing the spatial and structural relationships inherent in layers. Shampoo preserves this, allowing it to capture "intra-tensor" correlations.

Tensor Contraction in Shampoo
  • Tensor Contraction: For a parameter tensor of shape $n_1 \times n_2 \times \dots \times n_k$, Shampoo maintains $k$ separate matrices $H_i$ of size $n_i \times n_i$.
  • The Update Rule: Instead of multiplying the gradient by one giant matrix, Shampoo "contracts" the gradient tensor against these smaller matrices. In the matrix case ($m \times n$), the update looks like: $W_{t+1} = W_t - \eta L_t^{-1/4} G_t R_t^{-1/4}$, where $L$ and $R$ are the left and right preconditioners.
  • Kronecker Product Approximation: Mathematically, this is equivalent to approximating a massive $mn \times mn$ preconditioner using the Kronecker product of two much smaller matrices.
  • The $1/2k$ Root: A key theoretical contribution is the use of the $1/2k$ root for a $k$-order tensor (e.g., $1/4$ for matrices). This specific exponent is required to maintain the standard $O(1/\sqrt{t})$ convergence rate while ensuring the preconditioner remains stable.
  • Efficiency Heuristics: To keep the overhead low, the authors only recompute the matrix roots (SVD/Eigen-decomposition) every 20–100 steps. They also fall back to a diagonal update if a specific dimension is too large (e.g., > 1200).

Experimental Setup

The authors evaluated Shampoo on standard computer vision and natural language processing benchmarks:

  • CIFAR-10: Tested using ResNet-32 and Inception architectures.
  • CIFAR-100: Tested using a 55-layer ResNet.
  • LM1B (One Billion Word Benchmark): Tested using a Transformer model for statistical language modeling.

Key Results

Shampoo demonstrates that one might expect matrix inversions and SVDs to be slow, but because the dimensions of individual layers are relatively small (e.g., 256 or 512), these operations are extremely fast on modern GPUs compared to the cost of the forward/backward pass.

Shampoo Performance on Transformer/LM1B
  • Faster Convergence: In almost all tests, Shampoo reached lower training loss significantly faster (in terms of epochs) than SGD, AdaGrad, and Adam. For example, on the Transformer/LM1B task, Shampoo's loss curve dropped much more sharply than its competitors.
  • Competitive Wall-clock Time: Despite the extra math, Shampoo's "steps per second" were remarkably close to Adam and SGD. In the ResNet-55 experiment, Shampoo was actually faster in terms of steps per second, likely due to implementation efficiencies in TensorFlow's tensor operations.
  • Theoretical Soundness: The authors provide a regret bound showing that Shampoo is guaranteed to converge in the stochastic convex setting at the optimal rate.

Limitations and Future Work

While highly effective, Shampoo does come with a few trade-offs:

  • Memory Overhead: While much better than full-matrix methods, Shampoo still requires more memory than Adam to store the $H_i$ matrices.
  • Hyperparameter Sensitivity: While the paper suggests a default learning rate of 1.0 for some tasks, second-order-like methods can sometimes be more sensitive to tuning than robust first-order methods.
  • Inter-Tensor Correlations: The current version of Shampoo treats each layer's weight tensor independently. It does not capture correlations between different layers of the network.
  • Implementation Complexity: Implementing Shampoo requires efficient tensor contraction and matrix root kernels, which are more complex to write from scratch than a simple SGD update.

Summary updated

BibTeX
@inproceedings{gupta2018shampoo,
  author = {Vineet Gupta and Tomer Koren and Yoram Singer},
  title = {{Shampoo: Preconditioned Stochastic Tensor Optimization}},
  booktitle = {International Conference on Machine Learning},
  year = {2018},
  url = {https://openreview.net/forum?id=rybDZ2-_-S},
}
Figure