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]

        How an IR const node is represented in the emitted PyTorch module.

        • bufferFull: a non-learnable register_buffer(...) tensor
        • paramFull: a learnable nn.Parameter with the full tensor shape.
        Instances For
          @[reducible, inline]

          Map from IR node id to how its constant should be referenced in the PyTorch module.

          Instances For

            The Python attribute name used to reference a bound constant (self.<attr>).

            Instances For

              Default attribute name for a const node: self.const_<id>.

              Instances For

                Attribute name for a linear layer weight tensor in the ParamStore (self.linear_<id>_W).

                Instances For

                  Attribute name for a linear layer bias tensor in the ParamStore (self.linear_<id>_b).

                  Instances For

                    Attribute name for a convolution kernel tensor in the parameter store.

                    Instances For

                      Attribute name for a convolution bias tensor in the parameter store.

                      Instances For

                        Render a fixed-length natural-number vector as a Python tuple.

                        Instances For

                          Attribute name for a BatchNorm scale tensor (self.batchnorm_<id>_gamma).

                          Instances For

                            Attribute name for a BatchNorm bias tensor (self.batchnorm_<id>_beta).

                            Instances For

                              Attribute name for a BatchNorm running mean tensor (self.batchnorm_<id>_mean).

                              Instances For

                                Attribute name for a BatchNorm running variance tensor (self.batchnorm_<id>_var).

                                Instances For
                                  def Export.IRPyTorch.pyTensorFromFlat (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