Skip to content
class

InterpolateNearestBackward2D

InterpolateNearestBackward2D

Autograd node for 2-D nearest-neighbour interpolation.

Resizes a 4-D tensor of shape (N, C, H_in, W_in) to (N, C, H_out, W_out) by copying, for each output pixel, the single nearest input pixel:

y[i,j]=x ⁣[clamp⁡(⌊i⋅Hin/Hout⌋),  clamp⁡(⌊j⋅Win/Wout⌋)] y[i, j] = x\!\left[\operatorname{clamp}(\lfloor i \cdot H_{\text{in}} / H_{\text{out}} \rfloor),\; \operatorname{clamp}(\lfloor j \cdot W_{\text{in}} / W_{\text{out}} \rfloor)\right]

(the floor mapping above is the exact rule the forward backend kernel uses). Because the map is many-to-one, the backward scatter-adds each output gradient onto its unique source pixel — an input pixel selected by k output pixels receives the sum of those k gradients.

Attributes

schema_v1OpSchema
"interpolate_nearest_2d", AmpPolicy::KeepInput (pure gather, no arithmetic — input dtype is preserved).
H_in_, W_in_int
Saved input spatial dimensions.
H_out_, W_out_int
Requested output spatial dimensions.
orig_shape_Shape
Full (N, C, H_in, W_in) shape for backward reconstruction.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & input, int H_out, int W_out)

Compute the nearest-neighbour resize with autograd wiring.

Parameters

inputTensorImplPtr
4-D input tensor of shape (N, C, H_in, W_in).
H_outint
Desired output height.
W_outint
Desired output width.

Returns

TensorImplPtr

Resampled tensor of shape (N, C, H_out, W_out).

Raises

ShapeMismatch
If input is not 4-D.

Methods

2
fn

apply

int apply(int grad_out)

Backward pass: scatter-adds grad_out onto the source pixels using the same floor coordinate mapping as the forward.

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

Graph-mode backward. Covers integer upscaling — the case a decoder actually uses — and refuses anything else by name; see the definition.