TorchLean API

NN.IR.Infer

Shape Inference #

Shape inference and consistency checking for NN.IR.Graph.

NN.IR.Node stores an outShape field because many consumers want shape metadata to be available without re-running inference (pretty printers, exporters, verifiers, etc.).

This module provides an independent shape inference/checking procedure that recomputes the expected output shape of each node from:

For parameterized ops whose output shape depends on external parameters (notably OpKind.linear), we treat the node's declared outShape as an input to the checker and validate the local contracts we can check (e.g. input/output are vectors).

This is the single source of truth for Graph.checkShapes: adding a new OpKind should extend this match first, then the semantics/export/verification passes can rely on the same contract.

PyTorch analogy:

References / related systems:

Node-local inference #

Most IR ops are “shape transparent” (elementwise, permute, etc.). A few need special handling:

Infer the output shape of a node from its kind + parent shapes.

This function is used by Graph.checkInferredShapes below.

Instances For

    Infer shapes for every node (in topo/id order) and check that Node.outShape matches.

    This is meant as a compiler/back-end sanity check and as a clean IR invariant for the docs: well-formed graphs have self-consistent declared shapes.

    Instances For