Gradients of Functions of Large Matrices

Nicholas Krämer, Pablo Moreno-Muñoz, Hrittik Roy, Søren Hauberg

TL;DR

This paper introduces a way to efficiently calculate gradients for functions of massive matrices by differentiating the Lanczos and Arnoldi iterations—the "workhorses" of numerical linear algebra. By deriving new adjoint systems, the authors enable "matrix-free" automatic differentiation that is both fast and memory-efficient, outperforming standard methods in Gaussian processes, Bayesian neural networks, and physics-informed machine learning.

What Problem Does This Paper Address?

Many advanced machine learning models rely on functions of matrices $f(A)v$, where the matrix $A$ is far too large to store in memory. Examples include computing the log-determinant of a Hessian for Bayesian neural network (BNN) calibration or the matrix exponential for solving partial differential equations (PDEs).

To handle these "large-scale" problems, researchers use matrix-free algorithms like the Lanczos (for symmetric matrices) or Arnoldi (for non-symmetric matrices) iterations. These methods only require the ability to compute matrix-vector products ($Av$), avoiding the need to ever materialize the full matrix $A$.

However, a major gap remained: how do you differentiate through these iterations? If the matrix $A$ depends on some parameters $\theta$, we need the gradient $\nabla_\theta f(A(\theta))v$ to train the model. Standard automatic differentiation (backpropagation) through these iterative algorithms is notoriously slow and memory-intensive, often making large-scale optimization unfeasible.

Methods

The authors solve this by deriving adjoint systems for the Lanczos and Arnoldi iterations. Instead of backpropagating through every step of the iterative process, the adjoint method treats the iteration as a constrained dynamical system.

Key technical contributions include:

  • Adjoint Derivation: They provide the first formal derivation of the adjoint systems for both Lanczos and Arnoldi iterations.
  • Linear Complexity: They show that the "backward pass" (computing the gradient) has the same linear time and memory complexity as the "forward pass."
  • Matrix-Free AD: The entire gradient computation is implemented using only matrix-vector products, maintaining the "matrix-free" nature of the original algorithms.
  • Numerical Stability: The authors demonstrate that reorthogonalization—a standard technique to maintain numerical stability in the forward pass—is equally critical for the accuracy of the adjoint pass.

The researchers implemented these methods in a JAX library called matfree, which allows users to easily integrate these differentiable linear algebra routines into modern ML workflows.

Figure 1: Values (down) and gradients (up) of functions of large matrices.

Datasets

The authors evaluated their method across several domains:

  • Gaussian Processes: Benchmarked on UCI datasets including elevators, protein, kin40k, and kegg (both directed and undirected versions).
  • Bayesian Neural Networks: Used a "Visual Attention Network" (VAN) with over 4 million parameters pre-trained on ImageNet-1k. They used places365 as an out-of-distribution dataset for evaluation.
  • Physics-Informed ML: Created a custom PDE dataset involving a 128x128 grid to learn coefficient fields in wave equations.
  • Runtime Benchmarks: Used the 'bcsstk18' matrix from the SuiteSparse collection to compare scaling against standard backpropagation.

Key Insights and Ideas

  • Algorithmic Symmetry: The adjoint system for Arnoldi iterations follows a structure remarkably similar to the forward pass (an $A^\top H - \Lambda H^\top$ structure). This means the same optimized code structures used for forward passes can be adapted for gradients.
  • Efficiency Gains: The computational cost of the adjoint method scales linearly with the number of iterations (Krylov-space depth), whereas naive backpropagation's cost can explode or hit memory limits very quickly.
  • Better than Specialized Solvers: In the PDE case study, the Arnoldi-based matrix exponential provided more accurate gradients for a fixed "budget" of matrix-vector products compared to specialized ODE adjoint methods like those in the Diffrax library.
  • Unlocking BNNs: For BNNs, the ability to compute gradients of the log-determinant of the full Generalized Gauss-Newton matrix (rather than a cheap diagonal approximation) led to significantly better likelihood calibration and out-of-distribution detection.
Figure 3: Backpropagation vs our ad- joint method on a sparse matrix [63–65].

Key Results

  • Speed: On sparse matrices, the adjoint method was orders of magnitude faster than naive backpropagation as the Krylov-space depth increased (see Figure 3).
  • Gaussian Processes: The method matched the performance of GPyTorch (the state-of-the-art for GPs) in terms of RMSE, while offering a more flexible, "black-box" approach to automatic differentiation.
  • Bayesian Neural Networks: The Lanczos-based calibration achieved a marginal likelihood of -154,475 compared to -876,444 for the standard diagonal approximation, a massive improvement in model fit.
  • PDE Training: The Arnoldi iteration achieved lower gradient errors than standard Runge-Kutta methods (Dopri5, Tsit5) for the same number of matrix-vector products, leading to faster and more stable training (see Figure 5/6).
Figure 5: Arnoldi’s superior convergence on the forward pass (A1) is inherited by the gradients (A2; mind the shared y-axis) and ultimately leads to fast training (B). For training, Arnoldi uses ten matrix-vector products, and the other two use 15 (so they have equal error ≈10−4 in A1 and A2.)

Limitations and Future Work

  • Memory Constraints: While the method is matrix-free regarding $A$, it still requires storing the orthogonal basis matrix $Q$ in memory. For extremely long iterations on very large vectors, this could still become a bottleneck.
  • Real-Valued Only: The current derivation is limited to real-valued matrices; extending this to complex arithmetic would be necessary for certain physics applications (e.g., quantum mechanics).
  • Partial Reorthogonalization: The paper assumes full reorthogonalization. Deriving gradients for more efficient "partial" reorthogonalization schemes remains an open challenge.
  • Forward-Mode AD: The work focuses exclusively on reverse-mode AD (gradients). A separate derivation would be needed for forward-mode (Jacobian-vector products).

Summary updated

BibTeX
@inproceedings{kramer2024gradients,
  author = {Nicholas Krämer and Pablo Moreno-Muñoz and Hrittik Roy and Søren Hauberg},
  title = {{Gradients of Functions of Large Matrices}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=RL4FXrGcTw},
}
Figure