pub struct OptimizerState {
pub step: usize,
pub momentum: HashMap<String, Vec<f32>>,
pub variance: HashMap<String, Vec<f32>>,
pub third_moment: HashMap<String, Vec<f32>>,
pub param_steps: HashMap<String, usize>,
pub velocity: HashMap<String, Vec<f32>>,
pub params: ParamRegistry,
}Expand description
Unified state management for optimizer parameters.
This struct provides a consistent interface for tracking optimizer state across different algorithms, reducing code duplication and memory overhead.
Fields§
§step: usizeCurrent step counter for bias correction and scheduling
momentum: HashMap<String, Vec<f32>>First moment estimates (momentum buffers)
variance: HashMap<String, Vec<f32>>Second moment estimates (squared gradient buffers)
third_moment: HashMap<String, Vec<f32>>Optional third moment estimates (for higher-order methods)
param_steps: HashMap<String, usize>Per-parameter step counts (for adaptive methods)
velocity: HashMap<String, Vec<f32>>Velocity buffers for optimization methods like SGD with momentum
params: ParamRegistryStable parameter identity registry.
Every buffer map above is keyed by the canonical keys handed out by this
registry. It is deliberately not serialised: identity is reconstructed on
load from the checkpointed keys themselves (see crate::param_id), which
keeps the on-disk state format unchanged.
Implementations§
Source§impl OptimizerState
impl OptimizerState
Sourcepub fn param_key_for_tensor(&mut self, tensor: &Tensor) -> Result<String>
pub fn param_key_for_tensor(&mut self, tensor: &Tensor) -> Result<String>
Resolves the stable state key for an anonymous parameter tensor.
This is the supported replacement for the old format!("{:p}", …) idiom: the
returned key survives checkpoint save/load, whereas a heap address does not.
§Errors
Returns an error when the tensor dtype has no addressable buffer, or when the parameter ordering does not match a restored checkpoint.
Sourcepub fn param_key(&mut self, addr: usize, numel: usize) -> Result<String>
pub fn param_key(&mut self, addr: usize, numel: usize) -> Result<String>
Resolves the stable state key for an anonymous parameter given its buffer address and element count.
§Errors
Sourcepub fn param_key_named(
&mut self,
name: &str,
addr: usize,
numel: usize,
) -> String
pub fn param_key_named( &mut self, name: &str, addr: usize, numel: usize, ) -> String
Resolves the stable state key for a parameter that has a caller-supplied name.
Named keys make checkpoint resume independent of parameter visit order and should be preferred wherever the surrounding API carries names.
Sourcepub fn restore_param_key(&mut self, key: &str, numel: usize) -> Result<ParamId>
pub fn restore_param_key(&mut self, key: &str, numel: usize) -> Result<ParamId>
Rebuilds one registry slot from a checkpointed state key.
§Errors
Returns an error when key carries no recognised identity prefix.
Sourcepub fn get_or_create_momentum(
&mut self,
param_id: String,
size: usize,
) -> &mut Vec<f32>
pub fn get_or_create_momentum( &mut self, param_id: String, size: usize, ) -> &mut Vec<f32>
Gets or creates momentum buffer for a parameter.
Sourcepub fn get_or_create_variance(
&mut self,
param_id: String,
size: usize,
) -> &mut Vec<f32>
pub fn get_or_create_variance( &mut self, param_id: String, size: usize, ) -> &mut Vec<f32>
Gets or creates variance buffer for a parameter.
Sourcepub fn get_or_create_third_moment(
&mut self,
param_id: String,
size: usize,
) -> &mut Vec<f32>
pub fn get_or_create_third_moment( &mut self, param_id: String, size: usize, ) -> &mut Vec<f32>
Gets or creates third moment buffer for a parameter.
Sourcepub fn step_param(&mut self, param_id: String)
pub fn step_param(&mut self, param_id: String)
Increments the step counter for a specific parameter.
Sourcepub fn get_param_step(&self, param_id: &str) -> usize
pub fn get_param_step(&self, param_id: &str) -> usize
Gets the step count for a specific parameter.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears all state buffers to free memory.
The parameter identity registry is cleared alongside the buffers: leaving
registrations behind while dropping their state would keep params.len()
reporting parameters whose buffers no longer exist, and would leave the
registry’s bind cursor advanced past slots that are ready to be reused.
Sourcepub fn memory_usage(&self) -> StateMemoryStats
pub fn memory_usage(&self) -> StateMemoryStats
Gets memory usage statistics.
Trait Implementations§
Source§impl Clone for OptimizerState
impl Clone for OptimizerState
Source§fn clone(&self) -> OptimizerState
fn clone(&self) -> OptimizerState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for OptimizerState
impl Debug for OptimizerState
Source§impl Default for OptimizerState
impl Default for OptimizerState
Source§impl<'de> Deserialize<'de> for OptimizerState
impl<'de> Deserialize<'de> for OptimizerState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for OptimizerState
impl RefUnwindSafe for OptimizerState
impl Send for OptimizerState
impl Sync for OptimizerState
impl Unpin for OptimizerState
impl UnsafeUnpin for OptimizerState
impl UnwindSafe for OptimizerState
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,
Source§impl<T> ConfigSerializable for Twhere
T: Serialize + for<'de> Deserialize<'de>,
impl<T> ConfigSerializable for Twhere
T: Serialize + for<'de> Deserialize<'de>,
Source§fn save_to_file(&self, path: &Path) -> Result<(), TrustformersError>
fn save_to_file(&self, path: &Path) -> Result<(), TrustformersError>
Source§fn load_from_file(path: &Path) -> Result<Self, TrustformersError>where
Self: Sized,
fn load_from_file(path: &Path) -> Result<Self, TrustformersError>where
Self: Sized,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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