TorchLean API

NN.Examples.Interop.PyTorch.TorchExportCheck

PyTorch nn.Module → TorchLean IR Check #

This executable example exercises the model-agnostic PyTorch graph bridge:

  1. write the generated Python graph-capture adapter;
  2. write several compact supported PyTorch models (MLP/CNN/CNN head/attention/MHA blocks);
  3. capture them to torchlean.ir.v1 JSON;
  4. parse and validate each JSON artifact back into NN.IR.Graph; and
  5. exercise a real torch.save(model.state_dict()) checkpoint reload path; and
  6. run deliberate unsupported-op cases to confirm the bridge reports unsupported semantics clearly.

The check is scoped around the interop boundary rather than PyTorch performance. PyTorch may produce an artifact, but TorchLean accepts it only after parsing and validating the explicit IR JSON.

Run:

lake exe torchlean pytorch_export_check

Command-line help for the PyTorch export bridge check.

Instances For

    Write a real PyTorch checkpoint used by TinyCheckpointMLP.

    Python owns .pt loading/saving, and the graph bridge only sees the resulting initialized nn.Module. That mirrors the intended user workflow: load trained weights in Python, capture the model graph, then ask Lean to validate the exported IR.

    Instances For

      Keep subprocess failures readable in the runtime-check output.

      The full IO.userError still contains command, args, exit code, and stderr. The runtime check only prints the first informative lines so unsupported-op tests explain the boundary without hiding the signal.

      Instances For

        Run one supported capture path and parse the resulting graph in Lean.

        Instances For

          Run the supported capture paths and parse the resulting graphs in Lean.

          Instances For

            Run a deliberate unsupported op and require the Python bridge to reject it.

            Instances For

              Run a PyTorch graph that can be captured as a value graph but must not lower into the tensor IR yet.

              This is the intended behavior for container-valued PyTorch ops: the JSON keeps the tuple, then Lean rejects tensor lowering with a semantic explanation instead of the Python bridge crashing or incorrectly treating the tuple producer as a tensor op.

              Instances For

                Run deliberate unsupported ops and require the Python bridge to reject them.

                Instances For

                  Main runtime-check body.

                  Instances For

                    Entrypoint used by lake exe torchlean pytorch_export_check.

                    Instances For