TorchLean API

NN.API.Checkpoint

Runtime Checkpoints #

TorchLean examples often want the same simple workflow:

  1. train a model for a few steps,
  2. save its state, and
  3. restore that state before inference or further training.

The implementation delegates binary encoding and device transfers to Runtime.Autograd.Model.StateIO, while this module provides the checked runtime API used by trainers and data loaders.

What Is Supported #

Both checkpoint formats preserve the model's shape-indexed state layout:

This is enough to checkpoint any TorchLean runtime model implemented as a TorchLean.Module.Objective over native Float32 or binary64 Float, independent of architecture.

Results of Trainer.train use Checkpoint.State: trained.save path writes the trained state as Float tensors and trainer.load path data restores it.

Explicit immutable nn.State α checkpoints use Checkpoint.Encoding α. Instances preserve exact bits for Float, Float32, and configured binary formats, including arbitrary supported widths. The complex instance stores both coordinates using their component encoding. Loading checks the format tag as well as the state layout; it never silently casts between scalar formats.

Checkpoint persistence supported by a tensor element type.

Instances
    @[instance_reducible]

    Binary64 Float checkpoints keep exact binary64 values on CPU and binary32 values on CUDA.

    @[instance_reducible]

    Native Float32 checkpoints preserve exact binary32 payloads on both CPU and CUDA.

    def TorchLean.Checkpoint.save {α β : Type} [Storage α] [Storage β] [Context α] [Checkpointable α] {stateShapes inputShapes dataInputShapes : List Shape} (objective : Module.Objective α β stateShapes inputShapes dataInputShapes) (path : System.FilePath) :

    Save the current values of a TorchLean runtime module.

    The module's element type determines its persistence implementation. Native Float32 modules preserve exact binary32 payloads on CPU and CUDA. Binary64 Float modules preserve binary64 values on CPU and the runtime's binary32 values on CUDA.

    Instances For
      def TorchLean.Checkpoint.load {α β : Type} [Storage α] [Storage β] [Context α] [Checkpointable α] {stateShapes inputShapes dataInputShapes : List Shape} (objective : Module.Objective α β stateShapes inputShapes dataInputShapes) (path : System.FilePath) :

      Load a compatible checkpoint into a module.

      The reader checks the format header, state-tensor count, every shape, and every payload length before accepting the checkpoint.

      Instances For
        def TorchLean.Checkpoint.Optimizer.save {α β : Type} [Storage α] [Storage β] [Context α] {stateShapes inputShapes dataInputShapes : List Shape} (objective : Module.Objective α β stateShapes inputShapes dataInputShapes) (path : System.FilePath) :

        Save optimizer state retained by a module's runtime backend.

        This currently applies to eager CUDA Adam and AdamW, whose moment buffers live on the device. The checkpoint also preserves the eager session's random counter, so dropout continues with the next mask after a restore. The operation fails explicitly when the trainer has no backend-owned optimizer state instead of writing an incomplete resume checkpoint.

        Instances For
          def TorchLean.Checkpoint.Optimizer.load {α β : Type} [Storage α] [Storage β] [Context α] {stateShapes inputShapes dataInputShapes : List Shape} (objective : Module.Objective α β stateShapes inputShapes dataInputShapes) (path : System.FilePath) :

          Restore optimizer state and its random counter. Legacy files lacking the counter load with a warning: their moments are recoverable, but they cannot reproduce a stochastic continuation.

          Instances For

            Save an immutable state pack in its scalar encoding, with the layout supplied by model.

            Instances For

              Load immutable model state without mutating a runtime module.

              The model supplies the exact dependent state layout, so malformed, missing, extra, or incorrectly shaped tensors are rejected at the boundary.

              Instances For