Skip to content
class

AlignedConv

AlignedConv

Convolution operands with their channels aligned, so MLX never pads them.

MLX pads unaligned channels up to a multiple of 16 inside the Metal convolution — 2-D from 0.32.1 (pad_in_channels_conv_2D_gpu), 3-D in every release (pad_and_slice_conv_3D_gpu) — and fills the padding from a zero scalar it does not keep alive: the kernel reads it on the GPU after the function that made it has freed it. When the allocator hands that buffer to another array first — any scalar written while the work is still queued, which async_eval makes routine — the padded channels take its value instead of zero and the convolution comes out wrong, whole layers off and some NaN (a ResNet on MNIST: 4 of 16 Metal training forwards on MLX 0.32.1). Padding here, as a graph op, gives the same zero channels from buffers MLX tracks, and MLX then runs the kernel it padded for. Both operands get the same extra channels, all zero, so every added product is zero and the result is exact.

The conditions mirror MLX's own: groups == 1 and no input dilation, then for 2-D a stride-1 kernel of area >= 9 whose input channels are above 4 and unaligned while its output channels are aligned; for 3-D any unaligned channels, output channels included (sliced off after).