View source: R/nn_functional.R
| ggml_attention | R Documentation |
Returns a reusable layer object for use with ggml_apply, the
scaled dot-product attention of Attention Is All You Need with
n_heads heads computed in parallel. Applying the same object to
several tensor nodes shares one set of projection weights, which is how an
encoder block is reused across a stack.
ggml_attention(
d_model,
n_heads = 1L,
causal = FALSE,
bias = TRUE,
rope = FALSE,
rope_base = 10000,
dropout = 0,
name = NULL,
trainable = TRUE
)
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, so a query
attends only to keys at or before it (default |
bias |
Logical: add a bias to the output projection (default
|
rope |
Logical: apply rotary position embedding to queries and keys
(default |
rope_base |
Rotation base for |
dropout |
Dropout rate applied to the attention weights during training (default 0, no dropout). |
name |
Optional character name. |
trainable |
Logical; whether weights are updated during training. |
For the pipe-style form that appends attention to a graph
(x |> ggml_layer_attention(...)), see
ggml_layer_attention.
A ggml_layer object.
The parent must be a sequence node of shape c(seq_len, d_model) –
the same layout ggml_layer_lstm takes – and the output has
that same shape, so attention blocks stack directly.
Applied to a single node the layer is self-attention: queries, keys and
values all come from that node. Applied to a list of two nodes,
ggml_apply(list(query, context), attn), it is cross-attention:
queries come from the first, keys and values from the second, which may
have a different sequence length (but must share d_model). This is
the decoder-to-encoder path of a full transformer.
Four projections W_q, W_k, W_v, W_o, each
d_model x d_model, plus an output bias b_o when
bias = TRUE. Glorot-uniform initialised, zero bias.
ggml_layer_attention, ggml_apply
# One attention block shared between two sequences.
attn <- ggml_attention(d_model = 32L, n_heads = 4L)
x1 <- ggml_input(shape = c(10L, 32L))
x2 <- ggml_input(shape = c(10L, 32L))
o1 <- ggml_apply(x1, attn)
o2 <- ggml_apply(x2, attn) # shared weights
stopifnot(identical(o1$layer_id, o2$layer_id))
# Cross-attention: queries from x1, keys/values from a longer context.
ctx <- ggml_input(shape = c(15L, 32L))
o3 <- ggml_apply(list(x1, ctx), ggml_attention(32L, 4L))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.