Convolutions#

A convolution layer runs three computations per training step: the forward convolution, the input gradient and the weight gradient. Each is a set of dot products in which every product and every addition is rounded (Dot-product arithmetic). This page says which dot products those are and in which order their terms are summed. It also explains how conv_formats() runs them on the core routine of Quantized numerical kernels without forming the matrices that a textbook lowering to a matrix product would build. The layer guide (Layers, “Convolutions”) has the worked example.

The three sums#

Take a convolution in \(n_d \in \{1, 2, 3\}\) spatial dimensions. Its input \(x\) has \(C\) channels, and its weight \(W\) has \(C_{\text{out}}\) filters over a kernel of extent \(k\). It has stride \(s\), padding \(p\) and dilation \(d\). Each of these is a vector over the spatial dimensions, and products and comparisons of such vectors are per dimension. The batch has \(B\) samples. The output’s spatial extent is the vector \(\text{out}\), and \(\text{OUT} = \prod \text{out}\) and \(\text{IN}\) are the numbers of output and input positions. Without groups, output position \(o\) of filter \(c'\) is

\[y[b, c', o] \;=\; \sum_{c=0}^{C-1} \sum_{i \in [0, k)} W[c', c, i]\; x[b, c, \, o s - p + i d] \;+\; \text{bias}[c'],\]

where \(x\) reads zero outside its extent (the padding). With \(G = \partial L / \partial y\), the two gradients are

\[\begin{split}\frac{\partial L}{\partial W}[c', c, i] &= \sum_{b} \sum_{o} G[b, c', o]\; x[b, c, \, o s - p + i d], \\[4pt] \frac{\partial L}{\partial x}[b, c, h] &= \sum_{c'} \sum_{\substack{j \in [0, k)\\ o = (h + p - j d) / s \,\in\, \mathbb{Z}^{n_d} \cap [0,\, \text{out})}} W[c', c, j]\; G[b, c', o].\end{split}\]

The last one sums over every (filter, tap) pair whose output position \(o\) was computed from input position \(h\). With groups = \(g\) the channels split into \(g\) independent convolutions, each with \(C/g\) input and \(C_{\text{out}}/g\) output channels.

Each element of each result is one dot product, and the simulation rounds it as Dot-product arithmetic rounds a matrix product’s. For a SplitMac whose terms are \(a_1 b_1, \dots, a_K b_K\),

\[s_0 = 0, \qquad s_t = Q_{\text{acc}}\big(s_{t-1} + Q_{\text{mul}}(a_t b_t)\big), \qquad \text{result} = s_K,\]

and for a FusedMac \(s_t = Q_{\text{fma}}(a_t b_t + s_{t-1})\). KAHAN, BLOCK and TREE replace the recurrence with their own (Dot-product arithmetic, “Summing differently”). A rounded sum depends on its order, so the order is part of the definition. It is fixed per pass:

pass

terms \(t = 1 \dots K\), in order

\(K\)

forward

\((c, i)\), \(c\) outer, the kernel positions row-major (F.unfold’s order)

\(\tfrac{C}{g} \prod k\)

weight gradient

\((b, o)\), \(b\) outer, the output positions row-major

\(B \cdot \text{OUT}\)

input gradient

\((c', j)\), \(c'\) outer, the taps of the position’s class descending (below)

\(\tfrac{C_{\text{out}}}{g}\,\lvert T_r \rvert\)

Terms that read the padding are zeros, and they are part of the sum. In the forward pass a position at the border sums the same \(K\) terms as one in the middle, with some of them equal to \(W \cdot 0\).

Lowering to a matrix product, without the matrices#

The forward pass is a matrix product. Unfold the input into the matrix \(\tilde{X}_b\), with one row per \((c, i)\) and one column per output position:

\[\tilde{X}_b[(c, i),\, o] = x[b, c,\, o s - p + i d], \qquad y_b = W_{\text{flat}}\; \tilde{X}_b, \qquad W_{\text{flat}}[c', (c, i)] = W[c', c, i],\]

The forward pass is then the GEMM \([C_{\text{out}}, K] \times [K, \text{OUT}]\) per sample, in the rounded arithmetic of the mac and in exactly the order of the table above. The weight gradient is likewise \(G_{\text{flat}}\,\tilde{X}^{\top}\), with the samples stacked along \(K\).

The problem is size. \(\tilde{X}\) has \(\prod k\) entries for every entry of \(x\) (nine for a 3x3 kernel). F.unfold allocates that on every call, forward and backward.

conv_formats runs these GEMMs on the core routine of Quantized numerical kernels and leaves \(\tilde{X}\) implicit. For each step of its K-loop the kernel stages a \(16 \times 16\) tile of each operand. For a convolution, it computes the address in \(x\) (or \(G\), or \(W\)) of each tile element from the geometry and reads the element in place, or uses zero where the element is padding.

Splitting an index into coordinates (\(t \mapsto (c, i)\), or a column into the output position \(o\)) divides by the geometry’s extents. These are fixed for the call, so each division is a multiply-high and a shift by constants computed once on the host. Nothing is divided per element.

The arithmetic between the loads is the matrix product’s, unchanged. Every format, rounding mode and accumulate algorithm therefore applies, and each pass is bit for bit the GEMM over the explicit matrices. tests/test_qconv_gemm.py builds those matrices and checks this with torch.equal, stochastic rounding included. The difference is in memory: a pass allocates its result and nothing else.

Groups do not add launches. Group \(\gamma\) of sample \(b\) is element \(b g + \gamma\) of the kernel’s batch dimension, so every group of every sample is one batch element of a single launch. Each result lands in its own layout: for the forward pass, \([B, g, C_{\text{out}}/g, \text{OUT}]\) is \([B, C_{\text{out}}, \text{OUT}]\).

The input gradient, class by class#

With a strided convolution, the input gradient is the one pass that is not a plain GEMM. The textbook approach writes it as another convolution: insert \(s - 1\) zeros between the entries of \(G\), pad by \(d(k - 1) - p\), and correlate with the flipped kernel. That is again a GEMM, but a fraction \(1 - 1/\prod s\) of its terms are the inserted zeros (three quarters at stride 2 in two dimensions), and they cost as much as real terms.

The sum above tells which terms are real. Tap \(j\) reaches input position \(h\) when \(h + p - j d\) is a multiple of \(s\). That depends only on the residue \(r = (h + p) \bmod s\). The input positions therefore split into \(\prod s\) classes

\[H_r = \{\, h : (h + p) \bmod s = r \,\} = \{\, h_{0,r} + m s \,\}, \qquad T_r = \{\, j \in [0, k) : j d \equiv r \pmod{s} \,\},\]

and every position of a class takes the same taps. Per dimension, \(T_r\) is empty unless \(\gcd(d, s)\) divides \(r\); otherwise it holds every \(s / \gcd(d, s)\)-th tap. For \(h = h_{0,r} + m s\) and \(j \in T_r\) the output position is

\[o = m + \frac{h_{0,r} + p - j d}{s},\]

which is an exact division, done once per tap on the host. The input gradient is then one GEMM per class, \([C/g,\; (C_{\text{out}}/g)\,\lvert T_r \rvert] \times [(C_{\text{out}}/g)\,\lvert T_r \rvert,\; \lvert H_r \rvert]\), whose columns are scattered to their positions in \(\partial L / \partial x\).

Together the classes do \(1/\prod s\) of the zero-inserted work. At stride 2 in two dimensions, the four classes of a 3x3 kernel take 4, 2, 2 and 1 taps, nine in all, where the zero-inserted sum takes nine for every position. Some special cases:

  • At stride 1 there is one class, with every position and every tap. It is the transposed convolution itself.

  • A class with no taps (a 1x1 kernel at stride 2) is an empty sum, and its positions are zero.

Within a class the taps run in descending \(j\), the flipped kernel’s order. The remaining terms are therefore in the same order as in the zero-inserted sum.

Leaving the zeros out changes nothing where a zero term had no effect. Under a deterministic rounding, a plain running sum is not moved by a zero term: the product of a weight and an inserted zero rounds to zero, and \(Q(s_{t-1} + 0) = s_{t-1}\) because the running sum is already a value of the format. So for NAIVE and every rounding mode except SR, the result is bit for bit the zero-inserted one. (One exception: a weight of \(\pm\infty\) or NaN no longer turns the positions it does not reach into NaN, as \(\infty \cdot 0\) did.)

In the other cases a zero term was not free. Leaving it out changes the rounding, not the value being rounded:

  • Under SR each term uses a random draw, so the draws now fall on different terms.

  • KAHAN applies its pending compensation on every step, zero or not.

  • BLOCK and TREE count a block in terms. In the zero-inserted sum only one step in \(\prod s\) was a real term, so a block of block_size steps held about block_size \(/ \prod s\) terms, and more of the sum ran in the outer format. Now a block holds block_size terms, as in the other two passes and in a matrix product.

Compared with the float64 gradient, the class sum is as accurate as the zero-inserted one. It is within 0.5% under SR and KAHAN. For BLOCK and TREE it is 1-3.5% more accurate than a zero-inserted sum with the same number of terms per block (block_size times \(\prod s\)), which is the like-for-like comparison. tests/test_qconv_gemm.py checks both.

Stochastic rounding#

Under SR each element of a GEMM draws from its own stream, keyed on its index into the GEMM’s \([\text{batch}, M, N]\) result, and a GEMM takes one seed from the generator per call. A convolution pass follows the same rule, so its draws do not depend on the tiling, the thread count or the device’s launch geometry:

  • The forward pass and the weight gradient are one GEMM call each, keyed on the result’s own index.

  • The input gradient is one call per class, in row-major order of \(r\), keyed on the element’s index within its class.

torch.manual_seed therefore reproduces every pass.

Formats per element#

A palette mac (Dot-product arithmetic, “Palettes”) picks a format per element of each pass’s result. It does so through an integer map indexed like that result, with the spatial dimensions flattened:

  • \([C_{\text{out}}, \text{OUT}]\) for the forward pass;

  • \([C, \text{IN}]\) for the input gradient;

  • \([C_{\text{out}}, (C/g) \prod k]\) for the weight gradient.

Each can also be given as \([\cdot, 1]\) (one format per channel), as \([1, \cdot]\) (one per position), or with a leading batch dimension. The classes of the input gradient read the map at their own positions, so a map means the same thing whatever the stride.

What it costs#

The work of a pass is that of its GEMMs: \(B\,C_{\text{out}}\,\text{OUT}\,K\) multiply-adds for the forward pass, the same for the weight gradient, and \(1/\prod s\) of the zero-inserted count for the input gradient. Each runs at the rate of a matrix product in the same arithmetic; the address arithmetic in the tile loads adds a few percent. Memory use is the result.

Two shapes run below the kernel’s best speed:

  • A depthwise convolution (one channel per group) has \(M = 1\) and fills only one row of the \(16 \times 16\) tile.

  • A class of the input gradient with few positions or few taps is a small GEMM.

The kernels compute in the GEMM’s carrier: binary64 for a float64 model or a mac that names carrier=torch.float64, binary32 otherwise. An Apple GPU has no float64, so binary32 is its only carrier.