TorchLean API

NN.GraphSpec.Primitives.Spatial

GraphSpec Spatial Primitives #

This file extends the sequential GraphSpec core (NN.GraphSpec.Core) with single-input/single-output spatial operations used by convolutional 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 spatial operations?

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 spatial 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, an N-D convolution is parameterized by:

When you compose graphs with >>>, these parameter-shape lists concatenate, giving a typed interface for model parameters.

References / citations (informal pointers) #

def NN.GraphSpec.Primitive.conv {d : } (inC outC : ) (kernel stride padding spatial : TorchLean.Tensor [d]) :
Primitive [Spec.Shape.ofList (outC :: inC :: kernel.to (List )), [outC]] (Spec.Shape.ofList (inC :: spatial.to (List ))) (Spec.Shape.ofList (outC :: (Spec.convOutSpatial spatial kernel stride padding).to (List )))

Arbitrary-rank convolution on a channels-first tensor with no batch axis.

Inputs:

  • parameters kernel, bias (in that order),
  • input tensor x : (inChannels, spatial...).

Output:

The output has shape (outChannels, convOutSpatial spatial kernel stride padding...).

Instances For
    def NN.GraphSpec.Primitive.maxPool {d : } (channels : ) (kernel stride padding spatial : TorchLean.Tensor [d]) {hKernel : ∀ (i : Fin d), kernel.getScalar i 0} {hStride : ∀ (i : Fin d), stride.getScalar i 0} :
    Primitive [] (Spec.Shape.ofList (channels :: spatial.to (List ))) (Spec.Shape.ofList (channels :: (Spec.poolOutSpatialPad spatial kernel stride padding).to (List )))

    Arbitrary-rank max pooling on a channels-first tensor (parameter-free).

    Output shapes follow the standard pooling size formulas:

    Each spatial axis uses the corresponding kernel, stride, and padding entry.

    Instances For

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

      Output shape is [Spec.Shape.size s], 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.batchNorm (channels : ) (spatial : Spec.Shape) (hWellFormed : (Spec.Shape.dim channels spatial).wellFormed) :
        Primitive [[channels], [channels]] (Spec.Shape.dim channels spatial) (Spec.Shape.dim channels spatial)

        BatchNorm over every axis in spatial, independently for each channel.

        Parameters are the affine vectors (gamma, beta). GraphSpec keeps this primitive stateless; running statistics belong to the stateful model layer.

        Instances For
          def NN.GraphSpec.Chain.conv {d : } (inC outC : ) (kernel stride padding spatial : TorchLean.Tensor [d]) :
          Chain [Spec.Shape.ofList (outC :: inC :: kernel.to (List )), [outC]] (Spec.Shape.ofList (inC :: spatial.to (List ))) (Spec.Shape.ofList (outC :: (Spec.convOutSpatial spatial kernel stride padding).to (List )))

          Chain constructor for Primitive.conv.

          Instances For
            def NN.GraphSpec.Chain.maxPool {d : } (channels : ) (kernel stride padding spatial : TorchLean.Tensor [d]) {hKernel : ∀ (i : Fin d), kernel.getScalar i 0} {hStride : ∀ (i : Fin d), stride.getScalar i 0} :
            Chain [] (Spec.Shape.ofList (channels :: spatial.to (List ))) (Spec.Shape.ofList (channels :: (Spec.poolOutSpatialPad spatial kernel stride padding).to (List )))

            Chain constructor for Primitive.maxPool.

            Instances For

              Chain constructor for Primitive.flatten.

              Instances For
                def NN.GraphSpec.Chain.batchNorm (channels : ) (spatial : Spec.Shape) (hWellFormed : (Spec.Shape.dim channels spatial).wellFormed) :
                Chain [[channels], [channels]] (Spec.Shape.dim channels spatial) (Spec.Shape.dim channels spatial)

                Chain constructor for Primitive.batchNorm.

                Instances For