Module 04, Section 4.5

Transformer Expressiveness Theory

What can Transformers compute? Universal approximation, computational complexity classes, and how chain-of-thought reasoning extends their fundamental power.

We proved that Transformers can theoretically compute anything. In practice, they mostly compute the next plausible word, which is still pretty good.

Professor Proof, a complexity theorist with realistic expectations
Research-Focused Section

This section surveys theoretical results about the computational power of Transformers. The material is more abstract than the rest of this module and draws on complexity theory. It is included because these results have direct practical implications for understanding why certain tasks are easy or hard for LLMs, and why chain-of-thought prompting helps.

1. Universal Approximation

The classical universal approximation theorem for neural networks states that a single hidden layer with enough neurons can approximate any continuous function on a compact domain to arbitrary accuracy. A natural question: do Transformers have an analogous property for sequence-to-sequence functions?

1.1 Transformers as Universal Approximators of Sequence Functions

Yun et al. (2020) proved that Transformers are universal approximators of continuous sequence-to-sequence functions. Specifically, for any continuous function f: Rn×d → Rn×d and any ε > 0, there exists a Transformer network that approximates f uniformly to within ε. The key requirements are:

This result tells us that the Transformer architecture is, in principle, capable of representing any continuous mapping from input sequences to output sequences. Of course, universal approximation says nothing about whether gradient-based training will find a good approximation, or how large the network needs to be. Still, it provides the theoretical foundation: the architecture itself is not the bottleneck.

Note: The Role of the FFN

The proof of universal approximation critically relies on the feed-forward network, not just attention. Attention alone (without the FFN) is a linear operator over the value vectors; it can only compute weighted averages. The FFN provides the nonlinear transformations needed to approximate arbitrary functions. This is one theoretical justification for why the FFN matters so much in practice, as we discussed in Section 4.1.

2. Transformers as Computational Models

Beyond approximation, we can ask: what computational problems can Transformers solve? This requires connecting Transformers to formal computational complexity classes.

2.1 Fixed-Depth Transformers and TC0

A foundational result by Merrill and Sabharwal (2023) shows that a fixed-depth Transformer with log-precision arithmetic (O(log n) bits per number, where n is the input length) can be simulated by TC0 circuits, and vice versa. TC0 is the complexity class of problems solvable by constant-depth, polynomial-size threshold circuits.

This has concrete implications for what fixed-depth Transformers cannot do:

P (polynomial time) NC1 TC0 AC0 Fixed-Depth Transformer Graph connectivity Boolean formula evaluation (in NC1, not in TC0) Fixed-depth Transformers with log-precision ≈ TC0
Figure 4.8: The complexity class hierarchy. Fixed-depth Transformers with log-precision arithmetic correspond to TC0. Problems requiring sequential computation (NC1 and above) are beyond their reach without additional mechanisms.

2.2 The Precision Assumption Matters

The TC0 characterization assumes log-precision arithmetic: each number in the Transformer uses O(log n) bits. With unbounded precision (arbitrary-precision real numbers), a Transformer can simulate any Turing machine in polynomial time, making it trivially universal. This highlights that the precision of computation is a critical variable in the theoretical analysis.

In practice, Transformers use 16-bit or 32-bit floating point, which is somewhere between log-precision and unbounded precision. The practical implications of the theoretical results hold approximately: tasks requiring deep sequential reasoning are indeed harder for Transformers, even if the formal impossibility results rely on specific precision assumptions.

3. Limitations: What Transformers Struggle With

The theoretical results predict specific failure modes, and these predictions are borne out empirically. Here are the key categories of problems that fixed-depth Transformers find difficult:

3.1 Inherently Sequential Computation

Problems that require a long chain of dependent computation steps are hard for Transformers because each layer can only perform a constant amount of sequential work. The total sequential depth is the number of layers, typically 32 to 96. Tasks requiring more sequential steps than this (relative to the model's precision) become problematic.

Example: multi-step arithmetic. Computing ((3 + 7) * 2 - 4) / 3 requires evaluating operations in order. Each operation depends on the result of the previous one. A fixed-depth Transformer must either learn to parallelize this computation (difficult) or represent the intermediate results within a single forward pass (limited by depth).

3.2 Counting and Tracking State

Exact counting (e.g., "does this string have exactly 47 opening parentheses?") is difficult because log-precision attention scores cannot represent arbitrary integers precisely for long sequences. The model can approximate counting for small numbers but degrades as counts grow large.

3.3 Graph Reasoning

Problems like graph connectivity, shortest paths, and cycle detection require iterative algorithms that propagate information through the graph structure. A constant-depth Transformer can only propagate information a constant number of "hops" per layer, limiting its ability to reason about graph structures that require many hops.

Practical Caveat

These theoretical limitations apply to fixed-depth, fixed-precision Transformers. Real LLMs are very large (96+ layers), have moderately high precision (BF16/FP32), and are trained on massive datasets that may help them learn approximate solutions. The theory tells us which tasks will be hard and where to expect failure modes, not that the model will always fail. Large models often "mostly work" on these tasks but exhibit characteristic errors at the boundary of their capability.

4. Chain-of-Thought Extends Computational Power

This is perhaps the most practically important theoretical result. Chain-of-thought (CoT) reasoning, where the model generates intermediate reasoning steps before producing a final answer, fundamentally changes the computational model. Instead of computing the answer in a single forward pass (constant depth), the model now performs computation proportional to the number of generated tokens.

4.1 CoT as Additional Computation Steps

When a Transformer generates T tokens of chain-of-thought reasoning, each token's generation involves a full forward pass through the model. The information from each token is fed back as input for the next, creating an effective sequential computation depth of T × N (tokens times layers). This is a fundamentally different computation than a single forward pass.

Key Insight: CoT Makes Transformers Turing-Complete

Feng et al. (2023) and others have shown that a fixed-size Transformer with chain-of-thought (unbounded generation length) can simulate any Turing machine. The generated tokens serve as the Turing machine's tape, and each forward pass computes one step of the Turing machine's transition function. This means CoT is not just a prompting trick; it provably extends the computational class of the model from TC0 to Turing-complete.

4.2 Why CoT Helps on Hard Problems

Consider the task of multiplying two large numbers, say 347 × 892. A single forward pass through a Transformer must compute this in constant depth, which the theory predicts is very difficult (large integer arithmetic is not in TC0). But with CoT, the model can:

  1. Break the multiplication into partial products (347 × 2, 347 × 90, 347 × 800)
  2. Compute each partial product (one or two tokens of intermediate computation each)
  3. Add the partial products (again, with intermediate steps)
  4. Produce the final answer

Each individual step is simple enough for a single forward pass. The chain of thought provides the sequential depth that a single pass cannot.

4.3 Implications for LLM Design and Prompting

This theoretical framework has direct practical consequences:

5. Open Questions

Several fundamental questions remain open in this area:

Key Takeaways

Check Your Understanding

1. What does the universal approximation theorem for Transformers say, and what does it not say?

Show Answer
It says that a sufficiently large Transformer can approximate any continuous sequence-to-sequence function to arbitrary accuracy. It does not say that gradient-based training will find this approximation, how large the network needs to be, or that the approximation generalizes beyond the training distribution.

2. Why can a fixed-depth Transformer not solve arbitrary graph connectivity problems?

Show Answer
Graph connectivity requires propagating information across potentially long paths in the graph. A fixed-depth Transformer can only propagate information a constant number of steps (proportional to its depth). Graph connectivity is in NL (nondeterministic log-space) which contains problems not in TC0 (the class corresponding to fixed-depth Transformers with log-precision). For graphs with long shortest paths, the model cannot reach all nodes from a given starting point in a constant number of layers.

3. How does chain-of-thought reasoning change the computational class of a Transformer?

Show Answer
Without CoT, a fixed-depth Transformer is limited to TC0. With CoT (unbounded generation length), the model performs T forward passes sequentially (one per generated token), with information flowing from each step to the next through the generated tokens. This gives the model T x N sequential depth (tokens times layers), making it capable of simulating any Turing machine. CoT elevates the model from TC0 to Turing-complete.

4. Why does the precision assumption matter in the theoretical analysis?

Show Answer
With log-precision (O(log n) bits), a Transformer maps to TC0 and has clear limitations. With unbounded precision (arbitrary real numbers), a single-layer Transformer can trivially encode any computation in its weights, making the analysis meaningless. Practical models use 16 or 32-bit precision, which falls between these extremes. The log-precision results are more informative for understanding real limitations, even though the exact correspondence is approximate.

5. Give a practical example where CoT helps and explain why using the theory.

Show Answer
Multi-digit multiplication (e.g., 347 x 892) requires a sequence of dependent arithmetic operations: computing partial products, carrying digits, and summing results. This is an inherently sequential computation that exceeds what TC0 can handle for large numbers. Without CoT, the model must compute the answer in a single forward pass (constant depth), which the theory predicts will fail for large inputs. With CoT, the model breaks the computation into simple steps (each within TC0 capability), using the generated tokens to carry intermediate results from one step to the next.
What Comes Next

You now understand the Transformer architecture: how it processes tokens, what each component contributes, and what theoretical limits it faces. In Module 05, you will learn what to do with the logits that come out of the final layer. How do we turn a probability distribution over 50,000 tokens into actual generated text? The answer is more subtle than you might expect.