Skip to content
fn

contiguous_stride

→Stride
int contiguous_stride(const int & shape, int elem_size)

Computes the row-major (C-contiguous) byte-stride vector for a tensor with the given shape and element size.

The stride is built back-to-front: the innermost (last) axis has stride elem_size, and each outer axis's stride is the product of the inner stride and the inner dimension size. This matches NumPy's default 'C' order and is the layout expected by Apple Accelerate BLAS / vDSP routines.

Math

stride[i]=elem_size⋅∏j>ishape[j]\text{stride}[i] = \text{elem\_size} \cdot \prod_{j > i} \text{shape}[j]

See Also

TensorMeta::is_contiguous_for Reverse check — verifies a stored stride against the canonical one.

Parameters

shapeShape
A concrete shape. Empty shape yields an empty stride.
elem_sizestd::size_t
Element size in bytes — typically obtained from dtype_size.

Returns

Stride

Vector of the same length as shape. When the result is fed back into TensorMeta, TensorImpl::is_contiguous returns true.

Raises

InvalidArgument
A dimension is negative (throw_negative_dim). The tensor constructor derives its stride here, so no tensor is built with one.

Examples

::
    contiguous_stride({2, 3, 4}, 4)    // {48, 16, 4}  (float32)
    contiguous_stride({}, 4)           // {}           (scalar)