Trainer Runtime Internals #
Scalar-generic implementation of the trainer: an instantiated model with reusable forward and loss
evaluators (Runner), gradient accumulation and optimizer updates, and the stateful Stepper that
applies a configured optimizer and schedule.
Everything here is indexed by the runtime scalar α. Trainer.openTyped preserves that scalar at
the session boundary; the runtime-selected Trainer.open, Trainer.train, and Trainer.predict
paths expose Float.
A checked model instantiated under one runtime scalar.
This bundles the imperative runtime objective (parameters and buffers stored in refs) and reusable no-gradient forward and evaluation-loss evaluators over the same live state. Training forwards update their buffers; evaluation forwards leave them unchanged.
- runtimeObjective : TorchLean.Module.Objective α Unit (Runtime.Autograd.Model.Layers.Seq.stateShapes model) [σ, τ]
- trainingPredictor : Runtime.Autograd.Model.Module.Evaluator α Unit (Runtime.Autograd.Model.Layers.Seq.stateShapes model) [σ] [] τ
- evaluationPredictor : Runtime.Autograd.Model.Module.Evaluator α Unit (Runtime.Autograd.Model.Layers.Seq.stateShapes model) [σ] [] τ
- evaluationLossEvaluator : Runtime.Autograd.Model.Module.ObjectiveEvaluator α Unit (Runtime.Autograd.Model.Layers.Seq.stateShapes model) [σ, τ]
Instances For
Construct a runner from its objective and evaluators.
The executable runtime module behind a runner.
The reusable no-gradient evaluator for training or evaluation behavior.
Reusable evaluator for the evaluation-mode loss.
Runtime configuration used by the runner's objective.
Instances For
Finish runner construction once its executable objective has been instantiated.
Instances For
Instantiate a model and objective under a runtime scalar.
Explicit state is retained in α; otherwise stored Float initializers are cast with ofFloat.
Instances For
Read the complete parameter-and-buffer state.
Instances For
Replace the complete parameter-and-buffer state.
Instances For
Initialize the state owned by a runtime optimizer for this runner.
Instances For
Evaluate one input in the given mode (training by default).
Instances For
Scalar loss of one sample in the given mode (training by default).
Training uses the objective's random stream and buffer updates. Evaluation uses a reusable no-gradient evaluator over the live parameters without updating running buffers.
Instances For
Optimizer binding #
Bind a configured optimizer, its initialized state, and its scheduled state updater.
Instances For
Gradient accumulation and optimizer updates #
Mean parameter gradient and mean loss for a nonempty batch at one parameter point.
With withLoss := false the losses are not read back and the second component is 0.
For IEEE scalars we retain a pre-weighted sum alongside the ordinary sum and use it only if the
ordinary sum becomes nonfinite. Finite means are clamped to the observed componentwise range.
This avoids overflow in examples such as two equal large gradients; mixed-sign cancellation can
still lose precision, and this is not an accuracy guarantee.
Instances For
Compute mean parameter gradients for a nonempty batch at one parameter point.
Set value := true to return (meanGradient, meanLoss).
Instances For
Apply one optimizer update to a nonempty batch.
When useNative is set, supported CUDA optimizers accumulate gradients on device and use the
same moment state for every batch size. Other optimizers average explicit per-sample gradients.
Set loss := true to return (nextOptimizerState, meanLoss).
Instances For
Stateful steppers #
Stateful optimizer step functions and completed-step counter for one runner.
- runBatch : Array (TorchLean.Sample.Supervised α σ τ) → IO α
- runBatchSilently : Array (TorchLean.Sample.Supervised α σ τ) → IO Unit
Instances For
Construct a stepper from its hidden batch-step functions and counter.
The hidden loss-returning batch-step function.
The hidden batch-step function that does not read the loss back.
The hidden completed-step counter.
Apply one optimizer update, optionally returning its scalar loss.
batch := true takes a nonempty array and averages gradients at the same parameter point. The
counter advances once for the whole batch. loss := false avoids reading the loss back.
Instances For
Read the number of completed optimizer steps.
Instances For
Construct a Stepper for a runner, optimizer config, and optional scheduler.
Every step trains through the runner's objective, applies the schedule for the current step index, and advances the completed-step counter once per batch. A singleton batch uses the runtime's native single-sample update.