Subscribe
AI
6 min read

What Is a Computation Graph? How AI Models Actually Run

Every PyTorch model you have ever read looks like ordinary Python. You call a layer, you get a tensor back, you print shapes in a notebook and they exist immediately. And yet every modern training run, every compiled checkpoint, and every serving engine that squeezes extra tokens per second out of a GPU is built on a very different object: a computation graph, a directed graph of operations that quietly describes what the model actually computes. The tension between "code you run" and "graph you

What Is a Computation Graph? How AI Models Actually Run

Every PyTorch model you have ever read looks like ordinary Python. You call a layer, you get a tensor back, you print shapes in a notebook and they exist immediately. And yet every modern training run, every compiled checkpoint, and every serving engine that squeezes extra tokens per second out of a GPU is built on a very different object: a computation graph, a directed graph of operations that quietly describes what the model actually computes. The tension between "code you run" and "graph you compile" has been the central design argument in machine learning frameworks for a decade, and it is settled now in a way almost nobody notices: PyTorch won the argument, and then it rebuilt itself as a graph compiler anyway.

What a computation graph is

A computation graph represents a model as a directed graph. The nodes are operations - a matmul, an add, a softmax, an attention block. The edges are tensors flowing between them: weights, activations, and intermediate results. Running the model means walking the graph from inputs to outputs. The PyTorch 2 paper formalizes the two framework families that come out of this view: "eager mode" frameworks such as PyTorch and JAX, where the model is Python code executed step by step, and "graph mode" frameworks such as TensorFlow 1.x, Caffe, Theano, and CNTK, where the model is first defined as a complete graph and only then run.

A directed graph of geometric nodes and arrows: the computation graph behind a neural network model

The graph is not just a diagram. It is what makes automatic differentiation mechanical. Backpropagation is reverse-mode automatic differentiation applied to the graph: the chain rule is scheduled by walking the edges in reverse, and each node contributes a local derivative. The 1986 Nature paper by Rumelhart, Hinton and Williams that brought backpropagation into mainstream use is most naturally described as a procedure over layers of such a graph, and the underlying idea reaches back to Paul Werbos's 1974 thesis. A survey of automatic differentiation by Baydin, Pearlmutter, Radul and Siskind covers how frameworks turn this math into bookkeeping: each forward operation records enough information to run its backward counterpart.

What the whole-program view buys you

The reason anyone bothers with graphs is that a compiler that can see the entire computation can do things an interpreter cannot. If the framework knows that an elementwise multiply is immediately followed by an add, it can fuse them into one GPU kernel instead of paying to write the intermediate to memory and back. If it knows a tensor is never used again, it can reuse that memory. If it knows two operations are independent, it can overlap them. This is the pitch of XLA, the Accelerated Linear Algebra compiler that consumes model graphs from TensorFlow, PyTorch and JAX and optimizes them for GPUs, CPUs and accelerators, with a versioned intermediate representation called StableHLO as the portability layer between frameworks and backends.

There is also a lower-level reason graphs matter, and it has nothing to do with math. GPU work arrives as kernel launches, and each launch costs the CPU a few microseconds. When kernels themselves run for milliseconds, that overhead is invisible. When a model runs in tiny microsecond kernels - which is exactly what LLM decode at small batch size does - the CPU becomes the bottleneck and the GPU goes idle between launches.

Define-and-run, define-by-run, and back again

The first generation of mainstream neural network frameworks were graph mode. You assembled the graph, opened a session, and fed it data. Debugging meant staring at an opaque graph because nothing existed until you ran it.

Chainer, released by Preferred Networks in June 2015, was the first framework built around the opposite idea, which it called define-by-run: the graph is constructed implicitly, node by node, as the forward pass executes, so Python control flow, loops and conditionals can reshape the computation on every step (original paper, KDD version). PyTorch adopted the same imperative design and, with it, the research world. TensorFlow blinked first: Google shipped Eager Execution in late 2017, describing it as "an imperative, define-by-run interface," and Google confirmed that TensorFlow 2.0 would run eager by default, with tf.function re-creating graphs on demand.

By most counts, eager mode won. Research code with dynamic graphs is simply easier to write and debug, and PyTorch became the default language of deep learning research.

Except the win was never either/or. In 2024, the PyTorch 2 paper at ASPLOS described what actually happened: keep the eager interface, and recover the compiler's whole-program view at runtime. That is TorchDynamo, a Python-level just-in-time compiler that hooks into the CPython frame evaluator, inspects bytecode right before it runs, and extracts sequences of PyTorch operations into an FX graph - a real computation graph, captured on the fly, without asking the user to declare one. Non-PyTorch code it cannot understand still runs eagerly, in fragments between graph captures; these "graph breaks" are the tax of the hybrid design and the reason compiled runs sometimes inexplicably underperform. The extracted graphs go to TorchInductor, which generates GPU kernels via Triton and CPU code via C++. Across more than 180 real-world models on an NVIDIA A100, the stack delivered a 2.27x geometric-mean inference speedup and 1.41x on training. torch.compile shipped in PyTorch 2 and reached stable release in March 2023.

Where LLM serving puts graphs to work

The same split between model-as-code and model-as-graph reappears one layer down, in inference engines. NVIDIA introduced CUDA Graphs in CUDA 10.1: instead of re-issuing every kernel launch, you capture a fixed sequence of GPU operations once and replay the whole thing with a single call. NVIDIA's own measurement on a V100 is the standard illustration: a kernel that executes in 2.9 microseconds cost 9.6 microseconds per launch with naive synchronous launches, 3.8 microseconds once launches were overlapped, and 3.4 microseconds as part of a replayed graph. The graph replay costs only when kernels are short and numerous, which is precisely the shape of a decode step.

A two-lane diagram: a machine ejecting scattered blocks with gaps between them above, and the same blocks fused into one continuous train below

This is why vLLM's design docs devote an entire page to graph strategy. vLLM captures the decode step as CUDA graphs for a fixed ladder of batch sizes and replays the matching graph per token, and it exposes a menu of modes: FULL graphs where everything is captured, PIECEWISE graphs where attention stays eager because attention kernels change shape as the KV cache grows, and the default FULL_AND_PIECEWISE, full graphs for decode, piecewise for prefill, at the price of memory and longer startup. Run vLLM with --enforce-eager and you pay the launch overhead on every token; that flag exists precisely to measure what the graphs were buying. The reason attention is excluded from full capture is the graph's core limitation in one sentence: captured graphs freeze shapes and memory addresses, while a growing KV cache changes both every step.

Why this layer deserves a name

Three practical payoffs for knowing the graph is in there. When torch.compile is slower than expected, the diagnosis is usually graph breaks and recompiles, which is a graph-shaped problem, not a model-shaped one. When a checkpoint moves to new hardware, the portable artifact is a graph - ONNX exports and StableHLO programs, not source code. And when serving latency at small batch is bad, the fix is often to stop re-launching thousands of microsecond kernels per token, which means turning the graph into the deployment unit rather than the compile-time detail.

The history is a loop that closed: static graphs gave up usability, eager gave up optimization, and the modern stack refuses to choose - it watches your Python code, lifts whatever it can into a graph, and compiles the graph out of sight. Most users of a language model never touch this layer. It runs every token they read.

Sources

AILLMeducational