Turning Conv2D Into A Practical GPU Compute Pattern
A reusable optimization mindset for engineers building GPU compute kernels.
Convolution is one of the foundational operations in modern machine learning, but it is also a useful example of a broader engineering idea: many apparently specialized tensor operations can be reorganized into well-understood compute patterns that are easier to implement, optimize, and scale.
This article explains a practical way to implement 2D convolution by treating it as a structured form of matrix multiplication. The goal is not to promote a specific product or benchmark result, but to show a reusable optimization mindset for engineers building GPU compute kernels.
Key idea: A 1×1 convolution is effectively a matrix multiply, and larger K×K convolutions can be expressed as a sequence of matrix-like accumulations with careful attention to cache behavior and loop ordering.
Conv2D is everywhere in inference pipelines, image models, and feature extraction stages. Yet the implementation challenge is often less about the mathematical definition and more about the execution strategy: how data is laid out, how work is grouped, and how memory traffic is controlled.
A useful approach is to avoid treating convolution as a completely unique primitive. Instead, interpret it as a compute pattern closely related to matrix multiplication. This can reduce implementation complexity and create a clearer path for optimization.
A standard Conv2D operation can be described as applying a learned filter tensor across an input tensor to produce an output tensor.
Where: A is the input tensor of size N × H × W × C_in B is the weight tensor of size H_filter × W_filter × C_in × C_out C is the output tensor of size N × H_out × W_out × C_out
In practice, one of the most important simplifications comes from looking at the data in a way that matches the compute engine rather than the original mathematical notation.
A 1×1 convolution is the cleanest place to see the matrix multiplication analogy. At each spatial position, the input contributes a feature vector of length C_in. The weights map that vector into an output vector of length C_out.
If the input tensor is already stored in NHWC order, each spatial feature vector is naturally arranged in a way that can be interpreted as rows of a matrix, without requiring explicit data rearrangement.
Practical takeaway: For NHWC input, a 1×1 convolution can be treated directly as a matrix multiply from C_in to C_out, producing output in NHWC order with no transposition step.
This matters because unnecessary data movement is often one of the fastest ways to lose performance. When the original layout already matches the desired access pattern, the implementation can stay simple and efficient.
Once the filter grows beyond 1×1, each output element depends on multiple spatial locations. A 3×3 convolution, for example, combines nine separate spatial contributions. Conceptually, this becomes a set of matrix-like products that are accumulated together.
That means the problem is no longer just “do matrix multiplication,” but “do repeated matrix-like accumulation while minimizing memory waste.”
For a 3×3 filter, there are nine C_in × C_out contributions per output location. A naive implementation can achieve good arithmetic utilization, but it may repeatedly flush and reload input data through the cache hierarchy. That is where execution order becomes critical.
One of the most useful lessons in convolution optimization is that equivalent mathematics can produce very different memory behavior.
A straightforward ordering may process each filter position as if it were a separate matrix multiply and accumulate the result. While this is simple, it can cause the same input region to be fetched repeatedly into L1 cache and potentially L2 as well.
Reordering loops can improve locality, reduce refetch traffic, and increase effective cache reuse. In other words, the arithmetic stays the same, but the memory system sees a much friendlier pattern.
Optimization principle: When a kernel is already compute-valid, one of the highest-value changes is often to restructure loop nesting to improve data reuse before attempting low-level instruction tuning.
The following sample illustrates the core structure of the convolution loop nest. The details are implementation-specific, but the pattern is broadly recognizable: load weights, compute input coordinates, read vectorized input values, synchronize local memory, and accumulate partial products.
This kind of kernel is useful to study because it highlights where performance usually lives:
Real convolution kernels are shaped not only by filter math, but also by parameters such as padding, stride, and dilation. These affect both correctness and performance.
Zero padding is the most common approach and is the one reflected in the source implementation. A typical boundary-safe pattern checks whether the computed input coordinate falls within valid image bounds before issuing the read.
This is correct and