skip to content
← Scientific Spaces English

Implementing Muon with Streaming Power Iteration, Part 3: Refinement

Reviewing the previous two articles, Implementing Muon with Streaming Power Iteration, Part 1: A First Encounter and Implementing Muon with Streaming Power Iteration, Part 2: Acceleration, we introduced the streaming power iteration scheme for Muon, initially verified its feasibility, and further discussed accelerating its core operation—the QR decomposition—bringing its efficiency close to that of the Newton-Schulz iteration implementation.

In this article, we no longer limit ourselves to optimizing a single-step QR decomposition; instead, we take a more holistic view of streaming power iteration and, in the context of the concrete computational setting, further "refine" its implementation details to minimize computational bottlenecks as much as possible, pushing its efficiency toward the theoretical limit.

Existing Results #

Streaming power iteration is essentially "SVD on the fly during training." Its idea is to compute the SVD via power iteration and amortize the computation across training steps by caching the results of the previous step, making it possible to embed SVD inside an optimizer. As for Muon, it is merely a basic application of this, because Muon's core operation has SVD as its most fundamental implementation. Specifically, the update formula of Muon is

Here, the matrices are all of size , and we adopt the convention . Let the SVD of be (where and ), then , so implementing SVD effectively implements . Of course, direct SVD is usually quite expensive, but streaming power iteration makes it feasible.

In the previous article, we also discussed four speedup ideas for streaming power iteration. The first was enabling full-precision FP32 multiplication, which is universally applicable, while the remaining three ideas are mutually exclusive to some extent, allowing us to choose only one. I recommend choosing the second idea, as its theoretical upper bound is higher, and the subsequent refinements here will also build on this second approach. Substituting the second idea into the streaming power iteration version for Muon yields the iteration formulas:

Clearly, the most expensive operation now is , which is precisely the target of our upcoming optimizations.

Accelerating Decomposition #

To ensure efficiency, for here, we do not call the framework's built-in QR decomposition function. Instead, we use "SCQR (Shifted Cholesky QR)," which splits the QR decomposition of matrix into two steps: 1. Perform a Cholesky decomposition on to obtain an upper triangular matrix ; 2. Solve the equation to obtain the orthogonal matrix .

Both steps are theoretically very efficient, but they do not always succeed. Therefore, an additional check is needed; upon failure, it falls back to the built-in standard QR function, which almost certainly succeeds. However, once a fallback is triggered, end-to-end efficiency suffers significantly. The primary reason SCQR fails is that Cholesky decomposition is extremely sensitive to the condition number of the matrix, and the regularization term is used precisely to reduce the condition number of .

However, this also presents a trade-off dilemma: as grows larger, SCQR succeeds more easily, but the final result deviates further from orthogonality (i.e., larger error), leading to worse performance; as becomes smaller, the precision is naturally higher, but the probability of falling back to standard QR also increases, resulting in worse efficiency. Empirical tests show that setting and balances performance and efficiency quite well.

The acceleration ideas in the previous article all revolved around reducing the condition number. The first version of streaming power iteration was , and the matrix we needed to Cholesky decompose was , which raises the condition number of to the 4th power, leading to an obvious explosion. But after changing it to , even though becomes two operations, the condition number of the matrix in each Cholesky decomposition is only squared, significantly reducing the condition number and greatly improving the SCQR success rate, so the speed actually becomes faster.

Adjusting the Order #

All of the above is still a recap of the previous two articles (apologies for the rather lengthy preamble, but sharpening the ax does not delay the woodcutter); only in this section do we begin to discuss new optimization ideas. Looking closely, one can notice that we have currently introduced two operations, but these two operations were considered independently. Meanwhile, @YouJiacheng and @Kimi both discovered that if we consider them together, we can gain some acceleration techniques.

According to the default order, has the following computational procedure:

where , , , and —these four steps are all of complexity, while the rest are ; when , can become a bottleneck. Interestingly, through an identity transformation, we can make appear only once!

This equivalent version is well worth savoring! First, it can be proven that it is theoretically completely equivalent to the original version, and this equivalence does not depend on and having absolute orthogonality; after the transformation, only is of complexity, while all the rest are , and the total number of steps is even reduced by one (combining the original and into a single step)!

Note 1: According to @YouJiacheng 's account, this ingenious transformation came about after he gave Equation to Kimi and Kimi automatically discovered it;

Note 2: Eq. and actually have a slight difference—in Eq. , the first step is , whereas in Eq. , it is . These two algorithms are mathematically completely equivalent, but for finite-precision floating-point arithmetic there will be some differences: the matrix produced by multiplication in the latter has a larger condition number, and the Cholesky decomposition may require slightly increasing the regularization.

Simplifying Regularization #

For the matrix , the regularization term we add during Cholesky decomposition is , where and , but why this form was chosen has never been explained in detail. Here we will elaborate on it and, combining with the context of our problem, derive a simpler regularization term.

Due to the symmetric positive definiteness of , its SVD is identical to its eigenvalue decomposition. We might as well denote its SVD as , so . If we let the maximum and minimum singular values of be , then the maximum and minimum singular values of are . The condition number is the ratio of the maximum to minimum singular value, so it decreases from to

If we want to keep the condition number from exceeding , then , which indicates that ideally we should use 's largest singular value—that is, its spectral norm—as the benchmark to adjust . However, calculating the spectral norm is relatively complicated, so we switched to the simpler Frobenius norm , which is the origin of ; as for , that is purely an experimental conclusion.

However, "calculating the spectral norm is relatively complicated, so we switched to the Frobenius norm" is only a general conclusion for arbitrary matrices. The streaming power iteration we are doing here is itself used to compute the SVD; as training progresses, will get closer and closer to 's right singular matrix. Since varies slowly, is largely the same as well. Therefore, theoretically, will get closer and closer to a diagonal matrix, and its top-left element will get closer and closer to its spectral norm!

Similarly, will get closer and closer to 's left singular matrix, so will also get closer and closer to a diagonal matrix, whose top-left element will get closer and closer to its spectral norm. Therefore, in our scenario, the simplest and most accurate baseline is to directly use as the spectral norm approximation, meaning that suffices; in practice, strikes a good balance between effectiveness and efficiency.

Reference Implementation #

Combining the modifications in the two sections above, the reference implementation for the iteration from to is:

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

def shift_old(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):
    """Compute via Shifted Cholesky QR first; fall back to default QR if it fails."""
    R = jnp.linalg.cholesky(shift_old(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])

def v_step_old(M, V, eps=1e-9):
   return scqr(M.mT @ scqr(M @ V, eps), eps)


def shift(A, eps=1e-7):
    return A + eps * A[..., :1, :1] * jnp.eye(A.shape[-1])

def v_step(M, V, eps=1e-7):
    A = (M.mT @ M) @ V
    R = jnp.linalg.cholesky(shift(V.mT @ A, eps), upper=True)
    B = solve_triangular(R.mT, A.mT, lower=True).mT
    R = jnp.linalg.cholesky(shift(B.mT @ B, eps), upper=True)
    Q = solve_triangular(R.mT, B.mT, lower=True).mT
    return lax.cond(jnp.isfinite(Q).all(), lambda: Q, lambda: jnp.linalg.qr(A)[0])

Concurrent Work #

Between the publication of Implementing Muon with Streaming Power Iteration, Part 2: Acceleration and the release of this post, some interesting optimization works have also appeared externally. They share similar optimization ideas with the two modifications proposed in this article, so we can study them together.

First, after the previous post was published, @Ji_Ha_Kim also proposed some ideas for improvement. For instance, he noted that during an exchange with GPT, he found (link) that we might be able to eliminate one ! Specifically, we have

It is easy to see that is precisely the R from QR decomposition of , which can be obtained directly via Cholesky decomposition.

In other words, theoretically, after obtaining R from the first Cholesky decomposition, one could proceed directly to the second , eliminating one . However, this is only of theoretical interest, because this result relies on the strict orthogonality of and , which holds only under exact QR (i.e., ). In practice, to guarantee efficiency, we can only use SCQR, which leads to results that are not strictly orthogonal. Therefore, exploiting its orthogonality prematurely during algebraic transformations actually causes errors to accumulate in the iterative algorithm,

Meanwhile, Tri Dao's team also published 《Gram Newton-Schulz: A Fast, Hardware-Aware Newton-Schulz Algorithm for Muon》, proposing an acceleration approach for the operator. By definition, , and the team hoped to use Newton-Schulz iteration to compute the matrix rather than , so that when , the computational cost can be significantly reduced. In fact, many researchers had previously tried this idea but failed, whereas Tri Dao and his team ingeniously solved this problem with Restart.

Clearly, 's optimization direction and streaming power iteration's shift from to are consistent. Coincidentally, @Ji_Ha_Kim suggested changing 's Newton-Schulz iteration from a polynomial to a rational form, so that the same good performance can be achieved with fewer iterations. The problem with rational iteration is that it requires matrix inversion; however, taking into account 's specific context, it only requires inverting an positive-definite symmetric matrix, which can be computed via Cholesky decomposition and two operations, which is acceptable.

However, this causes the computational workflow of such rational iteration to heavily overlap with streaming power iteration, and each step also incurs an additional , so it seems that its speed cannot outperform streaming power iteration.

Summary #

In this post, I further "refine" the implementation details of streaming power iteration. The main improvements include: 1. Adjusting the computational order to reduce operations of complexity from four to one; 2. Leveraging the special context of streaming power iteration to simplify the regularization term. These optimizations further reduce the computational bottlenecks of streaming power iteration, pushing computational efficiency to the limit.