Skip to content
fn

permute_copy

void permute_copy(const int * in, int * out, const int & in_shape, const int & perm, int elem)

Copies a tensor of elem-byte elements into a permuted, densely-packed output buffer using the supplied axis permutation.

The output is the input viewed through its C-order strides reordered by perm, packed by strided::pack — axes merged, contiguous runs moved by memcpy, a merged 2-D transpose copied in tiles. This is an out-of-place layout transform — there is no in-place fast path.

Permutation moves bytes and never reads a value, so one entry point keyed by element width serves every dtype. It used to be one function per element type, and the backend's switch over them named float16 but not bfloat16 and had no 8- or 16-byte case for complex, so the backward of a transpose refused those dtypes on the CPU while Metal ran them.

Shape

out_shape[d] == in_shape[perm[d]] for every d.

Parameters

inconst std::byte*
Source buffer laid out densely (C-order) according to in_shape.
outstd::byte*
Destination buffer. Caller must pre-allocate numel(in_shape) * elem bytes. May not alias in.
in_shapeconst std::vector<int64_t>&
Shape of the source tensor. An empty vector is treated as a 0-D scalar and produces a single-element copy.
permconst std::vector<int>&
Axis permutation of length in_shape.size(). perm[d] names the source axis whose extent becomes output axis d. Must be a valid permutation of [0, ndim).
elemstd::size_t
Element width in bytes (dtype_size).

Notes

Cost is one pass over the bytes plus one odometer step per run, not per element; a permutation that keeps the last axis in place moves whole rows. Single-threaded.

Examples

``perm = {2, 0, 1}`` over ``in_shape = {H, W, C}`` yields a CHW output.
``perm = {0, 2, 3, 1}`` over ``in_shape = {N, C, H, W}`` performs an NHWC
re-layout.