What Transformers Can Compute
Circuit complexity, formal languages and the extra power of chain of thought.
A transformer with a fixed number of layers does a fixed amount of sequential work per output, however long its input. Circuit complexity turns that observation into theorems: depending on how attention and arithmetic are idealized, constant-depth transformers sit inside the classes or , which separates problems they can solve in parallel from problems that seem to need serial steps. The same theory explains what depth and chain of thought buy, and how compactly transformers describe what they can recognize.
What you’ll learn
- Define , and , and place standard problems (parity, majority, sorting, permutation composition, formula evaluation) among them.
- Sketch why unique hard attention stays in while log-precision softmax attention needs threshold gates and lands in uniform .
- Derive the consequences under standard conjectures, and explain how logarithmic depth and chain of thought add serial computation.
- State the succinctness results for hard-attention transformers and what they imply for verification.
- Use RASP to write transformer programs, and separate what transformers can express from what training finds.
Transformers as language recognizers
To ask what a transformer can compute, fix the model and vary the input length . A transformer recognizes a language if, for every , it reads a string of length and accepts exactly the strings in , for example by thresholding the output at one position. The weights are fixed; only grows. Two idealizations decide the answers:
- Attention. Soft attention is the usual softmax. Unique hard attention (UHAT) puts all weight on one maximizing position; average hard attention (AHAT, also called saturated) averages uniformly over all maximizing positions.
- Precision. Real hardware has fixed precision, but the theory usually allows bits per number: enough to write down a position index, and enough for an average over positions not to underflow.
Results without these limits are of a different kind. With positional encodings, transformers approximate any continuous sequence-to-sequence function on a compact domain (Yun et al., 2020), and with hard attention, unbounded precision and unboundedly many decoding steps they are Turing complete (Pérez, Barceló & Marinković, 2021). Such results allow the size of the network or its precision to grow with the input. This lesson keeps the network fixed and asks what changes with .
| Variant | Attention | Precision | Upper bound |
|---|---|---|---|
| UHAT | one maximizing position | (Hao, Angluin & Frank, 2022) | |
| AHAT (saturated) | uniform average over maximizers | (Merrill, Sabharwal & Smith, 2022) | |
| Softmax | softmax | L-uniform (Merrill & Sabharwal, 2023) | |
| Softmax | softmax | constant | (Li, Liu, Zhou & Ma, 2024) |
Circuit classes, plainly
| Class | Typical members | Intuition |
|---|---|---|
| pattern matching with a fixed number of quantifiers; adding two numbers; recognizing star-free regular languages | look for things in parallel, but never count exactly | |
| PARITY, MAJORITY, counting, iterated addition, multiplication, division, sorting numbers | parallel counting and arithmetic | |
| -complete | composing permutations of five objects (the word problem of ; Barrington, 1989); evaluating Boolean formulas | problems that seem to need rounds of combining |
| -complete | circuit value; feasibility of linear equalities with sign constraints on the variables (a form of linear programming); membership in an arbitrary context-free grammar with empty productions | apparently inherently sequential |
Hard attention in AC⁰, softmax in TC⁰
Local work is free. Embeddings, feed-forward layers, normalizations, and division act on a constant number of numbers of bits each. Any function of input bits has a truth table of rows, so a depth-2 circuit of polynomial size computes it.
Hard attention only compares. Unique hard attention selects the position with the largest score. "Is the selected position?" is an AND, over all , of comparisons between -bit numbers (with ties broken by position), and each comparison is in . Reading the selected value is an OR over positions. Nothing is ever counted, so a constant number of layers stays in :
Softmax sums over all positions. The output needs the sum of numbers of bits. Iterated addition is in but not in , since it includes counting the s of a bit string, and counting decides PARITY and MAJORITY. Averaging hard attention needs the same sum. With constant precision only constantly many values are representable, and Li et al. (2024) show that the rounded sums can then be computed without threshold gates, which is why that variant falls back to :
Layers compose. Each layer is a constant-depth circuit of polynomial size, and a constant number of layers composes to one. The circuits are built by a log-space procedure from , which gives uniformity. A sharper statement: log-precision transformers can be expressed in first-order logic with majority quantifiers, , a logical description of uniform (Merrill & Sabharwal, 2023, NeurIPS).
Exact characterizations. For hard attention the upper bounds can be matched by logics. Unique-hard-attention encoders recognize every language definable in first-order logic with arbitrary unary numerical predicates, a rich fragment of (Barceló, Kozachinskiy, Lin & Podolskii, 2024). With strict causal masking and no position embeddings, masked hard-attention transformers recognize exactly the star-free regular languages, those definable in linear temporal logic (Yang, Chiang & Angluin, 2024). So masked hard-attention transformers without position embeddings cannot recognize even , the strings of even length, which needs counting modulo 2 and is not star-free.
Depth, serial steps and chain of thought
Combine the upper bound with the table of classes. If , no constant-depth log-precision transformer composes permutations of five objects or evaluates Boolean formulas for all . If (L-uniform lies inside ), none solves the -complete problems in the table, such as sign-constrained linear equalities or membership in arbitrary context-free grammars with empty productions (Merrill & Sabharwal, 2023). They call this the parallelism trade-off: any architecture as parallelizable as the transformer inherits limits of this kind. State space models are no escape; Merrill, Petty and Sabharwal (2024) place common SSMs in as well.
Depth as serial computation. If constant depth is not enough, how much is? Liu et al. (2023) showed that a transformer with layers can simulate any finite automaton for steps, by composing state transitions in a balanced tree (a parallel prefix over the automaton's transformation semigroup), and that layers suffice when the automaton's groups are solvable, by Krohn–Rhodes theory. Gradient training found such shortcuts on their synthetic tasks, but the shortcuts were brittle out of distribution. Sanford, Hsu and Telgarsky (2024) proved that a constant number of attention layers simulates, and is simulated by, a constant number of rounds of massively parallel computation; for the -hop induction head task, following a chain of in-context lookups, depth is both sufficient and necessary under a standard conjecture, and in their experiments the largest learnable grew exponentially with depth.
Each position stores a pointer to another position, and the task is to follow the chain hops. In one layer, every position can read the pointer stored where its own pointer leads, composing its pointer with itself; after layers it knows its -th successor. How many layers are needed?
Show the answer
10 layers
gives , against steps for a recurrent network that follows one hop per step. Pointer doubling is the reason layers suffice, and the lower bound says a transformer cannot do much better.
Chain of thought. Each generated token feeds back as input, adding one round of serial computation. With intermediate steps, under their idealizations, decoders gain little from steps, recognize all regular languages with steps, and recognize exactly the languages in with polynomially many (Merrill & Sabharwal, 2024). Li, Liu, Zhou and Ma (2024) give a circuit-size version: constant-depth, constant-precision transformers lie in without chain of thought, but with steps they solve any problem that Boolean circuits of size solve. Feng et al. (2023) show the same split for arithmetic expressions, linear equations and dynamic programming. The Artificial Intelligence course's lesson Reasoning in Language Models builds the construction step by step.
Assume . Which task can a fixed-depth, log-precision transformer not solve for all input lengths in one forward pass, although a chain of thought of steps makes it easy?
- Deciding whether a bit string contains more 1s than 0s
- Sorting a list of numbers of bits
- Composing given permutations of five objects
- Checking whether a bit string has an even number of 1s
Show the answer
C. Composing given permutations of five objects
The word problem of is -complete (Barrington, 1989), so it lies outside unless the classes are equal. Writing the running product as tokens turns it into constant-size lookups. Majority, sorting and parity are all in .
Succinctness, programs and learnability
Succinctness. Expressivity asks which languages a model can recognize; succinctness asks how large a description must be. Bergsträßer, Cotterell and Lin (2026) studied masked unique-hard-attention transformers with fixed-precision numbers, which recognize only star-free languages. Yet within that class they are remarkably compact:
The constructions use languages built from a binary counter with cells (tilings of exponentially wide rows), whose shortest accepted strings have length at least . Two lessons follow. Compactness is a second axis of power: attention can refer to far-away positions by content and index arithmetic, so it can describe in polynomial size what an automaton must spell out state by state. And compactness has a price: the verification of even simple properties of such transformers is provably intractable in the worst case.
Programs. RASP (Weiss, Goldberg & Yahav, 2021) is a programming language whose primitives compile to attention: select builds a selector, a matrix of which query positions may read which key positions; aggregate averages values over the selected positions; selector_width counts them.
# built-in sequences: tokens, indices, length
# histogram: how many tokens equal the token at each position?
same = select(tokens, tokens, ==) # query i may read key j if tokens[j] == tokens[i]
hist = selector_width(same) # count of selected positions, per query
# reverse a sequence
opp = length - indices - 1
flip = select(indices, opp, ==) # query i reads key j = length - i - 1
reverse = aggregate(flip, tokens) # average over one selected position: tokens[j]Each select–aggregate pair is one attention head; elementwise operations are feed-forward layers. Weiss et al. found that transformers with as many layers and heads as a RASP program compiles to learned the task, while for most tasks smaller ones did markedly worse; the Tracr compiler (Lindner et al., 2023) turns RASP programs into actual transformer weights.
What training finds. Expressivity results are existence proofs. Zhou et al. (2024) proposed the RASP-Generalization Conjecture: decoder-only transformers trained on short inputs tend to generalize to longer ones when the task has a short program, valid for every length, in RASP-L, a restricted RASP that limits how positions may be used. Tasks with short RASP-L programs, such as counting, sorting and copying with unique tokens, length-generalized in their experiments; addition and parity did not, until the input and scratchpad formats were changed so that short programs existed. Hahn and Rofin (2024) proved a complementary obstacle: transformers representing highly sensitive functions such as PARITY sit in isolated, very sharp minima, and small perturbations turn them into much less sensitive functions, which biases training toward low sensitivity.
Key takeaways
- : PARITY and MAJORITY separate the first two; composition and formula evaluation are -complete; strictness beyond is conjectured.
- Unique hard attention only compares and selects, so it stays in ; log-precision softmax needs the sum over positions, so constant-depth transformers land in uniform , and constant precision falls back to .
- Under or , fixed-depth transformers cannot solve inherently serial problems in one pass. Depth simulates any automaton, layers follow hops, and chain-of-thought steps add serial rounds up to all of .
- Masked hard-attention transformers are succinct: polynomial size can require exponentially larger LTL formulas or RNNs and doubly exponentially larger automata, and verifying them is -complete.
- RASP writes transformer programs; the RASP-Generalization Conjecture links short RASP-L programs to length generalization, and sensitivity results explain why expressible functions such as PARITY are hard to learn.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Constant-depth softmax transformers whose numbers have bits recognize only languages in which class?
- Uniform
- , and no smaller class is known
- All of
Show the answer
B. Uniform
Every layer is a constant-depth threshold circuit: lookups for the local operations, and iterated addition for the softmax sums (Merrill & Sabharwal, 2023). Chain of thought, not a single pass, is what reaches .
Why does unique hard attention stay within while softmax attention with bits of precision needs threshold gates?
- Hard attention uses fewer parameters
- Softmax attention computes exponentials, which cannot evaluate
- Hard attention only compares scores and selects one position, while softmax sums over all positions, which includes counting
- Softmax attention is causal and hard attention is not
Show the answer
C. Hard attention only compares scores and selects one position, while softmax sums over all positions, which includes counting
Selecting a maximum is an AND of comparisons, in . A sum of numbers can count the s of a bit string, which decides PARITY and MAJORITY, both outside .
What do Liu et al. (2023) show about simulating a finite automaton for steps with a transformer?
- It needs layers, one per step
- layers always suffice, and suffice when the automaton's groups are solvable
- It is impossible for any automaton with more than two states
- One layer suffices for every automaton
Show the answer
B. layers always suffice, and suffice when the automaton's groups are solvable
Transitions compose associatively, so a balanced tree of compositions needs depth . Krohn–Rhodes theory gives constant-depth shortcuts for solvable groups; for non-solvable ones such as constant depth would imply .
What does the succinctness result for masked hard-attention transformers say?
- They recognize languages that no finite automaton recognizes
- They can be verified in polynomial time
- They are always smaller than equivalent recurrent networks
- Some languages recognized by polynomial-size transformers need exponentially larger LTL formulas or recurrent networks and doubly exponentially larger automata
Show the answer
D. Some languages recognized by polynomial-size transformers need exponentially larger LTL formulas or recurrent networks and doubly exponentially larger automata
These transformers recognize only star-free regular languages, so the gain is in description size, not in the class of languages. The flip side: non-emptiness and equivalence are -complete.
Further reading
- W. Merrill & A. Sabharwal, "The parallelism tradeoff: Limitations of log-precision transformers," TACL 11 (2023).
- Y. Hao, D. Angluin & R. Frank, "Formal language recognition by hard attention transformers: Perspectives from circuit complexity," TACL 10 (2022).
- B. Liu, J. T. Ash, S. Goel, A. Krishnamurthy & C. Zhang, "Transformers learn shortcuts to automata," ICLR (2023).
- P. Bergsträßer, R. Cotterell & A. W. Lin, "Transformers are inherently succinct," ICLR (2026).
- G. Weiss, Y. Goldberg & E. Yahav, "Thinking like transformers," ICML (2021).
- L. Strobl, W. Merrill, G. Weiss, D. Chiang & D. Angluin, "What formal languages can transformers express? A survey," TACL 12 (2024).