Skip to content
fn

contiguous_op

→TensorImplPtr
int contiguous_op(const int & a)

Python wrappers

Public Python APIs implemented by this engine symbol.

Return a dense, row-major copy of a whenever its strides diverge from the canonical contiguous layout; return a unchanged when it is already contiguous.

This is the public entry point every CRTP kernel forward() calls on CPU inputs before handing storage to a backend compute routine. Slices and transposes produce non-unit strides without copying data, but the Accelerate (CPU) kernels assume stride-1 row-major access, so materialising a contiguous copy is required to avoid garbage reads.

See Also

ContiguousBackward — the underlying op implementation. UnaryKernel, BinaryKernel, ReduceKernel — CRTP bases that invoke this helper during forward.

Parameters

aTensorImplPtr
Source tensor. Must be non-null. May live on either device.

Returns

TensorImplPtr

Either a itself (when a->is_contiguous() is true) or a freshly allocated TensorImpl with the same shape, dtype, and device but a contiguous storage whose elements are copied from a honouring its strides and storage_offset.

Raises

LucidError
If a is null.

Notes

Autograd. Implemented via ContiguousBackward so the operation participates in the autograd graph; the backward is the identity on the incoming gradient (a clone to give the upstream node an owning buffer). See ops/utils/Contiguous.cpp.

GPU path. On Device::GPU the underlying MLX backend handles non-contiguous storage natively via lazy evaluation, so the dispatched contiguous is typically a cheap pass-through. Kernel forward() helpers therefore guard the call with a device() == CPU && !is_contiguous() predicate.

Aliasing. The returned tensor never aliases a's storage when a copy was performed; consumers may freely mutate it without violating the version counter on a.