TorchLean API

NN.Runtime.PyTorch.Export.IRPyTorch

IRPyTorch #

IR → PyTorch code generation.

This module takes an op-tagged NN.IR.Graph plus a NN.MLTheory.CROWN.Graph.ParamStore payload and emits a standalone PyTorch nn.Module implementation as a Python source string.

What this is (and isn't):

Assumptions:

Failure modes (reported as Except String):

PyTorch context (comments only):

PyTorch’s ONNX exporter and torch.export can capture graphs for execution in other runtimes. TorchLean’s emitter here prints readable Python that mirrors the IR, rather than producing an execution-focused serialized graph artifact.

Flatten a tensor and render it as a Python list literal (used for torch.tensor([...]).reshape(...)).

Instances For

    Return true iff every array entry equals the first one, vacuously for an empty array.

    Instances For

      Options controlling IR-to-PyTorch emission.

      Most knobs here configure example emission, dtype handling, and how to materialize constant nodes as buffers or parameters.

      • className : String

        Class name to use in the emitted Python source.

      • dtypeExpr : String

        Python expression used for the tensor dtype (e.g. "torch.float32").

      • includeTrainingSkeleton : Bool

        If true, include a small training skeleton and runtime-check in the emitted script.

      • learnableConsts : Bool

        If true, emit IR const nodes as nn.Parameter when appropriate (learnable).

      Instances For
        @[instance_reducible]
        @[reducible, inline]

        Map from IR constant node id to its Python attribute name, for both buffers and parameters.

        Instances For
          def Export.IRPyTorch.attr (kind : String) (id : ℕ) (field : String := "") :

          Python attribute name for a node and optional parameter field. The spelling is shared by initialization and forward evaluation, including saved state keys.

          Instances For
            def Export.IRPyTorch.tensor (flatList : String) (shape : Spec.Shape) (dtypeVar : String := "dtype") :

            Emit a torch.tensor([...]).reshape(...) expression from a flat Python list literal and a Shape.

            Instances For

              Retrieve a node from the graph and validate its id invariant.

              This yields more actionable error messages than directly indexing into g.nodes.

              Instances For

                Emit a standalone PyTorch nn.Module class for an IR graph.

                This is the main entrypoint for IR exporters: it bundles:

                • imports,
                • a class definition with parameters/buffers materialized from the ParamStore,
                • a forward method implementing the IR, and
                • (optionally) a compact training file for export checks.
                Instances For