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 parameters, and
  3. reload those parameters later to sample / run inference.

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

What Is Supported #

Both checkpoint paths preserve the model's shape-indexed parameter layout:

This is enough to checkpoint any TorchLean runtime model implemented as a TorchLean.Module.ScalarModule over Float, independent of architecture.

def TorchLean.Checkpoint.saveParamBits {paramShapes : List Shape} (path : System.FilePath) (ps : Runtime.Autograd.Torch.TList Float paramShapes) (pretty : Bool := true) :

Write a parameter pack to a JSON bits checkpoint.

Use this when the model's shape-indexed parameter tensors are already available and only need to be persisted.

Instances For
    def TorchLean.Checkpoint.saveModuleParams {paramShapes inputShapes natInputShapes : List Shape} (m : Runtime.Autograd.TorchLean.ScalarModule Float paramShapes inputShapes natInputShapes) (path : System.FilePath) :

    Save the current values of a TorchLean runtime module.

    CPU modules use the exact Float.toBits JSON format. CUDA modules use the streamed float32 format so large models remain on the device while their parameters are written. This is architecture-agnostic: it works for any ScalarModule Float.

    Instances For
      def TorchLean.Checkpoint.loadModuleParams {paramShapes inputShapes natInputShapes : List Shape} (m : Runtime.Autograd.TorchLean.ScalarModule Float paramShapes inputShapes natInputShapes) (path : System.FilePath) :

      Load a JSON-bits or streamed-float32 checkpoint into a module.

      The format is detected from its header. Both readers check the parameter count, every tensor shape, and every payload length before accepting the checkpoint.

      Instances For
        def TorchLean.Checkpoint.saveOptimizerState {paramShapes inputShapes natInputShapes : List Shape} (m : Runtime.Autograd.TorchLean.ScalarModule Float paramShapes inputShapes natInputShapes) (path : System.FilePath) :

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

        This currently applies to eager CUDA Adam and AdamW, whose moment buffers live on the device. The operation fails explicitly when the selected trainer has no backend-owned optimizer state instead of writing an incomplete resume checkpoint.

        Instances For
          def TorchLean.Checkpoint.loadOptimizerState {paramShapes inputShapes natInputShapes : List Shape} (m : Runtime.Autograd.TorchLean.ScalarModule Float paramShapes inputShapes natInputShapes) (path : System.FilePath) :

          Restore optimizer state retained by the module's runtime backend.

          Instances For

            Load a JSON bits checkpoint as a parameter list (without mutating a module).

            This is useful when you want to run compiled inference directly and never instantiate a trainer.

            Instances For

              Read a JSON bits checkpoint, returning an error string instead of throwing an exception.

              This is useful in batch tools or CI-style runs where you want to keep going and report failures.

              Instances For

                Load a bits checkpoint whose parameter layout is determined by model.

                Instances For

                  Allocate runtime parameter handles from a checked parameter pack.

                  Instances For

                    Load a checkpoint into a runtime module attached to model.

                    Instances For

                      Load a model checkpoint when the caller supplied a path.

                      Instances For

                        Save a model's runtime parameters when the caller supplied a path.

                        Instances For