Skip to content
class

ContiguousBackward

ContiguousBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for materialising a C-contiguous copy of a tensor.

Forward calls Dispatcher::contiguous, which copies non-contiguous data into a freshly allocated, densely laid-out buffer. If the input is already contiguous the backend may return the same storage without performing a copy; the decision uses is_contiguous together with stride and offset metadata. Backward is essentially pass-through: because gradients are always dense, apply simply clones the gradient storage so upstream nodes receive a concrete, owning buffer with the input shape and dtype.

Math

y[i0,…,in−1]=x[i0,…,in−1]y_{[i_0, \ldots, i_{n-1}]} = x_{[i_0, \ldots, i_{n-1}]} (identity on elements; the operation differs only in physical layout).

Shape

out.shape == in.shape exactly; only the stride and offset metadata (and the underlying physical buffer) may change.

See Also

reshape_op — typically immediately follows contiguous_op.

Parameters

aTensorImplPtr
Input tensor with arbitrary strides, offset, and layout.

Attributes

schema_v1OpSchema
"contiguous", AmpPolicy::KeepInput, requires_grad=true.

Notes

Required as a fast-path normalisation before any op that assumes row-major storage (notably reshape_op and most MLX kernels). The backward pass clones the dense gradient because the original input could have had any layout — cloning guarantees the upstream gradient is a concrete buffer regardless of the forward path taken.

Static methods

1
static

forward

int forward(const int & a)

Run the forward pass and wire autograd in one step. Passes stride, storage_offset, and is_contiguous metadata to the backend dispatcher.

Methods

2
fn

apply

int apply(int grad_out)

Clone the dense gradient storage so that upstream nodes receive a concrete buffer regardless of the original input's layout.

fn

apply_for_graph

int apply_for_graph(const int & grad_out)

Graph-mode: the gradient passes through unchanged.

contiguous copies bytes into a dense layout without touching a value, so its derivative is the identity — and layout is not a property the graph carries, so nothing needs re-laying out on the way back. Ten symbols were blocked on this one missing override: clone, contiguous, float, half and the other dtype and layout conversions built over it.