| ggml_build_forward_expand_grads | R Documentation |
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.
ggml_build_forward_expand_grads(ctx, tensor, graph_size = 2048L)
ctx |
GGML context |
tensor |
Output tensor of the computation (usually a scalar loss) |
graph_size |
Node capacity of the graph (default 2048) |
Mark the tensors to differentiate with respect to using
ggml_set_param before building the graph.
Graph object (external pointer)
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.
ggml_build_backward_expand,
ggml_graph_get_grad
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.