L2 Intermediate · Module 5: Parallel patterns
5.6Convolution and stencils
In a convolution or stencil, each output reads a small neighbourhood of inputs, so neighbouring outputs share most of their reads. This chapter turns that reuse into speed with constant memory and shared-memory tiles with halos, measures when it matters (only once the filter is large enough to make the kernel compute-bound), and connects it to the short causal convolutions in modern LLM architectures.
By the end you will be able to
- ✓Compute the arithmetic intensity of a K×K convolution, and predict when it becomes compute-bound
- ✓Use __constant__ memory for weights that every thread reads at the same time
- ✓Load a tile plus its halo into shared memory, and count the extra loads the halo costs
- ✓Recognize convolutions in LLM-adjacent models, and when they are done as GEMMs instead
A stencil computes each output from a fixed neighbourhood of inputs around the same position: a 3×3 blur, a 5-point Laplacian, a time-step of a heat equation. A convolution is a stencil with learned weights:
Each output reads inputs. The output next to it reads almost the same inputs, shifted by one. That overlap is the whole optimization story: read each input once, use it times.
Arithmetic intensity
Each output does multiply-adds, which is FLOPs. If every input is read once and every output written once, that is 8 bytes of traffic per output (FP32):
| K | FLOP/byte | On this GPU (ridge ≈ 40 FLOP/byte) |
|---|---|---|
| 3 | 2.25 | memory-bound |
| 7 | 12 | memory-bound in principle, if reuse is perfect |
| 11 | 30 | approaching the ridge point |
Placing these on the roofline (3.3) predicts the lab before you run it. At nothing but bandwidth matters. At large , how fast the inner loop issues instructions starts to matter.
Three kernels
Naive. One thread per output, weights in global memory, inputs read straight from global memory. The L1 and L2 caches catch much of the reuse by themselves.
Constant memory. Every thread in a warp reads the same weight w[i][j] at the same moment. __constant__ memory (64 KB, with a
small cache on each SM) is built for this: one read is broadcast to all 32 lanes. With the loop unrolled, the compiler can even use the
constant as an operand of the FMA instruction directly.
__constant__ float c_filter[11 * 11];cudaMemcpyToSymbol(c_filter, host_filter, K * K * sizeof(float)); // host side, before the launchShared-memory tile with a halo. A block computing a 32 × 32 output tile needs a input tile: its own pixels plus a halo of on each side. The block loads that once, cooperatively, then every thread computes from shared memory.
// labs/m05/5.6/convolution.cu (32 x 8 threads, 4 output rows each)constexpr int T = 32, R = K / 2, S = T + K - 1;__shared__ float tile[S][S + 1];for (int j = tid; j < S * S; j += 256) { // load tile + halo, zero outside the image int sy = j / S, sx = j % S, ix = x0 + sx - R, iy = y0 + sy - R; tile[sy][sx] = (ix >= 0 && ix < W && iy >= 0 && iy < H) ? in[iy * W + ix] : 0.0f;}__syncthreads();for (int ty = threadIdx.y; ty < T; ty += 8) { float acc = 0.0f; for (int dy = 0; dy < K; dy++) for (int dx = 0; dx < K; dx++) acc += c_filter[dy * K + dx] * tile[ty + dy][tx + dx]; out[(y0 + ty) * W + x0 + tx] = acc;}The halo isn’t free. For the block loads values to produce outputs, which is 72% extra. Larger tiles make the halo relatively cheaper but use more shared memory.
Measured
2D convolution, 4096 x 4096 floats (67 MB in, 67 MB out): K naive constant tiled 3x3 1.104 ms 274 GFLOP/s 1.116 ms 271 GFLOP/s 1.086 ms 278 GFLOP/s 7x7 3.658 ms 450 GFLOP/s 2.048 ms 803 GFLOP/s 2.221 ms 740 GFLOP/s 11x11 9.035 ms 449 GFLOP/s 5.331 ms 762 GFLOP/s 4.776 ms 850 GFLOP/s cudaMemcpy of the image: 1.096 ms (the floor for any K)- 3×3: all three run at the speed of a plain copy. The caches already provide the reuse, and nothing else matters, exactly as the intensity predicted.
- 7×7 and 11×11: the naive kernel stalls at 450 GFLOP/s. Moving the weights to constant memory gives 1.8×: the weight reads were the bottleneck. Tiling adds a further 12% at 11×11, and is slightly slower at 7×7, where the halo and the barrier cost more than they save.
- Still far from the roof. 850 GFLOP/s is 10–15% of this GPU’s FP32 peak (6–8 TFLOP/s, depending on the clock it sustains). The inner loop issues one shared-memory load for each FMA, and an SM can issue far fewer loads per cycle than FMAs. The fix is to have each thread compute several outputs and reuse each loaded input from a register: register tiling (6.2), the same step that makes matmul fast.
Convolutions near LLMs
Transformers rarely use convolutions, but they appear around them:
-
Short causal convolutions. Mamba-style state-space layers and several hybrid models apply a depthwise, causal conv1d with a tiny kernel (often ) along the time axis, one filter per channel. Each output reads only its own channel’s last steps, so the intensity is about 1 FLOP/byte: purely memory-bound. During decode it shrinks to a rolling buffer of the last inputs per channel, a small state much like a tiny KV cache.
output Causal depthwise conv1d, 8192 steps x 2048 channels, K = 4: 1.085 ms, 123.7 GB/s PASSThe naive kernel already runs at copy speed: the caches provide the reuse.
-
Vision and audio front ends. A ViT patch embedding is a convolution with stride equal to its kernel size, and Whisper’s audio encoder starts with two 1D convolutions. Large convolutions like these are usually lowered to a GEMM (im2col: copy each input window into a column, then do one big matmul), so they run on the tensor-core kernels of Module 6.
Lab: convolution three ways 🔴
cd labs/m05/5.6 && nvcc -O2 -arch=native convolution.cu -o convolution && ./convolutionSources
Check your understanding
0 / 4 answered1.What is the ideal arithmetic intensity of a 5×5 FP32 convolution (read each input once, write each output once)?
2.Why does __constant__ memory suit convolution weights?
3.A block computes a 32×32 output tile with a 7×7 filter. How large an input tile must it load?
4.For the 3×3 convolution, all three kernels ran at the speed of cudaMemcpy. Why?