nn_build_permute_op: Build permute forward pass

View source: R/nn_layers.R

nn_build_permute_opR Documentation

Build permute forward pass

Description

Shared by the sequential and functional builds.

Usage

nn_build_permute_op(ctx, input_tensor, dims, input_shape)

Arguments

ctx

Compute context.

input_tensor

Input ggml_tensor.

dims

Permutation in ‘aperm' (source) order, over the sample’s axes.

input_shape

Per-sample input shape, used only to count the axes.

Details

The R per-sample shape maps onto ggml axes in order – R axis i is ne[i - 1] – with the batch on the axis just past the sample's, so a permutation of the sample's axes leaves the batch where it is.

Two conversions happen here, and both are easy to get backwards:

  1. 1-based R axes become 0-based ggml axes.

  2. dims arrives in aperm (source) order, while ggml_permute() takes destination positions: the argument at position i says where source axis i lands. Inverting the permutation is what bridges the two. A self-inverse permutation (a plain two-axis swap, the common case) is unchanged by this step, which is precisely why an error here can go unnoticed – see the test on a 3-D non-symmetric permutation.

The result is passed through ggml_cont(): ggml_permute() returns a non-contiguous view, and downstream ops (matmul, reshape) require contiguous data.

Value

A contiguous ggml_tensor with the axes reordered.


ggmlR documentation built on Oct. 23, 2026, 1:07 a.m.