betaFeedback

WorksComputer Science

A Tight Hierarchy for Chain of Thought

Abstract

Chain-of-thought (CoT) reasoning has emerged as a powerful paradigm for extending the computational capabilities of transformer-based models. A growing body of recent work has begun to uncover the fine-grained computational power of transformers augmented with CoT, as well as their connections to classical complexity theory. In this work, we advance this line of research by establishing the first hierarchy theorem for CoT-based computation. Our main result shows that for any reasonable resource bound r(n)≥nr(n) \geq n and any ε>0\varepsilon > 0, CoT(r(n))⊊CoT(r(n)1+ε), \mathrm{CoT}(r(n)) \subsetneq \mathrm{CoT}(r(n)^{1+\varepsilon}), where CoT(r(n))\mathrm{CoT}(r(n)) denotes the class of decision problems solvable by transformers using O(r(n))O(r(n)) CoT reasoning steps on inputs of length nn. Our result shows that increasing the number of CoT reasoning steps provably yields strictly greater computational power, establishing CoT steps as a fundamental resource in transformer-based computation. Unlike classical hierarchy theorems, which are typically proved via diagonalization, our approach leverages known coarse-grained simulations between CoT-augmented transformers and Turing machines in both directions. Building on these connections, we adapt the classical padding technique from complexity theory to this setting to obtain a tight hierarchy. A key technical contribution of our work is the implementation of padding within the transformer-based CoT model.

Introduction

A central objective in computing foundations is to understand how computational power scales with access to additional resources. The classical hierarchy theorems from computational complexity theory establish that increased time or space yields strictly greater computational capability. The two most fundamental results in this regard are the Time Hierarchy Theorem and the Space Hierarchy Theorem. Intuitively, the Time Hierarchy Theorem states that if t(n)≥nt(n) \ge n is a sufficiently nice complexity-bounding function (i.e., time-constructible function), then there are problems solvable in time slightly more than t(n)t(n) that cannot be solved in time t(n)t(n). In particular, the most well-known version of this principle states that TIME(t1(n))⊊(t_1(n)) \subsetneq TIME(t2(n))(t_2(n)) if t1(n)log⁡t1(n)=o(t2(n))t_1(n) \log t_1(n) = o(t_2(n)) [11]. Here TIME(t(n))(t(n)) is the class of languages that are decidable in time O(t(n))O(t(n)) on inputs of length nn using a multi-tape Turing machine. Hierarchy theorems formalize a basic structural principle: bounded resources induce strict stratifications of computational

A version of this paper will appear at NeurIPS 2026. power. Even though hierarchy theorems are intuitive, it is far from trivial to establish such quantitative results. Without such results, we cannot rule out the possibility of universal thresholds on resources that are required to solve any decidable problem.

Modern AI architectures, particularly transformer architectures, have introduced new models of computation that are both practically powerful and theoretically intriguing. Transformers, built on mechanisms such as self-attention and layered representations, have achieved remarkable success across domains including natural language processing, vision, and scientific computing. The success of transformer architectures led to a new line of research to understand their computational power. Recent research formulated this question by modeling transformers as a computation device, thus enabling rigorous study of the power and limitations of this new model.

A growing body of work has identified fundamental limitations of standard transformer architectures [10, 3, 13, 9, 16, 14, 15]. In particular, under realistic assumptions, constant-depth transformers are less powerful than finite state machines, arguably the simplest computational model. A recent survey [21] gives an excellent account of the expressive power of transformers as a computational device. The intuitive reason is that constant-depth transformers operate in a largely feedforward manner and lack the ability to perform the iterative computation required for multi-step reasoning. As a result, even tasks that are conceptually simple but require sequential composition of intermediate steps fall outside their effective computational capabilities [PNP24; Dzi+23; Zha+24].

Modern AI systems are augmented with interactive reasoning steps where the output of a step can be used to further the computation. This is known as chain-of-thought (CoT) reasoning. Chain of thought (CoT) refers to a mode of computation in which a model produces explicit intermediate steps before arriving at a final answer. Instead of mapping an input directly to an output in one pass, the model generates a sequence of reasoning states that progressively transform the input into the solution.

From a computational perspective, this modification significantly alters the computational landscape. A growing body of work shows that the number of such intermediate reasoning steps plays a crucial role in determining expressivity. Thus, chain of thought emerges not merely as a heuristic for improving empirical performance, but as a fundamental computational resource in transformer-based models. In particular, Perez et al. show that this model is Turing complete in the sense that anything that is computable using a Turing machine is computable by transformers with enough CoT steps [17]. This connection is made more explicit by Merrill and Sabharwal [14]. In particular, they showed that any language recognized by t(n)t(n)-time bounded Turing machine can also be recognized via a transformer with O(t(n))O(t(n)) CoT steps and any transformer with O(t(n))O(t(n)) CoT steps can be simulated by a Turing machine in time O(t2(n)polylog⁡(t(n)))O(t^2(n)\operatorname{polylog}(t(n))).

In this work, we propose to view the number of CoT steps as a resource (such as time and space in TM model) and ask the fundamental question:

Is there a strict hierarchy for CoT reasoning?

Let CoT(r(n)r(n)) be the class of problems that can be decided by transformers with O(r(n))O(r(n)) chain-of-thought steps. Our main result is that for any reasonable function r(n)r(n), and for any ε>0\varepsilon> 0,

CoT⁡(r(n))⊊CoT⁡(r(n)1+ε).\operatorname{CoT}(r(n)) \subsetneq\operatorname{CoT}(r(n)^{1+\varepsilon}).

Our result shows that chain-of-thought reasoning admits a fine-grained hierarchy: a small increase in the number of reasoning steps strictly increases computational power. This establishes CoT as a meaningful computational resource similar to time and space.

A classical technique underlying hierarchy theorems is diagonalization. In the setting of Turing machines, diagonalization constructs a language that disagrees with every Turing machine running within a given resource bound, by simulating each such machine on carefully chosen inputs and flipping its output. This approach relies on two key properties: (i) machines can be effectively enumerated, and (ii) there exists a universal machine that can take the description of another machine as input and simulate its execution with controlled overhead. These properties make it possible in the Turing machine model to carry out self-referential constructions in a clean and uniform way.

For transformers with chain of thought, the situation appears to be different. While one can, in principle, encode a transformer as a finite object (e.g., via its architecture and parameters), there is no conceptually clean notion of what it means for a transformer to take another transformer as input and simulate its computation. Transformer computation is specified by a fixed architecture, layers, attention heads, and learned weights—rather than a discrete, easily interpretable transition function. As a result, even defining a “universal transformer” that can parse such an encoding and simulate another transformer’s behavior in a step-by-step manner is conceptually unclear. This lack of a natural encoding-and-simulation approach makes it difficult to carry out the kind of self-referential constructions that diagonalization relies on.

Our Approach

Our approach builds on known simulations relating chain-of-thought computation and classical time-bounded computation. In particular, [14] establishes the following: for any t(n)≥nt(n) \ge n (1) CoT⁡(t(n))⊆TIME⁡(t3(n))\operatorname{CoT}(t(n)) \subseteq\operatorname{TIME}(t^{3}(n))1 and (2) TIME⁡(t(n))⊆CoT⁡(t(n))\operatorname{TIME}(t(n)) \subseteq\operatorname{CoT}(t(n)). By combining these two simulations and using the classical time hierarchy theorem, we can get a coarse hierarchy theorem for CoT as follows.

CoT⁡(t(n))⊆TIME⁡(t3(n))⊆CoT⁡(t3(n))⊆TIME⁡(t9(n))⊆CoT⁡(t9(n))\operatorname{CoT}(t(n)) \subseteq\operatorname{TIME}(t^{3}(n)) \subseteq\operatorname{CoT}(t^{3}(n)) \subseteq\operatorname{TIME}(t^{9}(n)) \subseteq\operatorname{CoT}(t^{9}(n))

This chain of containments implies that CoT⁡(t(n))⊊CoT⁡(t9(n))\operatorname{CoT}(t(n)) \subsetneq\operatorname{CoT}(t^{9}(n)): if not, TIME⁡(t3(n))=TIME⁡(t9(n))\operatorname{TIME}(t^{3}(n)) = \operatorname{TIME}(t^{9}(n)), contradicting the Time Hierarchy Theorem. However, this approach only gives us a very coarse hierarchy theorem. In particular, it will not separate CoT⁡(n)\operatorname{CoT}(n) from CoT⁡(n2)\operatorname{CoT}(n^{2}), let alone separate CoT⁡(n)\operatorname{CoT}(n) from CoT⁡(n1.01)\operatorname{CoT}(n^{1.01}). A way to obtain such separation is via a very efficient simulation of transformers by a Turing machine, such as CoT⁡(t(n))⊆TIME⁡(t1+o(1)(n))\operatorname{CoT}(t(n)) \subseteq\operatorname{TIME}(t^{1+o(1)}(n)). However, each decoding step of a transformer attends to all preceding tokens. This incurs a quadratic cost to simulate via Turing machines.

To obtain sharper results, we use the padding technique from complexity theory. The padding technique enables the transformation of coarse separations into finer-grained separations. By adapting padding to the transformer model, we are able to derive a tighter separation: CoT⁡(r(n))⊊CoT⁡(r(n1+ε))\operatorname{CoT}(r(n)) \subsetneq\operatorname{CoT}(r(n^{1+\varepsilon})). The main technical challenge lies in adapting the padding technique to the transformer setting, where inputs and intermediate representations must be encoded within the constraints of the model. This is our primary technical contribution.

Related Work

In addition to the works discussed in the introduction on the expressive power of transformers with chain-of-thought reasoning, we briefly review classical hierarchy theorems from complexity theory. The time hierarchy theorem was established in the seminal work of Hartmanis and Stearns [11]. This was followed by the space hierarchy theorem in [20]. These results were subsequently extended to nondeterministic computation. The nondeterministic time hierarchy theorem [Co73; SFM78] shows that NTIME⁡(t(n))⊊NTIME⁡(t′(n))\operatorname{NTIME}(t(n)) \subsetneq\operatorname{NTIME}(t'(n)), for suitably larger t′(n)t'(n), while a corresponding nondeterministic space hierarchy theorem was established by Ibarra [12]. Establishing hierarchy theorems is still an active area of research in complexity theory, for example for randomized computations there has been only partial progress [1, 7, 8].

Recent works have studied transformers as formal computational models, relating their expressive power to automata, circuit complexity, and classical complexity classes. The work of [17] showed that transformers with hard attention and arbitrary precision are Turing complete. Subsequent work focused on understanding the power of transformer variants under bounded precision and architectural restrictions. The work of [10] showed that constant-depth transformers cannot recognize several basic formal languages, including PARITY and Dyck languages. The work of [9] proved hard-attention transformers can be simulated by constant depth circuits (belonging to the complexity class AC0\mathrm{AC}^{0}). In [16], this result has been extended to saturated attention transformers by providing an upper bound of TC0\mathrm{TC}^{0}. Other works further explored limitations arising from bounded depth, restricted attention, and finite precision [3, 13, 15, 14 PNP24]. More recently, the role of intermediate decoding and chain-of-thought reasoning has received attention [14, 5]. We refer the reader to [21] for a comprehensive survey of this rapidly growing area.

Preliminaries

We consider decoder-only transformers with chain of thought that can generate intermediate tokens between reading the input and generating an answer. Our model mostly follows definitions as in [14]. In the main body of the paper, we describe the main points about the model. We start here with notation that we use throughout the paper.

Let [n]:={1,2,…,n}[n] := \{1, 2, \ldots, n\}. A function t:N→Nt : \mathbb{N} \to\mathbb{N} is time-constructible if there is a Turing machine MM that on every input length of nn runs for exactly t(n)t(n) steps and stops. All standard time functions, such as polynomials and exponentials, have this property. We (most of the time) use lowercase letters (x,y,…)(x,y,\ldots) for tokens in an alphabet Σ\Sigma; uppercase letters (X,Y,…)(X,Y,\ldots) for strings in Σ∗\Sigma^*. We use lowercase bold letters (x,y,…)(\mathbf{x},\mathbf{y},\ldots) for vectors; uppercase bold letters (X,Y,…)(\mathbf{X},\mathbf{Y},\ldots) for matrices. We work with vectors which can be either a row matrix or a column matrix. Sometimes, we do not specify row or column for conciseness – whether a vector is a row or a column should be clear from context. We write “identity” to be the identity map, i.e. for any vector xx, identity⁡(x)=x\operatorname{identity}(x)=x. In terms of matrix, we write In×nI_{n \times n} for the n×nn \times n identity matrix. We write 0m×n0_{m \times n} for the m×nm \times n zero matrix. We write “−-” to mean “some number in R\mathbb{R},” and likewise we write (−)d(-)^d to mean “some vector in Rd\mathbb{R}^d”.

We use indicator-notation: if ϕ\phi is a proposition, we write

1(ϕ)={1if ϕ is true0otherwise.\mathbb{1}(\phi)=\begin{cases} 1 & \text{if }\phi\text{ is true}\\ 0 & \text{otherwise.} \end{cases}

Definition 2.1. Let Σ\Sigma be a finite alphabet containing a designated accept symbol 1∈Σ1 \in\Sigma. For a function r:N→Nr : \mathbb{N} \to\mathbb{N}, a transformer T:Σ∗→ΣT : \Sigma^* \to\Sigma decides a language L⊆Σ∗L \subseteq\Sigma^* in r(n)r(n) chain-of-thought steps if the following holds.

On input X∈ΣnX \in\Sigma^n, define a sequence of strings {fk(X)}k≥0\{f^k(X)\}_{k \ge0} by f0(X)=Xf^0(X)=X, fk+1(X)=fk(X)⋅T(fk(X))f^{k+1}(X)=f^k(X)\cdot T(f^k(X)), where ⋅\cdot denotes concatenation. Each T(fk(X))T(f^k(X)) is called a decoding step. After r(n)r(n) decoding steps, the output of TT is the final token of fr(n)(X)f^{r(n)}(X). We say that TT accepts XX if this output equals 11. The language decided by TT is

L={X∈Σ∗:the final token of fr(∣X∣)(X) is 1}.L=\{X\in\Sigma^*:\text{the final token of }f^{r(|X|)}(X)\text{ is }1\}.

We define CoT⁡(r(n))\operatorname{CoT}(r(n)) to be the class of all languages L⊆Σ∗L \subseteq\Sigma^* that are decided by some transformer TT using at most O(r(n))O(r(n)) decoding steps.

Let TIME⁡(t(n))\operatorname{TIME}(t(n)) denote the class of decision problems decidable in O(t(n))O(t(n)) time steps using a multi-tape Turing machine. Let TIME⁡~(t(n))\widetilde{\operatorname{TIME}}(t(n)) denote TIME⁡(t(n)polylog⁡(t(n)))\operatorname{TIME}(t(n)\operatorname{polylog}(t(n))). [14] shows the following relations between TIME⁡\operatorname{TIME} and CoT⁡\operatorname{CoT}.

Theorem 2.2 ([14]). With the definition above, we have that

TIME⁡(r(n))⊆CoT⁡(r(n))⊆TIME⁡(r2(n)+n2~).\operatorname{TIME}(r(n)) \subseteq\operatorname{CoT}(r(n)) \subseteq\operatorname{TIME}\left(\widetilde{r^2(n)+n^2}\right).

In particular, [14] assumes the following: log-precision (i.e. for input of length nn and r(n)r(n) intermediate decoding steps, precision is O(log⁡(n+r(n)))O(\log(n+r(n)))); saturated attention; and multi-layernorm. We make the same assumptions for our constructions, together with a few reasonable additional assumptions. More details on model assumptions are explained in Appendix A, in particular Remark A.6.

A technical note is that we use ∞\infty in our constructions throughout this paper. In fact, we take ∞\infty to be the max possible value (with precision O(log⁡(n+r(n)))O(\log(n+r(n))). More specifically, we can take the precision to be C⋅log⁡(n+r(n))C \cdot\log(n+r(n)) for suitably large CC, so that the max possible value is much bigger than all other values needed to encode matrix and vector values.

A Hierarchy Theorem for CoT

In this section, we state and prove the main results. Proofs in this section require a few primitives for manipulating transformers, which are provided in Appendix B. We start with the following lemma. Note that for padding to work (in general), r(n)≥nr(n) \ge n is a technical requirement. Padding as a technique is not translatable to the sub-linear regime.

Lemma 3.1. Let ε>0\varepsilon> 0. Let r(n)≥nr(n) \ge n such that r(n)r(n) is time-constructible, nondecreasing, unbounded, and log⁡(r(n+1))=(1+o(1))log⁡(r(n))\log(r(n + 1)) = (1 + o(1)) \log(r(n)). If CoT⁡(r(n))=CoT⁡(r(n)1+ε)\operatorname{CoT}(r(n)) = \operatorname{CoT}(r(n)^{1+\varepsilon}), then CoT⁡(r(n)1+ε)=CoT⁡(r(n)1+2ε)\operatorname{CoT}(r(n)^{1+\varepsilon}) = \operatorname{CoT}(r(n)^{1+2\varepsilon}).

Proof of Lemma 3.1. First, we give an outline of how padding works (see Figure 1 for an illustration). Let LL be a language in CoT⁡(r(n)1+2ε)\operatorname{CoT}(r(n)^{1+2\varepsilon}). We will show that L∈CoT⁡(r(n)1+ε)L \in\operatorname{CoT}(r(n)^{1+\varepsilon}) under the assumption that CoT⁡(r(n))=CoT⁡(r(n)1+ε)\operatorname{CoT}(r(n)) = \operatorname{CoT}(r(n)^{1+\varepsilon}). Define a “padded” language L′={X(∗)f(∣X∣):X∈L}L' = \{X(*)^{f(|X|)} : X \in L\} (for an appropriately chosen function ff), where X(∗)f(∣X∣)X(*)^{f(|X|)} means a string XX padded with f(∣X∣)f(|X|) many *’s. Note that LL and L′L' are essentially the same except the input is padded with *’s. Thus, using the transformer TT for LL as a black box, we design a transformer T′T' for L′L' using the same absolute number of CoT steps, i.e. the number of CoT steps of TT on input XX is essentially the same as the number of CoT steps of T′T' on input X∗f(∣X∣)X*^{f(|X|)}. However, as a function of input length, T′T' will recognize L′L' in r(n)1+εr(n)^{1+\varepsilon} CoT steps. Now, by the assumption that CoT⁡(r(n))=CoT⁡(r(n)1+ε)\operatorname{CoT}(r(n)) = \operatorname{CoT}(r(n)^{1+\varepsilon}), we obtain that L′L' is indeed in CoT⁡(r(n))\operatorname{CoT}(r(n)) via a transformer SS. Since LL and L′L' are essentially the same languages, we can design a new transformer for LL by padding an input XX appropriately and simulate SS on the padded input. This ensures that the new transformer for LL runs in r(n)1+εr(n)^{1+\varepsilon} CoT steps.

Padding argument diagram showing transformers $T$, $T'$, $S$, and $R$, with input and generated-token regions, padding, computations, CoT-step budgets, and transitions for simulating $T$, applying the class-equality assumption, and generating padding while simulating $S$

Figure 1. Padding argument for Lemma 3.1. Write m=∣X∣m = |X| and N=m+f(m)N = m + f(m), with ff chosen as in the proof. The transformer T′T' simulates TT on a padded input with the same absolute CoT budget. The assumed class equality yields a transformer SS with budget O(r(N))O(r(N)). Finally, RR generates the padding using CoT and simulates SS. Dashed vertical lines separate supplied input from generated tokens; gray blocks denote padding. Bar lengths are schematic.

While this is the high-level approach, there are several technical challenges to adapt this to the transformer setting. First, to simulate TT, one technical challenge is that TT expects an unpadded input over Σ\Sigma. If we just run TT as it is on X∗f(∣X∣)X*^{f(|X|)}, then since attention of CoT steps are over * tokens as well, this would change the output for TT in ways that we can’t control. Another challenge is to copy the output of a position ii to a position j>ij > i far apart (where position ii means the iith token in the input string) — so that desired calculations can be carried out. To simulate SS, a technical challenge is when we receive XX as input, we need to include layers and heads that pads * tokens using CoT steps. The difficulty is to make sure that these layers/heads take effect only in certain steps.

We now give details of the construction. In this proof, we will focus on a polynomial r(n)r(n). We will explain how this can be extended to other classes of functions after the proof.

Assume CoT⁡(r(n))=CoT⁡(r(n)1+ε)\operatorname{CoT}(r(n)) = \operatorname{CoT}(r(n)^{1+\varepsilon}). Let L∈CoT⁡(r(n)1+2ε)L \in\operatorname{CoT}(r(n)^{1+2\varepsilon}), and let TT be a transformer that recognizes LL in O(r(n)1+2ε)O(r(n)^{1+2\varepsilon}) CoT steps. Let Σ\Sigma be the alphabet for LL.

Define the padded language L′={X(∗)⌊∣X∣c⌋:X∈L,∗∉Σ}L' = \{X(*)^{\lfloor|X|^c \rfloor} : X \in L, * \notin\Sigma\}, where c=1+2ε1+εc = \frac{1+2\varepsilon}{1+\varepsilon} and ∗* is a new character (just used for padding). So, the alphabet for L′L' is Σ′=Σ∪{∗}\Sigma' = \Sigma\cup\{*\}.

As mentioned above, the proof consists of the constructions of the following two transformers.

Transformer T′T': Using transformer TT, we can construct transformer T′T' that recognizes L′L' in O(r(n)1+ε)O(r(n)^{1+\varepsilon}) CoT steps. Then, by our assumption, L′∈CoT⁡(r(n))L' \in\operatorname{CoT}(r(n)). Let SS be the transformer that recognizes L′L' in O(r(n))O(r(n)) CoT steps.

Transformer RR: Using transformer SS, we construct a transformer RR that recognizes LL in O(r(n)1+ε)O(r(n)^{1+\varepsilon}) CoT steps.

Construction of T′T'

Note that for any i∈Ni \in\mathbb{N}, we say xix_i, xix_i, or ii to refer to position ii, i.e. step number ii, that a transformer takes.

We first give a high-level description of our construction: We take as input strings that look like X=x1…xmxm+1…xm+⌊mc⌋X = x_1 \ldots x_m x_{m+1} \ldots x_{m+\lfloor m^c \rfloor}, where m∈Nm \in\mathbb{N}. Let X1:=x1…xmX_1 := x_1 \ldots x_m and X2:=xm+1…xm+⌊mc⌋X_2 := x_{m+1} \ldots x_{m+\lfloor m^c \rfloor}. Let xix_i denote the embedding of token xix_i, xi(l)x_i^{(l)} denote the intermediate value after layer ll, and yiy_i denote the output at token xix_i. Our goal is to design a transformer to check if X∈L′X \in L', i.e. check if X1∈LX_1 \in L and X2=∗⌊mc⌋X_2 = *^{\lfloor m^c \rfloor}. To check if X1∈LX_1 \in L, we use a mask function to “ignore” tokens in X2X_2, and run CoT steps as in TT by attending over tokens in X1X_1 and CoT-generated tokens xm+⌊mc⌋+1,xm+⌊mc⌋+2,…x_{m+\lfloor m^c\rfloor+1}, x_{m+\lfloor m^c\rfloor+2}, \ldots. Note that the mask function is not necessary in our construction, it just helps with making our proof concise – see Remark 3.2. The challenge is to make xm+⌊mc⌋+1x_{m+\lfloor m^c\rfloor+1} equal to ymy_m so that the first CoT step of T′T' is exactly the same as the first CoT step of TT. To check if X2=∗⌊mc⌋X_2 = *^{\lfloor m^c\rfloor}, we just need a single decoding step.

Let the parameters of TT be: alphabet: Σ\Sigma; embedding dimension: dd; number of layers: LL; number of heads per layer: HH; output function: affine linear transformation γT\gamma_T.

We construct T′T' with parameters: alphabet: Σ′=Σ∪{∗}\Sigma' = \Sigma\cup\{*\}; input string: x1x2…xnx_1x_2 \ldots x_n, where n=m+⌊mc⌋n = m + \lfloor m^c\rfloor, m∈Nm \in\mathbb{N}; embedding dimension: D=2d+22D = 2d + 22. The embedding dimensions are (note that more details will be provided later in the proof):

  • All WE and PE used for LL remain the same. Say they occupy dimension numbers 1 to dd of our vectors. When simulating TT, T′T' acts on these dimensions only.

  • 1 dimension for saving is xi=∗x_i = *, WLOG, say dimension number d+1d+1.

  • 5 dimensions for additional PE: ii, mm, m+⌊mc⌋m+\lfloor m^c\rfloor, 1⌊mc⌋\frac{1}{\lfloor m^c\rfloor}, 1i\frac{1}{i}.

  • Dimensions that serve as placeholders. They are:

    • 1 dimension for saving if xm+1=⋯=xm+⌊mc⌋=∗x_{m+1} = \cdots= x_{m+\lfloor m^c\rfloor} = *, say dimension d+2d+2.

    • 1 dimension for saving 1(Att=1/⌊mc⌋)\mathbb{1}(\mathrm{Att} = 1/\lfloor m^c\rfloor), say dimension d+3d+3.

    • dd dimensions for saving the copied xn(L+1)x_n^{(L+1)}.

    • Other placeholder dimensions.

Word embedding (WE): We define WE for T′T' by using the exact same WE as for TT, except for adding a new dimension (dimension number d+1d+1) which is 1 iff character is ∗* (and 0 otherwise). That is, WE⁡(∗)\operatorname{WE}(*) is 1 at dimension d+1d+1, and 0 elsewhere.

Positional encoding (PE): In addition to PE of TT, we also use a supplementary PE (so that in case TT changes the values of its original PE via calculations, we still have additional PE values to use). For input length n=m+⌊mc⌋n = m + \lfloor m^c\rfloor, we define the supplement PE to be PE⁡n(i)=[imm+⌊mc⌋1⌊mc⌋1i]\operatorname{PE}_n(i) = \left[i\quad m\quad m+\lfloor m^c\rfloor\quad\frac{1}{\lfloor m^c\rfloor}\quad\frac{1}{i}\right]. Note that PE⁡n:N→RD\operatorname{PE}_n : \mathbb{N} \to\mathbb{R}^D. We just write the 5 dimensions that matter (the rest are understood to be all 0’s). We save supplement PE in 5 unique dimensions in embedding vectors (i.e. xix_i’s). Heads and layers: T′T' has L′:=L+5L' := L + 5 layers, each layer has HH heads.

  • Layers 1 to LL: exactly the same as the layers of TT. Note that each head in these layers acts on dimensions 1 to dd (i.e. the dimensions that T′T' shares with TT) only.

  • Layers L+1L + 1 to L+5L + 5: additional layers, to be described.

Multi-layernorms: put multi-layernorms only where TT has.

Attention mask function: we use causal mask; the only difference is that

mask⁡(i,j)=−∞if i>m+⌊mc⌋ and j∈{m+1,m+2,…,m+⌊mc⌋}.\operatorname{mask}(i,j)=-\infty\qquad\text{if }i>m+\lfloor m^c\rfloor\text{ and }j\in\{m+1,m+2,\ldots,m+\lfloor m^c\rfloor\}.

Note again that this tailored attention mask is avoidable (see Remark 3.2). That is, causal mask suffices; the use of the tailored mask is just for conciseness.

Output function: γT′:=δ2∘γT∘δ1\gamma_{T'} := \delta_2 \circ\gamma_T \circ\delta_1, where δ1,δ2\delta_1,\delta_2 are affine linear transformations Rd→Rd\mathbb{R}^d \to\mathbb{R}^d. Since composition of affine linear transformations is an affine linear transformation, we have that γT′\gamma_{T'} is an affine linear transformation.

How T′T' runs:

  • For x1x_1 to xmx_m: Run exactly as TT would run.

    Now, we denote the vector at position mm after layer LL (i.e. the last layer) of TT be vTv_T. Note that we have xm(L)=vTx_m^{(L)}=v_T (since T′T' runs exactly as TT for its first LL layers).

    Note that xm(L)x_m^{(L)} has dimension DD, while vTv_T has dimension dd. When we write xm(L)=vTx_m^{(L)}=v_T, it actually means that “at the dimensions that T′T' shares with TT, we have xm(L)=vTx_m^{(L)}=v_T.”

  • For xm+1x_{m+1} to xm+⌊mc⌋−1x_{m+\lfloor m^c\rfloor-1}: It does not matter what T′T' does here.

  • For xm+⌊mc⌋x_{m+\lfloor m^c\rfloor}: Copy the first dd dimensions of xm(L)x_m^{(L)} (=vT=v_T) to this position. Also check if xm+1=xm+2=⋯=xm+⌊mc⌋=∗x_{m+1}=x_{m+2}=\cdots=x_{m+\lfloor m^c\rfloor}=*.

    First, we add a layer (layer L+1L+1) with no head, so as to use its feedforward network to calculate 1(j=m)\mathbb{1}(j=m) and 1(j=m+⌊mc⌋)\mathbb{1}(j=m+\lfloor m^c\rfloor) (as in Lemma B.1). Note that layer needs to share the number of heads with other layers, but we can just make all heads “silent.”

    Note that for each 1\mathbb{1} function, we need 3 placeholder dimensions and 1 dimension. In total, we need 8 dimensions.

    To copy xm(L)x_m^{(L)} to position m+⌊mc⌋m+\lfloor m^c\rfloor, we do the following.

    – We add a layer (layer L+2L+2). This layer has a head (head 1). For query ii and key jj, choose Wq\mathbf{W}_q and Wk\mathbf{W}_k such that

qiTkj=1⋅(−1j)+1(i=m+⌊mc⌋)⋅1(j=m).\mathbf{q}_i^{\mathsf{T}}\mathbf{k}_j=1\cdot\left(-\frac{1}{j}\right)+\mathbb{1}(i=m+\lfloor m^c\rfloor)\cdot\mathbb{1}(j=m).

where 1(i=m+⌊mc⌋)\mathbb{1}(i=m+\lfloor m^c\rfloor) and 1(j=m)\mathbb{1}(j=m) are calculated from layer L+1L+1; 1 can be easily obtained by adding a 1 in a designated dimension in any layer before L+2L+2.

We just use 2 additional dimensions, one to line up 1 for qi\mathbf{q}_i and (−1j)\left(-\frac{1}{j}\right) for kj\mathbf{k}_j, and the other to line up 1(i=m+⌊mc⌋)\mathbb{1}(i=m+\lfloor m^c\rfloor) for qi\mathbf{q}_i and 1(j=m)\mathbb{1}(j=m) for kj\mathbf{k}_j.

Note that when average hardmax is applied:

η=η(i):=arg max⁡j:j≤i(qiTkj)={iif i≠m+⌊mc⌋,mif i=m+⌊mc⌋.\eta=\eta(i):=\underset{j:j\leq i}{\operatorname{arg\,max}}(\mathbf{q}_i^{\mathsf{T}}\mathbf{k}_j)= \begin{cases} i & \text{if }i\ne m+\lfloor m^c\rfloor,\\ m & \text{if }i=m+\lfloor m^c\rfloor. \end{cases}

We design Wv\mathbf{W}_v such that Wv\mathbf{W}_v copies dimensions 1 to dd of xη(L+1)x_\eta^{(L+1)} to some dd placeholder dimensions of xi(L+2)x_i^{(L+2)}.

We need dd placeholder dimensions here.

– Then, at output function γT′\gamma_{T'}, before applying γT\gamma_T, we perform a linear transformation to switch the dd placeholder dimensions above to dimension 1 to dd using the linear transformation W\mathbf{W}. Note that only at i=m+⌊mc⌋i=m+\lfloor m^c\rfloor do changes appear: vTv_T is now “copied” to position m+⌊mc⌋m+\lfloor m^c\rfloor of T′T', and we obtain ym+⌊mc⌋=γT(vT)y_{m+\lfloor m^c\rfloor}=\gamma_T(v_T). This makes xm+⌊mc⌋+1x_{m+\lfloor m^c\rfloor+1} in T′T' equal to xm+1x_{m+1} in TT, as desired.

To check if xm+1=⋯=xm+⌊mc⌋=∗x_{m+1}=\cdots=x_{m+\lfloor m^c\rfloor}=*, we need 3 additional layers (layers L+3L+3, L+4L+4, L+5L+5). Say we want to save this in dimension number d+2d+2.

– Design the following head (head 1 of layer L+3L+3). Attend to all positions where character is ∗*. Make query to be just mm (or any positive constant available), and make keys to be 1(xj=∗)\mathbb{1}(x_j = \ast) (by simply taking the value at the dimension d+1d+1). Make values to be all 1(j=m+⌊mc⌋)\mathbb{1}(j = m + \lfloor m^c \rfloor). We have the following cases:

  • If xm+1=⋯=xm+⌊mc⌋=∗x_{m+1} = \cdots= x_{m+\lfloor m^c \rfloor} = \ast, then attention weights are exactly 1/⌊mc⌋1/\lfloor m^c \rfloor at positions m+1,…,m+⌊mc⌋m+1,\ldots,m+\lfloor m^c \rfloor and 00 elsewhere. Therefore, attention (Att) is 1/⌊mc⌋1/\lfloor m^c \rfloor (as values are 00 except for being 11 at position m+⌊mc⌋m+\lfloor m^c \rfloor).

  • If among xm+1,…,xm+⌊mc⌋x_{m+1},\ldots,x_{m+\lfloor m^c \rfloor}, some are ∗\ast and the others are not, then Att is either 00 or 1/t1/t (where t<⌊mc⌋t < \lfloor m^c \rfloor).

  • If among xm+1,…,xm+⌊mc⌋x_{m+1},\ldots,x_{m+\lfloor m^c \rfloor}, none are ∗\ast, then we have uniform attention over all positions 1,…,m+⌊mc⌋1,\ldots,m+\lfloor m^c \rfloor. So, Att is 1m+⌊mc⌋⋅1=1m+⌊mc⌋\frac{1}{m+\lfloor m^c \rfloor}\cdot1 = \frac{1}{m+\lfloor m^c \rfloor}.

  • Now, we just need to check if Att⁡=1⌊mc⌋\operatorname{Att} = \frac{1}{\lfloor m^c \rfloor}, where 1⌊mc⌋\frac{1}{\lfloor m^c \rfloor} is given from PE. By Lemma B.5, we use the feedforward network of layer L+3L+3 and layer L+4L+4 to get 1(Att⁡=1⌊mc⌋)\mathbb{1}\left(\operatorname{Att} = \frac{1}{\lfloor m^c \rfloor}\right), as desired. Say the result is saved in dimension d+3d+3.

Note that we need 3 additional dimensions to use the construction in Lemma B.5.

  • Last, we use a layer (layer L+5L+5) to implement the feedforward network as in Lemma B.7, whose effect is to copy the results in dimension d+3d+3 to dimension d+2d+2 iff 1(i=m+⌊mc⌋)\mathbb{1}(i = m+\lfloor m^c \rfloor).

Note that we need 3 additional dimensions for this.

  • CoT steps: Run exactly as if TT would run (on the dimensions that TT acts on, ignoring other dimensions). Attention mask makes each CoT step attend to only x1x_1 to xmx_m and other CoT steps (i.e. xm+⌊mc⌋+1,xm+⌊mc⌋+2,…x_{m+\lfloor m^c \rfloor+1},x_{m+\lfloor m^c \rfloor+2},\ldots, which are equivalent to xm+1,xm+2,…x_{m+1},x_{m+2},\ldots of TT, respectively). Let the transformer run O(r(m)1+2ε)O(r(m)^{1+2\varepsilon}) CoT steps (as TT does).

  • Last, we output True (i.e. x1…xm+⌊mc⌋x_1\ldots x_{m+\lfloor m^c \rfloor} is indeed in L′L') if the following are correct:

    • The result of T′T' (equivalently TT) on x1x2…xmx_1x_2\ldots x_m says that X1=x1x2…xmX_1=x_1x_2\ldots x_m is in LL.

    • 1(xm+1=⋯=xm+⌊mc⌋=∗)=1\mathbb{1}(x_{m+1}=\cdots=x_{m+\lfloor m^c \rfloor}=\ast)=1.

This concludes our construction for T′T'.

We obtain that L′L' can be recognized in O(r(m)1+2ε)O(r(m)^{1+2\varepsilon}) CoT steps by the transformer T′T'. As rr is of polynomial order and c=1+2ε1+εc=\frac{1+2\varepsilon}{1+\varepsilon}, we have

O(r(m)1+2ε)=O(r(m+⌊m1+2ε1+ε⌋)1+ε).O(r(m)^{1+2\varepsilon}) = O\left(r\left(m+\left\lfloor m^{\frac{1+2\varepsilon}{1+\varepsilon}}\right\rfloor\right)^{1+\varepsilon}\right).

As we consider the input of L′L' is of length m+⌊mc⌋m+\lfloor m^c\rfloor, we obtain that L′∈CoT⁡(r(n)1+ε)L' \in\operatorname{CoT}(r(n)^{1+\varepsilon}), as desired.

Remark 3.2. We can simulate the effects of the attention mask for T′T', as detailed below.

  • Prepend five first layers of the transformer to do the following:

    • Layers 1, 2: Calculate 1(i≥m+⌊mc⌋+1)\mathbb{1}(i \ge m+\lfloor m^c\rfloor+1) by Corollary B.3.

    • Layers 3, 4: Calculate 1(m+1≤i≤m+⌊mc⌋)\mathbb{1}(m+1 \le i \le m+\lfloor m^c\rfloor) by Corollary B.3.

    • Layer 5: Use feedforward network to multiply −∞-\infty to 1(m+1≤i≤m+⌊mc⌋)\mathbb{1}(m+1 \le i \le m+\lfloor m^c\rfloor) to get (−∞)1(m+1≤i≤m+⌊mc⌋)(-\infty)\mathbb{1}(m+1 \le i \le m+\lfloor m^c\rfloor).

  • Next, for every layer of T′T' that does not involve multi-layernorm, use the exact same heads as for the original T′T' with the only difference that for every head, for any query ii and key jj, embed the query qiq_i and key kjk_j such that:

qiTkj=1(i≥m+⌊mc⌋+1)(1(m+1≤j≤m+⌊mc⌋)⋅(−∞))+S,q_i^{\mathsf{T}} k_j = \mathbb{1}(i \ge m+\lfloor m^c\rfloor+1)\left(\mathbb{1}(m+1 \le j \le m+\lfloor m^c\rfloor)\cdot(-\infty)\right)+S,

where SS is the value qiTkjq_i^{\mathsf{T}}k_j in the original T′T'.

So, when i≥m+⌊mc⌋+1i \ge m+\lfloor m^c\rfloor+1 and m+1≤j≤m+⌊mc⌋m+1 \le j \le m+\lfloor m^c\rfloor, we have qiTkj=−∞q_i^{\mathsf{T}}k_j=-\infty, meaning query ii will not attend to key jj. Otherwise, qiTkj=0+S=Sq_i^{\mathsf{T}}k_j=0+S=S, so attention is calculated as normal.

  • On the other hand, for every layer of T′T' that involves multi-layernorm, multi-layernorm will alter the values vi,1:=1(i≥m+⌊mc⌋+1)v_{i,1}:=\mathbb{1}(i \ge m+\lfloor m^c\rfloor+1) and vi,2:=1(m+1≤i≤m+⌊mc⌋)v_{i,2}:=\mathbb{1}(m+1 \le i \le m+\lfloor m^c\rfloor), which is not what we want.

*To solve this, say [xi vi,1 vi,2][x_i\ v_{i,1}\ v_{i,2}] is the current vector, and T′T' wants to perform multi-layernorm on the vector xix_i only. Denote xˉi=multi_layer_norm⁡(xi)\bar{x}_i=\operatorname{multi\_layer\_norm}(x_i). We can add a layer just to perform multi-layernorm on xix_i (by making a head such that value at jj is xjx_j, key at jj is j−mean⁡norm⁡\frac{j-\operatorname{mean}}{\operatorname{norm}}, and query is 11 – this head gives xˉi\bar{x}_i as attention). We save xˉi\bar{x}_i into placeholder dimensions. So, after this layer, we yield [xi vi,1 vi,2 xˉi][x_i\ v_{i,1}\ v_{i,2}\ \bar{x}_i].

Next we add a layer on what T′T' would have performed on xˉi\bar{x}_i. The difference is that now we already have xˉi\bar{x}_i, so no need to do multi-layernorm anymore (therefore, the numbers vi,1v_{i,1} and vi,2v_{i,2} are intact). We just need to use the numbers vi,1v_{i,1} and vi,2v_{i,2} to reproduce the mask and perform calculations just on xˉi\bar{x}_i (like in the bullet point above).

Thus, we have obtained the desired mask effect.

Construction of RR

We have shown that L′∈CoT⁡(r(n)1+ε)L' \in\operatorname{CoT}(r(n)^{1+\varepsilon}). By the assumption that CoT⁡(r(n))=CoT⁡(r(n)1+ε)\operatorname{CoT}(r(n)) = \operatorname{CoT}(r(n)^{1+\varepsilon}), we obtain L′∈CoT⁡(r(n))L' \in\operatorname{CoT}(r(n)). Let SS be the transformer that recognizes L′L' in O(r(n))O(r(n)) CoT steps. We will use SS to construct a transformer RR that recognizes LL in O(r(n)1+ε)O(r(n)^{1+\varepsilon}) CoT steps.

On a high level, we design RR as follows. RR gets X=x1…xmX = x_1 \ldots x_m as input. On XX, we want to simulate SS. To do that, for CoT steps ≤⌊mc⌋\leq\lfloor m^c \rfloor (i.e. at positions m+1,…,m+⌊mc⌋m+1,\ldots,m+\lfloor m^c\rfloor), we generate letters ∗* (i.e. we are trying to reconstruct X(∗)⌊mc⌋X(*)^{\lfloor m^c\rfloor}). Then, on CoT steps >⌊mc⌋> \lfloor m^c\rfloor, we simulate SS (as if we are in the CoT steps of SS on L′L').

Let the parameters of SS be: embedding dimension: dd; number of layers: LL; number of heads per layer: HH; output function: γS\gamma_S.

We now describe the parameters of RR:

Input (after embedding as for SS): x1,x2,…,xmx_1,x_2,\ldots,x_m, where xi∈Rdx_i \in\mathbb{R}^d.

Positional encoding: as noted in Remark A.6:

  • For xix_i, save ii to encode position. Also, use fixed numbers mm, ⌊mc⌋\lfloor m^c\rfloor in PE. So, PE⁡m(i)=[i  m  ⌊mc⌋]\operatorname{PE}_m(i) = [i\ \ m\ \ \lfloor m^c\rfloor]

  • For CoT steps: at xix_i where i>mi > m, PE is updated to ii.

Embedding dimension: D=3d+4D = 3d + 4, namely

  • dd for xix_i (matching with SS). WLOG, suppose:

    • SS acts on dimension 11 to dd.

    • Dimension dd is to save 1(xi=∗)\mathbb{1}(x_i = *).

  • Some 3 dimensions for positional encoding.

  • 2d+12d + 1 additional dimensions (WLOG, dimensions d+1d+1 to 3d+13d+1). In particular:

    • Dimensions d+1d+1 to 3d+13d+1 is for saving vˉ\bar{v} (this will be explained below).

Number of layers and heads: L+3L + 3 layers, HH heads in each layer.

  • The first LL layers of RR match with SS (these layers act only on dimensions that SS has effects on (i.e. dimensions 11 to dd)).

Attention masks: standard causal mask.

Output function: γR=δ∘γS\gamma_R = \delta\circ\gamma_S, where δ\delta is an affine linear transformation.

Descriptions and justifications of RR:

  • For x1,…,xm−1x_1,\ldots,x_{m-1}: Run exactly as SS.

  • For xm,…,xm+⌊mc⌋−1x_m,\ldots,x_{m+\lfloor m^c\rfloor-1} (i.e CoT steps 1 to ⌊mc⌋\lfloor m^c\rfloor):

    • Run calculations exactly as for SS.

    • Also, we need to simulate paddings here: for xix_i (m≤i≤m+⌊mc⌋−1m \leq i \leq m+\lfloor m^c\rfloor-1), we want the output yiy_i of position ii to be ∗* (i.e. yiy_i has WE for ∗*: 0 from dimensions 1 to d−1d-1, and 1 at dimension dd).

      • First, we use 2 layers (layers L+1L+1 and L+2L+2) to compute f(i):=1(m≤i≤m+⌊mc⌋−1)f(i) := \mathbb{1}(m \leq i \leq m+\lfloor m^c\rfloor-1) as in Corollary B.3. We save f(i)f(i) in a designated dimension. Also, we save g(i)g(i) at the dimension to indicate ∗* (i.e. dimension dd).

  • Let v=v(i):=xi(L+2)\mathbf{v}=\mathbf{v}(i):=\mathbf{x}_i^{(L+2)} represent the vector that we obtain for query ii after layer L+2L+2, restricted on dimensions 11 through d−1d-1. The idea is that we would want to make v=0d−1\mathbf{v}=0_{d-1} if f(i)=1f(i)=1, and v\mathbf{v} intact otherwise. We proceed as follows. Use a previous feedforward network (say, of layer L+2L+2) to save v\mathbf{v} in some separate d−1d-1 placeholder dimensions, say, WLOG, dimensions d+1d+1 to 2d−12d-1. Next, use a layer (layer L+3L+3) to calculate vˉ:=f(i)v\bar{\mathbf{v}}:=f(i)\mathbf{v} (on dimensions d+1d+1 to 2d−12d-1) - this can be done by Lemma B.9. Last, in the output function, calculate v−vˉ\mathbf{v}-\bar{\mathbf{v}} (on dimensions 11 to d−1d-1). Note that this returns v\mathbf{v} if f(i)=0f(i)=0, and 0d−10_{d-1} if f(i)=1f(i)=1, as desired.

    • Thus, the effect is that xi=∗\mathbf{x}_i=\ast, for all m+1≤i≤m+⌊mc⌋m+1\le i\le m+\lfloor m^c\rfloor.

    Note that in total, we need 3+2(d−1)=2d+13+2(d-1)=2d+1 additional dimensions.

  • For later steps: for xm+⌊mc⌋\mathbf{x}_{m+\lfloor m^c\rfloor} (and after): just run SS. For instance, at xm+⌊mc⌋\mathbf{x}_{m+\lfloor m^c\rfloor}, it is exactly as if SS runs on the input X′=x1…xm+⌊mc⌋X'=x_1\ldots x_{m+\lfloor m^c\rfloor}. Subsequently, step m+⌊mc⌋+rm+\lfloor m^c\rfloor+r of RR is exactly the same as CoT step number rr of SS. Therefore, RR will be able to accept or reject X=x1…xmX=x_1\ldots x_m correctly in

(⌊mc⌋)+O(r(m+⌊mc⌋))=O(r(m)c)\left(\lfloor m^c\rfloor\right)+O\left(r\left(m+\lfloor m^c\rfloor\right)\right)=O\left(r(m)^c\right)

CoT steps. The construction of RR is completed.

Now, with RR, we obtain that L∈CoT⁡(r(n)c)=CoT⁡(r(n)1+2ε1+ε)⊆CoT⁡(r(n)1+ε)L\in\operatorname{CoT}(r(n)^c)=\operatorname{CoT}\left(r(n)^{\frac{1+2\varepsilon}{1+\varepsilon}}\right)\subseteq\operatorname{CoT}(r(n)^{1+\varepsilon}). In particular, we obtain L∈CoT⁡(r(n)1+ε)L\in\operatorname{CoT}(r(n)^{1+\varepsilon}).

Thus, we can conclude that CoT⁡(r(n)1+ε)=CoT⁡(r(n)1+2ε)\operatorname{CoT}(r(n)^{1+\varepsilon})=\operatorname{CoT}(r(n)^{1+2\varepsilon}).

This completes the proof of Lemma 3.1.

Remark 3.3 (Extending to time-constructible functions). Fix ε>0\varepsilon>0, and suppose that r:N→Nr:\mathbb{N}\to\mathbb{N} is nondecreasing and unbounded, with r(n)≥nr(n)\ge n and

log⁡r(n+1)=(1+o(1))log⁡r(n).\log r(n+1)=(1+o(1))\log r(n).

The proof above will go through if we pad the input XX to X(∗)f(∣X∣)X(\ast)^{f(|X|)} for some function ff, such that

r1+2ε1+ε(n)≤r(n+f(n))≤r1+ε(n).r^{\frac{1+2\varepsilon}{1+\varepsilon}}(n)\le r(n+f(n))\le r^{1+\varepsilon}(n).

Write

α=1+2ε1+ε,β=1+ε,\alpha=\frac{1+2\varepsilon}{1+\varepsilon},\qquad\beta=1+\varepsilon,

and choose a rational constant θ\theta satisfying α<θ<β\alpha<\theta<\beta. Such a choice exists because

β−α=ε21+ε>0.\beta-\alpha=\frac{\varepsilon^2}{1+\varepsilon}>0.

Define

m(n)=min⁡{m∈N:m≥n, r(m)≥r(n)θ},f(n)=m(n)−n.m(n)=\min\{m\in\mathbb{N}:m\ge n,\ r(m)\ge r(n)^\theta\},\qquad f(n)=m(n)-n.

Unboundedness of rr ensures that m(n)m(n) exists, and f(n)f(n) is a nonnegative integer. For all sufficiently large nn, we have r(n)>1r(n)>1, so m(n)>nm(n)>n, and minimality gives

r(m(n)−1)<r(n)θ≤r(m(n)).r(m(n)-1)<r(n)^\theta\le r(m(n)).

Furthermore, the assumed growth condition and β/θ>1\beta/\theta>1 imply that

r(k+1)≤r(k)β/θr(k+1)\le r(k)^{\beta/\theta}

for all sufficiently large kk. Applying this with k=m(n)−1≥nk=m(n)-1\ge n, we obtain

r(n)α≤r(n)θ≤r(n+f(n))≤r(m(n)−1)β/θ<r(n)β.r(n)^\alpha\le r(n)^\theta\le r(n+f(n))\le r(m(n)-1)^{\beta/\theta}<r(n)^\beta.

Thus padding XX to X(∗)f(∣X∣)X(\ast)^{f(|X|)} satisfies the required growth inequalities for all sufficiently large input lengths.

Proof of Main Theorem

Equipped with Lemma 3.1, we are ready to prove our main theorem.

Theorem 3.4. For ε>0\varepsilon> 0 and r(n)≥nr(n) \ge n such that r(n)r(n) is time-constructible, nondecreasing, unbounded, and log⁡(r(n+1))=(1+o(1))log⁡(r(n))\log(r(n+1)) = (1+o(1))\log(r(n)), we have CoT(r(n))⊈CoT(r(n)1+ε)\mathrm{CoT}(r(n)) \not\subseteq\mathrm{CoT}(r(n)^{1+\varepsilon}).

Proof. Let ε>0\varepsilon> 0 be arbitrary. Suppose, for the sake of contradiction, that CoT(r(n))=CoT(r(n)1+ε)\mathrm{CoT}(r(n)) = \mathrm{CoT}(r(n)^{1+\varepsilon}).

By Theorem 2.2, we have

CoT(r(n))⊆TIME(r(n)3)⊆CoT(r(n)3)⊆TIME(r(n)9)⊆CoT(r(n)9),\mathrm{CoT}(r(n)) \subseteq\mathrm{TIME}(r(n)^3) \subseteq\mathrm{CoT}(r(n)^3) \subseteq\mathrm{TIME}(r(n)^9) \subseteq\mathrm{CoT}(r(n)^9),

By Time Hierarchy Theorem, TIME(r(n)3)⊈TIME(r(n)9)\mathrm{TIME}(r(n)^3) \not\subseteq\mathrm{TIME}(r(n)^9). Thus, from the chain of containment, CoT(r(n))⊈CoT(r(n)9)\mathrm{CoT}(r(n)) \not\subseteq\mathrm{CoT}(r(n)^9).

Now, note that CoT(r(n))=CoT(r(n)1+ε)\mathrm{CoT}(r(n)) = \mathrm{CoT}(r(n)^{1+\varepsilon}) by assumption. Then, by Lemma 3.1, we obtain that CoT(r(n))=CoT(r(n)1+2ε)\mathrm{CoT}(r(n)) = \mathrm{CoT}(r(n)^{1+2\varepsilon}). Now, applying the lemma again, we get that CoT(r(n))=CoT(r(n)1+4ε)\mathrm{CoT}(r(n)) = \mathrm{CoT}(r(n)^{1+4\varepsilon}). After ii applications, we get CoT(r(n))=CoT(r(n)1+2iε)\mathrm{CoT}(r(n)) = \mathrm{CoT}(r(n)^{1+2^i\varepsilon}). By choosing i=⌈log⁡28ε⌉i = \lceil\log_2 \frac{8}{\varepsilon} \rceil, we get that CoT(r(n))=CoT(r(n)9)\mathrm{CoT}(r(n)) = \mathrm{CoT}(r(n)^9), contradiction.

Thus, we can conclude now that CoT(r(n))⊈CoT(r(n)1+ε)\mathrm{CoT}(r(n)) \not\subseteq\mathrm{CoT}(r(n)^{1+\varepsilon}).

Conclusions and Open Problems

We established a hierarchy theorem for chain-of-thought reasoning, identifying the number of decoding steps as a fundamental computational resource. Our results raise an important open question of obtaining a hierarchy theorem that is as tight as classical time and space hierarchies in the Turing machine model. In particular, can we show that

CoT(r(n))⊈CoT(r(n)polylog⁡(r(n)))?\mathrm{CoT}(r(n)) \not\subseteq\mathrm{CoT}(r(n)\operatorname{polylog}(r(n)))?

Our proof relies on simulations of transformers with Turing machines combined with padding. The known simulations incur quadratic blow-up, and it is unclear whether near-linear time simulations of CoT(r(n))\mathrm{CoT}(r(n)) are possible. In particular, can we show

CoT(r(n))⊆TIME(r(n)1+o(1))?\mathrm{CoT}(r(n)) \subseteq\mathrm{TIME}(r(n)^{1+o(1)})?

Moreover, due to the use of the padding technique, we only get Theorem 3.4 for r(n)≥nr(n) \ge n. The case r(n)<nr(n) < n is an open question for saturated-attention transformers. We expect a positive answer.

Finally, the full computational power of chain-of-thought reasoning is not yet well understood and is a promising research theme.

Acknowledgments and Disclosure of Funding

N. V. Vinodchandran and Thanh Le were supported in part by NSF awards 2342244, 2413848, and a UNL Grand Challenges Grant. A. Pavan was supported in part by NSF awards 2342245 and 2413849.

Transformer Definition

A transformer is a neural network and a function Σ∗→Σ\Sigma^{*} \to\Sigma. In this paper, we work with decoder-only transformer. We define the model and its components below. We then explain assumptions for our constructions.

Definition A.1. A decoder-only transformer with HH heads, LL layers, model dimension dd consists of the following components:

  1. Embedding function:

e:Σ×N→Rd.e : \Sigma\times\mathbb{N} \to\mathbb{R}^{d}.

where for a∈Σa \in\Sigma, t∈Nt \in\mathbb{N}, input length nn, we define:

e(a,t)=WE(a)+PEn(t).(1)e(a,t) = \mathrm{WE}(a) + \mathrm{PE}_{n}(t). \tag*{(1)}

WE,PEn\mathrm{WE}, \mathrm{PE}_{n} are a word embedding function and a positional encoding function, respectively, where

WE:Σ→Rd\mathrm{WE} : \Sigma\to\mathbb{R}^{d}
PEn:[n]→Rd for n∈N.\mathrm{PE}_{n} : [n] \to\mathbb{R}^{d} \text{ for } n \in\mathbb{N}.

In particular, positional encoding is dependent on the length of the input string (see [21] for remarks on this).

  1. Attention heads: For each layer ℓ∈[L]\ell\in[L] and head k∈[H]k \in[H]:

skℓ:Rd×Rd→R(similarity / score function),s_{k}^{\ell} : \mathbb{R}^{d} \times\mathbb{R}^{d} \to\mathbb{R} \quad\text{(similarity / score function)},
vkℓ:Rd→Rd(value function).v_{k}^{\ell} : \mathbb{R}^{d} \to\mathbb{R}^{d} \quad\text{(value function)}.
  1. Activation block (feedforward + skip-connection): For each layer ℓ∈[L]\ell\in[L],

fℓ:(Rd)H×Rd→Rd(activation function).f^{\ell} : (\mathbb{R}^{d})^{H} \times\mathbb{R}^{d} \to\mathbb{R}^{d} \quad\text{(activation function)}.
  1. Output function:

γ:Rd→Rd.\gamma: \mathbb{R}^{d} \to\mathbb{R}^{d}.

Definition A.2. Given an input sequence x=(x1,…,xn)∈Σnx = (x_{1}, \ldots, x_{n}) \in\Sigma^{n}. We define one decoding step Σn→Σ\Sigma^{n} \to\Sigma of a decoder-only transformer as follows.

  1. Embeddings:

xi(0)=e(xi,i)for i∈[n].x_i^{(0)} = e(x_i,i) \quad\text{for } i \in[n].
  1. Multihead Self-attention: For each layer ℓ∈[L]\ell\in[L] and each position i∈[n]i \in[n], we compute HH attention heads. For head k∈[H]k \in[H], we compute:

aiℓ,k=∑j=1c(i)skℓ(xi(ℓ−1),xj(ℓ−1))Ziℓ,k⋅vkℓ(xj(ℓ−1)),where Ziℓ,k=∑j=1c(i)skℓ(xi(ℓ−1),xj(ℓ−1)),a_i^{\ell,k} = \sum_{j=1}^{c(i)} \frac{s_k^\ell\left(x_i^{(\ell-1)},x_j^{(\ell-1)}\right)}{Z_i^{\ell,k}} \cdot v_k^\ell\left(x_j^{(\ell-1)}\right), \quad\text{where } Z_i^{\ell,k} = \sum_{j=1}^{c(i)} s_k^\ell\left(x_i^{(\ell-1)},x_j^{(\ell-1)}\right),

where c(i)c(i) is ii for causal attention.

We define s=skℓs = s_k^\ell and v=vkℓv = v_k^\ell as follows (note that we drop ℓ,k\ell,k for conciseness). ss has two affine linear transformations Rd→Rd\mathbb{R}^d \to\mathbb{R}^d parametrized by (Wq,bq)(W_q,b_q) (for queries) and (Wk,bk)(W_k,b_k) (for keys) as parameters. Likewise, vv has one affine linear transformation (Wv,bv)(W_v,b_v) (for values) as parameter.

s(xi,xj)=exp⁡(qiTkjd+mask⁡(i,j)),(2)s(x_i,x_j) = \exp\left(\frac{q_i^{\mathsf{T}} k_j}{\sqrt{d}} + \operatorname{mask}(i,j)\right), \tag*{(2)}
v(xj)=Wvxˉj+bv(3)v(x_j) = W_v\bar{x}_j + b_v \tag*{(3)}

where xˉi=N1(xi)\bar{x}_i = \mathcal{N}_1(x_i), where N1=N1(ℓ)\mathcal{N}_1 = \mathcal{N}_1(\ell) is either a multi-layernorm (defined below) or an identity function,

qi=Wqxˉi+bq(4)q_i = W_q\bar{x}_i + b_q \tag*{(4)}
ki=Wkxˉi+bk,(5)k_i = W_k\bar{x}_i + b_k, \tag*{(5)}

and mask is the attention masking function (defined below).

Attention mask function, denoted mask⁡:N×N→{−∞,0}\operatorname{mask} : \mathbb{N} \times\mathbb{N} \to\{-\infty,0\}, decides whether keys get attended to or not. mask takes a query and a key as inputs and returns −∞-\infty if the key is forbidden from the query. mask returns 00 otherwise. For instance, attention mask function for (standard) causal masking is:

mask⁡(i,j)={0if j≤i,−∞otherwise.\operatorname{mask}(i,j) = \begin{cases} 0 & \text{if } j \le i,\\ -\infty& \text{otherwise.} \end{cases}

Different mask functions are studied (for example, see [22]).

  1. Activation block:

The activation function f=fℓf = f^\ell first aggregates attention heads’ outputs, and then feeds the results through a feedforward network (with skip-connection).

For each position ii, ff takes as input attention heads’ outputs ai,1,…,ai,H∈Rda_{i,1},\ldots,a_{i,H} \in\mathbb{R}^d and the previous layer value xi=xi(ℓ−1)x_i = x_i^{(\ell-1)}.

First ff sums up outputs for heads, yielding

ai=∑k=1Hai,k.(6)a_i = \sum_{k=1}^{H} a_{i,k}. \tag*{(6)}

aia_i is then passed through a linear transformation, and then a two layer feedforward (FFW) network (with a multi-layernorm (or identity), a non-linearity, and skip-connections in between) as follows

oi=W0ai+b0+xi(7)o_i = W_0a_i + b_0 + x_i \tag*{(7)}
oˉi=N2(oi)(8)\bar{o}_i = \mathcal{N}_2(o_i) \tag*{(8)}
xi′=W2ReLU⁡(W1oˉi+b1)+b2+oi.(9)x_i' = W_2\operatorname{ReLU}(W_1\bar{o}_i + b_1) + b_2 + o_i. \tag*{(9)}

Then, we get that xi(ℓ)=xi′x_i^{(\ell)} = x_i'.

4. Output: The final output at position ii is

yi=γ(xi(L)),y_i = \gamma(x_i^{(L)}),

where γ\gamma is an affine linear transformation from Rd→Rd\mathbb{R}^d \to\mathbb{R}^d, parametrized by (W,b)(\mathbf{W}, \mathbf{b}).

Remark A.3. With the definition above, parameters for a decoder-only transformer, denoted TT, include:

  • Embedding dimension (dd), number of heads (HH), number of layers (LL).

  • Word embedding function WE and positional encoding functions PEn\mathrm{PE}_n (for n∈Nn \in\mathbb{N}).

  • Attention mask function. Default to be causal mask.

  • For each attention head: 3 affine linear transformations (Wq,bq),(Wk,bk),(Wv,bv)(\mathbf{W}_{q}, \mathbf{b}_{q}),(\mathbf{W}_{k}, \mathbf{b}_{k}),(\mathbf{W}_{v}, \mathbf{b}_{v}), where W\mathbf{W} denotes the linear transformation, and b\mathbf{b} denotes the affine part.

  • For each layer: 2 multi-layernorms N1\mathcal{N}_1 and N2\mathcal{N}_2 (which can be identity); an affine linear transformation (W0,b0)(\mathbf{W}_0, \mathbf{b}_0); a 2-layer FFW network (parametrized by (W1,b1)(\mathbf{W}_1, \mathbf{b}_1) and (W2,b2)(\mathbf{W}_2, \mathbf{b}_2)).

  • For output function: an affine linear transformation (W,b)(\mathbf{W}, \mathbf{b}).

Remark A.4. For saturated attention (also known as average-hard attention), we modify multihead self-attention as follows

aiℓ,k=∑j=1c(i)αi,jℓ,kvjℓ(xj(ℓ−1)),(10)\mathbf{a}_i^{\ell,k} = \sum_{j=1}^{c(i)} \alpha_{i,j}^{\ell,k} \mathbf{v}_j^\ell\left(x_j^{(\ell-1)}\right), \tag*{(10)}

where (αi,j)j=1c(i)=hardmax⁡(s(xi,x1),s(xi,x2),…,s(xi,xc(i)))(\alpha_{i,j})_{j=1}^{c(i)} = \operatorname{hardmax}(s(x_i,x_1),s(x_i,x_2),\ldots,s(x_i,x_{c(i)})). We define hardmax to take as input a vector of real numbers, and return a probability distribution (of the same dimension) whose weights are equally distributed between the maximum positions, and 0 elsewhere.

Based on current theoretical results, saturated attention appears to be computationally equivalent to softmax attention, in the sense that no separation between them is known (see Table 1 of [21]). From a practical point of view, trained softmax systems usually exhibit attention concentrated almost equally on a few dominant positions. Thus, the work of [16] argues that average hardmax is a realistic approximation of how Transformers behave in practice. This motivates average hardmax as a reasonable and mathematically simple abstraction of softmax attention.

Definition A.5 (Multi-layernorm, [14]). Layernorm is defined as

layer_norm⁡(x)=x′∥x′∥,(11)\operatorname{layer\_norm}(\mathbf{x}) = \frac{\mathbf{x}'}{\lVert\mathbf{x}' \rVert}, \tag*{(11)}

where x′=x−mean⁡(x)\mathbf{x}' = \mathbf{x} - \operatorname{mean}(\mathbf{x}), where mean⁡(x)\operatorname{mean}(\mathbf{x}) computes the mean of vector x\mathbf{x}.

Then, let M:Rd→Rd\mathbf{M} : \mathbb{R}^d \to\mathbb{R}^d be a parameter matrix that projects vectors from Rd\mathbb{R}^d to Rd\mathbb{R}^d. Then projected pre-norm is defined as

proj_layer_norm⁡(v,M)=layer_norm⁡(Mv).(12)\operatorname{proj\_layer\_norm}(\mathbf{v},\mathbf{M}) = \operatorname{layer\_norm}(\mathbf{M}\mathbf{v}). \tag*{(12)}

If context is clear, we write proj_layer_norm⁡(v)\operatorname{proj\_layer\_norm}(\mathbf{v}).

Next, let dd be divisible by k∈Nk \in\mathbb{N}. For 1≤i≤k1 \le i \le k, let Mi:Rd→Rd/k\mathbf{M}_i : \mathbb{R}^d \to\mathbb{R}^{d/k} be a parameter projection matrix. Let ⟨⋅⟩i=1k\langle\cdot\rangle_{i=1}^k denote iterated concatenation. The multi-pre-norm of v∈Rd\mathbf{v} \in\mathbb{R}^d is defined as

multi_layer_norm⁡(v,M1,…Mk)=⟨proj_pre_norm⁡(v,Mi)⟩i=1k.(13)\operatorname{multi\_layer\_norm}(\mathbf{v},\mathbf{M}_1,\ldots\mathbf{M}_k) = \left\langle\operatorname{proj\_pre\_norm}(\mathbf{v},\mathbf{M}_i)\right\rangle_{i=1}^k. \tag*{(13)}

There is a result that says (compositions of) projected pre-norm can simulate multi-pre-norm. We use multi_layer_norm in our transformer model (we denote it by N\mathcal{N}).

Remark A.6. Our constructions of decoder-only transformers assume the following:

  • Positional encoding (PE):

  • PEPE uses the number ii to encode position ii. In addition, PEPE may contain other fixed numbers independent of ii (such as those dependent on the input length).

  • Without loss of generality (WLOG), we assume that our transformers have PEPE for generated tokens. That is, xix_i (for any i>ni > n where nn is the input length) has ii as PEPE. (This can be implemented easily by just using the output function γ\gamma to add 1 to PEPE jj (for xjx_j). If j≤n−1j \le n - 1, then this does not change anything, as the output is not taken to be the next token. If j≥nj \ge n, then that makes the PEPE for xj+1x_{j+1} exactly j+1j + 1.)

  • By Lemma B.11, WLOG, we can use 1/i1/i as a positional encoding value for xix_i.

  • Attention: We use saturated attention.

  • Precision: We assume precision as in [14]. That is, for input of length nn and r(n)r(n) intermediate decoding steps, precision is O(log⁡(n+r(n)))O(\log(n + r(n))).

  • Attention mask: We allow slightly tailored attention mask. That is, we forbid some CoT steps from attending to a certain portion of past CoT steps.

Note that this is to help with writing concise proofs rather than a necessary condition.

  • Attention head’s dimension: We don’t follow the exact definition of attentions in [14], as they consider dimension d/Hd/H for each head. But, their model and ours can simulate each other easily.

  • Layernorm: We allow the possibility of applying or not applying multi-layernorm.

In particular, Theorem 2.2 still holds under our assumptions.

Remark A.7. Attention mask is not required for our constructions. Yet, we use attention mask to simplify our construction. We explain how to “simulate” attention mask in Remark 3.2.

Primitives for Manipulating Transformers

In this subsection we list and prove computations that we will perform with transformers in our main proofs. The following lemma is implicit in [2]. We write it in a form that we will use. It essentially says that transformers can check equality between integers and a given integer.

Lemma B.1. Let current prefix be x1x2…xcx_1x_2 \ldots x_c, where each xi∈R6x_i \in\mathbb{R}^6, xi=[ci  t  0  0  0  0]Tx_i = [c_i\ \ t\ \ 0\ \ 0\ \ 0\ \ 0]^T for ci,t∈Zc_i, t \in\mathbb{Z}. Then, there exists a decoder-only transformer with 1 layer (which has 1 head) that, for any xix_i, computes 1(ci=t)\mathbb{1}(c_i = t) and saves into dimension 6. That is, there exists a decoder-only transformer that returns [ci  t  0  0  0  1(ci=t)]T[c_i\ \ t\ \ 0\ \ 0\ \ 0\ \ \mathbb{1}(c_i = t)]^T.

Proof. We design a transformer TT as follows:

  • Dimension: 6; number of layers: 1; number of heads per layer: 1.

  • WE, PE: not applied, since inputs are already given to vector form.

  • For layer 1: put N1=N2=identity\mathcal{N}_1 = \mathcal{N}_2 = \text{identity}; put W01=I\mathbf{W}_0^1 = \mathbf{I} and b0=06×1\mathbf{b}_0 = \mathbf{0}_{6 \times1}. We specify (W11,b11)(\mathbf{W}_1^1, \mathbf{b}_1^1) and (W21,b21)(\mathbf{W}_2^1, \mathbf{b}_2^1) below.

  • For the head in layer 1: put W1,1=06×6\mathbf{W}^{1,1} = \mathbf{0}_{6 \times6} and b1,1=06×1\mathbf{b}^{1,1} = \mathbf{0}_{6 \times1}. The effect of this is to make head output ai1,1=06×1\mathbf{a}_i^{1,1} = \mathbf{0}_{6 \times1} for all 1≤i≤c1 \le i \le c. That means, we do not want this head to have any effects.

  • Output function: we put W=I6×6\mathbf{W} = \mathbf{I}_{6 \times6} and b=06×1\mathbf{b} = \mathbf{0}_{6 \times1}.

Now, fix a position ii, with input xi(0)=[ci  t  0  0  0  0]T\mathbf{x}_i^{(0)} = [c_i\ \ t\ \ 0\ \ 0\ \ 0\ \ 0]^T.

Consider layer 1. The head produces ai1,1=06×1\mathbf{a}_i^{1,1} = \mathbf{0}_{6 \times1}. Since there is only one head ai1=ai1,1\mathbf{a}_i^1 = \mathbf{a}_i^{1,1}. Since we know we talk about layer 1, we can drop the superscript 1, and just write ai=06×1\mathbf{a}_i = \mathbf{0}_{6 \times1}. Then, since W01=I\mathbf{W}_0^1 = \mathbf{I} and b0=06×1\mathbf{b}_0 = \mathbf{0}_{6 \times1}, we yield that oi=xi(0)\mathbf{o}_i = \mathbf{x}_i^{(0)}. Next, since N2=identity\mathcal{N}_2 = \text{identity}, we have that oi=xi\mathbf{o}_i = \mathbf{x}_i. Now, let

W11=[0000000000001−100001−100001−10000000000],b11=[00−101−1].\mathbf{W}^{1}_{1} = \begin{bmatrix} 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & -1 & 0 & 0 & 0 & 0 \\ 1 & -1 & 0 & 0 & 0 & 0 \\ 1 & -1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \end{bmatrix}, \qquad \mathbf{b}^{1}_{1} = \begin{bmatrix} 0\\ 0\\ -1\\ 0\\ 1\\ -1 \end{bmatrix}.

We yield

ReLU⁡(W11xi+b11)=[00max⁡{0,ci−t−1}max⁡{0,ci−t}max⁡{0,ci−t+1}0].\operatorname{ReLU}(\mathbf{W}^{1}_{1}\mathbf{x}_{i}+\mathbf{b}^{1}_{1}) = \begin{bmatrix} 0\\ 0\\ \max\{0,c_i-t-1\}\\ \max\{0,c_i-t\}\\ \max\{0,c_i-t+1\}\\ 0 \end{bmatrix}.

Now, observe that since ci,t∈Zc_i,t \in\mathbb{Z}, we have

1(ci=t)=max⁡{0,ci−t−1}−2max⁡{0,ci−t}+max⁡{0,ci−t+1}.\mathbb{1}(c_i=t)=\max\{0,c_i-t-1\}-2\max\{0,c_i-t\}+\max\{0,c_i-t+1\}.

Therefore, we let

W21=[000000000000000000000000000000001−210],b21=06×1.\mathbf{W}^{1}_{2} = \begin{bmatrix} 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & -2 & 1 & 0 \end{bmatrix}, \qquad \mathbf{b}^{1}_{2}=\mathbf{0}_{6\times1}.

We obtain that xi(1)=[cit0001(ci=t)]\mathbf{x}^{(1)}_i=[c_i\quad t\quad0\quad0\quad0\quad\mathbb{1}(c_i=t)], as desired.

Remark B.2. Some notes on how we use different components of transformers to build desired properties:

  • Placeholder dimensions: we call dimensions 3 to 5 (informally) as placeholder dimensions. Their purpose is to save results of auxiliary calculations.

  • Dimensions to save results: dimension 6 is a dimension to save results.

  • Effects of 2-layer FFN network: multiply by constants, add constants, perform ReLU, move numbers between dimensions.

  • Effects of skip-connection: keep original values. That is, values cic_i and tt in dimensions 1 and 2 are kept intact in xi(1)\mathbf{x}^{(1)}_i thanks to skip connections.

  • For dimensions that we do not want to change, we can make our heads and linear transformations ignore them by ignoring respective rows/columns. Or, we can make values of the dimensions be 0 during calculations, and then recover their original values using skip-connections.

We obtain the following corollary for checking if an integer xx is between 2 integers t1t_1 and t2t_2.

Corollary B.3. Say x=[xt1t2]\mathbf{x}=[x\quad t_1\quad t_2] where x∈Zx\in\mathbb{Z} and t1,t2∈Zt_1,t_2\in\mathbb{Z}. There exists two nested 2-layer feedforward networks (with ReLU) that compute 1(t1≤x≤t2)\mathbb{1}(t_1\le x\le t_2).

Proof. First, use an affine linear transformation and a ReLU (of the first feedforward network) to compute f(x):=ReLU⁡(t1−x)+ReLU⁡(x−t2)f(x):=\operatorname{ReLU}(t_1-x)+\operatorname{ReLU}(x-t_2). Note that f(x)=0f(x)=0 iff t1≤x≤t2t_1\le x\le t_2. Next, use a second feedforward network to compute if 1(f(x)=0)≡1(t1≤x≤t2)\mathbb{1}(f(x)=0)\equiv\mathbb{1}(t_1\le x\le t_2).

Remark B.4. We can implement the nested feedforward networks of the above corollary using a transformer with 2 layers and 3 additional dimensions (2 as placeholder dimensions, and 1 as result-saving dimension).

Next, we have the following lemma for checking if x=tx=t, where t∈Rt \in\mathbb{R} is given, and x∈Rx \in\mathbb{R}. Note that we state our result for feedforward networks, but similar to Lemma B.1, we can transfer feedforward networks to transformers by making attention heads silent and using additional dimensions (to avoid effects of skip-connections).

Lemma B.5. Consider x=[xt00]\mathbf{x}=[x\quad t\quad0\quad0] where x,t∈Rx,t \in\mathbb{R}. Then, there exists two nested 2-layer feedforward networks (with ReLU) that return [___1(x=t)][\_\quad\_\quad\_\quad\mathbb{1}(x=t)].

Proof. First, use an affine linear transformation to get x−tx-t and t−xt-x (saved in dimensions 3 and 4). Then, use ReLU to get ReLU⁡(x−t)\operatorname{ReLU}(x-t) and ReLU⁡(t−x)\operatorname{ReLU}(t-x). Then use an affine linear transformation to get ReLU⁡(x−t)+ReLU⁡(t−x)=∣x−t∣\operatorname{ReLU}(x-t)+\operatorname{ReLU}(t-x)=|x-t| (saved in dimension 4). Next, the second 2-layer feedforward network, compute ReLU⁡(1−∞∣x−t∣)\operatorname{ReLU}(1-\infty|x-t|) (saved in dimension 5), where ∞\infty is a scalar. We get 1 if x=tx=t, and 0 otherwise.

Remark B.6. We can implement the two nested feedforward networks as in Lemma B.5 by using 2 layers of a transformer, with 2 placeholder dimensions and 1 result-saving dimension.

Next, we have the following lemma from [17]. We generalize so that it works for our case. The lemma essentially says that transformer can do “if” clauses (under some assumptions). We make an assumption that 0⋅∞=00\cdot\infty=0.

Lemma B.7. Let x∈Rm\mathbf{x}\in\mathbb{R}^{m} and y,z∈R≥0n\mathbf{y},\mathbf{z}\in\mathbb{R}_{\geq0}^{n}, and let b∈{0,1}b\in\{0,1\}. There exists a two-layer feedforward network f:Rm+2n+1→Rm+nf:\mathbb{R}^{m+2n+1}\to\mathbb{R}^{m+n} such that

f([x,y,z,b])={[x,y]if b=0[x,z]if b=1.f([\mathbf{x},\mathbf{y},\mathbf{z},b])= \begin{cases} [\mathbf{x},\mathbf{y}] & \text{if } b=0\\ [\mathbf{x},\mathbf{z}] & \text{if } b=1. \end{cases}

Proof. (Sketch) Let ∞⃗\vec{\infty} be the vector of dimension nn whose entries are all ∞\infty’s.

We utilize the 2-layer feedforward network to compute the following:

ReLU⁡(y−b⋅∞⃗)+ReLU⁡(z−(1−b)∞⃗),\operatorname{ReLU}(\mathbf{y}-b\cdot\vec{\infty})+\operatorname{ReLU}(\mathbf{z}-(1-b)\vec{\infty}),

which equals y\mathbf{y} if b=0b=0 and z\mathbf{z} if b=1b=1.

Remark B.8. We can implement the feedforward network in the lemma using 2n+12n+1 additional dimensions (say we use nn dimensions to save y−b∞⃗\mathbf{y}-b\vec{\infty}, nn dimensions to save z−(1−b)∞⃗\mathbf{z}-(1-b)\vec{\infty}, and 1 to save 1−b1-b). There can be more optimal usage of dimensions, but we don’t need that.

Next, we have a lemma that essentially says transformer can multiply a vector x\mathbf{x} with a number a∈{0,1}a\in\{0,1\}.

Lemma B.9. Let x∈Rd\mathbf{x}\in\mathbb{R}^{d} and a∈{0,1}a\in\{0,1\} together in a vector, denoted [xa0d0d]T[\mathbf{x}\quad a\quad\mathbf{0}_{d}\quad\mathbf{0}_{d}]^{T}. Then, there exists a 2-layer feedforward network (with ReLU) that returns [0d−(−)dax][\mathbf{0}_{d}\quad-\quad(-)_{d}\quad a\mathbf{x}].

Proof. First, use the first affine linear transformation to get

[0dax−∞⃗(1−a)−x−∞⃗(1−a)]T.[\mathbf{0}_{d}\quad a\quad\mathbf{x}-\vec{\infty}(1-a)\quad-\mathbf{x}-\vec{\infty}(1-a)]^{T}.

Next, after ReLU, we get

[0daReLU⁡(x−∞⃗(1−a))ReLU⁡(−x−∞⃗(1−a))].[\mathbf{0}_{d}\quad a\quad\operatorname{ReLU}(\mathbf{x}-\vec{\infty}(1-a))\quad\operatorname{ReLU}(-\mathbf{x}-\vec{\infty}(1-a))].

Then, we use the second linear transformation to get

ReLU⁡(x−∞⃗(1−a))−ReLU⁡(−x−∞⃗(1−a))={1⋅xif a=10⋅xif a=0.\operatorname{ReLU}(\mathbf{x}-\vec{\infty}(1-a))-\operatorname{ReLU}(-\mathbf{x}-\vec{\infty}(1-a))= \begin{cases} 1\cdot\mathbf{x} & \text{if } a=1\\ 0\cdot\mathbf{x} & \text{if } a=0. \end{cases}

Thus, we yield [0d−(−)dax][\mathbf{0}_{d}\quad-\quad(-)_{d}\quad a\mathbf{x}], as desired.

Remark B.10. We have some remarks on the previous lemma:

  • Above, we could have done calculation on the first dd dimensions, but we will leave them equal to zero, and do calculations in some dd placeholder dimensions. The reason is that when we use feedforward network in a transformer, after all calculations, the transformer performs a skip-connection to add the original vector into the resulting vector. In our case, the first dd dimensions will be added by xx in the end, and that may alter the results we want. Thus, we perform calculation in placeholder dimensions, which are unaltered by skip-connections.

  • So, to multiply xx by aa, we have used 2d2d-placeholder dimensions (dd of which are used to save the results, too).

Next, we have a lemma that says if each xix_i has ii as positional encoding, then for xix_i, we can calculate number 1i\frac{1}{i} (using a transformer).

Lemma B.11. Let x1,…,xnx_1,\ldots,x_n such that xi=[i00]x_i=[i\quad0\quad0]. Then, there exists a transformer with 2 layers that returns yi=[i−1i]y_i=[i\quad-\quad\frac{1}{i}].

Proof. Let us just write in terms of 3 dimensions for conciseness. Let’s be understood we may need to use more than 3 dimensions for the purpose of place-holding. Say we are considering query xix_i.

First, by Lemma B.1, we use a layer (layer 1) to get xi(1)=[i1(i=1)0]x_i^{(1)}=[i\quad\mathbb{1}(i=1)\quad0]. Next, use a head in layer 2 to attend uniformly over all tokens (with causal mask) with value 1(j=1)\mathbb{1}(j=1). From this, we get 1i\frac{1}{i} as the result, hence obtaining xi(2)=[i−1i]x_i^{(2)}=[i\quad-\quad\frac{1}{i}]. □\square

Remark B.12. By Lemma B.11, if we have ii as a positional encoding for xix_i, we can easily obtain 1i\frac{1}{i} for xix_i. Thus, WLOG, we can assume 1i\frac{1}{i} as a number given from positional encoding, too.

References

  1. [1][Bar02] Boaz Barak. “A Probabilistic-Time Hierarchy Theorem for ”Slightly Non-uniform” Algorithms”. In: Randomization and Approximation Techniques, 6th International Workshop, RANDOM 2002, Cambridge, MA, USA, September 13-15, 2002, Proceedings. Ed. by José D. P. Rolim and Salil P. Vadhan. Lecture Notes in Computer Science. Springer, 2002, pp. 194–208. DOI: 10.1007/3-540-45726-7\_16. URL: https://doi.org/10.1007/3-540-45726-7%5C_16.
  2. [2][CC22] David Chiang and Peter Cholak. “Overcoming a theoretical limitation of self-attention”. In: Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers). 2022, pp. 7654–7664.arxiv.org/abs/2202.12172
  3. [3]David Chiang, Peter Cholak, and Anand Pillay. “Tighter Bounds on the Expressivity of Transformer Encoders”. In: Proceedings of the 40th International Conference on Machine Learning (ICML). Vol. 202. Proceedings of Machine Learning Research. PMLR, 2023, pp. 5544–5560. URL: https://proceedings.mlr.press/v202/chiang23a.html.arxiv.org/abs/2301.10743
  4. [5]Lijie Chen, Binghui Peng, and Hongxun Wu. “Theoretical limitations of multi-layer Transformer”. In: 66th IEEE Annual Symposium on Foundations of Computer Science, FOCS 2025, Sydney, Australia, December 14-17, 2025. IEEE, 2025, pp. 2631–2653. DOI: 10.1109/FOCS63196.2025.00136. URL: https://doi.org/10.1109/FOCS63196.2025.00136.
  5. [7]Lance Fortnow and Rahul Santhanam. “Hierarchy Theorems for Probabilistic Polynomial Time”. In: 45th Symposium on Foundations of Computer Science, FOCS 2004, Rome, Italy, October 17-19, 2004, Proceedings. IEEE Computer Society, 2004, pp. 316–324. DOI: 10.1109/FOCS.2004.33. URL: https://doi.org/10.1109/FOCS.2004.33.
  6. [8]Lance Fortnow, Rahul Santhanam, and Luca Trevisan. “Hierarchies for semantic classes”. In: Proceedings of the 37th Annual ACM Symposium on Theory of Computing, Baltimore, MD, USA, May 22-24, 2005. Ed. by Harold N. Gabow and Ronald Fagin. ACM, 2005, pp. 348–355.DOI
  7. [9]Sophie Hao, Dana Angluin, and Roberta Frank. “Formal Language Recognition by Hard Attention Transformers: Perspectives from Circuit Complexity”. In: Transactions of the Association for Computational Linguistics 10 (2022), pp. 800–810.
  8. [10]Michael Hahn. “Theoretical Limitations of Self-Attention in Neural Sequence Models”. In: Transactions of the Association for Computational Linguistics 8 (2020), pp. 156–171.DOI
  9. [11]Juris Hartmanis and Richard E. Stearns. “On the Computational Complexity of Algorithms”. In: Transactions of the American Mathematical Society 117 (1965), pp. 285–306.DOI
  10. [12]Oscar H. Ibarra. “A Note on Nondeterministic Tape Complexities”. In: Journal of the ACM 14.2 (1967), pp. 341–343.
  11. [13]Bingbin Liu et al. “Transformers Learn Shortcuts to Automata”. In: International Conference on Learning Representations (ICLR). 2023. URL: https://openreview.net/forum?id=De4FYqjFueZ.arxiv.org/abs/2210.10749
  12. [14]William Merrill and Ashish Sabharwal. “The expressive power of transformers with chain of thought”. In: arXiv preprint arXiv:2310.07923 (2023).arxiv.org/abs/2310.07923
  13. [15]William Merrill and Ashish Sabharwal. “The Parallelism Tradeoff: Limitations of Log-Precision Transformers”. In: Transactions of the Association for Computational Linguistics 11 (2023), pp. 531–545.arxiv.org/abs/2207.00729
  14. [16]William Merrill, Ashish Sabharwal, and Noah A. Smith. “Saturated Transformers are Constant-Depth Threshold Circuits”. In: Transactions of the Association for Computational Linguistics 10 (2022), pp. 843–856.
  15. [17]Jorge Pérez, Pablo Barceló, and Javier Marinkovic. “Attention is turing-complete”. In: Journal of Machine Learning Research 22.75 (2021), pp. 1–35.
  16. [20]Richard E. Stearns, Juris Hartmanis, and Philip M. Lewis II. “Hierarchies of Memory-Limited Computations”. In: Proceedings of the Sixth Annual IEEE Symposium on Switching Circuit Theory and Logical Design (SWCT) (1965), pp. 179–190.DOI
  17. [21]Lena Strobl et al. “What Formal Languages Can Transformers Express? A Survey”. In: Transactions of the Association for Computational Linguistics 12 (2024), pp. 543–561. ISSN: 2307-387X. DOI: 10.1162/tacl_a_00663. URL: http://dx.doi.org/10.1162/tacl_a_00663.
  18. [22]Pawel Swietojanski et al. “Variable Attention Masking for Configurable Transformer Transducer Speech Recognition”. In: ICASSP 2023 - 2023 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP). 2023, pp. 1–5. DOI: 10.1109/ICASSP49357.2023.10094588.DOI

Paper details

vlnc:W_q6ens9m7
Version
v1 · latest
Published
Fresh — added to the Hub within the last 30 days
Added
License
CC BY 4.0
Usage

views · 16

downloads · 0

A view counts once per browser per day, a download each time the PDF is requested.

  • Computer Science

Not recorded for this paper

View statement
Looking for related papers…
Contents