{ "

Sophia Optimizer

\n

This is a PyTorch implementation of Sophia-G from paper Sophia: A Scalable Stochastic Second-order Optimizer for Language Model Pre-training. Official implementation is available at Liuhong99/Sophia.

\n

Sophia is more adaptive to heterogeneous curvatures than Adam, more resistant to non-convexity and rapid change of Hessian than Newton\u2019s method, and also uses a low-cost pre-conditioner.

\n

Sophia keeps diagonal Hessian estimates with EMA across iterations. The diagonal Hessian _^_0_^_ is calculated every _^_1_^_ steps.

\n_^_2_^_

Sophia uses EMA of gradients _^_3_^_, only considers positive entries of the diagonal Hessian and does per-coordinate clipping to the update.

\n_^_4_^_

where _^_5_^_ is a very small value to prevent division by _^_6_^_.

\n

Gauss-Newton-Bartlett (GNB) estimator

\n_^_7_^_

where _^_8_^_ are the inputs, _^_9_^_ is the batch size (number of inputs/tokens), _^_10_^_ is cross entropy loss, and _^_11_^_ are sampled from the logits _^_12_^_.

\n

Note that this hessian estimate is always positive and therefore we can replace _^_13_^_ with _^_14_^_.

\n

Sophia with Gauss-Newton-Bartlett (GNB) estimator is Sophia-G

\n

Here is an experiment that uses Sophia-G to train a transformer.

\n": "

Sophia Optimizer

\n

This is a PyTorch implementation of Sophia-G from paper Sophia: A Scalable Stochastic Second-order Optimizer for Language Model Pre-training. Official implementation is available at Liuhong99/Sophia.

\n

Sophia is more adaptive to heterogeneous curvatures than Adam, more resistant to non-convexity and rapid change of Hessian than Newton\u2019s method, and also uses a low-cost pre-conditioner.

\n

Sophia keeps diagonal Hessian estimates with EMA across iterations. The diagonal Hessian _^_0_^_ is calculated every _^_1_^_ steps.

\n_^_2_^_

Sophia uses EMA of gradients _^_3_^_, only considers positive entries of the diagonal Hessian and does per-coordinate clipping to the update.

\n_^_4_^_

where _^_5_^_ is a very small value to prevent division by _^_6_^_.

\n

Gauss-Newton-Bartlett (GNB) estimator

\n_^_7_^_

where _^_8_^_ are the inputs, _^_9_^_ is the batch size (number of inputs/tokens), _^_10_^_ is cross entropy loss, and _^_11_^_ are sampled from the logits _^_12_^_.

\n

Note that this hessian estimate is always positive and therefore we can replace _^_13_^_ with _^_14_^_.

\n

Sophia with Gauss-Newton-Bartlett (GNB) estimator is Sophia-G

\n

Here is an experiment that uses Sophia-G to train a transformer.

\n", "

Sophia-G Optimizer

\n

We extend the class _^_0_^_ defined in _^_1_^_ to implement the Sophia optimizer.

\n": "

Sophia-G Optimizer

\n

We extend the class _^_0_^_ defined in _^_1_^_ to implement the Sophia optimizer.

\n", "

Initialize a parameter state

\n\n": "

Initialize a parameter state

\n\n", "

Initialize the optimizer

\n\n": "

Initialize the optimizer

\n\n", "

Take an update step for a given parameter tensor

\n\n

We do the following parameter update,

\n_^_7_^_": "

Take an update step for a given parameter tensor

\n\n

We do the following parameter update,

\n_^_7_^_", "

Update the EMA of Hessian diagonal _^_0_^_

\n\n_^_3_^_": "

Update the EMA of Hessian diagonal _^_0_^_

\n\n_^_3_^_", "

_^_0_^_

\n": "

_^_0_^_

\n", "

Calculate weight decay

\n": "

Calculate weight decay

\n", "

Exponential moving average of Hessian diagonal, _^_0_^_

\n": "

Exponential moving average of Hessian diagonal, _^_0_^_

\n", "

Exponential moving average of gradients, _^_0_^_

\n": "

Exponential moving average of gradients, _^_0_^_

\n", "

Get _^_0_^_

\n": "

Get _^_0_^_

\n", "

Get _^_0_^_ and _^_1_^_

\n": "

Get _^_0_^_ and _^_1_^_

\n", "

Get maximum learning rate _^_0_^_

\n": "

Get maximum learning rate _^_0_^_

\n", "

Get optimizer state

\n": "

Get optimizer state

\n", "

In-place calculation of _^_0_^_ _^_1_^_

\n": "

In-place calculation of _^_0_^_ _^_1_^_

\n", "

Increment _^_0_^_ the number of optimizer steps

\n": "

Increment _^_0_^_ the number of optimizer steps

\n", "

Initialize state if empty

\n": "

Initialize state if empty

\n", "

Iterate through parameter groups

\n": "

Iterate through parameter groups

\n", "

Iterate through parameters

\n": "

Iterate through parameters

\n", "

Skip parameters without gradients

\n": "

Skip parameters without gradients

\n", "

This is the number of optimizer steps taken on the parameter, _^_0_^_

\n": "

This is the number of optimizer steps taken on the parameter, _^_0_^_

\n", "

Update EMA Hessian diagonal

\n_^_0_^_

\n": "

Update EMA Hessian diagonal

\n_^_0_^_

\n", "A simple PyTorch implementation/tutorial of Sophia optimizer": "A simple PyTorch implementation/tutorial of Sophia optimizer", "Sophia Optimizer": "Sophia Optimizer" }