ggml_layer_permute: Add Permute Layer

View source: R/nn_layers.R

ggml_layer_permuteR Documentation

Add Permute Layer

Description

Reorder a sample's axes. The batch axis is not part of dims and is never moved – dims indexes the per-sample shape, exactly as input_shape does.

Usage

ggml_layer_permute(
  model,
  dims,
  input_shape = NULL,
  name = NULL,
  trainable = TRUE
)

Arguments

model

A ggml_sequential_model object, or a ggml_tensor_node for the functional API.

dims

Integer vector, a permutation of seq_along(input_shape). Must not include the batch axis.

input_shape

Input shape, required for the first layer only.

name

Optional character name for the layer.

trainable

Logical; reserved for API consistency (no weights).

Value

The model object with the permute layer appended (invisibly), or a ggml_tensor_node in the functional API.

Argument order

dims follows base R's aperm: dims[i] names the source axis that ends up at position i. So dims = c(2, 1) means "the new first axis is the old second one".

This is deliberately the inverse of the underlying ggml_permute, whose arguments are destination positions; the layer converts between the two so that callers can think in the R idiom. Read ggml_permute itself as "source axis i goes to position p_i" – mixing the two conventions up silently inverts every non-trivial permutation.

Typical use

Reordering a sample's axes to match what the next layer reads: a c(features, seq_len) tensor into the c(seq_len, features) that attention and the recurrent layers expect, or the reverse.

Note that ggml_layer_embedding does not need this – it already reports c(seq_len, dim) and feeds attention, GRU and LSTM directly. Adding a permute there transposes the underlying tensor and breaks the graph.

See Also

ggml_permute for the low-level op and its destination-position convention.

Examples


# Swap a c(features, seq_len) sample into c(seq_len, features).
model <- ggml_model_sequential() |>
  ggml_layer_permute(c(2, 1), input_shape = c(32, 16))


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