pub struct TensorGradAccumulator { /* private fields */ }Expand description
Accumulates gradients across mini-batches before an optimizer step.
§Typical usage
use ipfrs_tensorlogic::grad_accumulator::{
TensorGradAccumulator, AccumulatorConfig, AccumulationMode,
};
let config = AccumulatorConfig {
accumulation_steps: 2,
mode: AccumulationMode::Mean,
max_grad_norm: Some(1.0),
};
let mut acc = TensorGradAccumulator::new(config);
// First mini-batch
acc.accumulate("weight", &[0.5, -0.3]).expect("example: should succeed in docs");
assert!(!acc.is_ready());
// Second mini-batch
acc.accumulate("weight", &[0.7, 0.1]).expect("example: should succeed in docs");
assert!(acc.is_ready());
// Retrieve accumulated gradients and reset
let grads = acc.step().expect("example: should succeed in docs");
assert!(grads.contains_key("weight"));Implementations§
Source§impl TensorGradAccumulator
impl TensorGradAccumulator
Sourcepub fn new(config: AccumulatorConfig) -> Self
pub fn new(config: AccumulatorConfig) -> Self
Create a new accumulator with the given configuration.
Sourcepub fn accumulate(
&mut self,
name: &str,
gradients: &[f64],
) -> Result<(), String>
pub fn accumulate( &mut self, name: &str, gradients: &[f64], ) -> Result<(), String>
Add gradients for the named parameter to the accumulation buffer.
If the buffer for name already exists its length must match
gradients.len(), otherwise an error is returned. On the first
call for a given name the buffer is created with the supplied length.
After all parameter gradients for a mini-batch have been added the
caller should increment the internal step counter by calling
accumulate for every parameter in each
mini-batch.
Sourcepub fn is_ready(&self) -> bool
pub fn is_ready(&self) -> bool
Returns true when every buffer has accumulated at least
accumulation_steps contributions.
Sourcepub fn step(&mut self) -> Result<HashMap<String, Vec<f64>>, String>
pub fn step(&mut self) -> Result<HashMap<String, Vec<f64>>, String>
Consume the accumulated gradients and return the final values.
- In
AccumulationMode::Meanmode each gradient vector is divided by the number of steps that were accumulated. - If
max_grad_normis configured, gradient vectors whose L2 norm exceeds it are rescaled. - All buffers are cleared after a successful step.
Returns an error if the accumulator is not ready (see
is_ready).
Sourcepub fn clip_grad_norm(gradients: &mut [f64], max_norm: f64) -> f64
pub fn clip_grad_norm(gradients: &mut [f64], max_norm: f64) -> f64
Clip a gradient vector in-place so that its L2 norm does not exceed
max_norm.
Returns the original L2 norm (before any scaling).
Sourcepub fn get_buffer(&self, name: &str) -> Option<&GradBuffer>
pub fn get_buffer(&self, name: &str) -> Option<&GradBuffer>
Borrow the buffer for the named parameter, if it exists.
Sourcepub fn buffer_count(&self) -> usize
pub fn buffer_count(&self) -> usize
Number of parameter buffers currently tracked.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Clear all buffers and reset the step counter. Lifetime statistics
(total_accumulations, total_clips) are preserved.
Sourcepub fn current_step(&self) -> usize
pub fn current_step(&self) -> usize
Current accumulation step (0-based within the current window).
Sourcepub fn stats(&self) -> AccumulatorStats
pub fn stats(&self) -> AccumulatorStats
Return a snapshot of the accumulator’s statistics.
Auto Trait Implementations§
impl Freeze for TensorGradAccumulator
impl RefUnwindSafe for TensorGradAccumulator
impl Send for TensorGradAccumulator
impl Sync for TensorGradAccumulator
impl Unpin for TensorGradAccumulator
impl UnsafeUnpin for TensorGradAccumulator
impl UnwindSafe for TensorGradAccumulator
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
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