liveBook · Manning

Zero padding

As explained in Grokking GPU Programming

Zero padding extends an input with zero-valued elements beyond its boundary.

When a stencil reads beyond an array’s edge, zero padding treats the missing value as 0—as if the data sat inside a black border. This boundary choice affects the result at the edges. section 9.8 section 9.6

Unlike skipping edge outputs, zero padding keeps those positions and supplies zero for missing neighbors. With stride 1, CNN “same padding” keeps the output dimensions equal to the input dimensions. section 9.8 section 9.6

A partial matrix tile

If a matrix dimension is not a multiple of the tile size, threads loading beyond the matrix bounds place 0.0 in the tile. These zeros do not change the dot product, so the tiled calculation remains correct. section 12.5

Figure 9.10 Zero padding in 2D. The highlighted rectangle marks the actual input grid. Every position outside it (top, bottom, left, right, and the four corner regions) reads as 0, as if the grid were embedded in a sea of zeros.

The diagram shows the 2D input grid surrounded by positions that read as zero, including its corners. section 9.8

How Grokking GPU Programming covers it

How deep it goes
Explains 1D and 2D boundary behavior and shows zero-filled tile loads, without detailing lower-level hardware behavior.
Examples
Uses a black border around image data, partial matrix tiles, and PyTorch conv2d with padding=1.
What you can do after
Apply zero-valued boundary reads in stencil kernels and use padding=1 for same-sized stride-1 convolution.
How it connects
Connects stencil boundary conditions with CNN convolution and tiled matrix multiplication.

Also explained in

Compare how 3 books explain zero padding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking GPU Programming. The section links point to the parts of the book it draws on.