ggml_build_backward_expand: Add the Backward Pass to a Computation Graph

View source: R/graph.R

ggml_build_backward_expandR Documentation

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)

Arguments

ctx

GGML context

graph

Graph from ggml_build_forward_expand_grads

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.

Value

NULL, invisibly; graph is modified in place

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.

See Also

ggml_build_forward_expand_grads, ggml_graph_get_grad, ggml_set_param


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