Repository navigation
Faster ANVIL kernel on #360: 0.2% end to end #405
ethantsliu
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I tried to speed up ANVIL's orthogonalization step (aka cascade) in the current best submission (ANVIL2, #360, 39.9 s).
The method
ANVIL orthogonalizes each update with six polynomial iterations. For the MLP weights,$X$ is a tall $2816 \times 768$ matrix, so its Gram matrix $A = X^\top X$ is only $768 \times 768$ . One iteration is
so the next iteration's Gram matrix is
Since we don't need$X$ , two iterations can be run on the $768 \times 768$ side and applied to $X$ once.
which is two Gram matrices and two products on the full$2816 \times 768$ matrix. Substituting $X_1 = X_0 P_1$ gives
so$X_1$ is never formed. The full matrix is used once to build $A_1$ and once to apply $P_1 P_2$ .
This method is borrowed from "Fast Polynomial Iteration for Rectangular Matrices" from Polar Express appendix, and Gram Newton-Schulz (Zhang, Amsel, Chen, Dao) develops it with the same choices I ended up with: a restart from$X$ every two iterations and a symmetric kernel for the small products.
The kernel
The rest is on$768 \times 768$ matrices, and it needs three products #360 does not have: $P_1 A_1$ , then $(P_1 A_1) P_1$ , and $P_1 P_2$ . Each multiplies two different symmetric matrices, where #360's kernel only multiplies a matrix by itself (for $A_1^2$ and $A_2^2$ ). That is the second operand.
My kernel is
ba_plus_cAAwith a second operand. Quack'sgemm_symmetricdoes the same in CuTeDSL.I restart from$X$ every two iterations because the Gram matrix loses small singular values in bf16. In a CPU emulation at this matrix size, restarting every two or three iterations stays as accurate as #360, but trying to do more causes divergence
We only apply this to$2816 \times 768$ ), where the Gram matrix is much smaller than $X$ .
mlp_bank(Results (8xH100)
Why it is so small
Caveats
Five paired seeds for the kernel and four for the ablation. My nodes ran #360 at 40.4 to 40.6 s against the official 39.9 s. Not a submission attempt and not a PR: adds about 350 lines for 80 ms.
code: https://github.com/ethantsliu/modded-nanogpt/tree/gram-space-anvil
All reactions