TorchLean API

NN.API.Trainer.Runner

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.

structure TorchLean.Trainer.Internal.Runner (α : Type) [Storage α] [Context α] {σ τ : Shape} (model : nn.Sequential σ τ) :

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.

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.

    def TorchLean.Trainer.Internal.Runner.runtime {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (runner : Runner α model) :

    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
            def TorchLean.Trainer.Internal.Runner.setState {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (runner : Runner α model) (state : nn.State α (Runtime.Autograd.Model.Layers.Seq.stateShapes model)) :

            Replace the complete parameter-and-buffer state.

            Instances For

              Initialize the state owned by a runtime optimizer for this runner.

              Instances For
                def TorchLean.Trainer.Internal.Runner.forward {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (runner : Runner α model) (input : Tensor α σ) (mode : nn.Mode := Runtime.Autograd.Model.Layers.Mode.train) :
                IO (Tensor α τ)

                Evaluate one input in the given mode (training by default).

                Instances For
                  def TorchLean.Trainer.Internal.Runner.loss {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (runner : Runner α model) (sample : Sample.Supervised α σ τ) (mode : nn.Mode := Runtime.Autograd.Model.Layers.Mode.train) :
                  IO α

                  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 #

                    def TorchLean.Trainer.Internal.withBoundOptimizer {σ τ : Shape} {model : nn.Sequential σ τ} {α β : Type} [Storage α] [Context α] [Runtime.FromFloat α] (runner : Runner α model) (config : optim.Optimizer) (scheduler : Option Scheduler.Config) (continuation : (optimizer : Runtime.Autograd.Model.Optimizer α (Runtime.Autograd.Model.Layers.Seq.stateShapes model)) → optimizer.State → (ℕ → optimizer.State → optimizer.State) → IO β) :
                    IO β

                    Bind a configured optimizer, its initialized state, and its scheduled state updater.

                    Instances For

                      Gradient accumulation and optimizer updates #

                      def TorchLean.Trainer.Internal.meanGradAndLoss {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (runner : Runner α model) (batch : Array (Sample.Supervised α σ τ)) (withLoss : Bool) :

                      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
                        def TorchLean.Trainer.Internal.meanGrad {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (runner : Runner α model) (batch : Array (Sample.Supervised α σ τ)) (value : Bool := false) :

                        Compute mean parameter gradients for a nonempty batch at one parameter point.

                        Set value := true to return (meanGradient, meanLoss).

                        Instances For
                          def TorchLean.Trainer.Internal.step {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (runner : Runner α model) (optimizer : Runtime.Autograd.Model.Optimizer α (Runtime.Autograd.Model.Layers.Seq.stateShapes model)) (state : optimizer.State) (useNative : Bool) (batch : Array (Sample.Supervised α σ τ)) (loss : Bool := false) :
                          IO (match loss with | false => optimizer.State | true => optimizer.State × α)

                          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 #

                            structure TorchLean.Trainer.Internal.Stepper (α : Type) [Storage α] [Context α] {σ τ : Shape} (model : nn.Sequential σ τ) :

                            Stateful optimizer step functions and completed-step counter for one runner.

                            Instances For
                              opaque TorchLean.Trainer.Internal.Stepper.create {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (runBatch : Array (Sample.Supervised α σ τ) → IO α) (runBatchSilently : Array (Sample.Supervised α σ τ) → IO Unit) (stepRef : IO.Ref ℕ) :
                              Stepper α model

                              Construct a stepper from its hidden batch-step functions and counter.

                              opaque TorchLean.Trainer.Internal.Stepper.action {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (stepper : Stepper α model) :
                              Array (Sample.Supervised α σ τ) → IO α

                              The hidden loss-returning batch-step function.

                              opaque TorchLean.Trainer.Internal.Stepper.silentAction {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (stepper : Stepper α model) :

                              The hidden batch-step function that does not read the loss back.

                              opaque TorchLean.Trainer.Internal.Stepper.counter {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (stepper : Stepper α model) :

                              The hidden completed-step counter.

                              def TorchLean.Trainer.Internal.Stepper.step {σ τ : Shape} {model : nn.Sequential σ τ} {Input α : Type} [Storage α] [Context α] (stepper : Stepper α model) (sample : Input) (batch loss : Bool := false) [Internal.BatchInput (Sample.Supervised α σ τ) (Array (Sample.Supervised α σ τ)) batch Input] :
                              IO (match loss with | false => Unit | true => α)

                              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
                                def TorchLean.Trainer.Internal.Stepper.steps {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] (stepper : Stepper α model) :

                                Read the number of completed optimizer steps.

                                Instances For
                                  def TorchLean.Trainer.Internal.Runner.stepper {σ τ : Shape} {model : nn.Sequential σ τ} {α : Type} [Storage α] [Context α] [Runtime.FromFloat α] (runner : Runner α model) (optimizer : optim.Optimizer) (scheduler : Option Scheduler.Config := none) :
                                  IO (Stepper α model)

                                  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.

                                  Instances For