Learn R Programming

ggmlR (version 0.8.4)

ggml_build_backward_expand: Add the Backward Pass to a Computation Graph

Description

Extends graph in place with the nodes that compute gradients of its output with respect to every tensor marked by ggml_set_param. After computing the graph, read a gradient with ggml_graph_get_grad.

Usage

ggml_build_backward_expand(ctx, graph)

Value

NULL, invisibly; graph is modified in place

Arguments

ctx

GGML context

graph

Graph from ggml_build_forward_expand_grads

The full sequence

Every step is required; skipping one gives either an error or -- for the last -- gradients that are silently all zero.

  1. ggml_set_param on each tensor to differentiate

  2. build the forward ops

  3. ggml_set_loss on the output

  4. ggml_build_forward_expand_grads

  5. ggml_build_backward_expand()

  6. ggml_graph_reset -- seeds d(loss)/d(loss) = 1

  7. ggml_graph_compute

  8. ggml_graph_get_grad to read a gradient

Which ops are differentiable

Gradients flow only through operations that implement a backward pass. Most do, but not all -- ggml_clamp is a notable exception, and among the state-space ops only ggml_ssm_conv is differentiable.

Details

The graph must come from ggml_build_forward_expand_grads: ggml_build_forward_expand produces one without gradient storage, and that is rejected rather than allowed to abort inside ggml.

See Also

ggml_build_forward_expand_grads, ggml_graph_get_grad, ggml_set_param