skip to content
← Scientific Spaces English

How to Estimate Matrix Spectral Norms More Scientifically?

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 , and without loss of generality, let . From the definition, the spectral norm represents the "expansion rate" of a linear layer—after an input vector passes through the linear layer, the norm of the output vector expands by at most a factor of . The spectral norm is also equal to the largest singular value of the matrix (for the proof, see "The Road to Low-Rank Approximation (2): SVD"), and we will not dwell on these basics here.

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 , its Lipschitz constant is precisely the spectral norm of the matrix , which gave rise to techniques such as spectral normalization and spectral regularization. Interested readers can refer to "Lipschitz Constraints in Deep Learning: Generalization and Generative Models".

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 to the leading right singular vector , iterations later, we have

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 . In addition, the results of power iteration are prone to fluctuations due to the initial value , so we can consider computing paths in parallel and taking the maximum.

If we simultaneously compute paths, and in each iteration replace L2 Normalize with orthogonalization of these vectors, that is,

then the results will converge to the top right singular vectors, from which we can subsequently obtain the top singular values. For the principle, refer to Implementing Muon with Streaming Power Iteration, Part 4: Principles. However, we will not elaborate on this extension in this article either, focusing our attention on estimating the spectral norm—that is, the largest singular value.

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 , which is already quite desirable in terms of complexity alone. If our purpose is merely monitoring, then the implementation above is sufficient. However, if our purpose is spectral normalization or spectral regularization, then we need the gradient of the spectral norm. Under the implementation above, it needs to backpropagate step by step along the loop of power iteration, which incurs a relatively high computational complexity.

In fact, in "Reflections from Spectral Norm Gradients to Novel Weight Decay", we had already derived the gradient of the spectral norm , so we can completely just define its custom gradient directly as to reduce the complexity of backpropagation. In addition, we can also exploit , directly outputting in the forward pass:

where is the stop-gradient operator. This way, during automatic differentiation, derivatives are taken only with respect to , and the result is precisely , avoiding backpropagation into 's internal power iteration.

No Waste #

Through steps of power iteration, we obtain the sequence of vectors , but in the end we only use to estimate , which seems rather "wasteful." Consequently, people have thought of utilizing all of them to form a better estimate of the spectral norm.

Barring surprises, are distinct from each other, but get closer and closer to , and we can think of them as " plus some noise." If pooled together for "denoising," it is indeed possible to achieve a more accurate result. Specifically, we consider using in a linear combination to build for a better approximation, and the subspace spanned by this set of vectors is called a "Krylov subspace."

For simplicity, we first perform an orthogonalization to obtain an equivalent orthonormal basis ; next, we want to find coefficients , such that the vector satisfies as closely as possible:

Multiplying both sides by yields , which indicates that are, respectively, an eigenvalue and eigenvector of , which means we only need to perform an eigenvalue decomposition on it, take its largest eigenvalue, and take the square root to obtain a better estimate. Since this is only a matrix, when is relatively small, performing an eigenvalue decomposition on it is by no means costly, so we can simply call an off-the-shelf function.

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.5

Empirical tests show that the effect of subspace acceleration is very evident: at , its accuracy already exceeds that of the original power iteration at , and it is faster. At , roughly 2/3 of this function's runtime comes from power iteration and 1/3 from QR decomposition, whereas eigenvalue decomposition accounts for only a very small fraction of the time.

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 —to construct the Krylov subspace acceleration. This can already secure most of the gains while reducing the computational load of the QR decomposition and eigenvalue decomposition, thereby achieving acceleration.

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 has the SVD , with singular values , then its Schatten- norm is defined as

being larger brings it closer to the exact value. When is even, computing the Schatten- norm is relatively feasible, which mainly relies on the following identities:

It is easy to see that the Schatten- norm is actually the Frobenius norm, which can be computed by taking the square root of the sum of elementwise squares. Therefore, the two identities above indicate that as long as we obtain , we can compute at relatively low cost and .

The first step, , has complexity , and subsequently multiplying it by itself repeatedly has a complexity of per step, so with a complexity of one can obtain . In theory, this complexity far exceeds that of power iteration; even when are relatively large, the first step alone far exceeds power iteration. Therefore, if we only want to estimate the spectral norm without requiring an upper bound, power iteration should still be given priority.

However, matrix multiplication can usually be fully parallelized, so when are not large or , the Schatten- norm will not run into a computational bottleneck, and we can consider it. Alternatively, when we strictly require an upper bound, this also seems to be the most direct route.

Numerical Stability #

To compute , a naive implementation starts from , performing repeatedly times to obtain , and then substitutes it into Eq. for the computation. However, because this is a hyperexponential operation, it quickly explodes to NaN or collapses to zero.

In general, itself will not explode numerically; the problem lies in explicitly computing . The solution is to renormalize after each squaring step [], which prevents numerical explosion and ensures that the scaling remains compact enough to avoid collapsing to zero. At the same time, we accumulate the normalization factor at each step in the log domain for use in the final calculation of .

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 , we need to compute, by some means, , which means we can simultaneously obtain , but ultimately we only use , which also feels somewhat 'wasteful'.

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, , compared to , is closer to the spectral norm, so we return as an upper bound on the spectral norm, leaving unused. But is really useless? Imagine if , then there are exactly two equations and two unknowns, and in theory we could solve for ! When , although we cannot obtain an exact solution, we can still narrow down the range of . Specifically, we have

That is, , which is essentially a quadratic inequality and easily solved to give

This yields, using and , an upper bound on , which is, compared to , a better estimate.《Fast Tight Spectral-Norm Bounds》 also generalizes this to a form that simultaneously uses , which is tighter but also more cumbersome; interested readers can consult the original post on their own. While using higher-order moments to improve the estimate is theoretically feasible, in practice it typically requires solving complex systems of nonlinear equations, which offers little practical value.

Limitations #

Theoretically, result can also be extended to use any and to obtain a more accurate upper bound:

However, when is relatively large, the practical significance of this result is very limited, because it requires explicitly computing and , which, when is large, will explode or collapse, which is not realistic. One improvement that can be considered is to factor out :

Then, setting , and performing an approximate expansion in the logarithmic domain:

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 , in fact, the accuracy of itself is already quite high, and using optimization ideas to further improve precision is of little significance.

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.