Skip to content
Autograd

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 AA to node BB means BB was computed using AA.

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 z=x2+x⋅yz = x^2 + x \cdot y with x=2x = 2 and y=3y = 3.

Step 1. Mark xx and yy as tracked inputs. They become leaf nodes.

Step 2. Compute a=x2=4a = x^2 = 4. This creates an interior node for aa, with an edge from xx.

Step 3. Compute b=x⋅y=6b = x \cdot y = 6. This creates an interior node for bb, with edges from both xx and yy.

Step 4. Compute z=a+b=10z = a + b = 10. This creates the output node, with edges from aa and bb.

²××++x= 2y= 3x²= 4x·y= 6z= 10inputs (leaf)computedoutput

Notice that xx has two outgoing edges: one to a=x2a = x^2, and one to b=x⋅yb = x \cdot y. This branching is essential. When computing how zz responds to a change in xx, 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: ∂(x2)∂x=2x\frac{\partial(x^2)}{\partial x} = 2x, so the backward function multiplies the incoming gradient by 2x2x. For addition: ∂(a+b)∂a=1\frac{\partial(a+b)}{\partial a} = 1 and ∂(a+b)∂b=1\frac{\partial(a+b)}{\partial b} = 1, 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 a=x2a = x^2 is a means to an end; we care about ∂z∂x\frac{\partial z}{\partial x}, not ∂z∂a\frac{\partial z}{\partial a}. 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, xx contributes to zz through two paths: via a=x2a = x^2 and via b=x⋅yb = x \cdot y. The total gradient ∂z∂x\frac{\partial z}{\partial x} is the sum of contributions from both:

∂z∂x=∂z∂a⋅∂a∂x⏟path through a+∂z∂b⋅∂b∂x⏟path through b=1⋅2x+1⋅y=4+3=7\frac{\partial z}{\partial x} = \underbrace{\frac{\partial z}{\partial a} \cdot \frac{\partial a}{\partial x}}_{\text{path through } a} + \underbrace{\frac{\partial z}{\partial b} \cdot \frac{\partial b}{\partial x}}_{\text{path through } b} = 1 \cdot 2x + 1 \cdot y = 4 + 3 = 7

The graph makes this explicit: two edges leave xx, so two partial contributions must be summed. This is the multi-variable chain rule, handled automatically by the graph structure.

For yy, there is only one path (through bb):

∂z∂y=∂z∂b⋅∂b∂y=1⋅x=2\frac{\partial z}{\partial y} = \frac{\partial z}{\partial b} \cdot \frac{\partial b}{\partial y} = 1 \cdot x = 2

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 y=W@xy = W @ x 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.