Muon replaces a weight matrix's raw update by an orthogonalised one, scaled by a factor that depends only on the matrix shape. For an MoE the expert weights of a layer are usually stored as one (E, m, n) tensor, and whether Muon treats it as matrices or as one big matrix changes how far each expert moves. Implement both and the diagnostics that show the difference.
newton_schulz(G, steps=5, eps=1e-7): if has more rows than columns, work on and transpose the result back. Start from and repeat steps times: , with , , .
muon_scale(m, n, mode="match_adamw"): "match_adamw" returns and "shape" returns .
muon_moe_step(W, G, M, lr, mu=0.95, mode="per_expert", scale_mode="match_adamw", steps=5)
W, G, M are (E, m, n): expert weights, this step's gradients, and the momentum buffers.mode="per_expert": for each expert.mode="stacked": reshape to one matrix (experts stacked along rows), orthogonalise it, multiply by muon_scale(E*m, n), and reshape back to (E, m, n).(W - lr * U, M', U).update_norms(U): the Frobenius norm of each expert's slice of U, as a list rounded to 4 decimals.