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
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
If one repeatedly executes
Now the most expensive operation becomes
First Speedup #
To accelerate, in the previous post we introduced Cholesky QR, which splits the QR decomposition of the matrix
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
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
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
Understanding this acceleration trick consists of two parts: first, adding this extra
By the uniqueness of the QR decomposition, right-multiplying by an upper triangular matrix does not change the result of
As for the condition number, it equals the ratio of the matrix's largest singular value to its smallest singular value. If a single
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
In other words,
without changing the convergence outcome of the power iteration. So what is the benefit of taking
But don't celebrate too early.
Therefore, we must carefully tune the value of
This can account for both Cholesky QR and the power iteration, with the natural cost of requiring two
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
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
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
Interestingly, Tricks 2, 3, and 4 all seem to point toward two passes of
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
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.