Scalar Objectives #
Executable scalar-objective definitions and runtime state. This module provides loss and gradient evaluation, explicit optimizer steps, state access, and optimizers bound to a live objective.
An immutable scalar-objective definition:
initStatestores initial trainable parameters and persistent buffers asFloattensors,lossis polymorphic in the scalar backend (same code works for Float/configured binary32/…).
You can instantiate this definition as an Objective under a chosen execution mode and scalar.
- initState : TorchLean.TensorPack Float stateShapes
Initial parameter-and-buffer state, cast from
Floatat instantiation time. - runtimeInit : Option (RuntimeInit.Plan stateShapes)
Optional storage-first initialization plan for executable
Floatruns.The ordinary tensors remain the semantic initial values. This plan records how a runtime may materialize the same initialization directly in backend storage without traversing a large nested Lean tensor first.
Differentiability flags aligned with
stateShapes; persistent buffers carryfalse.Validate static model and objective configuration before runtime allocation.
- validateDataInputs : TorchLean.TensorPack β dataInputShapes → Except String Unit
Validate non-differentiable inputs before they reach the runtime program.
- loss {α : Type} [TorchLean.Storage α] [Context α] {m : Type → Type} [Monad m] [Ops m α] : CurriedRef (fun (s : Spec.Shape) => Torch.Ops.Ref m α s) (stateShapes ++ inputShapes) (CurriedRef (fun (s : Spec.Shape) => Torch.Ops.DataRef m α β s) dataInputShapes (m (Torch.Ops.Ref m α Spec.Shape.scalar)))
Scalar loss over differentiable tensors followed by non-differentiable data tensors.
The second curried input pack can carry labels, bounded token IDs, or gather indices without converting them through the model's floating-point scalar type.
Instances For
Runtime state for a model together with a scalar objective.
This is lower level than PyTorch's loss classes: it owns model state as well as the objective. It
wraps Torch.ScalarTrainer and exposes objective evaluation, explicit gradients, and updates.
- trainer : Torch.ScalarTrainer α β stateShapes inputShapes dataInputShapes
Trainer that owns trainable parameters and persistent buffers.
- runtime : Config
Runtime configuration used to instantiate the module.
- tensorTransfer : Torch.TensorTransfer α
Concrete host/device tensor conversion selected when the module was instantiated.
Instances For
Create a runtime objective from an explicit scalar program and initial model state.
This is the low-level constructor; public training code starts from an ObjectiveDef and calls
ObjectiveDef.instantiate.
Instances For
Evaluate the scalar objective.
Instances For
Return state-shaped gradients, using zero for entries that do not require gradients.
Set value := true to return (gradient, objectiveValue) from one forward tape.
Instances For
Start a fresh optimizer history from this module's current state.
This also releases backend-owned moments and clears the selected update path. Use the returned state for subsequent updates; reinitialization permits switching between generic and native steps.
Instances For
Run one optimizer step using an explicit optimizer and state.
This mirrors a PyTorch training step:
- compute the explicit state gradient (
ScalarTrainer.runGrad) - update parameters via
opt.stepand return the new optimizer state
Set loss := true to return (nextOptimizerState, lossValue) from the same training tape.
On CUDA, mixing native steps and generic updates within one optimizer history is rejected.
Call initOptimizer to start a fresh history before switching paths.
Instances For
Read the complete parameter-and-buffer state as a shape-indexed list.
Instances For
Replace the complete parameter-and-buffer state.
Instances For
Mutable optimizer state bound to one executable module.
- module : Objective α β stateShapes inputShapes dataInputShapes
- state : IO.Ref State
- step : TorchLean.TensorPack α inputShapes → TorchLean.TensorPack β dataInputShapes → IO Unit
Instances For
Initialize an optimizer and bind its state and update operation to module.