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.
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
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
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 ...(seeNN.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
trueandbackend = .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
Floattensors.
Instances For
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 fromrest),--fast-kernels(boolean flag; removed fromrest).--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-modeand--cudais present, default todtype=float(CUDA eager supportsFloatupload/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
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 literalsopts : Optionsselecting the backend/kernel moderest : List Stringcontaining 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
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
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:
- parsing
--seed, - selecting an executable dtype/backend/device from flags,
- seeding TorchLean's global RNG stream (
API.rand) sonn.freshSeed/nn.withModelare deterministic.
Options for TorchLean.Module.run (banner printing, trailing ok, etc.).
- 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.
- any
(k :
{α : Type} →
[Semantics.Scalar α] →
[DecidableEq Spec.Shape] → [ToString α] → [Runtime.Scalar α] → (Float → α) → Options → List String → IO Unit)
: RunAction
Allow
--dtypeselection (the continuation must work for all executable scalar backends). - float
(k : Options → List String → IO Unit)
: RunAction
Force the scalar backend to builtin
Float(useful for Float-only IO bridges / CUDA upload paths).
Instances For
Generic help text for executables built on TorchLean.Module.run.
Instances For
CLI entrypoint helper for executable main functions.
This parses:
--seed N(viaAPI.CLI.takeSeed), and- runtime execution flags (
--dtype,--float32-mode,--backend,--cpu|--cuda,--fast-kernels,--fast-gpu-matmul-precision), then executes the chosenRunAction.
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.