pub struct GradientSparsifier {
pub config: SparsityConfig,
pub residual: Vec<f32>,
pub stats: SparsifierStats,
}Expand description
Stateful gradient sparsifier with optional residual accumulation.
Residual accumulation ensures that gradient information dropped in one round is carried forward and injected in subsequent rounds, preventing systematic bias toward always-large parameters.
Fields§
§config: SparsityConfigSparsification policy.
residual: Vec<f32>Accumulated residual from previously dropped gradient elements.
stats: SparsifierStatsCumulative statistics.
Implementations§
Source§impl GradientSparsifier
impl GradientSparsifier
Sourcepub fn new(config: SparsityConfig, gradient_len: usize) -> Self
pub fn new(config: SparsityConfig, gradient_len: usize) -> Self
Create a new sparsifier.
gradient_len is used to pre-allocate the residual buffer. If the
gradient length changes between calls, the residual is silently
zero-extended or truncated to match.
Sourcepub fn sparsify(&mut self, gradient: &[f32]) -> SparseGradient
pub fn sparsify(&mut self, gradient: &[f32]) -> SparseGradient
Sparsify a gradient vector.
Steps performed:
- If
accumulate_residuals, add the stored residual togradientelement-wise (extending or truncating the residual as needed). - Apply top-k and/or threshold selection.
- Update the residual with the dropped elements.
- Update statistics.
Returns a SparseGradient containing only the kept elements.
Sourcepub fn reset_residual(&mut self)
pub fn reset_residual(&mut self)
Clear the residual buffer (set all entries to zero).
Auto Trait Implementations§
impl Freeze for GradientSparsifier
impl RefUnwindSafe for GradientSparsifier
impl Send for GradientSparsifier
impl Sync for GradientSparsifier
impl Unpin for GradientSparsifier
impl UnsafeUnpin for GradientSparsifier
impl UnwindSafe for GradientSparsifier
Blanket Implementations§
impl<T> Allocation for T
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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