Skip to main content

Optimizer

Trait Optimizer 

pub trait Optimizer:
    Send
    + Sync
    + Clone
    + 'static {
    type State<const D: usize>: Clone + RecordState;

    // Required methods
    fn step<const D: usize>(
        &self,
        lr: f64,
        tensor: Tensor<D>,
        grad: Tensor<D>,
        state: Option<Self::State<D>>,
    ) -> (Tensor<D>, Option<Self::State<D>>);
    fn to_device<const D: usize>(
        state: Self::State<D>,
        device: &Device,
    ) -> Self::State<D>;
}
Expand description

An opinionated trait to simplify the process of implementing an optimizer.

Implementations don’t have to handle missing gradients, loading and exporting records, navigate the module parameter structure, handle tracked and untracked tensors, and the likes. Wrap one in a ModuleOptimizer to optimize a whole module.

Required Associated Types§

type State<const D: usize>: Clone + RecordState

The state of the optimizer for a single parameter of rank D.

It implements RecordState (which itself requires Send + Sync + 'static) so it can be decomposed into named tensors and scalars for the burnpack format.

Required Methods§

fn step<const D: usize>( &self, lr: f64, tensor: Tensor<D>, grad: Tensor<D>, state: Option<Self::State<D>>, ) -> (Tensor<D>, Option<Self::State<D>>)

The optimizer step is performed for one tensor at a time with its gradient and state.

Note that the state is passed as parameter, so implementations don’t have to handle the saving and loading of recorded states.

fn to_device<const D: usize>( state: Self::State<D>, device: &Device, ) -> Self::State<D>

Change the device of the state.

This function will be called accordingly to have the state on the same device as the gradient and the tensor when the step function is called.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

§

impl Optimizer for AdaGrad

§

type State<const D: usize> = AdaGradState<D>

§

impl Optimizer for Adafactor

§

type State<const D: usize> = AdafactorState<D>

§

impl Optimizer for Adam

§

type State<const D: usize> = AdamState<D>

§

impl Optimizer for AdamW

§

type State<const D: usize> = AdamWState<D>

§

impl Optimizer for Adan

§

type State<const D: usize> = AdanState<D>

§

impl Optimizer for Lamb

§

type State<const D: usize> = LambState<D>

§

impl Optimizer for Lion

§

type State<const D: usize> = LionState<D>

§

impl Optimizer for Muon

§

type State<const D: usize> = MuonState<D>

§

impl Optimizer for RmsProp

§

type State<const D: usize> = RmsPropState<D>

§

impl Optimizer for Sgd

§

type State<const D: usize> = SgdState<D>