TorchLean API

NN.Runtime.Autograd.Model.StateIO

Model State IO #

This module saves and restores the complete shape-indexed state of a TorchLean runtime module. State includes trainable parameters and persistent buffers; optimizer state has a separate format.

Two formats are provided for runtime state packs.

Exact Scalar Format #

Checkpoint.Encoding selects an exact scalar payload and a versioned format tag. Native and configured binary values use storage words; complex values store both component words. The Float instance retains the existing binary64 format, so old checkpoints remain readable. The streamed float32 format further down remains a separate device-oriented representation.

This is:

The file layout is:

{
  "format": "torchlean_state_bits_v1",
  "state": [
    { "shape": [d1, d2, ...], "values": [u64bits, u64bits, ...] },
    ...
  ]
}

Runtime Float32 Format #

Native Float32 modules and device-backed modules use a versioned binary stream. Each state tensor records its rank, dimensions, and element count before its little-endian binary32 payload. Loading checks all metadata against the expected shape-indexed state and rejects unsupported versions, truncated payloads, and trailing data. The stream is written one tensor at a time, so saving a large CUDA model does not construct a second host-side copy of the whole checkpoint.

Versioned header for streamed runtime float32 state checkpoints.

Instances For

    Encode a natural number as a JSON number.

    Instances For

      Encode one tensor as shape metadata plus exact scalar payloads.

      Instances For

        Decode one shape-checked tensor from the exact scalar state format.

        Instances For

          Encode state tensors in reverse order, preserving each tensor's bit representation.

          Instances For

            Encode shape-indexed model state as the JSON array stored under state.

            Instances For

              Decode an expected state layout from a tensor array, starting at offset.

              Instances For

                Decode the state JSON array into the expected shape-indexed state pack.

                Instances For

                  Write model state using the scalar type's exact encoding and versioned format tag.

                  Instances For

                    Read model state, rejecting incompatible scalar formats, shapes, and payload lengths.

                    Instances For

                      Streaming float32 module checkpoints #

                      Append one binary32 bit pattern in little-endian byte order.

                      Instances For

                        Decode one little-endian 32-bit word at offset.

                        Instances For

                          Encode a tensor as exact little-endian binary32 values.

                          Instances For

                            Decode an exact binary32 payload into a shape-checked tensor.

                            Instances For

                              Write one expected tensor shape to a streaming checkpoint.

                              Instances For

                                Read and validate one tensor shape from a streaming checkpoint.

                                Instances For
                                  def Runtime.Autograd.Model.StateIO.tensorFloat32Bytes {α : Type} [TorchLean.Storage α] (encode : α → Float32) {shape : Spec.Shape} (tensorRef : Torch.Param α shape) :

                                  Obtain one state tensor as raw float32 bytes without materializing the whole state pack.

                                  Instances For

                                    Stream shape-indexed runtime state to an open checkpoint handle.

                                    Instances For

                                      Write runtime model state as a streamed float32 checkpoint.

                                      Only one tensor payload is resident on the host at a time. This is the appropriate format for large CUDA models; it records the exact values used by the float32 runtime instead of expanding them into one in-memory JSON tree.

                                      Instances For

                                        Check whether a file begins with the streaming float32 checkpoint header.

                                        Instances For
                                          def Runtime.Autograd.Model.StateIO.readTensorFloat32Into {α : Type} [TorchLean.Storage α] [Torch.TensorTransfer α] (decode : Float32 → α) (useCuda : Bool) (handle : IO.FS.Handle) {shape : Spec.Shape} (tensorRef : Torch.Param α shape) :

                                          Read one float32 payload directly into an existing runtime state tensor.

                                          Instances For

                                            Stream a checkpoint into shape-indexed runtime state.

                                            Instances For

                                              Validate every tensor record and payload without changing the destination parameters.

                                              Instances For

                                                Read and validate the common header of a streamed float32 checkpoint.

                                                Instances For

                                                  Reject extra bytes after the last expected tensor payload.

                                                  Instances For

                                                    Load a streamed float32 checkpoint into existing runtime state.

                                                    Instances For