| ggml_diag_mask_inf | R Documentation |
Creates a graph node that sets elements above the diagonal to -Inf. This is used for causal (autoregressive) attention masking.
ggml_diag_mask_inf(ctx, a, n_past)
ctx |
GGML context |
a |
Input tensor (typically attention scores) |
n_past |
Number of past tokens (shifts the diagonal). Use 0 for standard causal masking where position i can only attend to positions <= i. |
In causal attention, we want each position to only attend to itself and previous positions. Setting future positions to -Inf ensures that after softmax, they contribute 0 attention weight.
The n_past parameter allows for KV-cache scenarios where the diagonal needs to be shifted to account for previously processed tokens.
Tensor with same shape as input, elements above diagonal set to -Inf
ctx <- ggml_init(16 * 1024 * 1024)
# Create attention scores matrix
scores <- ggml_new_tensor_2d(ctx, GGML_TYPE_F32, 4, 4)
ggml_set_f32(scores, rep(1, 16))
# Apply causal mask
masked <- ggml_diag_mask_inf(ctx, scores, 0)
graph <- ggml_build_forward_expand(ctx, masked)
ggml_graph_compute(ctx, graph)
ggml_free(ctx)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.