Fast Differentiable Matrix Square Root

Yue Song, Nicu Sebe, Wei Wang

This paper by Yue Song and colleagues tackles a persistent bottleneck in deep learning: the computation of the matrix square root. While it might sound like a niche mathematical operation, it is the engine behind powerful techniques like ZCA whitening, decorrelated batch normalization, and global covariance pooling.

TL;DR

The authors propose two new, highly efficient methods—Matrix Taylor Polynomial (MTP) and Matrix Padé Approximants (MPA)—to compute differentiable matrix square roots and their inverses. By combining these with a novel backward pass that solves the Lyapunov equation via the matrix sign function, they achieve massive speedups (up to 27x over SVD) while maintaining or improving accuracy in tasks like second-order Vision Transformers.

What Problem Does This Paper Address?

In modern computer vision, we often want to transform feature variances or whiten data to remove correlations. Mathematically, this requires finding the square root of a positive semi-definite matrix $A$ (denoted $A^{1/2}$) or its inverse ($A^{-1/2}$).

Conceptual overview of ZCA whitening and the performance tradeoffs of various matrix square root methods.

Currently, researchers have two main choices, both with significant downsides:

  1. Singular Value Decomposition (SVD): Highly accurate but notoriously slow on GPUs. Furthermore, its gradients can "explode" (become infinite) when the matrix has eigenvalues that are very close to each other, leading to training instability.
  2. Newton-Schulz (NS) Iteration: A more GPU-friendly approximation. However, it is still computationally expensive, requiring many matrix multiplications in both the forward and backward passes to reach a good approximation.

The gap is clear: the field needs a method that is fast, stable, and accurate enough for deep neural networks to use in real-time during training.

Methods

The authors split their solution into two parts: a faster "forward" pass to get the square root and a leaner "backward" pass to calculate the gradients.

The Forward Pass: MTP and MPA

Instead of the standard NS iteration, the authors propose:

  • Matrix Taylor Polynomial (MTP): Uses a Taylor series expansion to approximate the square root. It’s very fast but can be numerically unstable if not handled carefully.
  • Matrix Padé Approximants (MPA): This is the "star" of the paper. It uses a rational approximation (a polynomial divided by another polynomial). MPA converges much faster and more accurately than Taylor series or NS iterations for the same amount of computation.

The Backward Pass: The Lyapunov Solver

The real breakthrough comes in the gradient calculation. The authors observe that the partial derivative of a matrix square root satisfies a continuous-time Lyapunov equation ($BX + XB = C$).

Instead of using the standard, heavy-duty backpropagation through the NS iterations (which requires 10 matrix multiplications per step), they solve this Lyapunov equation iteratively using the matrix sign function. This approach is significantly more efficient and avoids the "for-loop" bottlenecks found in SVD-based gradients.

Key Insights and Ideas

  • Rational > Polynomial: The move from simple polynomials (Taylor) to rational functions (Padé) allows for a much tighter approximation of the square root function with fewer operations.
  • Leveraging Classical Control Theory: By framing the gradient problem as a Lyapunov equation, the authors were able to pull a solution from classical numerical analysis that is much more efficient than the "automatic differentiation" typically used in deep learning frameworks.
  • GPU Optimization: The proposed methods rely almost entirely on batched matrix multiplications, which are the "bread and butter" of GPU hardware, unlike SVD which often requires sequential processing.

Key Results

The authors validated their methods across several standard benchmarks, including CIFAR-10/100 for Decorrelated Batch Normalization, ImageNet for Second-order Vision Transformers (So-ViT), and synthetic data for stress tests.

Performance comparisons showing the massive speedup of MPA-Lya over SVD across different batch sizes and matrix dimensions.
  • Speed: For a batch size of 64, their MPA-Lya method is 2.58x faster than the Newton-Schulz iteration and a staggering 27.25x faster than the standard SVD implementation.
  • Accuracy: In numerical tests, MPA achieved lower Mean Absolute Error (MAE) than the NS iteration, even when using fewer computational steps.
  • Performance: On ImageNet, the MPA-based Vision Transformer (So-ViT-10) achieved a top-1 accuracy of 78.83%, slightly outperforming SVD-based versions while training significantly faster.

Limitations and Future Work

  • Algorithmic Inconsistency: The authors acknowledge that their forward pass (MPA) and backward pass (Lyapunov solver) are not mathematically "consistent"—meaning the backward pass isn't the exact derivative of the specific approximation used in the forward pass. While they show this doesn't hurt performance in practice, it might be a concern for certain theoretical applications.
  • Scope: The paper focuses primarily on square roots. While they mention extensions to the inverse square root, the primary evaluation is on covariance-related tasks.
  • Future Directions: The authors suggest extending this framework to other differentiable matrix functions, such as matrix logarithms or exponents, which are used in neural style transfer and manifold learning.

Summary updated

BibTeX
@inproceedings{song2022fast,
  author = {Yue Song and Nicu Sebe and Wei Wang},
  title = {{Fast Differentiable Matrix Square Root}},
  booktitle = {International Conference on Learning Representations},
  year = {2022},
}
Figure