PyTorch state_dict Bridge #
This module is the general weight-interchange path for PyTorch users.
The important split is:
- Weights move through PyTorch
state_dicts. PyTorch’s own documentation recommends saving a module’s learned parameters withtorch.save(model.state_dict(), path)because that is the most flexible restoration format. - Graphs move through graph capture (
torch.export, FX, ONNX, or TorchLeanNN.IR.Graph). Astate_dictalone does not describe the model architecture; it only names tensors.
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:
- PyTorch documentation, "Saving and Loading Models":
https://docs.pytorch.org/tutorials/beginner/saving_loading_models.html - PyTorch
torch.exportuser guide:https://docs.pytorch.org/docs/stable/user_guide/torch_compiler/export.html - PyTorch FX overview:
https://docs.pytorch.org/docs/stable/fx.html