README.md
May 9, 2026 · View on GitHub
Pytorch implementation of PSGD
An overview
PSGD (Preconditioned SGD) is a general purpose (mathematical and stochastic, convex and nonconvex) 2nd order optimizer. It reformulates a wide range of preconditioner estimation and Hessian fitting problems as a family of strongly convex Lie groups like Riemannian manifold optimization problems.
Notations: or the loss; the (stochastic) gradient wrt ; the Hessian; the Hessian-vector product (Hvp) with ; the preconditioner applying on ; takes the upper or lower triangular part of a matrix; takes spectral norm; superscripts , and for transpose, conjugate and Hermitian transpose, respectively.
The new PSGD implementation is a superset of the old one (deprecated), and further supports four more matmul-only/inverse-free geometries for updating . The choices (default) and recover the Newton-Schulz (NS) iterations with stability guarantee. A few torch.optim.Optimizer wrapping examples are provided:
- Simplest standalone DDP wrapping. This is the simplest PSGD 0/1/2D momentum whitening optimizer using Kron preconditioner fitted with and update rule , which recovers the NS iterations for the inverse 4th root of . It's light, fast and the recommended implementation.
- DDP wrapping and FSDP wrapping. They are wrapping examples of the functions inside psgd.py for gradient/momentum whitening of tensors of any order (with noticeable overhead cost due to einsum etc.). For most problems, whitening either gradient or momentum works equally well. For problems with sparse gradients, typically momentum whitening is preferred.
- My customized non-standard wrapping inside psgd.py. It supports a wide range of choices: gradient/momentum whitening, Hessian fitting with Hvp, different geometries and Lie groups for ...
The PSGD theory has two orthogonal parts: criteria for preconditioner fitting and preconditioner fitting in Lie groups.
Criteria for preconditioner fitting
PSGD was originally designed for preconditioning the gradient such that metrics of the spaces of preconditioned gradient and parameters are matched, i.e., , where denotes the perturbation operation and is symmetric positive definite (SPD). This leads to the original preconditioner fitting criterion ref. The finite-difference notation may not be common in machine learning (ML). But, note that PSGD was invented before popular automatic differentiation (AD) tools like Tensorflow. Manually calculating the Hvp was cubersome then. With AD, we can simply replace pair with to obtain the Newton-style preconditioner fitting criterion . For the gradient/momentum whitening preconditioner, we just replace pair with to have criterion ref, where is an auxiliary variable and can be optionally integrated out as it is indepedent of .
Preconditioner fitting in Lie groups
The above preconditioner fitting criteria are always convex in the Euclidean space, the manifold of SPD matrices and the Lie groups. But, they are strongly convex only in the Lie groups ref. The here defines the coordinate transform such that PSGD reduces to an SGD for . Lie group is a natural tool for this purpose by preserving invariances like the coordinate orientations such that is always invertible. Also, the multiplicative updates in Lie group avoid explicit matrix inverse. There are virtually endless choices for the group forms of , say the Kronecker product preconditioner ref, the affine Lie group ref, and the low rank approximation (LRA) group ref.
Table I: Variations of preconditioner fitting criterion
| Criterion | Solution | Notes |
|---|---|---|
| Reduces to secant equation when (see quasi-Newton methods, e.g., BFGS). | ||
| Reduces to Newton's method when . | ||
| reduces to Fisher information matrix with per-sample gradient (see Gauss-Newton and natural gradient methods, e.g., KFAC). | ||
| Relates to the AdaGrad family, e.g., Adam(W), RMSProp, Shampoo, . |
Note 1: can be a nuisance or an auxiliary variable in the last two criteria since it is independent of and can be integrated out as , i.e., the Hutchinson's estimator.
Table II: Lie group () preconditioners with storage and computation numbers for with
| Lie Group | Update of ($0<\mu\le 2$) | Storages | Computations | Class |
|---|---|---|---|---|
| DenseNewton | ||||
| Tri matrices | DenseNewton | |||
| LRAWhiten/Newton | ||||
| , , , | KronWhiten/Newton | |||
| , , | KronWhiten/Newton | |||
| , , $0\le r\ll n$ | , , , , | LRAWhiten/Newton | ||
| same as kron | KronWhiten/Newton |
Note 1: The other four inverse-free preconditioner update methods have similar forms and complexities. Please check ref for further details.
Note 2: For the gradient/momentum whitening preconditioner, we simply replace pair with , where is a dummy variable that can be optionally integrated out.
Hessian fitting accuracy and connection to BFGS
This script generates the following plot showing the typical behaviors of different Hessian fitting methods.
- With a static and noise-free Hessian-vector product model, both BFGS and PSGD converge linearly to the optimal preconditioner while closed-form solution only converges sublinearly with rate .
- With a static additive noisy Hessian-vector model , BFGS diverges easily. With a constant step size , the steady-state fitting errors of PSGD are proportional to .
- With a time-varying Hessian and , PSGD locks onto good preconditioner estimations quicker than BFGS without a divergence stage. The closed-form solution is not good at tracking due to its sublinear rate of convergence.
Implementation details for psgd.py
One can find the functional APIs for all the preconditioners in Table II in psgd.py. The update_precond... and precond_grad... functions are for updating and applying on , respectively. See kron and lra for their usages. These low level functional APIs provide all the flexibility for customizing your own PSGD implementations.
These functional APIs are lightly wrapped into classes KronWhiten/Newton, LRAWhiten/Newton and DenseNewton for easy use. Three main differences from torch.optim.SGD:
- The loss to be minimized is passed through as a closure to the optimizer to support more dynamic behaviors, notably, Hessian-vector product approximation with finite difference method when the 2nd order derivative is unavailable. The closure should return a loss or a list/tuple with its first element as the loss.
- Momentum here is the moving average of gradient so that its setting is decoupled from the learning rate, which is always normalized in PSGD.
- As any other regularizations, (coupled) weight decay should be explicitly realized by adding an regularization to the loss. Similarly, decoupled weight decay is not included inside these PSGD class implementations.
A few more details. The Hessian-vector products are calculated as a vector-jacobian-product (vjp), i.e., in torch, maybe not always the most efficient way for a specific problem. Except for the Kronecker product preconditioners, no native support of complex parameter optimization (you can define complex parameters as view of real ones in order to use other preconditioners).
Demos
There are plenty of demos: Rosenbrock function minimization, vision transformer, generative pre-trained transformer, logistic regression, tensor rank decomposition, gradient by the straight through estimator (STE), etc.. For this tiny vision transformer demo, the PSGD-Kron-gradient-whitening preconditioner can outperform Adam(W) with the same hyperparameter settings.
For very sparse gradients, PSGD prefers whitening the momentum, and this GPT2 example shows that PSGD can outperform Adam(W) again with virtually the same hyperparameter settings (needs to reduce lr_params by times to match with Adam(W)'s lr).
Resources
- Preconditioned stochastic gradient descent, arXiv:1512.04202, 2015. (General ideas of PSGD, preconditioner fitting criteria and Kronecker product preconditioners.)
- Preconditioner on matrix Lie group for SGD, arXiv:1809.10232, 2018. (Focus on affine Lie group preconditioners, including feature normalization or whitening (per batch or layer) as special affine preconditioners. Use PSGD for gradient whitening.)
- Black box Lie group preconditioners for SGD, arXiv:2211.04422, 2022. (Mainly about the LRA preconditioner. I also have prepared these supplementary materials for detailed math derivations.)
- Stochastic Hessian fittings with Lie groups, arXiv:2402.11858, 2024. (Properties of PSGD, also a good summary of PSGD. The Hessian fitting problem is shown to be strongly convex in under certain mild assumptions.)
- Curvature-informed SGD via general purpose Lie-group preconditioners, arXiv:2402.04553, 2024. (Plenty of benchmark results and analyses for PSGD vs. other optimizers.)
- There are a few more efficient and specialized PSGD implementations, say Evan's, Lucas' and more. My Tensorflow implementations (TF 1.x and TF 2.x) are too old and not maintained. The implementation here provides a testbed for different choices and focuses on the clarity of math.