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:

NN.IR.Operator defines the operations and their static attributes. This file adds node identities, dependencies, and declared output shapes. Parameter payloads (weights, biases, and constants) live in backend-specific stores keyed by node id. This split keeps one graph format usable across:

Like a PyTorch FX graph or TorchScript IR, nodes are operations, edges are data dependencies, and execution follows topological order. TorchLean additionally attaches explicit shape metadata to every node for verification and proofs.

References / related systems:

Conventions (important) #

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

structure NN.IR.Node :

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

  • id :

    Node id. Structural validation requires this to equal the index in Graph.nodes.

  • parents : Array

    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
    @[instance_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

          Return the sole parent id when an IR node has unary arity.

          Instances For

            Return both parent ids when an IR node has binary arity.

            Instances For
              theorem NN.IR.mem_of_unaryParent?_eq_some {parents : Array } {parent : } (h : unaryParent? parents = some parent) :
              parent parents

              The parent returned by the unary decoder belongs to the source array.

              theorem NN.IR.fst_mem_of_binaryParents?_eq_some {parents : Array } {left right : } (h : binaryParents? parents = some (left, right)) :
              left parents

              The first parent returned by the binary decoder belongs to the source array.

              theorem NN.IR.snd_mem_of_binaryParents?_eq_some {parents : Array } {left right : } (h : binaryParents? parents = some (left, right)) :
              right parents

              The second parent returned by the binary decoder belongs to the source array.

              structure NN.IR.Graph :

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

              • nodes : Array Node

                The nodes, in topological order: every parent id is strictly smaller than the index of the node referencing it. Evaluation is then a single left-to-right pass with no scheduling step.

              Instances For
                @[instance_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 $\mathrm{nodes}[i].\mathrm{id}=i$.

                    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
                      theorem NN.IR.Graph.getNode_id_eq {g : Graph} {id : } {node : Node} (h : g.getNode id = Except.ok node) :
                      node.id = id

                      A successful checked lookup returns a node whose stored id is the requested array index.

                      Explain why Node.hasValidArity failed.

                      This returns a human-facing message rather than structured data; callers use it for diagnostics.

                      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
                            @[instance_reducible]

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