View source: R/nn_functional.R
| ggml_layer_attention | R Documentation |
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.
ggml_layer_attention(
x,
d_model,
n_heads = 1L,
causal = FALSE,
context = NULL,
mask = NULL,
rope = FALSE,
rope_base = 10000,
dropout = 0,
bias = TRUE,
name = NULL,
trainable = TRUE
)
x |
A |
d_model |
Integer, model width. Must be divisible by |
n_heads |
Integer, number of attention heads (default 1). |
causal |
Logical: mask out positions after the current one, making the
layer autoregressive (default |
context |
Optional |
mask |
Optional |
rope |
Logical: apply rotary position embedding to the queries and keys
(default |
rope_base |
Rotation base for |
dropout |
Dropout rate applied to the attention weights during training (default 0, no dropout). used by LLaMA-family models). Larger values stretch the rotation over longer contexts. |
bias |
Logical: add a bias to the output projection (default
|
name |
Optional layer name. |
trainable |
Logical. |
A new ggml_tensor_node of shape c(seq_len, d_model).
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.
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.
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.
ggml_attention for the shared-weights layer object.
# 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)
# Padding mask, fed as a second input alongside the sequence.
pad <- ggml_input(shape = c(10L, 10L), name = "mask")
enc <- x |> ggml_layer_attention(32L, n_heads = 4L, mask = pad)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.