ggml_layer_custom: Define a Custom Layer From an R Function

View source: R/nn_functional.R

ggml_layer_customR Documentation

Define a Custom Layer From an R Function

Description

Turns an arbitrary composition of ggml operations into a reusable functional layer. ggml_layer_custom() does not build anything itself — it returns a layer function that behaves like any other ggml_layer_*() and can be piped into a model.

Usage

ggml_layer_custom(forward, name = NULL, output_shape = NULL)

Arguments

forward

A function of (ctx, x) returning a ggml tensor, where ctx is the compute context and x the parent tensor. All ggml operations take ctx as their first argument.

name

Optional character name for the layer. Used as the base for auto-generated node names.

output_shape

Output shape excluding the batch dimension. Defaults to NULL, meaning the layer preserves its input shape (correct for element-wise layers such as custom activations). Supply an integer vector when forward changes the shape, or a function of the input shape.

Details

The forward function is called once while the compute graph is being built, with the active ggml_context and the parent tensor. Because the returned tensor becomes an ordinary graph node, gradients flow through a custom layer automatically — as long as every operation used inside forward implements a backward pass.

Value

A layer function of (x, name = NULL) that appends a custom node to the graph and returns the new ggml_tensor_node.

Weights

Custom layers are stateless: forward maps activations to activations and owns no trainable parameters. Put learnable weights in a neighbouring ggml_layer_dense(), or use the autograd API (ag_*) when a layer needs its own parameters.

Examples


# Mish activation: x * tanh(softplus(x))
layer_mish <- ggml_layer_custom(
  name    = "mish",
  forward = function(ctx, x) ggml_mul(ctx, x, ggml_tanh(ctx, ggml_softplus(ctx, x)))
)

inp <- ggml_input(shape = 8L)
out <- inp |>
  ggml_layer_dense(16L) |>
  layer_mish() |>
  ggml_layer_dense(2L, activation = "softmax")
model <- ggml_model(inputs = inp, outputs = out)


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