Skip to main content

Trainer

Struct Trainer 

Source
pub struct Trainer {
    pub model: Lfm2Model,
    pub optimizer: Box<dyn Optimizer>,
    pub ctx: Arc<WgpuContext>,
    pub engine: Arc<ComputeEngine>,
    pub clip_grad_norm: Option<f32>,
    pub grad_accum_steps: usize,
    pub step_in_accum: usize,
    pub scaler: Option<GradScaler>,
    pub distributed_grad_sync: Option<FileGradSyncConfig>,
    pub lr_scheduler: Option<Box<dyn LRScheduler>>,
    pub length_bucket: usize,
    /* private fields */
}
Expand description

Runs optimization steps on a model.

Fields§

§model: Lfm2Model

The model being trained.

§optimizer: Box<dyn Optimizer>

Optimizer applied to the trainable parameters.

§ctx: Arc<WgpuContext>

GPU context.

§engine: Arc<ComputeEngine>

Kernel engine.

§clip_grad_norm: Option<f32>

Clip the global gradient norm to this value before each step.

§grad_accum_steps: usize

Micro-batches (sequences) per optimizer step.

§step_in_accum: usize

Micro-batches accumulated so far in the current window.

§scaler: Option<GradScaler>

Dynamic loss scaler (fp16-style overflow protection).

§distributed_grad_sync: Option<FileGradSyncConfig>

Multi-process gradient averaging through a shared directory.

§lr_scheduler: Option<Box<dyn LRScheduler>>

Learning-rate schedule, advanced once per optimizer step (before it is read, so step k uses the schedule’s rate at k).

§length_bucket: usize

Sequences are right-padded to a multiple of this many tokens (with the padding excluded from the loss). Causal attention keeps real tokens from seeing the padding, so results are unchanged, but GPU buffer sizes repeat and can be reused. 1 disables padding.

Implementations§

Source§

impl Trainer

Source

pub fn new( model: Lfm2Model, optimizer: Box<dyn Optimizer>, ctx: Arc<WgpuContext>, engine: Arc<ComputeEngine>, ) -> Self

A trainer with no clipping, accumulation, scaler or schedule.

Source

pub fn with_length_bucket(self, tokens: usize) -> Self

Pad sequences to a multiple of tokens (see length_bucket).

Source

pub fn with_clip_grad_norm(self, max_norm: f32) -> Self

Clip the global gradient L2 norm to max_norm.

Source

pub fn with_grad_accum_steps(self, steps: usize) -> Self

Accumulate gradients over steps sequences per optimizer step. Each sequence’s loss is divided by steps, so the update matches the mean over the window.

Source

pub fn with_grad_scaler(self, scaler: GradScaler) -> Self

Scale the loss before backward and skip steps whose gradients overflow.

Source

pub fn with_file_grad_sync(self, cfg: FileGradSyncConfig) -> Self

Average gradients across processes before each optimizer step.

Source

pub fn with_lr_scheduler(self, scheduler: Box<dyn LRScheduler>) -> Self

Set the learning rate from scheduler before each optimizer step.

Source

pub fn optimizer_steps(&self) -> usize

Optimizer steps taken so far.

Source

pub fn set_optimizer_steps(&mut self, steps: usize)

Restore the optimizer-step counter (and advance the LR schedule to match) when resuming from a checkpoint.

Source

pub fn train_step( &mut self, input_ids: &[u32], target_ids: &[u32], train_params: &[Tensor], ) -> Result<StepStats, TrainError>

Train on one sequence with every target supervised.

Source

pub fn train_step_masked( &mut self, input_ids: &[u32], target_ids: &[u32], loss_mask: Option<&[u8]>, train_params: &[Tensor], ) -> Result<StepStats, TrainError>

Train on one sequence, optionally ignoring target positions via loss_mask. loss_mask is aligned with target_ids (1 = include, 0 = ignore).

Gradients accumulate into train_params; every grad_accum_steps calls they are (optionally) synced, clipped and applied, then cleared.

Source

pub fn train_batch( &mut self, batch: &Batch, train_params: &[Tensor], ) -> Result<Vec<StepStats>, TrainError>

Train on every sequence of batch (one micro-batch per sequence, padding stripped). Sequences without supervised tokens are skipped.

Auto Trait Implementations§

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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

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 = !

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

fn try_from(value: U) -> Result<T, !>

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

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

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

Source§

fn vzip(self) -> V