The Computation Graph
How deep learning frameworks track every operation to enable automatic differentiation.
The previous article established the chain rule: to differentiate a composition of functions, multiply the local gradients at each step. But a real network might chain together hundreds of operations. To apply the chain rule systematically, a framework needs to remember what was computed and in what order. The computation graph is that record.
What Is a Computation Graph?
A computation graph is a directed acyclic graph (DAG) where:
- Nodes represent values, either scalar numbers or tensors.
- Edges represent dependencies: an edge from node to node means was computed using .
Nodes come in two kinds. Leaf nodes are inputs and parameters: values that were not themselves computed from other tracked values. Interior nodes are the results of operations applied to other nodes.
Every time a tracked value participates in an operation, the operation creates a new interior node and records which nodes it depended on. The graph grows as the forward computation proceeds.
Building the Graph Step by Step
Consider with and .
Step 1. Mark and as tracked inputs. They become leaf nodes.
Step 2. Compute . This creates an interior node for , with an edge from .
Step 3. Compute . This creates an interior node for , with edges from both and .
Step 4. Compute . This creates the output node, with edges from and .
Notice that has two outgoing edges: one to , and one to . This branching is essential. When computing how responds to a change in , both paths contribute.
What Each Node Records
A node is more than just a value. An interior node stores:
- Its output value, computed during the forward pass.
- A backward function: a small piece of code that, given the gradient flowing into this node from above, computes the gradient flowing out to each of its inputs.
The backward function is derived directly from the operation that created the node. For squaring: , so the backward function multiplies the incoming gradient by . For addition: and , so the backward function passes the incoming gradient unchanged to both inputs.
This is the local gradient from the chain rule, packaged up at build time and ready to be used during the backward pass.
Leaf Nodes and Gradient Tracking
Not every value needs a gradient. The intermediate result is a means to an end; we care about , not . Only leaf nodes that are explicitly marked for tracking accumulate gradients.
In practice, weight matrices are marked as trackable (they are what we want to differentiate with respect to), while input data is not (the data is fixed; we only adjust weights). Intermediate nodes inherit tracking from their inputs: if any input is tracked, the output is too, so the graph is built through the entire computation.
The gradient accumulated at a leaf is the total derivative of the output with respect to that leaf, summing contributions from every path through the graph.
Multiple Paths and Gradient Accumulation
In the graph above, contributes to through two paths: via and via . The total gradient is the sum of contributions from both:
The graph makes this explicit: two edges leave , so two partial contributions must be summed. This is the multi-variable chain rule, handled automatically by the graph structure.
For , there is only one path (through ):
Connection to Deep Learning
In a neural network, every weight matrix is a leaf node. Every operation — matrix multiplication, activation function, layer normalization — adds interior nodes to the graph. After a forward pass, the graph contains a complete record of every computation that produced the output.
This record is exactly what makes efficient differentiation possible. Rather than differentiating the entire network symbolically (which would produce a huge expression), we differentiate each operation locally, store the result, and then combine them during the backward pass. The chain rule ties them all together.
The graph is built implicitly, without any extra work from the programmer. Writing inside a tracked context is enough: the framework intercepts the operation, records the dependency, and stores the backward function.
In Code
Marking inputs as trackable causes the framework to build the graph during every forward computation:
import torch
x = torch.tensor([2.0], requires_grad=True)
y = torch.tensor([3.0], requires_grad=True)
a = x ** 2 # node: x², records its backward function (squaring)
b = x * y # node: x·y, records its backward function (multiplication)
z = a + b # node: z = 10, records its backward function (addition)
print(z.grad_fn) # <AddBackward0 ...>: the node that produced z
print(z.grad_fn.next_functions) # its parents in the graph: the nodes of a and b
z.backward() # traverses the graph in reverse
print(x.grad) # tensor([7.]) — ∂z/∂x = 2x + y = 4 + 3
print(y.grad) # tensor([2.]) — ∂z/∂y = x = 2
Each line that computes a new value from tracked tensors silently adds a node to the graph. Calling .backward() then traverses that graph from the output back to the leaves, multiplying local gradients at each step. The next article covers exactly how that traversal works.
The computation graph is the data structure; backpropagation is the algorithm that walks it. With the graph in place, computing every gradient simultaneously costs roughly the same as a single forward pass, regardless of how many parameters the network has.