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§
Source§impl ModuleOptimizer
impl ModuleOptimizer
Sourcepub 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.
Sourcepub 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.
Sourcepub fn with_grad_clipping(self, gradient_clipping: GradientClipping) -> Self
pub fn with_grad_clipping(self, gradient_clipping: GradientClipping) -> Self
Sourcepub fn with_group<O>(
self,
group: ParamGroup,
optim: O,
grad_clipping: Option<GradientClipping>,
) -> Selfwhere
O: DynOptimizer + 'static,
pub fn with_group<O>(
self,
group: ParamGroup,
optim: O,
grad_clipping: Option<GradientClipping>,
) -> Selfwhere
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.
Source§impl ModuleOptimizer
impl ModuleOptimizer
Sourcepub fn step<M: AutodiffModule>(
&mut self,
lr_module: ModuleLearningRate,
module: M,
grads: GradientsParams,
) -> M
pub fn step<M: AutodiffModule>( &mut self, lr_module: ModuleLearningRate, module: M, grads: GradientsParams, ) -> M
Update the module parameters with the given gradients, advancing the optimizer state.
Sourcepub fn step_multi<M: AutodiffModule>(
&mut self,
lr_module: ModuleLearningRate,
module: M,
grads: MultiGradientsParams,
) -> M
pub fn step_multi<M: AutodiffModule>( &mut self, lr_module: ModuleLearningRate, module: M, grads: MultiGradientsParams, ) -> M
Like step, but accumulating gradients sourced from multiple devices.
Sourcepub fn to_record(&self) -> OptimizerRecord
pub fn to_record(&self) -> OptimizerRecord
Decompose the optimizer state into a serializable OptimizerRecord.
Sourcepub fn load_record(self, record: OptimizerRecord) -> Self
pub fn load_record(self, record: OptimizerRecord) -> Self
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.
Sourcepub 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.
Sourcepub fn from_bytes(self, bytes: Bytes) -> Result<Self, RecordError>
pub fn from_bytes(self, bytes: Bytes) -> Result<Self, RecordError>
Load the optimizer state from an in-memory burnpack byte buffer.
Trait Implementations§
Source§impl Clone for ModuleOptimizer
impl Clone for ModuleOptimizer
Source§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 moreAuto 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> CloneExpand for Twhere
T: Clone,
impl<T> CloneExpand for Twhere
T: Clone,
fn __expand_clone_method(&self, _: &Scope) -> T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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