Learn R Programming

ggmlR (version 0.8.4)

ggml_layer_attention: Add a Multi-Head Attention Layer

Description

Scaled dot-product attention with n_heads heads, in the pipe-style form that appends a layer to a functional graph. For the reusable layer object that shares one set of weights between several applications -- how an encoder block is reused across a stack -- see ggml_attention.

Usage

ggml_layer_attention(
  x,
  d_model,
  n_heads = 1L,
  causal = FALSE,
  bias = TRUE,
  name = NULL,
  trainable = TRUE
)

Value

A new ggml_tensor_node of shape c(seq_len, d_model).

Arguments

x

A ggml_tensor_node of shape c(seq_len, d_model), or a list of two such nodes list(query, context) for cross-attention.

d_model

Integer, model width. Must be divisible by n_heads and match the parent's feature dimension.

n_heads

Integer, number of attention heads (default 1).

causal

Logical: mask out positions after the current one, making the layer autoregressive (default FALSE). Self-attention only.

bias

Logical: add a bias to the output projection (default TRUE).

name

Optional layer name.

trainable

Logical.

Shapes

x must be a sequence node of shape c(seq_len, d_model), the same layout ggml_layer_lstm takes. The output has that same shape, so attention blocks stack directly and a residual connection (ggml_layer_add(list(x, attn_out))) needs no reshaping.

How it is computed

All heads are computed in one batched pass rather than in a loop: the projections are reshaped to [d_head, n_heads, seq, batch] and permuted so that ggml_mul_mat() batches over the head and batch axes. The node count is therefore independent of n_heads.

Cross-attention

Pass a list of two nodes, ggml_layer_attention(list(query, context), ...), to take queries from the first and keys/values from the second. The two may differ in sequence length but must share d_model. A single node is self-attention.

See Also

ggml_attention for the shared-weights layer object.

Examples

Run this code
# \donttest{
# A transformer encoder block: attention + residual, then a feed-forward
# sublayer + residual.
x    <- ggml_input(shape = c(10L, 32L))
attn <- x |> ggml_layer_attention(d_model = 32L, n_heads = 4L)
h    <- ggml_layer_add(list(x, attn))
ff   <- h |> ggml_layer_dense(32L, activation = "relu")
out  <- ggml_layer_add(list(h, ff))

# GPT-style causal self-attention.
dec <- x |> ggml_layer_attention(32L, n_heads = 4L, causal = TRUE)
# }

Run the code above in your browser using DataLab