The spectral norm is a core concept in matrix analysis and also plays an important role in deep learning—from the Lipschitz constraints required back in the WGAN era, to the training stability of LLMs today, to the burgeoning Muon optimizer, all are closely related to the spectral norm of matrix parameters. Therefore, how to estimate the spectral norm efficiently and accurately has become increasingly important and is well worth our in-depth study.
As is well known, power iteration is the standard scheme for estimating the spectral norm, but there is still plenty of room for improvement. In this post, I will briefly organize some ideas for estimating the spectral norm, including improving the convergence speed of power iteration and estimating strict upper bounds on the spectral norm, among others.
Spectral Norm #
The spectral norm is defined as
where
I first encountered the spectral norm back in the days when GANs were popular, when WGAN "burst onto the scene," emphasizing the necessity of Lipschitz constraints on the discriminator. Meanwhile, for a linear layer
In recent years, the increasingly popular Muon optimizer has also had a close connection with the spectral norm, often being viewed as steepest descent under the spectral norm. At the same time, in Beyond MuP, Part 4: Maintaining Parameter Stability, we also proposed constraining the spectral norm of parameters to ensure training stability. All of these create a demand for accurate computation of the spectral norm.
Power Iteration #
Computing the spectral norm via SVD is the most direct approach, but it is obviously too expensive. We usually use power iteration
It converges at a rate of
The proof can be found in "Reflections from Spectral Norm Gradients to Novel Weight Decay". In practice, if we only care about the spectral norm rather than the singular vectors, the convergence rate is often more optimistic than
If we simultaneously compute
then the results will converge to the top
Computing Gradients #
First, here is a reference implementation of power iteration based on Jax:
import jax, jax.lax as lax
import jax.numpy as jnp
@jax.jit(static_argnums=(1,))
def l2_normalize(x, axis=-2):
return x / jnp.linalg.vector_norm(x, axis=axis, keepdims=True)
@jax.jit(static_argnums=(1, 2, 3))
def spec_norm_v1(w, T=10, k=1, key=42):
v_shape = w.shape[:-2] + w.shape[-1:] + (k,)
v_init = jax.random.normal(jax.random.PRNGKey(key), v_shape)
v_step = lambda i, v: l2_normalize(w.mT @ (w @ v))
v = lax.fori_loop(0, T, v_step, v_init)
return jnp.linalg.vector_norm(w @ v, axis=-2).max(axis=-1)Clearly, the complexity of power iteration is
In fact, in "Reflections from Spectral Norm Gradients to Novel Weight Decay", we had already derived the gradient of the spectral norm
where
No Waste #
Through
Barring surprises,
For simplicity, we first perform an orthogonalization to obtain an equivalent orthonormal basis
Multiplying both sides by
This idea is equivalent to a simplified version of the "Lanczos algorithm" in modern SVD solvers; practical SVD involves more and finer treatments. In addition, this acceleration idea also shares similarities with Randomized SVD: Randomized SVD typically projects using a Gaussian random matrix, whereas here the projection is done using an orthonormal basis from the Krylov subspace.
Acceleration Technique #
Reference code for power iteration incorporating Krylov subspace acceleration is as follows:
@jax.jit(static_argnums=(1, 2))
def spec_norm_v2(w, T=10, key=42):
v_shape = w.shape[:-2] + w.shape[-1:] + (1,)
v_init = jax.random.normal(jax.random.PRNGKey(key), v_shape)
v_step = lambda v, _: (l2_normalize(w.mT @ (w @ v)),) * 2
v = lax.scan(v_step, v_init, length=T)[1].swapaxes(0, -1)[0]
v = jnp.linalg.qr(v)[0]
return jnp.linalg.eigvalsh((u := w @ v).mT @ u)[..., -1]**0.5Empirical tests show that the effect of subspace acceleration is very evident: at
Readers who have followed the "Streaming Power Iteration" series might think of using the Cholesky QR introduced there to speed up the QR decomposition. Unfortunately, because the results of power iteration become increasingly collinear, the condition number of the matrix to undergo QR decomposition continuously deteriorates. This happens to be the exact regime where Cholesky QR is "powerless", with an extremely high failure rate, so it basically cannot be used for acceleration.
A more effective acceleration approach is to use only the results from the last few steps of power iteration—for instance, the last three steps
Estimating the Upper Bound #
Strictly speaking, power iteration and its accelerated versions can only obtain a lower bound on the spectral norm, which is certainly sufficient in many cases. But if, in certain scenarios, we must obtain an upper bound—such as wanting to ensure that a matrix's norm is strictly less than 1 via spectral normalization—we need to look elsewhere.
In such cases, a basic approach is to compute the Schatten norm, which is precisely an upper bound on the spectral norm. Suppose matrix
It is easy to see that the Schatten-
The first step,
However, matrix multiplication can usually be fully parallelized, so when
Numerical Stability #
To compute
In general,
The computational procedure is summarized as follows:
A simple reference implementation:
@jax.jit
def tr(w):
return w.trace(axis1=-1, axis2=-2)[..., None, None]
@jax.jit(static_argnums=(1,))
def spec_norm_v3(w, T=5):
m = (m := w.mT @ w) / (s := tr(m))
ms_step = lambda i, ms: ((m := ms[0] @ ms[0]) / (s := tr(m)), 2 * ms[1] + jnp.log(s))
m, logs = lax.fori_loop(0, T, ms_step, (m, jnp.log(s)))
logf = 0.5 * jnp.log((m**2).sum(axis=[-1, -2], keepdims=True))
return jnp.exp((logs + logf) / 2**(T + 1))Multiple Moments #
To compute
Is there a way, just like the Krylov subspace methods in power iteration, to make use of all these results to improve estimation accuracy? As it turns out, there is!《Fast Tight Spectral-Norm Bounds》 provides an idea for improving estimation results via nonlinear programming. Let us first look at a simple case: suppose we have computed the 2nd and 4th moments of the singular values,
Then, according to the results of the previous two sections,
That is,
This yields, using
Limitations #
Theoretically, result
However, when
Then, setting
However, our goal is to find an upper bound on the spectral norm, and ensuring the upper-bound property while performing an approximate expansion appears quite complicated. On the other hand, if we have already computed up to a relatively large
Conclusion #
This article has briefly summarized several approaches to estimating the spectral norm. In practical applications, if one only needs to monitor the spectral norm approximately, power iteration and its subspace-accelerated variants are usually sufficient; if a strict upper bound must be guaranteed, one can consider computing Schatten norms. The "waste nothing" philosophy extended from both routes—Krylov subspaces utilizing iteration history, and nonlinear programming utilizing multi-order moment information—also provides us with great inspiration for algorithm design.