Downloads · 30 days
0
tboissin/newton_schulz_triton
newton_schulz_triton is a machine learning model from tboissin. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
This repository provides a Triton-accelerated implementation of the Preconditioned Newton–Schulz (NS) iteration used for fast approximate polar decomposition / orthogonalization. The method follows the Turbo-Muon form…
Downloads · 30 days
0
Access
Public
Updated Dec 5, 2025
Repo size
—
Likes
2
Public
Click a slice to open those files.
.py60.1 KB · 85%
From the Hugging Face model README
This repository provides a Triton-accelerated implementation of the Preconditioned Newton–Schulz (NS) iteration used for fast approximate polar decomposition / orthogonalization. The method follows the Turbo-Muon formulation (Boissin, Massena et al., 2025), including AOL preconditioning fused directly into the first iteration.
The implementation is designed for large matrices on modern accelerators, with minimal memory traffic and fully fused Triton kernels.
import torch
from kernels import get_kernel
kern = get_kernel("tboissin/newton_schulz_triton")
newton_schulz = torch.compile(kern.newton_schulz)
n = 4096
x = torch.randn((16, n, n), device="cuda", dtype=torch.bfloat16)
# Preconditioned (recommended)
out = newton_schulz(x, iter=4, precondition=True, epsilon=1e-7)
# Without preconditioning (requires 5 steps to match accuracy)
out2 = newton_schulz(x, iter=5, precondition=False, epsilon=1e-7)
# Orthogonality error
err = torch.norm(out @ out.mT - torch.eye(n, device="cuda", dtype=torch.bfloat16), dim=(1,2)).mean()
err2 = torch.norm(out2 @ out2.mT - torch.eye(n, device="cuda", dtype=torch.bfloat16), dim=(1,2)).mean()
print("Orthogonality error (preconditioned):", err.item())
print("Orthogonality error (not preconditioned):", err2.item())
Default is newton_schulz(G, iter=4, precondition=True, epsilon=1e-7, dtype=torch.bfloat16).
This routine implements the Turbo-Muon variant of the Newton–Schulz (NS) method with AOL preconditioning, as described in “Turbo-Muon: Accelerating Orthogonality-Based Optimization with Pre-Conditioning” (Boissin, Massena et al., 2025). It serves as the fast orthogonalization step used in Muon-style optimizers.
Given an input matrix (or batch) G ∈ ℝ^{…, m, n}, the function computes an approximate polar factor X whose rows or columns are close to orthonormal and span the same subspace as G. For non-square matrices, the algorithm returns the closest pseudo-orthogonal factor in the Stiefel sense:
The implementation uses three Triton kernels:
ns_line_1(X, out=A)
Computes the Gram matrix
A = X @ Xᵀ
ns_line_2(A, alpha=c, beta=b, out=B)
Applies a 4th-order matrix polynomial:
B = bA + cA²
ns_line_3(B, X, a, out=C)
Performs the NS update step:
C = aX + B @ X
Together, these realize one Newton–Schulz iteration:
[ X_{k+1} = a_k X_k + b_k X_k X_k^{T} X_k + c_k X_k X_k^{T} X_k X_k^{T} X_k ]
The coefficients ((a_k, b_k, c_k)) are drawn from a small fixed table optimized for up to five iterations.
torch.TensorInput tensor with shape (..., m, n). The last two dimensions are treated as a matrix; leading dimensions represent batch dimensions. The device follows that of the input.
Number of Newton–Schulz iterations (1–5). Turbo-Muon typically uses 4 iterations, achieving accuracy comparable to classical 5-step NS but faster.
Enables AOL preconditioning, fused with the first NS iteration:
ns_line_1.AOL preconditioning enforces safe spectral scale and improves conditioning, allowing one fewer NS iteration.
If disabled, the input is normalized by approximate Frobenius norm:
[ X₀ = \frac{G}{|G|_F + \epsilon} ]
Lower bound for denominators to ensure numerical stability.
torch.dtype, default = torch.bfloat16Working dtype for all computations. Supports BF16 for performance or FP32 for higher precision.
torch.TensorApproximate polar factor of G, with the same shape and dtype. If the input is tall (m > n), the routine transposes internally to operate on a wide matrix, then transposes back.
The output satisfies approximately:
and spans the same column/row space as G.
Uses torch.compile() for graph-level optimization.
Coefficients (a, b, c) are stored in ns_consts; ns_consts[-iter:] selects the appropriate schedule.
When preconditioning is enabled, the first triple is consumed by the fused AOL-NS step.
Three work buffers are reused to avoid allocation overhead:
The design mirrors the Turbo-Muon formulation, matching the polar accuracy of optimized 5-step Muon at lower runtime.
torch.compile is supported and advised (kernel is not compiled by default).newton_schulz_autograd. This feature is experimental.Boissin, Massena et al., 2025.
Turbo-Muon: Accelerating Orthogonality-Based Optimization with Pre-Conditioning.
MIT License.
Credits for ns_line_1 and ns_line_2: https://github.com/microsoft/dion which is licensed under the MIT License.