TorchLean API

NN.IR.Graph

IR Graph #

NN.IR.Graph is TorchLean’s canonical op-tagged DAG IR.

Today it is used as the shared target for:

Longer-term, the intent is to use the same IR as a bridge target for:

What this file commits to is the graph structure (ops, dependencies, and shapes). Parameter payloads (weights/bias/const values) live in backend-specific stores keyed by node id. This split keeps one graph format usable across:

If you are coming from PyTorch: the mental model is similar to a PyTorch FX graph or TorchScript IR: nodes are ops, edges are “data dependencies”, and execution is in topological order. The difference is that TorchLean attaches explicit shape metadata at every node, since our verification and proof tooling needs shape information to be first-class.

References / related systems:

Conventions (important) #

This file does not implement evaluation or shape inference. Those live in:

inductive NN.IR.OpKind :

Operation kinds in an op-tagged computation graph.

Instances For
    @[implicit_reducible]

    The minimum number of parent nodes expected by an OpKind.

    This is a structural convention only.

    For example, linear has arity 1 here because weights/biases are typically stored externally and keyed by the node id; the only data dependency in the graph itself is the activation input.

    Instances For

      An optional maximum number of parent nodes expected by an OpKind.

      For concat, the verifier permits an arbitrary number of inputs (at least 2), so this returns none.

      Instances For

        A short human-readable tag for error messages and debugging output.

        Instances For

          Human-facing operation description including operation-local parameters.

          tag is intentionally short and stable for grouping/log filtering. describe is for diagnostics: it prints axes, shapes, seeds, and convolution/pooling metadata so malformed graph dumps are useful without cross-referencing the original builder.

          Instances For
            structure NN.IR.Node :

            Node in the graph. Edges are implicit via parent indices.

            • id :

              Node id. By convention this is also the node's index in Graph.nodes.

            • parents : List

              Parent node ids, i.e. data dependencies. Each parent must be smaller than id.

            • kind : OpKind

              Operation tag and any operation-local metadata.

            • outShape : Spec.Shape

              Declared output shape. NN.IR.Infer can recompute/check this from parents.

            Instances For
              @[implicit_reducible]

              Check the basic parent-count convention for this node kind.

              Instances For

                Check that every parent id is strictly smaller than this node id (topological order).

                This is the single most important invariant for the IR:

                • it guarantees acyclicity,
                • it makes evaluation/inference a simple left-to-right pass,
                • and it makes backends predictable (no hidden recursion or “graph rewriting during execution”).
                Instances For

                  Render a compact, user-facing summary (useful in error messages).

                  Instances For
                    structure NN.IR.Graph :

                    Entire graph as an array of nodes. Parents must have smaller ids (topo order).

                    Instances For
                      @[implicit_reducible]

                      Number of nodes in the graph.

                      Instances For

                        Safe node lookup by id (treating ids as array indices).

                        Instances For

                          Total node lookup that enforces the common "id discipline" invariant (nodes[id].id = id).

                          This is convenient for backends that treat node ids as array indices (verifiers, exporters, pretty printers). The error message is meant to point to a builder bug rather than a user error.

                          Instances For

                            Safe outShape lookup by id.

                            Instances For

                              Explain why Node.hasValidArity failed.

                              This is intentionally stringly-typed; it is primarily meant for human-facing error messages.

                              Instances For

                                Basic well-formedness check used by verifier code paths.

                                This checks:

                                • node ids match array indices (common construction invariant),
                                • each node respects its op arity convention, and
                                • all parent ids are strictly smaller than the node id (topological order).

                                We keep this as a boolean predicate because some passes want a fast “yes/no” filter. If you need a human-facing error, use checkWellFormed.

                                Instances For

                                  Like wellFormed, but returns a helpful error message on failure.

                                  This is useful when you want a clean user error rather than a silent false.

                                  Instances For
                                    @[implicit_reducible]

                                    Default node used only to satisfy generic container APIs; real graphs should not rely on it.