pub struct SoAOptimizerState { /* private fields */ }Expand description
Structure of Arrays (SoA) layout for optimizer state.
Momentum and variance for every registered parameter live in two contiguous
arrays rather than in per-parameter allocations, so a sweep over one moment
touches consecutive cache lines. Each parameter owns the half-open range
[momentum_offset, momentum_offset + size) of SoAOptimizerState::momentum
(and likewise for the variance array); ranges are padded up to a cache-line
boundary so no two parameters share a cache line.
The moments are persistent: update_parameter_soa reads the previous values
out of these arrays and writes the new ones back. An earlier revision recomputed
both from the current gradient on every call, which silently removed all of
Adam’s adaptivity.
Implementations§
Source§impl SoAOptimizerState
impl SoAOptimizerState
Sourcepub fn new(alignment: AlignmentConfig) -> Self
pub fn new(alignment: AlignmentConfig) -> Self
Creates a new SoA optimizer state.
Sourcepub fn add_parameter(&mut self, id: String, size: usize) -> Result<()>
pub fn add_parameter(&mut self, id: String, size: usize) -> Result<()>
Adds a parameter to the SoA layout, reserving zeroed moment storage for it.
Registering the same id twice is a no-op, so callers may register lazily.
§Errors
Returns an error when size is zero, which would produce an empty block.
Sourcepub fn get_parameter_info(&self, id: &str) -> Option<&ParameterInfo>
pub fn get_parameter_info(&self, id: &str) -> Option<&ParameterInfo>
Gets parameter information by ID.
Sourcepub fn momentum_of(&self, id: &str) -> Option<&[f32]>
pub fn momentum_of(&self, id: &str) -> Option<&[f32]>
Reads the persisted first moment of a parameter (primarily for tests).
Sourcepub fn variance_of(&self, id: &str) -> Option<&[f32]>
pub fn variance_of(&self, id: &str) -> Option<&[f32]>
Reads the persisted second moment of a parameter (primarily for tests).
Sourcepub fn step_count(&self) -> usize
pub fn step_count(&self) -> usize
Current global step counter.
Sourcepub fn update_parameter_soa(
&mut self,
param_id: &str,
param: &mut [f32],
grad: &[f32],
lr: f32,
betas: (f32, f32),
eps: f32,
weight_decay: f32,
) -> Result<()>
pub fn update_parameter_soa( &mut self, param_id: &str, param: &mut [f32], grad: &[f32], lr: f32, betas: (f32, f32), eps: f32, weight_decay: f32, ) -> Result<()>
Updates momentum and variance for a parameter using optimized memory access.
Sourcepub fn layout_stats(&self) -> LayoutStats
pub fn layout_stats(&self) -> LayoutStats
Gets memory layout statistics.
Trait Implementations§
Source§impl Debug for SoAOptimizerState
impl Debug for SoAOptimizerState
impl Send for SoAOptimizerState
impl Sync for SoAOptimizerState
Auto Trait Implementations§
impl Freeze for SoAOptimizerState
impl RefUnwindSafe for SoAOptimizerState
impl Unpin for SoAOptimizerState
impl UnsafeUnpin for SoAOptimizerState
impl UnwindSafe for SoAOptimizerState
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> 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