ParallelTrainer

Struct ParallelTrainer 

Source
pub struct ParallelTrainer<M, O, B: Backend> {
    pub model: M,
    pub optimizer: O,
    /* private fields */
}
Expand description

High-level training loop with gradient accumulation.

Splits a large effective batch into accumulation_steps micro-batches, accumulates gradients across all of them, then performs a single optimizer step. This simulates a larger batch size without requiring more memory.

§Example

let model = Sequential::new(vec![...]);
let optimizer = Adam::new(model.parameters(), 1e-3);
let mut trainer = ParallelTrainer::new(model, optimizer, 4);

// Each call accumulates 1/4 of the gradient; every 4th call steps.
for (i, (x, y)) in data.iter().enumerate() {
    if let Some(loss) = trainer.accumulate_step(&x, &y, mse_loss)? {
        println!("step {}: loss = {:.4}", i, loss);
    }
}

Fields§

§model: M

The model.

§optimizer: O

The optimizer.

Implementations§

Source§

impl<M, O, B> ParallelTrainer<M, O, B>
where M: Module<B>, O: Optimizer<B>, B: Backend,

Source

pub fn new(model: M, optimizer: O, accumulation_steps: usize) -> Self

Create a new ParallelTrainer.

accumulation_steps: number of micro-batches before each optimizer step.

Source

pub fn accumulate_step<F>( &mut self, input: &Tensor<B>, target: &Tensor<B>, loss_fn: F, ) -> Result<Option<f64>>
where F: Fn(&Tensor<B>, &Tensor<B>) -> Result<Tensor<B>>,

Process one micro-batch. Returns Some(avg_loss) when an optimizer step was performed (every accumulation_steps calls), else None.

Source

pub fn flush(&mut self) -> Result<Option<f64>>

Force an optimizer step with whatever gradients have been accumulated so far. Useful at the end of an epoch when remaining micro-batches < accumulation_steps.

Auto Trait Implementations§

§

impl<M, O, B> Freeze for ParallelTrainer<M, O, B>
where M: Freeze, O: Freeze,

§

impl<M, O, B> RefUnwindSafe for ParallelTrainer<M, O, B>

§

impl<M, O, B> Send for ParallelTrainer<M, O, B>
where M: Send, O: Send,

§

impl<M, O, B> Sync for ParallelTrainer<M, O, B>
where M: Sync, O: Sync,

§

impl<M, O, B> Unpin for ParallelTrainer<M, O, B>
where M: Unpin, O: Unpin, B: Unpin,

§

impl<M, O, B> UnwindSafe for ParallelTrainer<M, O, B>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V