Skip to content
fn

im2col_f32

void im2col_f32(const float * x, float * cols, int C, int H, int W, int KH, int KW, int OH, int OW, int stride_h, int stride_w, int pad_h, int pad_w, int dilation_h, int dilation_w)

Single-precision 2-D im2col unfold.

Unfolds the 2-D feature map xx of shape (C,H,W)(C, H, W) into a matrix where each column is a KH×KWK_H \times K_W receptive-field patch flattened across channels. The output column matrix has shape (C⋅KH⋅KW,OH⋅OW)(C \cdot K_H \cdot K_W, O_H \cdot O_W) so that conv2d = sgemm(W_reshape, cols).

Out-of-bounds positions (those reaching outside the input due to padding, stride, or dilation) are written as zero.

Math

For each output position (oh,ow)(o_h, o_w), kernel offset (kh,kw)(k_h, k_w) and channel cc, the corresponding input position is ih=oh⋅sh+kh⋅dh−ph,iw=ow⋅sw+kw⋅dw−pwi_h = o_h \cdot s_h + k_h \cdot d_h - p_h, \quad i_w = o_w \cdot s_w + k_w \cdot d_w - p_w and cols[c \cdot K_H K_W + k_h K_W + k_w, o_h \cdot O_W + o_w] = x[c, i_h, i_w] (zero if OOB).

Parameters

xconst float*
Input tensor, shape (C,H,W)(C, H, W) row-major.
colsfloat*
Output column matrix, shape (C⋅KH⋅KW,OH⋅OW)(C \cdot K_H \cdot K_W, O_H \cdot O_W).
Cint
Channel count.
H, Wint
Input spatial dimensions.
KH, KWint
Kernel height and width.
OH, OWint
Output spatial dimensions.
stride_h, stride_wint
Per-axis stride.
pad_h, pad_wint
Per-axis zero-padding.
dilation_h, dilation_wint
Per-axis dilation.

Notes

Memory layout is NCHW (channel-first) — the batch dimension is iterated at the caller, with one im2col per batch element.