View source: R/nn_functional.R
| ggml_layer_transformer_block | R Documentation |
The standard pre-LN encoder block, in one call: normalize, attend, add the residual, normalize again, run the position-wise feed-forward, add the second residual. Composed from the existing layers – there is no new node type – so the pieces can still be wired by hand when a block needs to differ.
ggml_layer_transformer_block(
x,
d_model,
n_heads = 1L,
ff_dim = NULL,
activation = "silu",
norm = c("rms", "layer"),
causal = FALSE,
mask = NULL,
rope = FALSE,
dropout = 0,
attn_dropout = dropout,
name = NULL
)
x |
A |
d_model |
Integer, model width. Must match the input's feature
dimension and be divisible by |
n_heads |
Integer, number of attention heads (default 1). |
ff_dim |
Integer, width of the feed-forward hidden layer. Defaults to
|
activation |
Feed-forward activation (default |
norm |
|
causal |
Logical: causal self-attention (default |
mask |
Optional attention mask node, |
rope |
Logical: rotary position embedding on queries and keys
(default |
dropout |
Dropout rate applied to each sublayer's output before it is added back to the residual (default 0, meaning no dropout). |
attn_dropout |
Dropout rate for the attention weights specifically;
defaults to |
name |
Optional prefix for the layers inside the block. |
A new ggml_tensor_node of shape c(seq_len, d_model),
so blocks stack directly.
Normalization comes BEFORE each sublayer, not after, which is what makes deep stacks trainable without a warmup schedule: the residual path stays un-normalized from input to output, so the gradient reaches the early layers undiminished. The original 2017 transformer normalized after; every large model since does it this way.
ggml_layer_attention,
ggml_layer_sequence_pooling
# A two-block encoder with a pooled regression head.
x <- ggml_input(shape = c(10L, 32L))
h <- x |> ggml_layer_positional_embedding()
for (i in 1:2) h <- h |> ggml_layer_transformer_block(32L, n_heads = 4L)
y <- h |> ggml_layer_sequence_pooling() |> ggml_layer_dense(1L)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.