Polar Express: The Online Stage With the Paper's Coefficients
Once the polynomials are precomputed, applying Polar Express to a matrix takes a few lines. The paper prints eight coefficient rows (a,b,c) for degree 5 and lower bound ℓ=10−3; row t is the polynomial ax+bx3+cx5 of iteration t, and the eighth row is the degree-5 Newton-Schulz polynomial, which all later iterations equal. The rows are given to you as COEFFS in the starter code.
Implement the two functions the way the paper's code does.
polar_express(G, steps=5)
G has shape (…,m,n) with at least two dimensions; leading dimensions are a batch. If m>n, work on the transpose (last two axes swapped) and swap back at the end.
- Normalise each matrix: X0=X/(1.01∥X∥F+10−7), with the Frobenius norm taken over the last two axes.
- Prepare the rows: apply the safety factor 1.01 to the first seven rows, (a/1.01,b/1.013,c/1.015), and leave the eighth as it is. Use the first
steps rows; if steps is larger than 8, repeat the eighth row.
- For each row compute, with A=XXT,
X←aX+(bA+cA2)X.
- Return the result as a float array with the shape of
G.
scalar_map(x, steps=5)
- Apply the same prepared rows to a number or array of singular values, without the normalisation of step 2. Return a NumPy array of floats.
Notes
steps=0 returns the normalised input.
- The paper runs this in bfloat16 on a GPU. Here everything is ordinary double precision.