Skip to content
class

UnfoldBackward

UnfoldBackward

Python wrappers

Public Python APIs implemented by this engine symbols.

Autograd node for the Unfold (im2col) operation.

Slides a kernel-sized window over each spatial dimension of the input and lays the resulting patches out as columns of a 3-D matrix:

y[b, c⋅∏K+k0⋅K1⋯+kN−1, o0⋅∏O′+⋯+oN−1]=x[b, c, s0o0+d0k0, …].y[b,\, c \cdot \prod K + k_0 \cdot K_1 \cdots + k_{N-1},\, o_0 \cdot \prod O' + \cdots + o_{N-1}] = x[b,\, c,\, s_0 o_0 + d_0 k_0,\, \ldots].

The output column count equals ∏iOi\prod_i O_i, the same value a matching convolution would produce. Supports N∈{1,2,3}N \in \{1, 2, 3\} inferred from kernel.size().

Attributes

schema_v1OpSchema
Registered op ("unfold", AmpPolicy::KeepInput).
kernel_vector<int>
Kernel sizes per spatial axis.
stride_vector<int>
Strides per spatial axis.
pad_vector<int>
Zero-padding per spatial axis.
dilation_vector<int>
Dilations per spatial axis.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & x, const int & kernel, const int & stride, const int & pad, const int & dilation)

Run the forward im2col extraction.

Parameters

xTensorImplPtr
Input of shape (B, C, S_0, ..., S_{N-1}) where N = kernel.size().
kernelvector<int>
Per-axis kernel size (length N).
stridevector<int>
Per-axis stride.
padvector<int>
Per-axis zero-padding.
dilationvector<int>
Per-axis dilation.

Returns

TensorImplPtr

Column matrix of shape (B, C * prod(K), prod(O)).

Raises

ShapeMismatch
If the input rank disagrees with N + 2 or any output extent is non-positive.

Methods

2
fn

apply

→vector<Storage>
int apply(int grad_out)

Backward — the fold (col2im) operation that scatters the patches back to spatial positions, summing overlaps.

Parameters

grad_outStorage
Upstream gradient shaped like the forward output.

Returns

vector<Storage>

Single-element vector containing dx with the original input shape.

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

Graph-mode backward: the adjoint is the other op of the pair (fold for unfold, unfold for fold), recorded so it can be differentiated again.