TorchLean API

NN.Runtime.PyTorch.Export.ONNX

ONNX Graph Adapter #

This module emits a Python-side ONNX adapter for TorchLean graph import.

The adapter does not give ONNX a separate Lean semantics. It reads an ONNX model, lowers the supported static graph fragment to the same torchlean.ir.v1 JSON used by the torch.export/FX bridge, and then the Lean side should call NN.Runtime.PyTorch.Import.TorchExport.parseGraph.

That boundary is intentional: ONNX parsing, protobuf handling, and shape inference stay outside Lean; TorchLean accepts only the small checked graph artifact.

Every "kind" string written by the adapter comes from NN.Runtime.PyTorch.Wire, the table the Lean importer parses with.

Options for the generated ONNX-to-TorchLean-IR adapter script.

  • functionName : String

    Name of the Python helper function emitted into the script.

  • includeDebugTargets : Bool

    Include ONNX node names/op types in each node for debugging.

Instances For

    Python sections #

    Each section is an array of Python lines ending in one blank separator line.

    Imports and the artifact format marker.

    Instances For

      Static shape collection and attribute readers.

      Instances For

        One-line _lower_node rule mapping an ONNX op type to a payload-free kind.

        Instances For

          _lower_node: single-node lowerings keyed on the ONNX op type.

          Instances For

            Kinds whose single parent is the first ONNX input.

            Instances For

              Kinds that take exactly two ONNX inputs as parents.

              Instances For

                _ir_parent_names: select the ONNX inputs that become IR parents for a lowered kind.

                Instances For

                  _lower_conv: channels-first Conv with explicit padding, optionally unwrapping a batch.

                  Instances For

                    _lower_gemm: Gemm as matmul plus an optional broadcast bias add.

                    Instances For
                      def Export.PyTorch.ONNX.broadcastLine (target source fromShape : String) :

                      One add_node(<broadcastTo>, ...) call expanding a per-channel tensor to the input shape.

                      Instances For

                        _lower_batchnorm: inference-mode BatchNormalization expanded into elementwise nodes.

                        Instances For

                          The exported entry point: walk the ONNX graph and write the artifact.

                          Instances For

                            Command-line main for the generated script.

                            Instances For

                              Emit a Python script that lowers a conservative ONNX fragment to torchlean.ir.v1.

                              Supported first-pass ops are tensor ops that map to current NN.IR.OpKind: elementwise arithmetic/activations, MatMul, ReduceSum, ReduceMean, Softmax, Reshape, Flatten, Concat, Transpose, Gemm, inference-style BatchNormalization, and arbitrary-rank channels-first Conv. Graph initializers become constant nodes. The graph artifact is still separate from the runtime payload store, so this adapter validates graph structure and shapes; executing imported convolution or linear constants still requires a matching payload importer.

                              Instances For