Convolution lowering puts each receptive field in a matrix column so flattened filters can score all output positions together.
Summary
How do four overlapping patches become the four output values, and what must be stored? Use the input rows , , . The filter is , with stride one, no padding and no bias. Use deep-learning cross-correlation: do not flip the filter.
The mechanism
Flatten channel, kernel-row and kernel-column in that order; visit output positions row by row.
| Output location | Patch column | Dot product with |
|---|---|---|
| Top-left | ||
| Top-right | ||
| Bottom-left | ||
| Bottom-right |
The four column scores reshape to a two-by-two output; no input reconstruction happens during that reshape. The patch matrix stores sixteen values from nine source values, ratio .
Learning objective
Each local patch becomes one column in the matrix product
Derivation
Why do the middle image pixels appear in both output columns?
For one image with channels, let and . Unfold gives of shape ; flattened filters have shape . Compute plus per-output-channel bias, then reshape to . For batch size , explicit columns store entries.
Dense work is multiply-accumulates, or approximately twice that many FLOPs under the two-FLOP convention. The tiny example uses sixteen MACs even though its particular filter contains zeros. Materialized storage and whole-operator memory traffic are different quantities.
Change storage or channel structure
Avoid full materialization
New task: preserve these outputs without storing every patch simultaneously.
Picture and change: process one four-value column at a time and emit its score.
Check: patch scratch falls from sixteen to four scalar values here, while all four outputs remain minus four. Input, output and filter storage remain; repeated reads and indexing still cost work.
Use when: unfold storage or traffic is binding; benchmark an appropriate implicit/tiled kernel under the same operator.
Separate depthwise and pointwise work
New task: apply per-channel spatial filters, then mix channels.
Picture and change: take two input channels, four output positions and two-by-two depthwise filters. Depthwise work is MACs. Mixing those channels into three outputs with a pointwise filter adds MACs.
Check: total work is 56 MACs versus 96 for a dense two-by-two convolution with three output channels. These are different parameterizations, not generally interchangeable learned functions.
Use when: the architecture explicitly separates spatial filtering and channel mixing.
Reconstruct backward accumulation
Why is folding patches not an inverse reshape?
Reasoned answer
Overlapping columns contribute to the same input coordinate, so backward sums their contributions. Folding ones gives center coverage four in this example. Actual input gradients also include filter coefficients; with this filter and unit output gradients, the center derivative is zero.
Variants
For each axis, output length is for valid parameters. Here is symmetric padding, dilation and stride; apply the formula independently to height and width. Groups restrict each filter to input channels, changing the MAC count accordingly. Depthwise is a grouped case; pointwise performs dense channel mixing at each location.
Implicit GEMM indexes virtual patch tiles instead of storing the complete unfold. Direct kernels, Winograd and FFT implementations use different layouts or transforms. FFT requires the correct padding/correlation convention; Winograd changes numerical behavior. Shape, hardware, precision and workspace determine useful choices, not a universal framework implementation rule.
Interview focus
Derive complete input, filter, column and output shapes, then state flatten order, bias and resource convention. Algebraic lowering is not evidence that the actual kernel allocated the patch matrix.
Common pitfalls
Flipping the example’s filter changes all outputs to positive four; convolution versus cross-correlation can matter. Explicit storage expansion depends on shape, stride, padding and dilation, not a constant nine-fold multiplier. Forward output reshape and backward overlap-add solve different tasks. Profile depthwise/pointwise work and their fusion boundary separately.
Related
- CNN architecture.
- FlashAttention. Same idea: rearrange for hardware.