← back to archive

Does a Faster Optimizer Kernel Speed Up the NanoGPT Speedrun?

Ethan TS. Liu with Justin Kim, Dev Patel, and Ahilan Karuppusami | November 25, 2026

The shortcut

In NanoGPT’s current speedrun record (#360, 39.9 seconds), each of the ANVIL optimizer's six maps is \(X \leftarrow XP\) with \(P = aI + bA + cA^2\) and \(A = X^\top X\). Together, these six maps push every singular value of \(X\) toward 1, approximating \(UV^\top\) as Muon's Newton–Schulz does. Since the MLP matrices are \(2816 \times 768\), the Gram matrix \(A\) is only \(768 \times 768\). We can note that, after calculating our current map's Gram matrix, the next map's Gram matrix doesn't need \(X\) at all:

\[ (XP)^\top (XP) \;=\; P^\top (X^\top X)\,P \;=\; PAP \quad \text{(since $P$ is symmetric)} \]

So let's build a chain of two on the small side! With \(P_i = a_iI + b_iA_{i-1} + c_iA_{i-1}^2\):

\[ \begin{aligned} \text{original:}\quad & A_0 = X^\top X, && X_1 = XP_1, && A_1 = X_1^\top X_1, && X_2 = X_1P_2 \\ \text{ours:}\quad & A_0 = X^\top X, && A_1 = P_1A_0P_1, && Q = P_1P_2, && X_2 = XQ \end{aligned} \]

Both result in the same \(X_2\), but the original touches the tall matrix with dims \(2816 \times 768\) four times, whereas we only do this twice (at the cost of three \(768 \times 768\) products). It turns out this trick is already known. It appears in an appendix of Polar Express, and Gram Newton-Schulz builds on it further. But there are still a couple of details we need to work out before it actually runs fast.

But if we can build a chain of two, why not a chain of six? Actually, the problem is the numerical representation itself. Our runs are in bf16. The Gram matrix's eigenvalues are \(\sigma^2\), so a direction with \(\sigma = 0.01\) becomes \(10^{-4}\), which is just too far below the rounding error of the large entries.

How far below? bf16 keeps only 8 significant bits, so every stored number carries a relative rounding error of about \(2^{-8} \approx 0.004\). The entries of \(A\) are dominated by the largest directions, so with the largest \(\sigma = 1\):

\[ \text{noise in } A \approx 2^{-8} \approx 0.004, \qquad \sigma = 0.5 \mapsto 0.25, \quad \sigma = 0.06 \mapsto 0.0036, \quad \sigma = 0.01 \mapsto 0.0001 \]

So any direction with \(\sigma \lesssim 0.06\) is already risky. Sorting the 768 singular values from largest to smallest, \(\sigma_1 \ge \sigma_2 \ge \dots \ge \sigma_{768}\), let's make two test matrices: one where they fall off steeply (\(\sigma_i = 1/i\)) and one where they fall off gently (\(\sigma_i = 1/\sqrt{i}\)):

eigenvalues of A against the bfloat16 rounding noise (largest σ = 1)110−210−410−6110100768bf16 noise ≈ 0.004buried in the noisesteep: σᵢ = 1/igentle: σᵢ = 1/√ii = rank of the singular value, σ₁ ≥ σ₂ ≥ … ≥ σ₇₆₈ (log scale)σᵢ² (eigenvalue of A)

Going back to \(X\) refreshes those small directions, whereas chaining on the Gram matrix just feeds the same noise through map after map. Let's check this with a CPU emulation of the bfloat16 arithmetic, on a matrix with many small singular values, and measure how far the result lands from the exact one, \(\lVert \tilde X_6 - X_6 \rVert_F / \lVert X_6 \rVert_F\):

how far the bfloat16 result lands from the exact one1%10%100%relative error ‖bf16 − exact‖ / ‖exact‖ (log scale)the steep test matrix (2816×768, σᵢ = 1/i), CPU emulation of bfloat16chains (all six maps always run)1+1+1+1+1+1 (original)3.4%2+2+2 (ours)1.3%3+32.1%4+210.6%5+1diverges →6diverges →

Chains of two or three are fine, but a chain of four reaches about 11% error, and as you go further, chains of five and six just diverge. On this steep matrix ours actually beats the original, but on the gentle one it's slightly worse (0.74% vs 0.62%), so overall the accuracy is about the same. However, the emulation turned out to be too optimistic. Chains of three passed this test, and then went NaN at step 400 in one of three training runs! (A chain of four diverged before the clock even started.) Chains of two were fine in all 16 of our training runs, so that's what we use. Gram Newton-Schulz makes a similar choice: it runs five iterations as a chain of two followed by a chain of three.

The second detail is the small products themselves. Since \(A_0 = X^\top X\) is symmetric, and every other small matrix is a polynomial in it,

\[ P_1 = a_1I + \underbrace{b_1A_0 + c_1A_0^2}_{B_1}, \qquad A_1 = P_1A_0P_1, \qquad Q = P_1P_2 \]

they are all symmetric too, so \(M_{ij} = M_{ji}\). (In floating point they're only nearly symmetric, so mirroring actually makes them exactly what they should be.) This means we need only compute the lower triangle and mirror it over. A \(768 \times 768\) output is a \(6 \times 6\) grid of \(128 \times 128\) tiles, and the lower triangle is 21 of those 36, so we get skip about 42% of the work. Every small product here also has the same shape,

\[ S\,(aI + B) \;=\; aS + SB, \qquad \text{e.g. } Q = P_1(a_2I + B_2), \]

with \(S\) and \(B\) both symmetric, so a single kernel that computes \(aS + SB\) on one triangle covers all of them. The original already has a half-matrix Triton kernel for \(X^\top X\) and \(B = bA + cA^2\), so ours is just that kernel with a second input. Again, Gram Newton-Schulz ends up with this same idea, using symmetric kernels for exactly these products.

Note that we only do this for the MLP matrices. For the square attention matrices, the Gram matrix is just as big as \(X\), so there is nothing to save, and the update actually gets slower (0.79×). The small \(256 \times 768\) group is also too small for this to pay off.

Does it help?

In isolation, yes. On one H100, the MLP map sequence goes from 0.396 ms to 0.304 ms, which is 1.30× faster. To be fair, the trainer compiles the original maps (with torch.compile) and against a compiled copy the speedup is 1.21×. And once we include the rest of the MLP update (the momentum, the normalization, and writing the weights), it's only 1.08×.

So what happens in an actual training run? We ran ten paired seeds on 8×H100, and the step loop is faster in all ten pairs, by \(0.054 \pm 0.006\) ms per step (mean ± s.e.), or about 0.065 s per run. However, on the official whole-run clock, the saving is only \(0.047 \pm 0.021\) s (\(p = 0.049\)) (unfortunately, three of the ten pairs are actually slower!). The time outside the step loop varies by about a tenth of a second between runs, which is more than our entire effect... :(

The ceiling

Let's try just skipping the six maps entirely and see how much time that saves. This run can't actually train (its loss ends around 3.46), but it represents the upper limit of how much time any faster implementation of these maps could possibly save. In isolation, the maps cost 0.593 ms per step across all the weight groups, or 1.8% of a 33.5 ms step. In training, removing them saves only \(0.209 \pm 0.005\) ms per step, about 0.25 s of the step loop, or 0.6% of the run!

saving per step against the originalisolated, one H100in training, 8xH100 (10 paired seeds)0.00.20.40.6milliseconds per stepremove all six maps(all weight groups)0.5930.209Gram-space order(MLP group only)0.0370.054

However, only about a third of the maps' isolated cost actually shows up in a training step, presumably because the rest overlaps with other work on the GPU. Strangely, if you implement this change, it actually saves more in training (0.054 ms) than in the microbenchmark (0.037 ms). I'm not sure why, but this is a good reminder that isolated timings don't predict much in a hyper-optimized trainer such as this one. For reference, an open PR that cuts the six maps down to four reports 0.19 s, which checks out, fitting under our 0.25 s ceiling.

Why so small?

The first reason is simply that the exposed optimizer time is tiny to begin with. The second reason is a bit more interesting. The Gram-space order cuts the MLP group's arithmetic by about a third (104 to 71 GFLOP per GPU per step), but the map time by only about a quarter.

Why? Well, a \(768 \times 768\) product has 27% of the arithmetic of a \(2816 \times 768\) one, but our kernel only launches 63 thread blocks for it across the three MLP matrices, which is fewer than the H100's 132 SMs.

Where does 63 come from? The original stacks all its MLP weights into 24 matrices of \(2816 \times 768\) (an up and a down projection for each of 12 MLP slots), and the optimizer work is split across the 8 GPUs, so each GPU only updates \(24 / 8 = 3\) of them. Our kernel then cuts each output into \(128 \times 128\) tiles and gives each tile its own thread block, which runs on one SM (one of the GPU's 132 processors):

\[ \begin{aligned} 2816 \times 768 \text{ output:}\quad & \tfrac{2816}{128} \times \tfrac{768}{128} = 22 \times 6 = 132 \text{ tiles}, && 3 \times 132 = 396 \text{ blocks} \\ 768 \times 768 \text{ output, one triangle:}\quad & \underbrace{1 + 2 + \dots + 6}_{\text{row } k \text{ has } k \text{ tiles on or below the diagonal}} = 21 \text{ of } 36 \text{ tiles}, && 3 \times 21 = 63 \text{ blocks} \end{aligned} \]

So with the same tiles, a big output would give every SM about three blocks of work, whereas a small product leaves roughly half of the 132 SMs with nothing to do. (The big products actually run on cuBLAS, which picks its own tile sizes, so the 396 is only illustrative. The 63 is exact for our kernel.)

At this size, latency rather than arithmetic probably sets the time. A rough model of a kernel's time is

\[ t_{\text{kernel}} \;\approx\; t_{\text{launch}} \;+\; \max\!\Big(\frac{\text{FLOPs}}{\text{GPU throughput}},\;\; t_{\text{one block}}\Big) \]

When every SM is busy, the first term in the max wins, so fewer FLOPs means less time. But with 63 blocks on 132 SMs, there is only one round of blocks, and the kernel is bottlenecked by its slowest block. That block's time is set by how long it takes to load its inputs from memory and run its loop, plus the fixed cost of launching the kernel. So, fewer tiles just means more idle SMs, not a faster kernel. So trading big products for small ones is a win on paper, but not here on the GPU. Counting FLOPs, which is how we first estimated the gain, was just the wrong model here. In fact, the same change on larger matrices that fill the GPU gets much closer to its FLOP savings: at aspect ratio 2, the maps speed up 1.06× with 768 columns but 1.30× at \(8192 \times 4096\).

References

Work done for EECS 182 at UC Berkeley.