Struct ModuleOptimizer
pub struct ModuleOptimizer { /* private fields */ }Expand description
Optimizes a whole module by applying a per-parameter Optimizer to each of its parameters.
It is non-generic over the module and optimizer: any O: Optimizer is type-erased behind a
dynamic optimizer, and per-parameter states are kept as type-erased states keyed by
ParamId. Build one with optimizer.into() or
OptimizerConfig::init().
It is possible to use different optimizers for different parameters. To do so, use the ModuleOptimizer::with_group function to add an optimizer for all parameters matching the provided group.
Implementations§
§impl ModuleOptimizer
impl ModuleOptimizer
pub fn has_gradient_clipping(&self) -> bool
pub fn has_gradient_clipping(&self) -> bool
Check if the optimizer has gradient clipping. If there are multiple optimizers, checks if any group has gradient clipping.
pub fn grad_clipping(&self) -> Option<&GradientClipping>
pub fn grad_clipping(&self) -> Option<&GradientClipping>
Access the gradient clipping. If there are multiple optimizers, returns the first optimizer’s GradientClipping.
pub fn with_grad_clipping(
self,
gradient_clipping: GradientClipping,
) -> ModuleOptimizer
pub fn with_grad_clipping( self, gradient_clipping: GradientClipping, ) -> ModuleOptimizer
pub fn with_group<O>(
self,
group: ParamGroup,
optim: O,
grad_clipping: Option<GradientClipping>,
) -> ModuleOptimizerwhere
O: DynOptimizer + 'static,
pub fn with_group<O>(
self,
group: ParamGroup,
optim: O,
grad_clipping: Option<GradientClipping>,
) -> ModuleOptimizerwhere
O: DynOptimizer + 'static,
Adds an optimizer specific to a parameter group.
Parameters matching this group will be optimized using the provided optimizer and gradient clipping configuration.
§Matching Rules
- Precedence: If a parameter matches multiple groups, the last group added takes precedence.
- Fallback: The first optimizer added must match all parameters to act as a global fallback.
§Side Effects
- State Reset: Adding a new group will reset any existing optimizer states for parameters that match the new group.
§impl ModuleOptimizer
impl ModuleOptimizer
pub fn step<M>(
&mut self,
lr_module: impl Into<ModuleLearningRate>,
module: M,
grads: GradientsParams,
) -> Mwhere
M: Module,
pub fn step<M>(
&mut self,
lr_module: impl Into<ModuleLearningRate>,
module: M,
grads: GradientsParams,
) -> Mwhere
M: Module,
Update the module parameters with the given gradients, advancing the optimizer state.
pub fn step_multi<M>(
&mut self,
lr_module: impl Into<ModuleLearningRate>,
module: M,
grads: MultiGradientsParams,
) -> Mwhere
M: Module,
pub fn step_multi<M>(
&mut self,
lr_module: impl Into<ModuleLearningRate>,
module: M,
grads: MultiGradientsParams,
) -> Mwhere
M: Module,
Like step, but accumulating gradients sourced from multiple devices.
pub fn to_record(&self) -> OptimizerRecord
pub fn to_record(&self) -> OptimizerRecord
Decompose the optimizer state into a serializable OptimizerRecord.
pub fn load_record(self, record: OptimizerRecord) -> ModuleOptimizer
pub fn load_record(self, record: OptimizerRecord) -> ModuleOptimizer
Load the optimizer state from an OptimizerRecord.
State tensors are materialized on the default device; no device argument is needed because
each parameter’s state is migrated to that parameter’s (gradient’s) device on the next
step — see the to_device call in the step path. The load device
is therefore irrelevant to correctness.
pub fn into_bytes(&self) -> Result<Bytes, RecordError>
pub fn into_bytes(&self) -> Result<Bytes, RecordError>
Serialize the optimizer state to an in-memory burnpack byte buffer.
pub fn from_bytes(self, bytes: Bytes) -> Result<ModuleOptimizer, RecordError>
pub fn from_bytes(self, bytes: Bytes) -> Result<ModuleOptimizer, RecordError>
Load the optimizer state from an in-memory burnpack byte buffer.
pub fn save<P>(&self, path: P) -> Result<(), RecordError>
pub fn save<P>(&self, path: P) -> Result<(), RecordError>
Save the optimizer state to a burnpack file on disk.
pub fn load<P>(self, path: P) -> Result<ModuleOptimizer, RecordError>
pub fn load<P>(self, path: P) -> Result<ModuleOptimizer, RecordError>
Load the optimizer state from a burnpack file on disk.
Trait Implementations§
§impl Clone for ModuleOptimizer
impl Clone for ModuleOptimizer
§fn clone(&self) -> ModuleOptimizer
fn clone(&self) -> ModuleOptimizer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl<O> From<O> for ModuleOptimizerwhere
O: Optimizer,
impl<O> From<O> for ModuleOptimizerwhere
O: Optimizer,
§fn from(optim: O) -> ModuleOptimizer
fn from(optim: O) -> ModuleOptimizer
Auto Trait Implementations§
impl !RefUnwindSafe for ModuleOptimizer
impl !UnwindSafe for ModuleOptimizer
impl Freeze for ModuleOptimizer
impl Send for ModuleOptimizer
impl Sync for ModuleOptimizer
impl Unpin for ModuleOptimizer
impl UnsafeUnpin for ModuleOptimizer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<K, Q> Comparable<Q> for K
impl<K, Q> Comparable<Q> for K
§impl<K, Q> Equivalent<Q> for K
impl<K, Q> Equivalent<Q> for K
§fn equivalent(&self, key: &Q) -> bool
fn equivalent(&self, key: &Q) -> bool
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more