skip to content
← Scientific Spaces English

Implementing Muon with Streaming Power Iteration, Part 2: Acceleration

In the first article, Implementing Muon with Streaming Power Iteration, Part 1: A First Encounter, I abstracted streaming power iteration out as a new implementation of Muon. Because the new scheme directly approximates the SVD, it offers richer room for extension compared to the standard implementation based on Newton-Schulz iteration, making it well worth deeper exploration.

Computationally, the main change in the new scheme is that the Newton-Schulz iteration is replaced with decomposition, which brings some slowdown. In the previous post, we already discussed some basic acceleration methods, but they have not yet caught up with the standard implementation. In this article, we continue to investigate acceleration, striving to narrow the gap as much as possible.

Streaming Iteration #

We will retain all concepts and notation from the first article; readers with questions should refer back to it first. To begin with, the update formula for Muon is

where is standardly implemented via Newton-Schulz iteration, which is also the most expensive computation in the Muon optimizer. In contrast, the update formula for the streaming power iteration scheme is

If one repeatedly executes , that is standard power iteration, and the result will converge to 's right singular matrix, thereby realizing 's SVD, and in turn computing . However, executing a full power iteration at every step is overly expensive, so instead we cache the result of the previous step, , and then perform only one iteration at each step as an approximation; this is the meaning of "streaming".

Now the most expensive operation becomes decomposition. The most naive implementation is naturally to call the built-in QR function, whose underlying principle is the Householder transformation; its stability is very good, but its speed is relatively slow.

First Speedup #

To accelerate, in the previous post we introduced Cholesky QR, which splits the QR decomposition of the matrix into two steps: 1. Perform Cholesky decomposition on to obtain the upper triangular matrix ; 2. Solve the equation to obtain the orthogonal matrix . Both steps are theoretically very efficient, but in actual computation, they will fail if the condition number is too large. For this reason, we also introduced the Shift trick, which adds to a regularization term ( ) to reduce the condition number.

Combining the two, we refer to it in short as "SCQR (Shifted Cholesky QR)"; a reference implementation based on JAX is as follows:

import jax.numpy as jnp
from jax.scipy.linalg import solve_triangular
from jax import lax

def shift(A, eps=1e-9):
    return A + eps * jnp.linalg.matrix_norm(A, keepdims=True) * jnp.eye(A.shape[-1])

def scqr(A, eps=1e-9):
    """First compute with Shifted Cholesky QR; fall back to the default QR if it fails."""
    R = jnp.linalg.cholesky(shift(A.mT @ A, eps), upper=True)
    Q = solve_triangular(R.mT, A.mT, lower=True).mT
    return lax.cond(jnp.isfinite(Q).all(), lambda: Q, lambda: jnp.linalg.qr(A)[0])

Note that the smaller is, the more easily SCQR fails, whereas the larger is, the further the result deviates from orthogonality and the worse the performance, so must be "just right", which causes this scheme to still have a relatively high probability of falling back to standard QR. In addition, the previous post mentioned that adding to power iteration (i.e., changing to ) can stabilize training results, with an effect even more pronounced under SCQR.

Full Precision #

The above is basically the entirety of the first article. All we can say is that it indeed ran the whole process through and verified feasibility, and compared to directly calling the framework's built-in QR decomposition function, SCQR also provided some speedup, but the speed was still noticeably slower than the Newton-Schulz iteration implementation of , so we still have to find ways to speed it up.

This section introduces the first speedup trick—enabling "full-fledged" FP32 precision matrix multiplication. First, it should be pointed out that the several newly added steps in streaming power iteration are all computed in FP32 precision. However, starting with the introduction of the TF32 format on the A100, when FP32 arrays perform matrix multiplication in certain frameworks (such as Jax, which I use to run small experiments, or certain versions of Torch), they are converted to the TF32 format by default to accelerate computation; achieving true FP32 precision multiplication requires enabling it manually.

Some readers might wonder: shouldn't increasing multiplication precision slow things down, so how does it speed things up instead? This is indeed very counterintuitive, but it is actually not hard to understand: lowering matrix precision often increases its condition number, thereby increasing the probability of SCQR failure and the chances of falling back to standard QR, which increases elapsed time; conversely, improving precision increases the success rate of SCQR, and QR happens to be the most time-consuming part, so the total time actually becomes shorter.

Documentation indicates that Jax has always defaulted to using TF32 for FP32 multiplications, so one needs to use jax.config.update('jax_default_matmul_precision', 'highest') to manually enable it; Torch is a bit more complicated: versions 1.7 to 1.11 defaulted to TF32 multiplication, but starting from 1.12, FP32 multiplication became the default. Considering that Torch is now already at version 2.11, presumably most users will not need to manually enable it.

Double Orthogonalization #

The second speedup trick I thought of is adding an extra orthogonalization step for the matrix of left singular vectors, that is, changing this power iteration step to

This step is likewise very counterintuitive: adding an extra , yet the speed ends up faster in the end. The reason is similar to the previous section: both work by reducing the condition number of the matrix to be decomposed, thereby increasing the success rate of SCQR. Since SCQR itself is extremely fast, executing it twice adds very little time, and instead significantly speeds things up by drastically reducing the number of fallbacks to standard QR.

Understanding this acceleration trick consists of two parts: first, adding this extra step theoretically does not change the power iteration; second, adding this extra step indeed reduces the condition number. The first question is easy to understand: if , then , where is also an upper triangular matrix, meaning the QR decomposition can be written in the form of right-multiplying by an upper triangular matrix, so

By the uniqueness of the QR decomposition, right-multiplying by an upper triangular matrix does not change the result of , so theoretically it is equivalent to .

As for the condition number, it equals the ratio of the matrix's largest singular value to its smallest singular value. If a single step is performed, then the matrix to undergo Cholesky decomposition is ; note that orthogonal transformations do not change singular values, and thus do not change the condition number, so at this point the condition number of the matrix to be decomposed reaches that of raised to the 4th power! If two steps are performed, then the matrix to undergo Cholesky decomposition will be , where is the first orthogonal matrix, at which point the condition number is only that of squared, which is significantly lower.

Shift Invariance #

The third acceleration trick is something I, together with @YouJiacheng , arrived at during our discussions; it leverages the shift invariance of eigenvector bases. As we know, the power iteration can also be understood as finding the eigenvector matrix of the positive-definite matrix , and positive-definite matrices have a property—adding a multiple of the identity matrix to them leaves the eigenvector matrix unchanged.

In other words, and have the same matrix of eigenvectors, so we can change the power iteration to

without changing the convergence outcome of the power iteration. So what is the benefit of taking and adding to it? The answer is likewise to reduce the condition number, namely , so it can also improve the success rate of Cholesky QR. Note that here we are referring to Cholesky QR rather than SCQR, because setting an appropriate on the outside guarantees the condition number without needing another Shift; this ensures that the resulting outcome is strictly orthogonal, which is also a nice property.

But don't celebrate too early. being larger naturally makes Cholesky QR more likely to succeed, but at the same time it also reduces the convergence speed of the power iteration! This is because the convergence speed of the power iteration depends on the ratio of adjacent singular values: the smaller is, the faster the convergence (with singular values sorted from largest to smallest), whereas , so the larger is, the slower the power iteration converges, and the final performance also deteriorates.

Therefore, we must carefully tune the value of to balance the success rate of Cholesky QR against the convergence speed of the power iteration. In my tests, I found that choosing and yields relatively good results. Another approach is to use a larger to guarantee the Cholesky QR success rate, and then iterate two steps to improve the convergence speed of the power iteration, namely

This can account for both Cholesky QR and the power iteration, with the natural cost of requiring two operations per step.

Multi-Step Correction #

The fourth acceleration trick is called "SCQR2", which is a general multi-step correction technique for SCQR. Let us revisit the two steps of SCQR (given the matrix to be decomposed ):

The problem with SCQR is that the larger is, the easier it is for the Cholesky decomposition to succeed, but becomes less orthogonal. The idea behind SCQR2 is to first use a larger to perform one SCQR step; although the result is not orthogonal at this point, compared to the original , it is closer to orthogonal, indicating that the condition number has already been reduced. At this point, we can use a smaller to perform another SCQR step on the result to correct its orthogonality. A rough implementation is as follows:

def shift(A, eps=1e-9):
    return A + eps * jnp.linalg.matrix_norm(A, keepdims=True) * jnp.eye(A.shape[-1])

def scqr(A, eps=1e-9):
    """Shifted Cholesky QR
    """
    R = jnp.linalg.cholesky(shift(A.mT @ A, eps), upper=True)
    return solve_triangular(R.mT, A.mT, lower=True).mT

def scqr2(A, eps1=1e-4, eps2=1e-8):
    """Run SCQR twice, falling back to default QR on failure"""
    Q = scqr(scqr(A, eps1), eps2)
    return lax.cond(jnp.isfinite(Q).all(), lambda: Q, lambda: jnp.linalg.qr(A)[0])

In terms of the principle, we need to understand why a second correction is feasible. Suppose the first SCQR produces ; although it deviates from orthogonality, it has the form of "". As we noted earlier, right-multiplying by an upper triangular matrix does not change the QR result, which allows us to perform a second SCQR on top of the first SCQR. Of course, in principle we can also perform more correction steps.

Summary of Methods #

We have discussed four acceleration tricks above; here we provide a brief summary of their characteristics.

The first trick is to increase the precision of FP32 matrix multiplication, which is general-purpose—Jax requires manually enabling it, whereas newer versions of Torch already have it enabled by default. The second, third, and fourth tricks are isolated from one another and cannot be stacked together. Intuitively, trick 2 has a higher ceiling, because tricks 3 and 4 both take as input, meaning that the condition number has already been amplified and they merely try to remedy it afterward, whereas method 2 modifies the input to , reducing the condition number at its source.

Interestingly, Tricks 2, 3, and 4 all seem to point toward two passes of . Except that Trick 3, with carefully tuned , can use only one pass of , all the others require at least two passes of , so this indeed appears to be the safest choice. In terms of speed, if Trick 3 can be tuned to use only one pass of , then it is the fastest; otherwise, it is as fast as Trick 2. Trick 4 is somewhat unstable: if we take and perform SCQR2, it is very fast, but the results are poor; switching to guarantees performance, but the speed drops.

I recommend the combination of Tricks 1 and 2, which provides relatively solid guarantees in both effectiveness and efficiency. In standalone testing, its speed is roughly half that of the Newton-Schulz iteration. Some readers might think, "All that effort for only half the speed?" In fact, this is already quite ideal—after all, our computations are all done in FP32, and we also need two passes of . On the other hand, the step accounts for only about 1% of the end-to-end computation time; doubling it merely adds another 1% of time, which is entirely acceptable.

Furthermore, the efficiency of the Newton-Schulz iteration depends on the number of iteration steps; if one uses Polar Express's coefficients and further increases the number of steps to improve precision, the speed gap between it and our approach here will narrow even further. In summary, streaming power iteration is indeed slower, but it also yields richer and more accurate results (SVD), allowing it to accomplish much more.

Summary #

This article introduced further techniques for streaming power iteration, the essence of which is to try to reduce the condition number of the matrix, thereby improving the success rate of Cholesky QR.