liveBook · Manning

Computation graph

As explained in Deep Learning with PyTorch, Second Edition

A graph representation captured from model execution that records operations and data flow.

A computation graph records the operations that transform inputs into outputs. In PyTorch, the graph can support two different jobs: calculating gradients during training and representing a model for export. section 5.5.1 section 17.2.2

A graph behind a loss

Set `requires_grad=True` on model parameters, compute a loss, and call `loss.backward()`. PyTorch follows the recorded operations backward and puts the loss derivatives in `params.grad`. If you repeat this in a training loop, zero the gradient first: backward calls accumulate gradients rather than replace them. section 5.5.1

For deployment, `torch.export` takes a `torch.nn.Module` and an example input and produces an ahead-of-time traced graph. Its nodes describe operations and the path data takes through the model. This exported representation serves a different purpose from the graph traversed by `backward()`. section 17.2.2 section 5.5.1

Listing 17.9 Examining exported program graph representation
# In[]
print(exported_program)

# Out[]
ExportedProgram:
  class GraphModule(torch.nn.Module):
    def forward(self, p_fc1_weight: "f32[5, 10]", p_fc1_bias: "f32[5]",
     p_fc2_weight: "f32[2, 5]", p_fc2_bias: "f32[2]", x: "f32[2, 10]"):
      # File: .py:10 in forward, code: x = self.fc1(x)
      linear: "f32[2, 5]" = torch.ops.aten.linear.default(x, p_fc1_weight, p_fc1_bias);
       x = p_fc1_weight = p_fc1_bias = None

      # File: .py:11 in forward, code: x = self.relu(x)
      relu: "f32[2, 5]" = torch.ops.aten.relu.default(linear);  linear = None

      # File: .py:12 in forward, code: x = self.fc2(x)
      linear_1: "f32[2, 2]" = torch.ops.aten.linear.default(relu, p_fc2_weight, p_fc2_bias);
       relu = p_fc2_weight = p_fc2_bias = None
      return (linear_1,)

Inspect an exported program to see a concrete operation sequence: linear transformation, ReLU, then another linear transformation, with tensor shapes shown along the way. section 17.2.2

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Explains nodes and backward traversal, then shows exported operators; stops short of graph-construction internals.
Examples
Uses a parameter-gradient calculation and an exported graph containing linear and ReLU operations.
What it assumes
Assumes basic use of tensors and model calls while introducing graph behavior through gradient and export examples.
What you can do after
Apply it via PyTorch autograd to calculate gradients, or via torch.export to inspect a traced model.
How it connects
Connects autograd, gradients, loss functions, training loops, model tracing, and deployment.

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 Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.