TorchLean API

NN.Runtime.Autograd.Torch.Core.Trainer

Torch Trainer Helpers #

Ops instances and scalar trainer construction for the Torch-style runtime. Shape-indexed mutable parameter storage lives in Trainer.Parameters.

@[reducible, inline]

Monad used for the eager Ops instance: read an Internal.EagerSession α and execute in IO.

This is the backend that makes Ops programs execute immediately by mutating a hidden runtime tape.

Instances For
    @[implicit_reducible]

    Ops instance for the eager Torch-style runtime.

    This interprets Ops primitives by immediately executing them against the hidden mutable tape in the current Internal.EagerSession.

    @[implicit_reducible]

    Ops instance for the compiled graph-building monad GraphM.

    This interprets Ops primitives by recording typed IR nodes (rather than executing immediately). See Runtime.Autograd.Compiled.GraphM and Torch.LinkedSession for how these graphs are later run.

    Persistence hooks for optimizer state owned by a backend-specific trainer.

    The payload is intentionally opaque to callers. CUDA eager training, for example, owns Adam's moment buffers on the device and streams them without first constructing host tensors. A trainer that has no hidden optimizer state leaves this hook absent.

    Instances For
      structure Runtime.Autograd.Torch.ScalarTrainer (α : Type) (paramShapes inputShapes : List Spec.Shape) (natInputShapes : List Spec.Shape := []) :

      Bundle a scalar-loss training loop for a fixed parameter pack and input signature.

      This is the low-level trainer object used by module-backed execution:

      • forward computes a scalar loss,
      • lossAndBackward computes that loss and its parameter gradients from one tape,
      • backward exposes just the gradients when the loss is not needed,
      • stepWithLoss applies an SGD update and returns the loss from the same tape,
      • step applies the update without requiring callers to read the loss,
      • getParams reads current parameter values.
      Instances For

        Extract gradients (as a typed TList) for a list of eager TensorRefs from a dense gradient array.

        Instances For

          Record all parameters as tape leaves in an eager session, returning their corresponding TensorRefs.

          This is the eager analogue of "using" a parameter pack during a forward pass.

          Instances For

            Record all input tensors as tape leaves in an eager session, returning their corresponding TensorRefs.

            Instances For
              def Runtime.Autograd.Torch.scalarTrainer {α : Type} [Context α] [Internal.CudaBridge.TensorConv α] [DecidableEq Spec.Shape] {paramShapes inputShapes natInputShapes : List Spec.Shape} (opts : Options := { }) (initRequiresGrad : List Bool := List.replicate paramShapes.length true) (loss : {m : TypeType} → [Monad m] → [inst : Ops m α] → CurriedRef (fun (s : Spec.Shape) => Ops.Ref m α s) (paramShapes ++ inputShapes) (CurriedRef (fun (s : Spec.Shape) => Ops.NatTensorRef m α s) natInputShapes (m (Ops.Ref m α Spec.Shape.scalar)))) :
              Curried.Fn α paramShapes (IO (ScalarTrainer α paramShapes inputShapes natInputShapes))

              Build a ScalarTrainer from an initial parameter pack and a backend-generic loss definition.

              loss is written once against the Ops interface over a concatenated context paramShapes ++ inputShapes. Depending on opts.backend, we either:

              • compile the loss once (compiled backend), or
              • execute it eagerly by building a runtime tape each step (eager backend).
              Instances For