TorchLean API

NN.API.Public.Facade.Runtime.Module

TorchLean Module Runtime Facade #

Executable module operations for advanced runtime and example code.

@[reducible, inline]
abbrev TorchLean.Module.ScalarModule (α : Type) [Context α] [DecidableEq Spec.Shape] (paramShapes inputShapes : List Spec.Shape) :

Executable module instance with mutable runtime parameters and optimizer state.

Instances For
    @[reducible, inline]
    abbrev TorchLean.Module.ScalarModuleDef (paramShapes inputShapes : List Spec.Shape) :

    The module-definition type used by Module runtime operations.

    Instances For
      def TorchLean.Module.instantiate {α : Type} [Runtime.SemanticScalar α] [DecidableEq Shape] [Runtime.Scalar α] [Runtime.Autograd.Torch.Internal.CudaBridge.TensorConv α] {paramShapes inputShapes : List Shape} (opts : Options) (defn : ScalarModuleDef paramShapes inputShapes) (cast : Floatα := Runtime.ofFloat) :
      IO (ScalarModule α paramShapes inputShapes)

      Instantiate an executable runtime module from a public ScalarModuleDef.

      This is the generic public entrypoint for custom runtime tasks outside the standard supervised module constructors such as Module.instantiateMse or Module.instantiateCrossEntropyOneHot.

      Instances For

        Run one inference step through a supervised runtime module.

        This is the public sibling of the direct runtime pattern nn.eval1 opts model m.trainer.params x.

        Instances For

          Run one no-grad inference step through a supervised runtime module.

          Use this for sampling/reporting paths that should not build an autograd tape.

          Instances For

            Instantiate a supervised MSE module directly from a sequential model.

            Instances For

              Instantiate a supervised one-hot cross-entropy module directly from a sequential model.

              Instances For

                Instantiate a custom supervised runtime module directly from a sequential model.

                Use this when a public example keeps the ordinary nn.Sequential model surface but needs a custom loss/module definition instead of the standard MSE or cross-entropy module constructors.

                Instances For

                  Instantiate the standard PPO actor-critic supervised runtime module from rollout-shaped actor and critic networks.

                  Instances For

                    Build a sequential model, instantiate a one-hot cross-entropy runtime module for it, and continue with both values.

                    This packages the common public example pattern nn.withModel mkModel fun model => let m ← Module.instantiateCrossEntropyOneHot ....

                    Instances For

                      Build a sequential model, instantiate an MSE runtime module for it, and continue with both values.

                      Instances For
                        def TorchLean.Module.withModuleDefModel {σ τ : Shape} {α β : Type} [Runtime.SemanticScalar α] [DecidableEq Shape] [Runtime.Scalar α] [Runtime.Autograd.Torch.Internal.CudaBridge.TensorConv α] (mkModel : nn.M (nn.Sequential σ τ)) (opts : Options) (moduleDefOf : (model : nn.Sequential σ τ) → ScalarModuleDef (nn.paramShapes model) [σ, τ]) (cast : Floatα := Runtime.ofFloat) (k : (model : nn.Sequential σ τ) → ScalarModule α (nn.paramShapes model) [σ, τ]IO β) :
                        IO β

                        Build a sequential model, instantiate a custom supervised runtime module for it, and continue with both values.

                        This packages the common public example pattern nn.withModel mkModel fun model => let m ← Module.instantiate ... (moduleDefOf model).

                        Instances For
                          def TorchLean.Module.withScalarLossModel {σ τ : Shape} {α β : Type} [Runtime.SemanticScalar α] [DecidableEq Shape] [Runtime.Scalar α] [Runtime.Autograd.Torch.Internal.CudaBridge.TensorConv α] (mkModel : nn.M (nn.Sequential σ τ)) (opts : Options) (loss : {α : Type} → [inst : Runtime.TensorScalar α] → [inst_1 : DecidableEq Shape] → Runtime.Autograd.TorchLean.Program α [τ, τ] Shape.scalar) (cast : Floatα := Runtime.ofFloat) (k : (model : nn.Sequential σ τ) → ScalarModule α (nn.paramShapes model) [σ, τ]IO β) :
                          IO β

                          Build a sequential model, instantiate a runtime module for a custom scalar loss program, and continue with both values.

                          This is the custom-loss sibling of withMseModel / withCrossEntropyOneHotModel. Use it when the model is ordinary nn.Sequential, but the loss needs task-specific logic beyond the standard MSE or cross-entropy module constructors.

                          Instances For

                            Evaluate one supervised sample through a runtime module and return the scalar loss value.

                            This packages the common public example pattern Module.forward ...; Tensor.toScalar.

                            Instances For
                              def TorchLean.Module.adamHandle {α : Type} [Runtime.TensorScalar α] [DecidableEq Shape] {paramShapes inputShapes : List Shape} (m : ScalarModule α paramShapes inputShapes) (lr beta1 beta2 epsilon : α) :
                              have opt := Runtime.Autograd.TorchLean.adam lr beta1 beta2 epsilon; IO (NN.API.TorchLean.Optim.Handle α paramShapes inputShapes opt.State)

                              Create an Adam optimizer handle bound to a concrete runtime module.

                              This packages the common public example pattern optim.runtimeAdam ...; optim.handle m opt.

                              Instances For
                                def TorchLean.Module.adamWHandle {α : Type} [Runtime.TensorScalar α] [DecidableEq Shape] {paramShapes inputShapes : List Shape} (m : ScalarModule α paramShapes inputShapes) (lr weightDecay beta1 beta2 epsilon : α) :
                                have opt := Runtime.Autograd.TorchLean.adamw lr weightDecay beta1 beta2 epsilon; IO (NN.API.TorchLean.Optim.Handle α paramShapes inputShapes opt.State)

                                Create an AdamW optimizer handle bound to a concrete runtime module.

                                Instances For
                                  def TorchLean.Module.sgdHandle {α : Type} [Runtime.TensorScalar α] [DecidableEq Shape] {paramShapes inputShapes : List Shape} (m : ScalarModule α paramShapes inputShapes) (lr : α) :
                                  have opt := Runtime.Autograd.TorchLean.sgd lr; IO (NN.API.TorchLean.Optim.Handle α paramShapes inputShapes opt.State)

                                  Create an SGD optimizer handle bound to a concrete runtime module.

                                  Instances For
                                    def TorchLean.Module.optimizerInputs {α : Type} [Runtime.TensorScalar α] [Runtime.Scalar α] [DecidableEq Shape] {paramShapes inputShapes : List Shape} (m : ScalarModule α paramShapes inputShapes) (cfg : NN.API.TorchLean.Trainer.Optimizer) :
                                    IO (TensorPack α inputShapesIO Unit)

                                    Create a one-step update function for any typed module input pack from the public optimizer config used by the trainer API.

                                    This is the generic bridge for custom training loops: richer examples can keep their own control flow while still choosing SGD/Adam/AdamW through the same optim.* config surface as Trainer.RunConfig.

                                    Instances For
                                      def TorchLean.Module.optimizerStep {α : Type} {σ τ : Shape} [Runtime.TensorScalar α] [Runtime.Scalar α] [DecidableEq Shape] {paramShapes : List Shape} (m : ScalarModule α paramShapes [σ, τ]) (cfg : NN.API.TorchLean.Trainer.Optimizer) :
                                      IO (SupervisedSample α σ τIO Unit)

                                      Create a sample-step function from the public optimizer config used by the trainer API.

                                      This is the bridge for custom training loops: richer examples can keep their own control flow while still choosing SGD/Adam/AdamW through the same optim.* config surface as Trainer.RunConfig.

                                      Instances For