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






