liveBook · Manning

Computation graph

As explained in Build a Reasoning Model (From Scratch)

A recorded graph of differentiable operations in PyTorch that enables gradient computation during backpropagation.

PyTorch records each differentiable operation connecting model parameters to an output. This computation graph lets a later backward pass compute gradients for updating model weights. section 6.8

Use the graph correctly when computing sequence-level log probabilities:

  • Remove torch.inference_mode() when gradients are needed; inference mode prevents PyTorch from tracking the operations. section 6.8
  • Sum the selected token log probabilities so the sequence score remains connected to the model through the graph. section 6.8
  • Check the result for a grad_fn such as SumBackward0, which indicates that backpropagation can proceed through the score. section 6.8

Inspecting a sequence score

A computed value such as tensor(-16.2998, grad_fn=<SumBackward0>) shows that the summed log probability is attached to a computation graph rather than detached as an ordinary number. section 6.8

Figure 6.16 The fourth stage in the GRPO pipeline computes log probabilities for each rollout. It is related to the logprob scorer we developed in the previous chapter.

Places differentiable log-probability computation as the fourth stage of the GRPO rollout pipeline. section 6.8

How Build a Reasoning Model (From Scratch) covers it

How deep it goes
Defines operation recording and gradient propagation, then stops short of requiring a detailed graph tutorial.
Examples
Uses sequence-level log-probability code, SumBackward0 output, and rollout scoring to expose graph participation.
What it assumes
Assumes PyTorch tensor and model operations, while treating detailed computation-graph internals as optional background.
What you can do after
Apply it via PyTorch by retaining gradients through sequence scores for policy-training updates.
How it connects
Connects differentiable operations with gradients, backpropagation, backward passes, policy optimization, and model parameters.

Also explained in

Compare how 7 books explain computation graph →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Reasoning Model (From Scratch). The section links point to the parts of the book it draws on.