Skip to content
class

AffineGridBackward

AffineGridBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for affine grid generation.

Builds an (N, H, W, 2) sampling grid by applying the per-sample affine matrix θn∈R2×3\theta_n \in \mathbb{R}^{2 \times 3} to a regular H×WH \times W output lattice:

(xsys)=θn(xtyt1) \begin{pmatrix} x_s \\ y_s \end{pmatrix} = \theta_n \begin{pmatrix} x_t \\ y_t \\ 1 \end{pmatrix}

where (xt,yt)(x_t, y_t) are the normalised output coordinates in [−1,1][-1, 1] and (xs,ys)(x_s, y_s) are the corresponding source coordinates that will be passed to grid_sample. The backward propagates grad_out (shape (N, H, W, 2)) back into theta by summing the outer products with the homogeneous lattice coordinates.

Math

Lattice generation depends on align_corners_:

xt={−1+2j/(W−1)align_corners−1+(2j+1)/Wotherwise,yt={−1+2i/(H−1)align_corners−1+(2i+1)/Hotherwise x_t = \begin{cases} -1 + 2 j / (W - 1) & \text{align\_corners} \\ -1 + (2 j + 1) / W & \text{otherwise} \end{cases}, \quad y_t = \begin{cases} -1 + 2 i / (H - 1) & \text{align\_corners} \\ -1 + (2 i + 1) / H & \text{otherwise} \end{cases}

Attributes

schema_v1OpSchema
"affine_grid", AmpPolicy::Promote.
align_corners_bool
When true, the four corners of the output lattice map exactly to [-1, +1] on each axis; when false, the lattice is placed on pixel centres.
N_, H_, W_int
Batch size and output spatial dimensions.
orig_theta_shape_Shape
Original (N, 2, 3) shape used to materialise the gradient.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & theta, int N, int H, int W, bool align_corners)

Generate the sampling grid with autograd wiring.

Parameters

thetaTensorImplPtr
Affine matrices of shape (N, 2, 3).
Nint
Batch size (must equal theta.shape()[0]).
Hint
Output grid height.
Wint
Output grid width.
align_cornersbool
Lattice placement mode.

Returns

TensorImplPtr

Sampling grid of shape (N, H, W, 2) with last-axis ordering (x, y) (i.e. [..., 0] is the x-coordinate).

Methods

2
fn

apply

int apply(int grad_out)

Backward pass: gradient w.r.t. the affine matrices theta.

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

The same contraction, recorded: dtheta = sum over the grid of g^T [x, y, 1].