Skip to content
class

PermuteBackward

PermuteBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for an arbitrary axis permutation yip0,ip1,…=xi0,i1,…y_{i_{p_0}, i_{p_1}, \dots} = x_{i_0, i_1, \dots} with permutation pp.

The forward pass is a view — it does not copy data. The output shares the input's underlying Storage and only rewrites its shape and stride according to perm, paying an O(ndim)O(\text{ndim}) bookkeeping cost. Materialising the permuted layout into contiguous memory is the caller's responsibility (via a subsequent contiguous() call), which keeps simple .T followed by matmul free of redundant copies.

The backward is the same kind of operation in reverse: apply the inverse permutation to grad_out so the gradient is reshuffled back to the input's axis order.

Math

\qquad \frac{\partial L}{\partial x} = \mathrm{permute}\!\Bigl(\frac{\partial L}{\partial y}, p^{-1}\Bigr)$$ where $p^{-1}[p[i]] = i$ is the inverse permutation. **Shape** Output shape is `[input.shape[perm[0]], input.shape[perm[1]], …]`. `ndim` is preserved.

Attributes

schema_v1OpSchema
"permute", AmpPolicy::KeepInput — pure metadata operation, no promotion required.
perm_std::vector<int>
Normalised (non-negative) forward permutation saved by forward; used to derive the inverse permutation in apply.

Raises

ValueError
If perm is not a permutation of 0..ndim-1 or its length differs from ndim.
IndexError
If any element of perm is outside [-ndim, ndim).

Notes

Composability: a permute followed by another permute does not materialise the intermediate layout; both forward passes rewrite metadata only, so chains like a.T.contiguous() pay exactly one data copy regardless of how many transposes precede the contiguous call.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & a, const int & perm)

Normalise perm_user, build the output shape / stride, dispatch the (view-only) forward permute through the backend, and wire the backward node.

Parameters

aTensorImplPtr
Input tensor.
permstd::vector<int>
Permutation of 0..ndim-1. Negative indices are wrapped to axis + ndim before validation.

Returns

TensorImplPtr

Output tensor sharing storage with a but with permuted shape and stride.

Raises

ValueError
If perm is not a valid permutation of 0..ndim-1.
IndexError
If any element of perm falls outside [-ndim, ndim).

Methods

3
fn

apply

int apply(int grad_out)

Eager-mode backward: ∂L/∂x=permute(∂L/∂y,p−1)\partial L/\partial x = \mathrm{permute}(\partial L/\partial y, p^{-1}). out_shape_ is the permuted output shape, which becomes the input shape of the inverse permute call.

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

Graph-mode backward used when create_graph=True. Applies the inverse permutation through permute_op so the result remains tracked in the autograd graph for second-order differentiation.

fnconst

node_name

int node_name()

Node-name override used by the autograd graph dumper.