Conv (generic N-D, spec layer) #
This file defines a channels-first N-D convolution and transpose-convolution spec over a
single sample (no batch dim), generalizing Conv1D/Conv2D/Conv3D and ConvTranspose{d}d to an
arbitrary spatial rank d.
PyTorch analogy: this corresponds to torch.nn.Conv{d}d with:
groups = 1,dilation = 1,- per-axis
strideandpadding, - and the usual output-size formula (floor division, like PyTorch):
For each axis a : Fin d:
out[a] = (in[a] + 2*padding[a] - kernel[a]) / stride[a] + 1
The weight tensor has shape (outC × inC × kernel[0] × ... × kernel[d-1]) and the bias has
shape (outC).
Implementation notes:
- We intentionally implement convolution using "natural nested loops" (outer axes first) and a
single accumulator
foldlstyle, matching the evaluation-order discipline ofConv2D.lean. - Padding semantics are implemented via
get_at_or_zeroplus an explicit guard for the left/top/front padding region (to avoid negative indices, whichNatcannot represent). - This file is self-contained and does not modify the existing
Conv1D/Conv2D/Conv3Dfiles.
Small generic helpers #
Instances For
Given:
- an output index tuple
outIdx, - a kernel index tuple
kIdx, - per-axis
strideandpadding, compute the corresponding input index tuple (into the unpadded input), or returnnoneif we are in the left/top/front padding region on some axis.
Right/bottom/back padding is handled by get_at_or_zero when the computed index is out of bounds.
Instances For
Given:
- an output index tuple
outIdx, - a kernel index tuple
kIdx, - per-axis
strideandpadding, compute the corresponding input index tuple for transpose convolution, ornoneif the equalityout + padding = in * stride + kcannot be satisfied on some axis.
Implementation detail: for each axis we solve
in = (out + padding - k) / stride
and require divisibility (% stride = 0) plus out + padding ≥ k.
Out-of-bounds input indices are handled by get_at_or_zero at the call site.
Instances For
Instances For
Spec definition #
Parameters for a generic N-D convolution (weights + bias), channels-first.
- kernel : Tensor α (Shape.ofList (outC :: inC :: kernel.toList))
Kernel weights, shape
(outC, inC, kernel[0], ..., kernel[d-1]). - bias : Tensor α (Shape.dim outC Shape.scalar)
Bias, shape
(outC).
Instances For
Output size along a single axis (PyTorch formula, floor division).
Instances For
Generic N-D convolution forward pass on a single channels-first input (no batch dimension).
Mathematically, for output channel oc and output spatial index o : Vector Nat d:
y[oc,o] = Σ_{ic, k} x_pad[ic, o*stride + k] * W[oc,ic,k] + b[oc]
where k ranges over the kernel window and x_pad is input with zero-padding.
Instances For
Gradient of convolution output w.r.t. the kernel weights (given grad_output).
Instances For
Gradient of convolution output w.r.t. the bias (sum over spatial positions).
Instances For
Gradient of convolution output w.r.t. the input (the "input-gradient" / transpose-convolution map).
This mirrors conv{1,2,3}d_input_deriv_spec but for arbitrary spatial rank d.
Instances For
Convolution backward pass: returns (dKernel, dBias, dInput).
Instances For
ConvTranspose (generic N-D) #
Parameters for a generic N-D transpose convolution (weights + bias), channels-first.
PyTorch analogy: this is torch.nn.ConvTranspose{d}d with:
output_padding = 0,dilation = 1,groups = 1,- per-axis
strideandpadding, - and weight layout
(inC, outC, k0, ..., k(d-1)).
- kernel : Tensor α (Shape.ofList (inC :: outC :: kernel.toList))
Kernel weights, shape
(inC, outC, kernel[0], ..., kernel[d-1]). - bias : Tensor α (Shape.dim outC Shape.scalar)
Bias, shape
(outC).
Instances For
Output size along a single axis for transpose convolution (output_padding = 0).
Instances For
Generic N-D transpose convolution forward pass on a single channels-first input (no batch dim).
For output channel oc and output spatial index o : Vector Nat d we define:
y[oc,o] = Σ_{ic, k} x[ic, (o + padding - k) / stride] * W[ic,oc,k] + b[oc]
where each axis must satisfy out + padding ≥ k and divisibility by stride (% stride = 0).
Instances For
Gradient of transpose convolution output w.r.t. the kernel weights (given grad_output).
Instances For
Gradient of transpose convolution output w.r.t. the bias (sum over spatial positions).
Instances For
Gradient of transpose convolution output w.r.t. the input (given grad_output).
Instances For
Transpose convolution backward pass: returns (dKernel, dBias, dInput).
Instances For
2D specializations (compat) #
TorchLean also exposes compatibility names for 2D convolution specs. They are implemented as specializations of the generic N-D convolution spec above.
Output spatial shape (outH,outW) for a Conv2D with given hyperparameters.
Instances For
Output shape including channels: (outC,outH,outW).
Instances For
Conv2D forward pass on a single image C×H×W (no batch dimension).
PyTorch note: this matches the usual Conv2d definition (cross-correlation form, i.e. the kernel
is not spatially flipped).
Instances For
Gradient of Conv2D output w.r.t. the kernel weights (given grad_output).
Instances For
Gradient of Conv2D output w.r.t. the bias (sum over spatial positions).
Instances For
Gradient of Conv2D output w.r.t. the input image.
Instances For
Conv2D backward pass: returns (dKernel, dBias, dInput).
Instances For
ConvTranspose2D (compat) #
We keep the transpose-convolution names for compatibility, but implement them as specializations of the generic N-D transpose convolution spec above.
Kernel layout for transpose-convolution: (inC, outC, kH, kW).
Instances For
Parameters for a 2D transpose convolution.
- kernel : ConvTransposeKernel outC inC kH kW α
kernel.
- bias : Tensor α (Shape.dim outC Shape.scalar)
bias.
Instances For
Output spatial shape (outH,outW) for ConvTranspose2d (with output_padding = 0).
Instances For
Output shape including channels: (outC,outH,outW).
Instances For
ConvTranspose2D forward pass on a single image C×H×W (no batch dimension).
This is written as an output-indexed sum (no in-place updates), matching the standard definition.
Instances For
Gradient of ConvTranspose2D output w.r.t. the kernel weights.
Instances For
Gradient of ConvTranspose2D output w.r.t. the bias (sum over spatial positions).
Instances For
Gradient of ConvTranspose2D output w.r.t. the input image.
Instances For
ConvTranspose2D backward pass: returns (dKernel, dBias, dInput).
Instances For
Friendly aliases #
We keep the _spec suffixes as the canonical names, but provide a few shorthands so call sites can
read more like PyTorch-style pseudocode.
Alias for conv_spec.
Instances For
Alias for conv_backward_spec.
Instances For
Alias for conv_kernel_deriv_spec.
Instances For
Alias for conv_bias_deriv_spec.
Instances For
Alias for conv_input_deriv_spec.