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:
Primitive.conv2dwrapsSpec.conv2dSpecandRuntime.Autograd.TorchLean.conv2d;Primitive.maxPool2dwraps the corresponding Spec/runtime pooling operation;Primitive.batchnormChwwraps channel-first BatchNorm;Primitive.globalAvgPool2dChwwraps channel-wise global average pooling;Primitive.flattenis the bridge from image-like tensors to vector classifiers.
The corresponding model examples live under NN.GraphSpec.Models.
Important scope note:
- These primitives all fit the chain graph language
Graph ps σ τbecause they have one input tensor and one output tensor (no merging of paths). - Residual networks require skip connections (
y + x), which are multi-input and require sharing. For that, useNN.GraphSpec.DAG, whose DAG primitive constructors reuse these sequential adapters when possible.
Why only these vision ops?
GraphSpec only exposes an operation once we have both sides of the contract in place:
- a pure Spec meaning, and
- 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:
kernel : OIHW outC inC kH kWbias : Vec outC
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) #
- Convolutional networks: LeCun et al. (1998), “Gradient-based learning applied to document recognition”.
- BatchNorm: Ioffe & Szegedy (2015), “Batch Normalization: Accelerating Deep Network Training…”.
- Global average pooling: Lin et al. (2013), “Network In Network”.
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 = (inH + 2*padding - kH) / stride + 1 and similarly for outW.
This is intentionally close to the underlying Spec/TorchLean op.
PyTorch analogy: torch.nn.functional.conv2d on an NCHW tensor, specialized here to CHW.
Instances For
MaxPool2D on CHW tensors (parameter-free).
Output shapes follow the standard pooling size formulas:
outH = (inH - kH) / stride + 1 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 (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
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
Global average pooling over spatial dims (CHW c h w → Vec c).
This is a common classifier head for CNNs: average each channel over the h×w spatial grid.
Reference: Lin et al. (2013), “Network In Network”.
PyTorch analogy: torch.nn.AdaptiveAvgPool2d((1,1)) followed by flatten.
Instances For
Graph constructor for Primitive.conv2d.
Instances For
Graph constructor for Primitive.flatten.
Instances For
Graph constructor for Primitive.batchnorm_chw.
Instances For
Graph constructor for Primitive.global_avg_pool2d_chw.