TorchLean API

NN.API.Runtime.Module

Runtime Modules and Executable Entrypoints #

Scalar-module instantiation, runtime initialization, CLI execution config parsing, and executable main helpers.

Model constructors (re-export) #

This namespace re-exports ready-made model constructors (MLP/CNN/ResNet18/etc.) for runnable examples and integration checks.

For compositional building blocks, prefer API.TorchLean.NN and API.TorchLean.Layers.

ScalarModule API (Session-Like Interface) #

The ScalarModule interface is the TorchLean equivalent of "instantiate a model, then do forward, backward, and optimizer steps" in an imperative runtime.

This section mostly re-exports Runtime.Autograd.TorchLean.Module.* and adds small CLI-friendly helpers (Module.withModule / Module.withModuleRuntime) that select dtype/backend from flags.

def NN.API.TorchLean.Module.instantiateWithOptions {α : Type} [Semantics.Scalar α] [DecidableEq Spec.Shape] [Runtime.Autograd.Torch.Internal.CudaBridge.TensorConv α] {paramShapes inputShapes : List Spec.Shape} (defn : ScalarModuleDef paramShapes inputShapes) (cast : Floatα) (opts : Options) :
IO (ScalarModule α paramShapes inputShapes)

Instantiate a ScalarModuleDef under explicit Torch options (backend, fastKernels, useGpu, etc.).

This is the most direct "runtime" entrypoint (used by the CPU/CUDA example binaries), since it threads the same options record all the way down to the eager tape / CUDA tape selection.

The shorter instantiate entrypoint selects the backend and then uses default runtime options.

Instances For
    def NN.API.TorchLean.Module.instantiateFloatWithRuntimePlanOptions {paramShapes inputShapes : List Spec.Shape} (defn : ScalarModuleDef paramShapes inputShapes) (opts : Options) (plan : RuntimeInit.Plan paramShapes) :
    IO (ScalarModule Float paramShapes inputShapes)

    Instantiate a Float module with runtime-side parameter initializers.

    This is the public runtime-initialization entrypoint. The initializer plan is indexed by the same paramShapes list as the module, so Lean checks that every parameter has exactly one initializer. In CUDA mode, supported initializers allocate device buffers directly instead of first constructing every parameter as a large nested Lean tensor.

    Instances For
      def NN.API.TorchLean.Module.instantiateFloatWithRuntimeInitOptions {paramShapes inputShapes : List Spec.Shape} (defn : ScalarModuleDef paramShapes inputShapes) (opts : Options) (inits : List RuntimeInit.FloatInit) :
      IO (ScalarModule Float paramShapes inputShapes)

      List-based wrapper for checkpoint/JSON boundaries.

      If the caller has a statically known parameter list, prefer instantiateFloatWithRuntimePlanOptions; this wrapper checks the list length before applying it.

      Instances For

        Execution configuration parsed from CLI flags.

        Supported flags (parsed by ExecConfig.parseAndStrip):

        • --dtype ... / --float32-mode ... (see NN.API.DType)
        • --backend eager|compiled
        • --cpu / --cuda (eager device selection)
        • --fast-kernels (eager-only performance hooks, no effect on compiled backend)
        • --fast-gpu-matmul-precision fp32|fp64 (fast-kernel CUDA matmul precision)
        • dtype : DType

          Scalar dtype selection.

        • backend : Backend

          Execution backend selection.

        • useGpu : Bool

          Eager execution device selector.

          When true and backend = .eager, TorchLean uses the CUDA tape backend.

        • fastKernels : Bool

          Enable runtime-only eager fast kernels (tight-loop implementations for a few hot ops).

        • fastGpuMatmulPrecision : GpuMatmulPrecision

          GPU precision for fast-kernel matmul over Lean Float tensors.

        Instances For

          Parse a backend selector string into a runtime Backend.

          Instances For

            Parse a fast-kernel CUDA matmul precision selector.

            Instances For

              Parse CLI flags handled by ExecConfig and return (cfg, rest).

              Consumed flags:

              • --backend eager|compiled (at most once),
              • --cpu / --cuda (boolean flags; last one wins; removed from rest),
              • --fast-kernels (boolean flag; removed from rest).
              • --fast-gpu-matmul-precision fp32|fp64 (at most once).

              All dtype/Float32 selection flags are delegated to DType.parseAndStripWithDefault.

              Default dtype policy:

              • If the user does not specify --dtype / --float32-mode and --cuda is present, default to dtype=float (CUDA eager supports Float upload/download).
              • Otherwise default to dtype=float32 (executable IEEE-754 float32 semantics).
              Instances For

                Convert a parsed CLI execution config to runtime Options.

                Instances For

                  Parse CLI flags with the standard TorchLean default dtype policy.

                  Instances For

                    Log the chosen execution config to stdout for reproducible runs.

                    Instances For
                      def NN.API.TorchLean.Module.withRuntime (args : List String) (k : {α : Type} → [Semantics.Scalar α] → [DecidableEq Spec.Shape] → [ToString α] → [Runtime.Scalar α] → (Floatα)OptionsList StringIO Unit) :

                      Parse runtime flags (--dtype, --backend, --cpu|--cuda, --fast-kernels, --fast-gpu-matmul-precision) and choose an executable scalar α, then call k with:

                      • cast : Float → α for building inputs from literals
                      • opts : Options selecting the backend/kernel mode
                      • rest : List String containing the remaining CLI arguments

                      This is useful for scripts that need to build a dataset/loader (and maybe determine shapes/batch sizes) before instantiating a concrete ScalarModuleDef.

                      Instances For
                        def NN.API.TorchLean.Module.withModule {paramShapes inputShapes : List Spec.Shape} (defn : ScalarModuleDef paramShapes inputShapes) (args : List String) (k : {α : Type} → [inst : Semantics.Scalar α] → [inst_1 : DecidableEq Spec.Shape] → [ToString α] → (Floatα)ScalarModule α paramShapes inputShapesList StringIO Unit) :

                        Instantiate a ScalarModuleDef under CLI runtime flags (--dtype, --backend, --cpu|--cuda, --fast-kernels, --fast-gpu-matmul-precision), then call a continuation.

                        This provides the cast function Float → α so call sites can build inputs from float literals.

                        Instances For
                          def NN.API.TorchLean.Module.withModuleRuntime {paramShapes inputShapes : List Spec.Shape} (defn : ScalarModuleDef paramShapes inputShapes) (args : List String) (k : {α : Type} → [inst : Semantics.Scalar α] → [inst_1 : DecidableEq Spec.Shape] → [ToString α] → [Runtime.Scalar α] → ScalarModule α paramShapes inputShapesList StringIO Unit) :

                          Like withModule, but also provides an API.Runtime.Scalar α instance (for numeric literals).

                          Instances For

                            Executable main Helpers #

                            TorchLean has a lot of pure, type-indexed code (models live in Type 2), but runnable scripts still want a "single entrypoint" that handles:

                            Options for TorchLean.Module.run (banner printing, trailing ok, etc.).

                            • banner? : Option (OptionsString)

                              Optional banner to print before executing the program.

                            • flush : Bool

                              Flush stdout after printing the banner (if present).

                            • printOk : Bool

                              Print "{exeName}: ok" on success.

                            Instances For

                              Print the configured executable banner, if one was supplied.

                              Instances For

                                How run should select the scalar backend for an executable.

                                Instances For

                                  Generic help text for executables built on TorchLean.Module.run.

                                  Instances For
                                    def NN.API.TorchLean.Module.run (exeName : String) (args : List String) (action : RunAction) (runOpts : RunOptions := { }) :

                                    CLI entrypoint helper for executable main functions.

                                    This parses:

                                    • --seed N (via API.CLI.takeSeed), and
                                    • runtime execution flags (--dtype, --float32-mode, --backend, --cpu|--cuda, --fast-kernels, --fast-gpu-matmul-precision), then executes the chosen RunAction.

                                    It also seeds TorchLean's global RNG stream (API.rand) so code that draws init seeds via API.nn.freshSeed/API.nn.withModel is deterministic by default, matching the PyTorch pattern of calling torch.manual_seed once in main.

                                    Instances For