TorchLean API

NN.API.Trainer.Manual.Control

Manual Training #

Dependent runners, callback composition, custom batch streams, and reporting helpers for workflows that need more control than the ordinary Trainer.new interface.

Manual Training #

Direct control over runners, callbacks, data streams, and optimization steps. The ordinary Trainer interface is built from these definitions; custom training code can use them without crossing into a second API namespace.

def TorchLean.Trainer.Manual.accuracyOneHotBatched {σ : Spec.Shape} {classes batch : } {task : SeqTask (Spec.Shape.dim batch σ) (Spec.Shape.dim batch (Spec.Shape.dim classes Spec.Shape.scalar))} {α : Type} [Context α] [DecidableEq Spec.Shape] (runner : Runner α task) (samples : List (Sample.Batch α batch σ (Spec.Shape.dim classes Spec.Shape.scalar))) :

Count correct predictions in a one-hot labeled batched dataset.

Minibatch analogue of accuracyOneHot. The task already has a leading batch axis, so the implementation scores each row independently and accumulates totals.

Returns (correct, total) where total = batch * numBatches.

Instances For

    Callback event fired after each training step.

    • epoch :

      Current epoch number.

    • step :

      Global optimizer-step counter.

    • loss : α

      Loss reported for this step.

    Instances For

      Callback event fired at the end of an epoch (how many steps ran).

      • epoch :

        Epoch number that just completed.

      • steps :

        Number of steps executed in the epoch.

      Instances For

        Hooks for instrumenting callback-based training loops.

        Callbacks are ordinary IO hooks. They can print progress, update an in-memory curve, sample CUDA allocator state, or forward events to a project-specific metrics backend.

        Instances For

          No-op callbacks.

          Instances For

            Combine two callback collections by running them in sequence.

            Instances For
              @[implicit_reducible]

              for callbacks: a no-op callback collection.

              @[implicit_reducible]

              Callbacks form a monoid under sequential composition.

              Build callbacks that run action once at the start of training.

              Instances For

                Build callbacks that observe every training step.

                Instances For

                  Build a training callback that samples the CUDA allocator at a fixed step cadence.

                  The callback owns a small IO.Ref for the previous sample, so examples can compose it with ordinary loss-logging callbacks without threading allocator state through their training loops.

                  Instances For

                    Build callbacks that run at the end of each epoch.

                    Instances For

                      Build callbacks that run once at the end of training, with the final report.

                      Instances For

                        Step-indexed source of already-collated module inputs.

                        TorchLean.Data.batchLoader is the right interface when the data is a finite supervised dataset. Other training jobs draw batches from a rule or an external source: replay buffers, collocation samplers, synthetic scale inputs, or file-backed sequence windows. StepBatchStream is the direct stream interface for those cases.

                        The stream is still fully typed: each produced sample is a _root_.Runtime.Autograd.Torch.TList matching the module's inputShapes. The training loop below is model-agnostic and only assumes that the module can run forward and stepWith on those samples.

                        Instances For

                          Constant stream for fixed-batch overfit runs and fixed-sample training jobs.

                          Instances For
                            def TorchLean.Trainer.Manual.StepBatchStream.ofFn {α : Type} {inputShapes : List Spec.Shape} (f : Runtime.Autograd.Torch.TList α inputShapes) :
                            StepBatchStream α inputShapes

                            Build a stream from a pure step-indexed sample function.

                            Instances For
                              def TorchLean.Trainer.Manual.StepBatchStream.cycle {α : Type} {inputShapes : List Spec.Shape} (xs : List (Runtime.Autograd.Torch.TList α inputShapes)) (h : xs []) :
                              StepBatchStream α inputShapes

                              Cycle through a nonempty list of samples.

                              This adapter lets list-backed datasets use the step-stream trainer. The explicit nonempty proof keeps empty datasets from turning into silent modulo-by-zero behavior.

                              Instances For
                                def TorchLean.Trainer.Manual.withMode {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Context α] [DecidableEq Spec.Shape] {β : Type} (runner : Runner α task) (value : Runtime.Autograd.TorchLean.NN.Mode) (action : IO β) :
                                IO β

                                Run an action with the runner temporarily switched to value mode.

                                Use this for callback-based validation passes during training.

                                Instances For

                                  Mean loss for an already-instantiated scalar module over a typed minibatch loader.

                                  General streaming evaluation path used by the runtime examples. It is not CIFAR-specific: any supervised task whose loss module consumes [dim n σ, dim n τ] can use the same loader. The loader stores ordinary per-example samples (x : σ, y : τ); this definition asks TorchLean.Data.epoch for raw minibatches and calls TorchLean.Data.collateSupervised to build one shape-typed batch at a time.

                                  Two details matter for larger examples:

                                  • We force shuffle := false for evaluation so before/after metrics are deterministic.
                                  • We do not call TorchLean.Data.BatchLoader.batchDataset, because that would materialize every collated minibatch at once. Streaming keeps the same API usable for image, sequence, and scientific ML examples where the batch tensors are much larger than small tabular datasets.
                                  Instances For
                                    def TorchLean.Trainer.Manual.meanLossBatchLoader {σ τ : Spec.Shape} {n : } {task : SeqTask (Spec.Shape.dim n σ) (Spec.Shape.dim n τ)} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (loader : Data.BatchLoader α n σ τ) :
                                    IO α

                                    Mean loss over a typed minibatch loader through a Trainer.Manual.Runner.

                                    Runner-facing form of meanLossModuleLoader. Use it when the example is built around Trainer.Manual.run, task modes, and the proof layer trainer abstraction. Use meanLossModuleLoader directly when the example has already instantiated a runtime TorchLean.Module.ScalarModule, which is the common fast path for CUDA examples.

                                    Instances For
                                      def TorchLean.Trainer.Manual.accuracyOneHotBatchLoader {σ : Spec.Shape} {classes batch : } {task : SeqTask (Spec.Shape.dim batch σ) (Spec.Shape.dim batch (Spec.Shape.dim classes Spec.Shape.scalar))} {α : Type} [Context α] [DecidableEq Spec.Shape] (runner : Runner α task) (loader : Data.BatchLoader α batch σ (Spec.Shape.dim classes Spec.Shape.scalar)) :

                                      One-hot accuracy over a typed minibatch loader without materializing all collated batches.

                                      Instances For
                                        def TorchLean.Trainer.Manual.trainModuleLoaderWith {σ τ : Spec.Shape} {n : } {paramShapes : List Spec.Shape} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (module : Runtime.Autograd.TorchLean.ScalarModule α paramShapes [Spec.Shape.dim n σ, Spec.Shape.dim n τ]) (optimizer : Runtime.Autograd.TorchLean.Optimizer α paramShapes) (epochs : ) (loader : Data.BatchLoader α n σ τ) (callbacks : Callbacks α := Callbacks.empty) :

                                        Train a runtime scalar module from a typed minibatch loader.

                                        Shared real epoch loop for model examples that already have a runtime module, including CUDA runs. It mirrors the PyTorch structure:

                                        1. create an optimizer state for the module parameters;
                                        2. for each epoch, ask the general TorchLean.Data.batchLoader for shuffled raw batches;
                                        3. collate each raw batch into a shape-typed (xBatch, yBatch) sample;
                                        4. report the scalar loss through callbacks;
                                        5. run forward/backward/optimizer.step through TorchLean.Module.stepWith.

                                        The function is polymorphic in the input shape σ, target shape τ, batch size n, scalar type α, parameter shapes, and optimizer. It is not image-specific. CNN, ResNet, ViT, MLP, sequence, operator-learning, and future model examples should all be able to use this path whenever their supervised loss module has input shapes [dim n σ, dim n τ].

                                        Instances For
                                          def TorchLean.Trainer.Manual.trainModuleLoaderStepsWith {σ τ : Spec.Shape} {n : } {paramShapes : List Spec.Shape} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (module : Runtime.Autograd.TorchLean.ScalarModule α paramShapes [Spec.Shape.dim n σ, Spec.Shape.dim n τ]) (optimizer : Runtime.Autograd.TorchLean.Optimizer α paramShapes) (steps : ) (loader : Data.BatchLoader α n σ τ) (callbacks : Callbacks α := Callbacks.empty) :

                                          Train a runtime scalar module for exactly steps optimizer updates.

                                          trainModuleLoaderWith above is epoch-based: each unit means one full pass over the loader. This variant is update-based, which is the convention used by runnable examples that expose a --steps flag.

                                          The loop still draws shuffled minibatches from TorchLean.Data.batchLoader epoch by epoch, but it stops as soon as the requested number of optimizer updates has run. The returned loader is the next loader state, so callers can continue training from the next shuffled epoch if they want to.

                                          Instances For
                                            def TorchLean.Trainer.Manual.trainModuleStreamStepsWith {inputShapes paramShapes : List Spec.Shape} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (module : Runtime.Autograd.TorchLean.ScalarModule α paramShapes inputShapes) (optimizer : Runtime.Autograd.TorchLean.Optimizer α paramShapes) (steps : ) (stream : StepBatchStream α inputShapes) (callbacks : Callbacks α := Callbacks.empty) :

                                            Train a scalar module from a step-indexed batch stream.

                                            Shared loop for workloads whose batches are produced step by step rather than by one finite TorchLean.Data.batchLoader epoch:

                                            • RL algorithms can sample replay or rollout batches,
                                            • PDE examples can resample collocation points,
                                            • generated workloads can stream synthetic inputs without storing a dataset.

                                            The function is generic in inputShapes. It does not know whether the sample is [x, y], [state, action, target], or []; it only asks the stream for the next typed input list and then runs the same forward/backward/optimizer.step machinery as the loader-based trainer.

                                            Instances For
                                              def TorchLean.Trainer.Manual.trainModuleStreamStepsReport {inputShapes paramShapes : List Spec.Shape} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (module : Runtime.Autograd.TorchLean.ScalarModule α paramShapes inputShapes) (optimizer : Runtime.Autograd.TorchLean.Optimizer α paramShapes) (opts : Runtime.Autograd.Torch.Options) (steps : ) (stream : StepBatchStream α inputShapes) (cudaMemWatch : := 0) (extraCallbacks : Callbacks α := Callbacks.empty) :

                                              Report-oriented stream-training entrypoint.

                                              Callers pass the module, optimizer, runtime options, step count, and stream, and get standard before/after reporting plus CUDA memory watching.

                                              Instances For
                                                def TorchLean.Trainer.Manual.trainModuleStreamStepsCurveFloat {inputShapes paramShapes : List Spec.Shape} (module : Runtime.Autograd.TorchLean.ScalarModule Float paramShapes inputShapes) (optimizer : Runtime.Autograd.TorchLean.Optimizer Float paramShapes) (opts : Runtime.Autograd.Torch.Options) (steps : ) (stream : StepBatchStream Float inputShapes) (cudaMemWatch : := 0) (extraCallbacks : Callbacks Float := Callbacks.empty) :

                                                Float stream trainer that records a per-step loss curve.

                                                Generated and file-backed batches do not always have one finite loader to summarize. This entrypoint keeps their training curves in the same JSON format as the supervised examples.

                                                Instances For
                                                  def TorchLean.Trainer.Manual.trainLoaderWith {σ τ : Spec.Shape} {n : } {task : SeqTask (Spec.Shape.dim n σ) (Spec.Shape.dim n τ)} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] [Runtime.FromFloat α] (runner : Runner α task) (cfg : LoaderTrainConfig) (loader : Data.BatchLoader α n σ τ) (callbacks : Callbacks α := Callbacks.empty) :

                                                  Train from a runner-backed loader with explicit callbacks instead of inline printing in example code.

                                                  Runner-facing public path for PyTorch-style custom loops:

                                                  • keep the optimizer/scheduler logic in the library,
                                                  • inject logging, evaluation, and prediction reporting through callbacks.

                                                  This path keeps the Runner abstraction, including task modes and scheduler support. For CUDA-heavy entrypoints that already have a TorchLean.Module.ScalarModule, prefer trainModuleLoaderWith; both paths consume the same general TorchLean.Data.batchLoader.

                                                  Instances For

                                                    Small Reporting Helpers (IO) #

                                                    These definitions factor out common "print a loss/accuracy table" patterns for runnable model commands. They do not affect semantics: they only call the underlying runner functions and print human-facing summaries. Public examples should reach them through Trainer.Manual only when the ordinary Trainer.new / trainer.train API is too small for the example.

                                                    def TorchLean.Trainer.Manual.Report.reportProbes {β : Type} (title : String) (probes : List β) (lineOf : βIO String) :

                                                    Print a titled list of named report lines.

                                                    Instances For
                                                      def TorchLean.Trainer.Manual.Report.reportMeanLoss {σ τ : Spec.Shape} {task : SeqTask σ τ} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (dataset : Runtime.Autograd.Train.Dataset (Sample.Supervised α σ τ)) (label : String) :

                                                      Convenience: mean loss on a dataset, printed with a label.

                                                      Instances For
                                                        def TorchLean.Trainer.Manual.Report.reportMeanLossLoader {σ τ : Spec.Shape} {batch : } {task : SeqTask (Spec.Shape.dim batch σ) (Spec.Shape.dim batch τ)} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (loader : Data.BatchLoader α batch σ τ) (label : String) :

                                                        Convenience: mean loss on a typed minibatch loader, streamed batch by batch.

                                                        Instances For
                                                          def TorchLean.Trainer.Manual.Report.reportMeanLossModuleLoader {σ τ : Spec.Shape} {batch : } {paramShapes : List Spec.Shape} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (module : Runtime.Autograd.TorchLean.ScalarModule α paramShapes [Spec.Shape.dim batch σ, Spec.Shape.dim batch τ]) (loader : Data.BatchLoader α batch σ τ) (label : String) :

                                                          Convenience: mean loss on a typed minibatch loader for an already-instantiated runtime module.

                                                          Use this in direct CUDA/runtime examples to avoid building a Runner only for logging. The data path is still the same public loader path: TorchLean.Data.batchLoader plus TorchLean.Data.collateSupervised.

                                                          Instances For
                                                            def TorchLean.Trainer.Manual.Report.reportClassProbes {σ : Spec.Shape} {classes : } {task : SeqTask σ (Spec.Shape.dim classes Spec.Shape.scalar)} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (probes : List (String × Spec.Tensor α σ × )) (title : String := "predictions") (includeLogits : Bool := false) :

                                                            Report predicted classes on a list of named inputs.

                                                            Each entry is (name, x, expectedClass). If includeLogits := true, also prints the raw model outputs.

                                                            Instances For
                                                              def TorchLean.Trainer.Manual.Report.reportClassProbesBatchedFromSingle {σ : Spec.Shape} {classes batch : } {task : SeqTask (Spec.Shape.dim batch σ) (Spec.Shape.dim batch (Spec.Shape.dim classes Spec.Shape.scalar))} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (probes : List (String × Spec.Tensor α σ × )) (title : String := "predictions") (includeLogits : Bool := false) :

                                                              Report predicted classes on a list of named inputs, for a batched model.

                                                              This expects inputs of the unbatched input shape σ and replicates each one across the batch axis, then reports the prediction for row 0.

                                                              Instances For

                                                                Convenience: mean loss + one-hot accuracy on a dataset, printed with a label.

                                                                Instances For

                                                                  Batched variant of reportLossAccuracyOneHot.

                                                                  Instances For
                                                                    def TorchLean.Trainer.Manual.Report.reportLossAccuracyOneHotLoader {σ : Spec.Shape} {classes batch : } {task : SeqTask (Spec.Shape.dim batch σ) (Spec.Shape.dim batch (Spec.Shape.dim classes Spec.Shape.scalar))} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (runner : Runner α task) (loader : Data.BatchLoader α batch σ (Spec.Shape.dim classes Spec.Shape.scalar)) (label : String) :

                                                                    Loader variant of reportLossAccuracyOneHotBatched, streaming through minibatches.

                                                                    Instances For
                                                                      def TorchLean.Trainer.Manual.trainModuleLoaderStepsReport {σ τ : Spec.Shape} {n : } {paramShapes : List Spec.Shape} {α : Type} [Context α] [DecidableEq Spec.Shape] [ToString α] (module : Runtime.Autograd.TorchLean.ScalarModule α paramShapes [Spec.Shape.dim n σ, Spec.Shape.dim n τ]) (optimizer : Runtime.Autograd.TorchLean.Optimizer α paramShapes) (opts : Runtime.Autograd.Torch.Options) (steps : ) (loader : Data.BatchLoader α n σ τ) (cudaMemWatch : := 0) (extraCallbacks : Callbacks α := Callbacks.empty) :

                                                                      Train a runtime module for a fixed number of optimizer updates with the standard runtime reports.

                                                                      Common path for direct-module training, not example-only code. It composes the generic step loop with before/after mean-loss reporting and CUDA allocator telemetry, while still accepting extra callbacks for projects that want their own metrics, validation, or tracing.

                                                                      Instances For

                                                                        Float-specialized module training that also records a scalar loss curve.

                                                                        The training loop itself is the same as trainModuleLoaderStepsReport; this entrypoint adds the standard Curve callback used by JSON logs and website widgets.

                                                                        Instances For
                                                                          def TorchLean.Trainer.Manual.trainModuleLoaderStepsLoggedFloat {σ τ : Spec.Shape} {n : } {paramShapes : List Spec.Shape} (module : Runtime.Autograd.TorchLean.ScalarModule Float paramShapes [Spec.Shape.dim n σ, Spec.Shape.dim n τ]) (optimizer : Runtime.Autograd.TorchLean.Optimizer Float paramShapes) (opts : Runtime.Autograd.Torch.Options) (steps : ) (loader : Data.BatchLoader Float n σ τ) (log : Training.LogDestination) (title : String) (notes : Array String := #[]) (seriesName : String := "loss") (cudaMemWatch : := 0) :

                                                                          Train a Float runtime module, write a standard scalar-curve log, and return the train report.

                                                                          High-level path used by runnable training commands. The caller provides the model, optimizer, loader, runtime options, and metadata notes; the library owns the callback composition, CUDA telemetry, before/after reports, and JSON curve emission.

                                                                          Instances For