TorchLean Runtime Facade #
This module defines the NN.API.TorchLean namespace: the main re-export layer over the executable
TorchLean runtime:
- tensor primitives (
add,matmul,reshape, ...) - derived functional ops (
F.*) - losses / norms
- autograd entry points (grad/vjp/jacobian, etc.)
- neural-network layer combinators (
NN.Seq,NN.LayerDef, ...)
The goal is to expose a stable, well-namespaced "public surface" without forcing users to import internal runtime modules directly.
How This Relates To NN.API.Public #
NN.API.Public is the higher-level, PyTorch-like facade intended for most examples and tutorial
code. It builds on this runtime facade and adds more ergonomic constructors (record-based configs,
fewer proof arguments, etc.).
PyTorch Mapping #
API.TorchLean.NN.*corresponds to pieces oftorch.nnandtorch.nn.functional(https://pytorch.org/docs/stable/nn.html,https://pytorch.org/docs/stable/nn.functional.html)API.TorchLean.Optim.*/API.TorchLean.Trainer.*correspond totorch.optim(https://pytorch.org/docs/stable/optim.html)
Most layer helpers keep the PyTorch name but append a shape/layout suffix such as CHW or NCHW
so the expected tensor layout stays visible at the call site.
TorchLean differs in two crucial ways:
- Shapes are part of types, so "wrong shape" often becomes a type error.
- Some backends are proof-only; use
NN.API.DTypeselection when writing executables.
Recommended Import #
This is the canonical module for the runtime facade: import NN.API.Runtime.
For most user-facing code, prefer NN.API.Public instead. It keeps the same runtime machinery
behind a smaller, more PyTorch-shaped surface, while this module stays closer to the executable
TorchLean internals and lower-level compatibility re-exports.
Core Exports #
Most of this namespace is a curated re-export of _root_.Runtime.Autograd.TorchLean.*, so users can
import NN.API.Runtime and get a stable API surface without importing internal runtime modules.
Rough grouping:
- execution control:
Backend,Options - program interface:
Ops,RefTy,Program,CompiledOut,CompiledScalar, ... - primitive tensor ops:
add,matmul,reshape, elementwise activations, pooling, ... - training utilities:
trainCycle*,meanLoss
Unpack a 2-element RefList into a pair.
Instances For
Optimizer Handles (PyTorch-Like) #
TorchLean optimizers are purely functional in their state: opt.step returns a new state.
This small wrapper stores the optimizer state in an IO.Ref so users can write:
let h ← API.TorchLean.Optim.handle m (TorchLean.Optim.sgd lr)
h.step sample
without manually threading the optimizer state through the training loop.
A mutable optimizer handle bound to a concrete TorchLean ScalarModule.
The internal optimizer state is stored in an IO.Ref and updated when you call h.step sample.
- module : Runtime.Autograd.TorchLean.ScalarModule α paramShapes inputShapes
The module whose parameters will be updated in-place.
- state : IO.Ref State
Mutable optimizer state.
One training step on a single sample, updating the internal optimizer state.
Instances For
Create an optimizer handle for a module by initializing optimizer state from the module's current parameters.
Instances For
Adapter typeclass used by the seq! macro to treat both layers and already-sequential models as
composable building blocks.
This exists purely for ergonomics: it lets examples mix LayerDef and Seq in the same seq!
expression without relying on Lean's coercion insertion heuristics.
- asSeq {σ τ : Spec.Shape} : F σ τ → Seq σ τ
Convert a layer-like thing into a
Seqsoseq!can compose it.
Instances
A single LayerDef can always be viewed as a 1-layer sequential model (seq1).
A sequential model is already a sequential model (identity).
Compose either layers or sequential models without relying on coercions.
This is the helper used by the seq! ... macro so examples can write
seq! layer1, model2, layer3 while still mirroring PyTorch's "stack layers" style.
Instances For
Deterministic RNG helpers re-exported for the runtime facade.
These are small utilities used by demos and training loops (keyOf, nextSeed, uniform, mask).
Sequential Layer Helpers #
Runtime.Autograd.TorchLean.NN exposes layers (LayerDef σ τ) and sequential models
(Seq σ τ). For demos we often want to "just stack layers", so this namespace provides small
helpers that return Seq directly (and compute a few common derived shapes like flattenLinear).
For the more fully-documented public surface (named-field configs, blocks, etc.), see
NN.API.Public under API.nn.
Lift a single layer into a sequential model.
Instances For
Linear layer over vectors (returns a 1-layer Seq).
Instances For
Elementwise SiLU/Swish.
Instances For
Elementwise sigmoid.
Instances For
Elementwise square.
Instances For
Reduce-sum to a scalar.
Instances For
Flatten any input shape into a 1D vector of length Spec.Shape.size s.
Instances For
Dropout layer that is active in training mode and identity in eval mode.
Instances For
Flatten -> Linear head, with the input dimension computed from the input shape.
Instances For
Sequential 2D convolution layer for CHW inputs.
Instances For
Sequential max-pooling layer for CHW inputs.
Instances For
Sequential padded max-pooling layer for CHW inputs.
Instances For
Sequential average-pooling layer for CHW inputs.
Instances For
Sequential padded average-pooling layer for CHW inputs.
Instances For
Global average-pooling over C×H×W inputs.
PyTorch analogy: torch.nn.functional.adaptive_avg_pool2d(x, output_size=1) followed by
flattening the spatial axes.
Instances For
Global average-pooling over N×C×H×W inputs.
PyTorch analogy: torch.nn.functional.adaptive_avg_pool2d(x, output_size=1) and then reshaping
to (N, C).
Instances For
Sequence-wise layer normalization.
PyTorch analogy: torch.nn.LayerNorm(embedDim) applied to each position in a sequence.
Instances For
Sequence-wise RMS normalization.
PyTorch analogy: an RMSNorm-style layer over (seqLen × embedDim) tensors.
Instances For
Mode-aware batch norm on a single C×H×W image tensor.
PyTorch analogy: torch.nn.BatchNorm2d(channels) on a single sample, with the layer's mode
controlling whether running statistics are updated or reused.
Instances For
Eval-mode batch norm on a single C×H×W image tensor with explicit running statistics.
PyTorch analogy: torch.nn.BatchNorm2d(...).eval() with running_mean and running_var.
Instances For
Instance normalization over N×C×H×W tensors.
PyTorch analogy: torch.nn.InstanceNorm2d(c, affine=True) with NCHW layout.
Instances For
Group normalization over N×C×H×W tensors.
PyTorch analogy: torch.nn.GroupNorm(groups, c) with NCHW layout.
Instances For
Batch norm over N×C×H×W tensors in training mode.
PyTorch analogy: torch.nn.BatchNorm2d(c) during training, where batch statistics are used.
Instances For
Multi-head self-attention over sequence embeddings.
PyTorch analogy: torch.nn.MultiheadAttention(embed_dim=dModel, num_heads=numHeads) in self-
attention mode, with explicit n × dModel shapes.
Instances For
Parameter list type for a given model (a TList over Seq.paramShapes).
Instances For
Loss function over a model output and a target.
This is expressed in terms of RefTy so it works uniformly for eager execution and compiled
execution.
Instances For
Initialize model parameters by casting the model's Float initializers elementwise using cast.
Instances For
Initialize model parameters using the runtime literal injection API.Runtime.ofFloat.
Instances For
Pack explicit weight and bias tensors for a single Layers.linear model.
Instances For
Mean-squared error loss (mse) between yhat and y.
Instances For
Cross-entropy loss between logits and one-hot targets. PyTorch analogue: nn.CrossEntropyLoss.
Instances For
Detach the model output before feeding it into a loss.
This is useful when you want to compute a metric loss without backpropagating through it.
Instances For
Build a TorchLean Program that computes a scalar loss from (params, x, target).
This is the bridge between Seq.program (which produces model outputs) and the autograd entry
points (which expect a scalar-valued program).
Instances For
VJP of the model output w.r.t. parameters.
Instances For
VJP of the model output w.r.t. inputs.
Instances For
Jacobian (reverse-mode) of the model output w.r.t. parameters, returned as rows.
Instances For
Gradient of loss(model(params, x), target) w.r.t. parameters.
Instances For
Gradient of loss(model(params, x), target) w.r.t. inputs (x and target).
Instances For
JVP of a scalar loss w.r.t. parameters in direction vparams.
Instances For
HVP (Hessian-vector product) of a scalar loss w.r.t. parameters in direction vparams.
Instances For
Type of a pure tensor function expressed in RefTy form.
This matches the calling convention expected by TorchLean.Program/autodiff compilation.
Instances For
Turn an Fn into a single-input TorchLean Program.
Instances For
Forward-mode Jacobian (rows) of a pure function.
Instances For
Hessian for a scalar-valued pure function.
Instances For
Model constructors (re-export) #
This namespace re-exports a small set of ready-made model constructors (MLP/CNN/ResNet18/etc.), primarily for runnable demos and smoke tests.
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.
Note: instantiate (without options) is a convenience wrapper that only selects the backend and
uses default runtime options.
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
Parse CLI flags with the standard TorchLean default dtype policy.
Instances For
Log the chosen execution config to stdout (for reproducible demos).
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
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
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.
Instances For
Built-in loss choices for SeqTask.
- mse (reduction : Loss.Reduction := Runtime.Autograd.TorchLean.Loss.Reduction.mean) : SeqLoss
- crossEntropyOneHot (reduction : Loss.Reduction := Runtime.Autograd.TorchLean.Loss.Reduction.mean) : SeqLoss
Instances For
A supervised task is just a model plus a choice of loss.
Instances For
Build a ScalarModuleDef for a task, choosing an explicit model mode (train/eval).
This is the underlying "instantiate me as a runnable module" step for training.
Instances For
Default module definition for a task (training mode).
Instances For
Constructor: regression task (MSE loss).
Instances For
Constructor: one-hot classification task (cross-entropy loss).
Instances For
Parameter shapes for a task (delegates to Seq.paramShapes).
Instances For
Optimizer hyperparameter configuration for the supervised training helpers.
We keep this small for examples and lightweight trainers. It mirrors a few common PyTorch
optimizers by name/defaults, but it does not try to cover the full option surface of
torch.optim.*.
- sgd
(lr : Float)
(momentum : Float := 0.0)
: OptimizerConfig
SGD optimizer config.
PyTorch analogy:
torch.optim.SGD(..., lr=..., momentum=...)whenmomentum > 0, and plain SGD whenmomentum = 0. - adam
(lr : Float)
(beta1 : Float := 0.9)
(beta2 : Float := 0.999)
(epsilon : Float := 1e-8)
: OptimizerConfig
Adam optimizer config.
- adamw
(lr : Float)
(weightDecay : Float := 1e-2)
(beta1 : Float := 0.9)
(beta2 : Float := 0.999)
(epsilon : Float := 1e-8)
: OptimizerConfig
AdamW optimizer config (decoupled weight decay).
Instances For
Instances For
Step-based training configuration for fit / fitDataset.
Fields:
steps: number of parameter updates,optimizer: optimizer hyperparameters,scheduler: optional learning-rate schedule (applied per step),logEvery: progress printing frequency (0disables logging).
- steps : ℕ
Number of training steps.
- optimizer : OptimizerConfig
Optimizer configuration.
- scheduler : Option Schedulers.Config
Scheduler configuration.
- logEvery : ℕ
Log once every this many steps.
Instances For
Instances For
Epoch-based training configuration for fitLoader (data-loader training).
Fields:
epochs: number of epochs (each epoch iterates once over the loader),optimizer: optimizer hyperparameters,scheduler: optional learning-rate schedule (applied per step/epoch depending on helper),logEvery: progress printing frequency (0disables logging).
- epochs : ℕ
Number of epochs to train for.
- optimizer : OptimizerConfig
Optimizer configuration.
- scheduler : Option Schedulers.Config
Scheduler configuration.
- logEvery : ℕ
Log once every this many steps.
Instances For
Instances For
Extract the base learning rate encoded in an optimizer configuration.
Instances For
Resolve the learning rate to use at a given training step.
If a scheduler is present, it takes precedence over the optimizer's baked-in base learning rate.
Otherwise this simply returns optimizerLR cfg.
Instances For
Map a state update over every optimizer-state entry in a shape-indexed parameter list.
Instances For
Set the learning rate field of every Adam optimizer state entry to lr.
Instances For
Set the learning rate field of every momentum-SGD optimizer state entry to lr.
Instances For
Set the learning rate field of every AdamW optimizer state entry to lr.
Instances For
A fully instantiated supervised task runner.
This bundles:
- the imperative
ScalarModule(parameters/buffers stored in refs), - compiled predictors and loss functions for both
.trainand.evalmodes (so switching mode is cheap), - and the current mode stored in an
IO.Ref.
The mode influences both operator behavior (e.g. dropout/batchnorm) and whether buffers are updated during training.
- module : Runtime.Autograd.TorchLean.ScalarModule α (paramShapes task) [σ, τ]
Instantiated scalar module storing parameters/buffers in mutable refs.
- predictorTrain : CompiledOut α (paramShapes task ++ [σ]) τ
Compiled forward predictor specialized to training-mode behavior.
- predictorEval : CompiledOut α (paramShapes task ++ [σ]) τ
Compiled forward predictor specialized to eval-mode behavior.
- lossTrain : CompiledScalar α (paramShapes task ++ [σ, τ])
Compiled loss function for training-mode behavior.
- lossEval : CompiledScalar α (paramShapes task ++ [σ, τ])
Compiled loss function for eval-mode behavior.
Mutable mode flag (
.train/.eval) used by stateful layers (e.g. dropout/batchnorm).
Instances For
Instantiate a Runner by explicitly providing a Float → α cast and backend.
Use this when you want to run the same task over different numeric backends (e.g. Float vs
IEEE32Exec) or when you want custom literal injection.
Instances For
Instantiate a Runner by explicitly providing a Float → α cast and a backend selector.
This is a backward-compatible wrapper over instantiateWithOptions.
Instances For
Instantiate a Runner using the standard runtime literal injection API.Runtime.ofFloat.
This is the common entrypoint for executable examples.
Instances For
Instantiate a Runner using the standard runtime literal injection API.Runtime.ofFloat and a
backend selector.
This is a backward-compatible wrapper over instantiateWithRuntimeOptions.
Instances For
Run a TorchLean task with CLI-style dtype/backend selection, then call k with a fully constructed
runner.
This is used by lake exe entrypoints: run takes care of parsing dtype flags and instantiating
the underlying module/compiled programs.
Instances For
Read the current parameter list from a runner.
Instances For
Read the runner's current mode (.train or .eval).
Instances For
Set the runner mode (.train or .eval).
Instances For
Convenience: setMode runner .train.
Instances For
Convenience: setMode runner .eval.
Instances For
Predicate: are we in training mode?
Instances For
Pick the predictor compiled for the runner's current mode (.train or .eval).
Instances For
Pick the loss program compiled for the runner's current mode (.train or .eval).
Instances For
Refresh mode-dependent runner buffers using one supervised sample.
This mutates the module parameters only in .train mode, mirroring PyTorch-style buffer updates
for layers such as normalization. In .eval mode it is a no-op.
Instances For
Run one forward/backward pass on a single supervised sample and return gradients for all parameters.
This is the TorchLean analogue of the loss.backward() payload in PyTorch, except TorchLean returns
the gradients explicitly instead of storing them in .grad fields.
Instances For
Predict on one input tensor using the runner's active mode (.train or .eval).
Instances For
Predict on a list of inputs by repeatedly calling predict.
Instances For
For classification heads: run predict, then take argmax over the logits (if defined).
Instances For
Compute (correct, total) for a one-hot classification dataset.
Instances For
Instances For
Mean scalar loss over a list of supervised samples (uses the runner's active mode).
Instances For
Mean scalar loss over a dataset (materialized via dataset.toList).
Instances For
Scalar loss for one sample through the instantiated runtime module.
Instances For
Fit on a small in-memory list of supervised samples for a fixed number of steps.
This is the simplest training-loop helper: it is intended for examples and small synthetic datasets.
For loader-based training, see fitLoader.
Instances For
fit over a dataset (materialized as a list).
Instances For
Fit over a DataLoader for cfg.epochs epochs, returning the final report and the updated loader.
This corresponds to the common PyTorch pattern:
for epoch in ...: for batch in loader: step(batch).
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Instances For
Stateful training loop object: a Runner plus an optimizer state and a step counter.
This is the TorchLean analogue of holding a PyTorch optimizer object plus the model, ready to
step() on batches.
- runner : Runner α task
Underlying task runner (module + compiled predictors/losses).
Run a single optimization step on one supervised sample, returning the loss value.
Run an epoch over an explicit list of samples, returning the per-step loss values.
Read the total number of
stepSamplecalls performed so far.
Instances For
Construct a Stepper for a runner, optimizer config, and optional scheduler.
This is the recommended way to build custom training loops without reimplementing the optimizer
logic: call stepper, then choose stepSample for single batches or epochSamples for explicit
sample lists.
Instances For
Run one optimization step on a single supervised sample.
Instances For
Run one epoch over a list of supervised samples, returning the per-step losses.
Instances For
NN.API.TorchLean.Trainer is a stable facade over the internal Supervised training machinery.
Usage note:
If you're writing tutorials or user code, prefer the PyTorch-shaped entrypoint NN.API.train
(available after import NN). This API.TorchLean.Trainer namespace is the underlying runtime
layer that API.train delegates to, and it exposes more knobs than most users need.
The intended workflow is:
- pick a
Task(regression / classification), - call
instantiateto get aRunner(parameters + buffers + backend state), - call
fit/fitDataset/fitLoader, or build aStepperfor custom loops.
This API is backend-agnostic: the same code can run in .eager mode or via a compiled backend,
depending on the backend argument passed to instantiate.
A supervised task is just a model plus a choice of loss.
Instances For
A fully instantiated supervised task runner.
This bundles:
- the imperative
ScalarModule(parameters/buffers stored in refs), - compiled predictors and loss functions for both
.trainand.evalmodes (so switching mode is cheap), - and the current mode stored in an
IO.Ref.
The mode influences both operator behavior (e.g. dropout/batchnorm) and whether buffers are updated during training.
Instances For
Optimizer hyperparameter configuration for the supervised training helpers.
We keep this small for examples and lightweight trainers. It mirrors a few common PyTorch
optimizers by name/defaults, but it does not try to cover the full option surface of
torch.optim.*.
Instances For
Epoch-based training configuration for fitLoader (data-loader training).
Fields:
epochs: number of epochs (each epoch iterates once over the loader),optimizer: optimizer hyperparameters,scheduler: optional learning-rate schedule (applied per step/epoch depending on helper),logEvery: progress printing frequency (0disables logging).
Instances For
Stateful training loop object: a Runner plus an optimizer state and a step counter.
This is the TorchLean analogue of holding a PyTorch optimizer object plus the model, ready to
step() on batches.
Instances For
Regression task with mean-squared error loss by default.
Instances For
One-hot classification task with cross-entropy loss by default.
Instances For
SGD optimizer config.
PyTorch analogue: torch.optim.SGD
(https://pytorch.org/docs/stable/generated/torch.optim.SGD.html).
Instances For
AdamW optimizer config with standard defaults (PyTorch-style weightDecay = 0.01).
PyTorch analogue: torch.optim.AdamW
(https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html).
Instances For
Epoch-based training config over a data loader.
Instances For
Attach a scheduler to a step-based training config.
Instances For
Attach a scheduler to an epoch-based loader training config.
Instances For
Step-based constant learning-rate schedule.
Instances For
Step-based exponential-decay schedule.
Instances For
Epoch-based constant learning-rate schedule.
Instances For
Epoch-based step-decay schedule.
Instances For
Epoch-based exponential-decay schedule.
Instances For
Instantiate a runner under explicit Torch options (backend, useGpu, fastKernels, ...).
This is the recommended entrypoint when you want CUDA eager execution from the training helpers
without dropping down to TorchLean.Module directly.
Instances For
Instantiate a runner (parameters + buffers + backend state) for the given task.
This allocates and initializes model parameters (via Seq.initParams) and sets up the chosen
execution backend (.eager vs .compiled).
Instances For
CLI-oriented runner entry point.
This parses dtype/backend flags (via NN.API.DType / Module.ExecConfig) and then calls the
continuation k under the selected scalar backend.
Instances For
Get the current model parameters from a runner.
Instances For
Read the current mode (train vs eval).
Instances For
Set the mode (train vs eval).
Instances For
Switch to training mode.
Instances For
Switch to eval mode.
Instances For
Check whether the runner is in training mode.
Instances For
Run forward+backward on one supervised sample and return gradients for all parameters.
Instances For
Predict on a single input tensor.
This runs the forward pass under the runner's current mode.
Instances For
Predict on a list of inputs (runs the forward pass repeatedly).
Instances For
Predict the argmax class for a classification task, if argmax is well-defined for α.
This is a convenience wrapper over predict + Metrics.argmax?.
Instances For
Count correct predictions in a one-hot labeled sample list (returns (correct, total)).
Instances For
Mean loss over an explicit list of samples.
Instances For
Mean loss over an entire Dataset.
Instances For
Fit on an explicit list of samples for a fixed number of steps.
Returns a small report with mean loss before/after.
Instances For
Fit on a Dataset for a fixed number of steps.
Instances For
Fit using a DataLoader for a fixed number of epochs.
Instances For
Construct a stateful stepper for custom loops.
This is useful if you want to control:
- evaluation cadence,
- logging,
- validation, early stopping, etc.
The returned Stepper still uses TorchLean's optimizer/scheduler implementations.
Instances For
Run a single training step on one sample using a Stepper.
Instances For
Run an epoch over a list of samples using a Stepper (returns the per-step losses).
Instances For
Convenience: instantiate + fit on a list of samples.
Returns both the Runner (so you can keep using the trained parameters) and the fit report.
Instances For
Convenience: instantiate + fit on a Dataset.
Returns both the Runner and the fit report.
Instances For
Convenience: instantiate + fit using a DataLoader.
Returns the Runner, the fit report, and the updated loader state (shuffled epoch cursor).
Instances For
Execution config parsed from CLI flags (dtype/backend/fast-kernels).
Instances For
Parse and strip execution flags, returning (config, remainingArgs).
Instances For
Log the chosen execution config to stdout (useful for reproducible demos).
Instances For
Re-export of the low-level imperative scalar trainer interface.
This exposes forwardT/backwardT/stepT from Runtime.Autograd.TorchLean.ScalarTrainer.
Most users should prefer the higher-level NN.API.train / API.TorchLean.Trainer APIs.
Imperative session API: a tape-backed interface that can run in eager or compiled mode.
This is roughly analogous to using PyTorch "eager tensors", except TorchLean makes the tape/session
explicit. The Session surface is useful for:
- interactive experiments in
IO, - debugging (inspect intermediate values),
- building higher-level runners.