TorchLean API

NN.Runtime.PyTorch.Export.StateDict

PyTorch state_dict Bridge #

This module is the general weight-interchange path for PyTorch users.

The important split is:

Lean should not try to parse PyTorch pickle/zip checkpoints directly. Instead, we emit a small Python adapter that loads a checkpoint with PyTorch, normalizes common wrappers such as {"state_dict": ...}, and writes shape-checkable JSON:

{
  "params": { "layer.weight": [[...]], "layer.bias": [...] },
  "meta": { "layer.weight": { "shape": [out, in], "dtype": "torch.float32" } }
}

NN.Runtime.PyTorch.Import.Core then parses the "params" object into typed TorchLean tensors. Architecture-specific loaders are still useful, but only for mapping names and shapes. The transport format itself is model-agnostic.

References:

Options for the generated Python checkpoint-to-JSON adapter.

This adapter is intentionally conservative: it accepts tensor-valued entries only, drops common DataParallel prefixes when requested, and writes plain JSON rather than a PyTorch-specific binary format. That makes the output easy to inspect, diff, and parse in Lean.

  • functionName : String

    Name of the Python helper function emitted into the generated script.

  • stripDataParallelPrefix : Bool

    If true, strip a leading "module." from keys produced by torch.nn.DataParallel.

  • includeMeta : Bool

    If true, include a "meta" object with per-key shape and dtype strings.

  • weightsOnlyExpr : String

    Python expression passed as weights_only to torch.load.

Instances For

    Render a Python boolean literal.

    Instances For

      Emit a standalone Python script that converts a PyTorch checkpoint into TorchLean JSON.

      The script handles three common checkpoint layouts:

      • a raw state_dict;
      • { "state_dict": state_dict };
      • { "model_state_dict": state_dict }.

      Usage of the generated script:

      python export_state_dict_json.py model.pt model.json
      

      The resulting model.json is accepted by Import.PyTorch.loadWeights?.

      Instances For