TorchLean API

NN.GraphSpec.Primitives.Vision

GraphSpec Vision Primitives #

This file extends the sequential GraphSpec core (NN.GraphSpec.Core) with single-input/single-output vision operation adapters used by classic CNN pipelines.

These are not model definitions. They are reusable nodes in the GraphSpec vocabulary:

The corresponding model examples live under NN.GraphSpec.Models.

Important scope note:

Why only these vision ops?

GraphSpec only exposes an operation once we have both sides of the contract in place:

  1. a pure Spec meaning, and
  2. an executable TorchLean program meaning.

The general always-available primitives (linear, relu, softmax) live in NN.GraphSpec.Core; this file is the current vision extension pack. More packs can be added as we decide which runtime/spec operations should become architecture-level GraphSpec nodes.

Parameter convention (sequential GraphSpec) #

Each primitive has an explicit type-level parameter-shape list ps : List Shape.

For example, convolution is parameterized by:

so the primitive has ps = [OIHW ..., Vec ...]. When you compose graphs with >>>, these ps lists concatenate, giving a typed “ABI” for model parameters.

References / citations (informal pointers) #

def NN.GraphSpec.Primitive.conv2d (inC outC kH kW stride padding inH inW : ) {h_inC : inC 0} {h_kH : kH 0} {h_kW : kW 0} {hStride : stride 0} :

2D convolution on CHW tensors (channel-first, no batch).

Inputs:

  • parameters kernel, bias (in that order),
  • input tensor x : CHW inC inH inW.

Output:

CHW outC outH outW where

outH = Spec.Shape.slidingWindowOutDim inH kH stride padding and similarly for outW.

This is close by design to the underlying Spec/TorchLean op. PyTorch analogy: torch.nn.functional.conv2d on an NCHW tensor, specialized here to CHW.

Instances For
    def NN.GraphSpec.Primitive.maxPool2d (kH kW inH inW inC stride : ) {h_kH : kH 0} {h_kW : kW 0} {hStride : stride 0} :

    MaxPool2D on CHW tensors (parameter-free).

    Output shapes follow the standard pooling size formulas:

    outH = Spec.poolOutDim inH kH stride 0 and similarly for outW.

    PyTorch analogy: torch.nn.functional.max_pool2d (with matching kernel_size / stride).

    Instances For

      Flatten any tensor to a 1D vector (parameter-free).

      Output shape is .dim (Spec.Shape.size s) .scalar, i.e. a vector whose length is the number of elements of the input shape.

      This is a reshape/view operation (no arithmetic), used to connect convolutional features to a vector-valued classifier head.

      PyTorch analogy: torch.flatten(x).

      Instances For
        def NN.GraphSpec.Primitive.batchnormChw (channels height width : ) (h_c : channels > 0) (h_h : height > 0) (h_w : width > 0) :

        BatchNorm on CHW tensors (channel-first, no batch).

        Parameters are (gamma, beta) vectors of length channels. This models the learnable affine part of batch normalization.

        Note: this op does not carry running mean/variance state inside GraphSpec. If/when we model those, they will need an explicit state/effect model outside of this pure graph language.

        Reference: Ioffe & Szegedy (2015).

        Instances For
          def NN.GraphSpec.Graph.conv2d (inC outC kH kW stride padding inH inW : ) {h_inC : inC 0} {h_kH : kH 0} {h_kW : kW 0} {hStride : stride 0} :

          Graph constructor for Primitive.conv2d.

          Instances For
            def NN.GraphSpec.Graph.maxPool2d (kH kW inH inW inC stride : ) {h_kH : kH 0} {h_kW : kW 0} {hStride : stride 0} :

            Graph constructor for Primitive.max_pool2d.

            Instances For
              def NN.GraphSpec.Graph.batchnormChw (channels height width : ) (h_c : channels > 0) (h_h : height > 0) (h_w : width > 0) :

              Graph constructor for Primitive.batchnorm_chw.

              Instances For