ggml_build_forward_expand_grads: Build a Forward Graph That Can Be Differentiated

View source: R/graph.R

ggml_build_forward_expand_gradsR Documentation

Build a Forward Graph That Can Be Differentiated

Description

Like ggml_build_forward_expand, but the graph also reserves storage for gradients, which is what ggml_build_backward_expand requires. A graph from the plain forward builder carries none, so passing it to the backward builder is an error.

Usage

ggml_build_forward_expand_grads(ctx, tensor, graph_size = 2048L)

Arguments

ctx

GGML context

tensor

Output tensor of the computation (usually a scalar loss)

graph_size

Node capacity of the graph (default 2048)

Details

Mark the tensors to differentiate with respect to using ggml_set_param before building the graph.

Value

Graph object (external pointer)

Graph size

graph_size is the node capacity. The backward pass appends nodes to the forward ones, so it needs more room than a forward-only graph: roughly three times the forward node count is a safe starting point. Too small a value fails while the graph is being built, not silently.

See Also

ggml_build_backward_expand, ggml_graph_get_grad

Examples


ctx <- ggml_init(16 * 1024 * 1024)
a <- ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 4)
b <- ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 4)
ggml_set_f32(a, c(1, 2, 3, 4))
ggml_set_f32(b, c(5, 6, 7, 8))
ggml_set_param(a)
loss  <- ggml_sum(ctx, ggml_mul(ctx, a, b))
ggml_set_loss(loss)
graph <- ggml_build_forward_expand_grads(ctx, loss)
ggml_build_backward_expand(ctx, graph)
ggml_graph_reset(graph)     # seeds d(loss)/d(loss) = 1
ggml_graph_compute(ctx, graph)
# d(sum(a*b))/da == b
ggml_get_f32(ggml_graph_get_grad(graph, a))
ggml_free(ctx)


ggmlR documentation built on Oct. 23, 2026, 1:07 a.m.