TorchLean API

NN.API.Trainer.Manual.Loops

Runtime Supervised Training #

Training loops for in-memory datasets and DataLoaders. A list-valued batch means gradient accumulation: every dataset item is differentiated at the same parameter point, the typed gradient packs are averaged, and the optimizer is applied once. An item may itself contain a fixed-size tensor minibatch; use a list batch of one for one vectorized pass in that case.

Gradient accumulation #

Add two shape-aligned gradient packs and materialize every result tensor.

Instances For

    Scale a shape-aligned gradient pack and materialize every result tensor.

    Instances For

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

      Mutable model buffers advance before differentiation. Trainable parameters remain fixed until all sample gradients have been collected.

      Instances For

        Compute the mean loss and parameter gradients for a nonempty sample batch at one parameter point.

        Use this form only when the caller needs the loss value. On CUDA, reading that scalar back to the host synchronizes the device.

        Instances For
          def TorchLean.Trainer.Manual.Internal.stepBatch {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Context α] [DecidableEq Spec.Shape] (runner : Runner α task) (opt : Runtime.Autograd.TorchLean.Optimizer α (paramShapes task)) (state : opt.State) (useNativeSingleton : Bool) (batch : List (Runtime.Autograd.Torch.TList α [σ, τ])) :
          IO opt.State

          Apply one optimizer update without reading a loss scalar.

          useNativeSingleton is chosen once by the surrounding loop. Keeping it fixed prevents a final partial loader batch from switching between device-resident and host optimizer state.

          Instances For
            def TorchLean.Trainer.Manual.Internal.stepBatchAndLoss {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Context α] [DecidableEq Spec.Shape] (runner : Runner α task) (opt : Runtime.Autograd.TorchLean.Optimizer α (paramShapes task)) (state : opt.State) (useNativeSingleton : Bool) (batch : List (Runtime.Autograd.Torch.TList α [σ, τ])) :
            IO (opt.State × α)

            Apply one optimizer update and return the loss from the gradient-producing tapes.

            Instances For
              def TorchLean.Trainer.Manual.Internal.runSampleSteps {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (cfg : TrainConfig) (nextBatch : IO (List (Runtime.Autograd.Torch.TList α [σ, τ]))) (watchMemory : IO Unit) (opt : Runtime.Autograd.TorchLean.Optimizer α (paramShapes task)) (initialState : opt.State) (scheduleState : opt.Stateopt.State) :

              Run the fixed-step in-memory loop for one concrete optimizer state type.

              Instances For

                In-memory datasets #

                def TorchLean.Trainer.Manual.trainSamples {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] [Runtime.FromFloat α] (runner : Runner α task) (cfg : TrainConfig) (samples : List (Runtime.Autograd.Torch.TList α [σ, τ])) :

                Train on an in-memory sample list for a fixed number of optimizer updates.

                cfg.batchSize controls how many dataset items contribute to each update. It does not multiply the number of optimizer steps. If an item is already a vectorized tensor minibatch, use batchSize := 1 unless you intend to accumulate gradients across several such minibatches.

                Instances For

                  Train over a dataset by materializing its sample list.

                  Instances For

                    Data loaders #

                    Run all loader epochs for one concrete optimizer state type.

                    Instances For
                      def TorchLean.Trainer.Manual.Internal.runLoaderEpochs.trainBatches {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (cfg : LoaderTrainConfig) (opt : Runtime.Autograd.TorchLean.Optimizer α (paramShapes task)) (useNativeSingleton : Bool) (stepIdx : ) (state : opt.State) (batches : List (List (Runtime.Autograd.Torch.TList α [σ, τ]))) :
                      IO ( × opt.State)
                      Instances For
                        def TorchLean.Trainer.Manual.Internal.runLoaderEpochs.trainEpochs {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (cfg : LoaderTrainConfig) (opt : Runtime.Autograd.TorchLean.Optimizer α (paramShapes task)) (scheduleState : opt.Stateopt.State) (useNativeSingleton : Bool) (epoch remaining stepIdx : ) (loader : Runtime.Autograd.Train.DataLoader (Runtime.Autograd.Torch.TList α [σ, τ])) (state : opt.State) :
                        Instances For

                          Train over a DataLoader.

                          Every loader batch contributes one averaged-gradient optimizer update. The scheduler advances once per epoch.

                          Instances For