Skip to content
class

MaxPoolNdBackward

MaxPoolNdBackward

Python wrappers

Public Python APIs implemented by this engine symbols.

Autograd node for N-dimensional max pooling.

Each output element is the maximum of a K0×⋯×KN−1K_0 \times \cdots \times K_{N-1} window of the (optionally zero-padded) input: y[b,c,o]=max⁡0≤k<Kx[b,c,s o+k]y[b, c, o] = \max_{0 \le k < K} x[b, c, s\,o + k] (extended to NN axes). The forward pass also produces a tensor of argmax plane-local indices into the unpadded input (size prod(S)), with a negative entry marking a window that saw only padding; these are saved as saved_argmax_ and consumed by the backward pass to route each gradient back to exactly one input element.

Attributes

schema_v1OpSchema
Registered op ("max_pool1d" / "max_pool2d" / "max_pool3d", AmpPolicy::KeepInput).
K_int[N]
Pooling window size per spatial axis.
stride_int[N]
Per-axis stride (already resolved from the 0 sentinel during the forward pass).
pad_int[N]
Per-axis zero-padding.
saved_argmax_Storage
Flat indices of the per-window winners; same logical shape as the output.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & x, const int (&)[N] K, const int (&)[N] stride, const int (&)[N] pad, bool ceil_mode)

Forward — pool each window down to its maximum element.

Parameters

xTensorImplPtr
Input of shape (B, C, S_0, ..., S_{N-1}).
Karray<int, N>
Per-axis window size.
stridearray<int, N>
Per-axis stride. stride[i] == 0 defaults to K[i] (non-overlapping windows).
padarray<int, N>
Per-axis zero-padding amount.

Returns

TensorImplPtr

Output of shape (B, C, O_0, ..., O_{N-1}) with each Oi=⌊(Si+2pi−Ki)/si+1⌋O_i = \lfloor (S_i + 2 p_i - K_i)/s_i + 1 \rfloor.

Raises

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

Methods

3
fn

apply

→vector<Storage>
int apply(int grad_out)

Backward — scatter grad_out into the input positions recorded in saved_argmax_. Elements that did not win any window receive zero gradient.

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 same scatter, written in ops.

fn

release_own_saved

void release_own_saved()