RNNs are not Transformers (Yet): The Key Bottleneck on In-Context Retrieval

## Abstract

Abstract This paper investigates the gap in representation powers of Recurrent Neural Networks (RNNs) and Transformers in the context of solving algorithmic problems. We focus on understanding whether RNNs, known for their memory efficiency in handling long sequences, can match the performance of Transformers, particularly when enhanced with Chain-of-Thought (CoT) prompting. Our theoretical analysis reveals that CoT improves RNNs but is insufficient to close the gap with Transformers. A key bottleneck lies in the inability of RNNs to perfectly retrieve information from the context, even with CoT:
for several tasks that explicitly or implicitly require this capability, such as associative recall and determining if a graph is a tree, we prove that RNNs are not expressive enough to solve the tasks while Transformers can solve them with ease.
Conversely, we prove that adopting techniques to enhance the in-context retrieval capability of RNNs, including Retrieval-Augmented Generation (RAG) and adding a single Transformer layer, can elevate RNNs to be capable of solving all polynomial-time solvable problems with CoT, hence closing the representation gap with Transformers. 1 1 1 Code is available at https://github.com/dangxingyu/rnn-icrag

## 1 Introduction

Transformer models (Vaswani et al., ) have become the dominant choice of the backbone for large language models (LLMs). The core component of Transformers is its self-attention module, which allows the model to route information densely across the entire sequence. However,
this design leads to high inference costs for modeling long sequences, including a memory cost that is at least linear in the sequence length due to the need for maintaining intermediate attention keys and values for each token, and a time cost quadratic in the sequence length for computing the attention score for each pair of tokens.

Recently, Recurrent Neural Networks (RNNs) have been an increasingly popular choice in sequence modeling tasks due to their ability to maintain a memory size constant in sequence length during inference, thus being more memory efficient than Transformers.
Katharopoulos et al. () showed that Transformers with a special type of kernelized linear attention can be expressed as RNNs. Gu et al. () took a different path to design RNNs by structuring latent states as State Space Models (SSMs) from control theory. These ideas have led to a series of development of modern RNNs, including RWKV (Peng et al., ), RetNet (Sun et al., ), and Mamba (Gu & Dao, ). Most notably, Mamba can achieve competitive performance with Transformers on several sequence modeling tasks with linear time and constant memory complexity in sequence length.

Can RNNs replace Transformers yet?
The rise of these modern RNNs has led to an interest in understanding their limitations.
A recent work by Arora et al. () showed that an important family of RNNs, input-independent gating SSMs, are empirically inferior to Transformers in a task that has a long history in artificial intelligence, associative recall (AR) (Willshaw et al., ; Hopfield, ; Hinton & Anderson, ):
Given a series of key-value pairs as a string, the model is required to recall the value given a key.
On the theory side, Sanford et al. () and Jelassi et al. () demonstrated that constant-memory RNNs do not have sufficient representation power to solve the tasks of averaging a given subset of input vectors ($q$-sparse averaging) and repeating the input sequence (copying), respectively, while there exist shallow Transformers that can solve these tasks.

However, the above results do not exclude the possibility that enhancing RNNs with additional prompting techniques or minor architectural changes may close the gap with Transformers. In fact, Transformers themselves are not perfect either and may need additional techniques at inference time to perform well on certain tasks.
As a notable example,
Chain-of-Thought (CoT) (Wei et al., ), a prompting technique that asks the model to generate a series of intermediate tokens before giving the final answer, has been known to be crucial for Transformers to perform well on tasks that require mathematical or algorithmic reasoning.
Feng et al. (); Li et al. () explained this from the perspective of representation power: Transformers alone do not have sufficient representation power to solve problems beyond a certain circuit complexity class ($\mathrm{TC}^{0}$), but with CoT, they can even simulate any polynomial-time Turing machines.

The effectiveness of CoT on Transformers naturally leads to the following question:

Can similar enhancements, such as adopting CoT, improve RNNs so that they can be on par with Transformers?

Figure: Figure 1: Hierarchy of Representation Power. While RNN with chain-of-thought (CoT) with $O(\log n)$ bit memory provably has strictly stronger representation power than RNN without CoT under mild complexity assumptions (Section 4.1), it is still exponentially weaker than Transformer with CoT in representing solutions to algorithmic problems (Section 4.2.2). We proceed to show that the incapability of RNNs in In-context Retrieval is the root cause of the gap and propose two forms of In-context Retrieval Augmented Generation (In-context RAG) to close the gap by illustrating their power to simulate any polynomial-time Turing machines (Sections 5.1 and 5.2).

##### Our Contributions.

This paper answers the above question from theory by examining various ways to close the gap in the representation powers of RNNs and Transformers on algorithmic problems.
Through a series of lower and upper bound results, we show that CoT improves the representation power of RNNs, but to close the gap with Transformers, CoT alone is not enough
to overcome a key bottleneck of RNNs: their inability to retrieve information from the context, which we call in-context retrieval for short.
We further illustrate that addressing this in-context retrieval bottleneck is sufficient to close this gap: RNNs can solve all polynomial-time solvable problems if adopting techniques to enhance the in-context retrieval capability, including involving Retrieval-Augmented Generation (RAG) and appending a single Transformer layer.
Our main contributions are listed as follows:

- 1.
CoT improves RNNs but cannot close the representation gap with Transformers. (Section 4)
•
On the positive side, we prove that CoT makes RNNs strictly more expressive under mild assumptions from circuit complexity.
•
On the negative side, we show that adopting CoT is not enough to close the representation gap between RNNs and Transformers: the memory efficiency of RNNs fundamentally limits their ability to perform in-context retrieval, even with CoT.
This point is made concrete by proving that RNNs with CoT cannot solve a set of fundamental algorithmic problems that directly ask for in-context retrieval, including associative recall. We further exemplify that in-context retrieval can be implicitly required in tasks that appear unrelated, by proving the inability of RNNs to solve the classic problem of determining if a graph is a tree ($\mathrm{IsTree}$).
•
On the other hand, we prove that Transformers have the representation power to solve many of the above tasks with ease, including $\mathrm{IsTree}$. Moreover, Transformers with CoT can even simulate RNNs with CoT efficiently, with only a small multiplicative factor in the number of parameters.
Technically, the key insight for the first separation is that RNNs without CoT is a shallow circuit, while RNNs with CoT can be an exponentially deeper circuit. But on the negative side, RNNs are so memory efficient that they can trigger streaming lower bounds (Sanford et al., ), especially for problems that require in-context retrieval.
- 2.
Enhancing the in-context retrieval capability of RNNs can close the representation gap. (Section 5)
•
We prove that allowing RNNs to invoke function calls to perform a certain primitive of in-context retrieval is sufficient to boost their representation power to solve all polynomial-time solvable problems with CoT, hence closing the representation gap between RNNs and Transformers.
•
Alternatively, as one layer of transformer is sufficient to perform many in-context retrieval operations,
we prove that implicitly enhancing the in-context retrieval capability of RNNs by adding just one transformer layer at the end of the architecture is also sufficient to close the representation gap.
Technically, the key insight for the above upper bounds is that RNN can focus on the local reasoning steps and use the in-context retrieval module to adaptively fetch the relevant information from the context.

Figure: Figure 2: We train RNNs (Mamba) and Transformers (LLaMA 2 Touvron et al. ()) with a frozen word embedding and decoding head of three different model sizes (0.5M, 1M, 2M) on IsTree with three different sizes of graph (16, 32, 64) under three different setups. Vanilla means the model directly predicts the label. COT means the model will generate a chain-of-thought process based on DFS (see Algorithm 1) before prediction. Retrieval means the model will generate the chain of search queries and reasoning before prediction (see Algorithm 2). We observe that (1) Both Transformer and RNNs can’t solve the IsTree question without a chain of thought; (2) RNNs’ performance with chain-of-thought decays quickly when the number of nodes increase, which is consistent with our theory; (3) All models reach almost perfect accuracy when enhanced with retrieval.
Refer to caption: extracted/2402.18510v3/main/istree1.png

## 2 Related Works

##### State Space Machines and Linear Transformers.

There has been a recent surge of interest in state space machines and (kernalized) linear transformers (Gu et al., ; Katharopoulos et al., ; Peng et al., ; Sun et al., ; Gu & Dao, ; Fu et al., ; Poli et al., ; Luo et al., ; Peng et al., ; Wang et al., ), which are a class of models that combine the parallelizability of the Transformer with the memory efficiency of the RNN. These models can process both a sequential and a recurrent form, and can use the former for fast parallelizable training and the latter for memory-efficient inference.
However, these models are still empirically inferior to the Transformer in terms of performance. Our work investigates the reasons behind this gap and proposes to close it by enhancing the in-context retrieval capability.

##### Chain of Thought (CoT).

Chain of thought (Wei et al., ; Nye et al., ; Kojima et al., ; Wang & Zhou, ) is an augmentation to the Transformer, that allows it to solve more complex reasoning tasks by generating a reasoning process before outputting the answer. It has been shown that Transformers with CoT provably have more expressive power than the original Transformer without CoT (Feng et al., ; Li et al., ). However, the expressive power of RNNs with CoT has not yet been systematically studied. Theorem F.1 in Feng et al. () shows that RNN cannot output a particular format of CoT for evaluating arithmetic expressions and solving linear equations while Transformers with the same amount of parameters can. Concurrent work (Yang et al., ) discovers that linear Transformers, a special class of RNNs, are not able to solve some dynamic programming problems with CoT, unless the number of parameters grows with the length of the input. One high-level message our work conveys is similar to theirs: RNNs have limited representation power to perform reasoning with CoT.
However, we show that such limitation is not specific to the output format or architecture and apply tools from streaming complexity to prove lower bounds on a broader range of tasks and memory-efficient architectures.

##### Streaming Algorithms.

Our lower bound leverages the technique in streaming algorithms. Streaming algorithms are algorithms that take constant (typically just 1) pass over the input and use sublinear space, hence including RNNs with fixed state space as a special case. Works in streaming algorithms date back to the 1980s (Munro & Paterson, ) and have been formalized and popularized in the 1990s (Alon et al., ) due to the need to process large data streams. The lower bound in our work is a direct application of the technique in streaming algorithms to the study of RNNs and we mainly consider the streaming algorithms for (1) indexing the input (Munro & Paterson, ) and (2) determining whether the input is a tree (Henzinger et al., ).

##### Retrieval Augmented Generation.

Our work proposes to use retrieval augmentation to close the representation gap between RNNs and Transformers. This is consistent with the recent trend of retrieval augmented generation (Guu et al., ; Borgeaud et al., ; Rubin & Berant, ). Empirically, retrieval augmented generation has been shown to improve the performance of recurrent models in various tasks (Kuratov et al., ; Akyürek et al., ) and our work provides a theoretical foundation for this phenomenon. Our work also shows that an attention layer can be used to simulate the retrieval process, which is consistent with the finding that attention can improve the performance of RNNs (Vaswani et al., ; Arora et al., ; Park et al., ; Peng et al., ; Hao et al., ). It has also been shown empirically that attention can be used to simulate complex retrieval process (Jiang et al., ).

##### Comparison Between Transformers and RNNs (Without CoT).

A line of works focused on the comparison between RNNs and Transformers in terms of recognizing or generating formal languages (Bhattamishra et al., ; Hahn, ; Merrill et al., ). These works show that the lack of recurrent structure in Transformers makes them fail to recognize some formal languages that RNNs can recognize. However, Liu et al. (); Yao et al. (); Hao et al. () show that such limitation can be mitigated when we consider bounded length of input or bounded grammar depth. Our work differs from these works in that we consider the expressive power of RNNs and Transformers with CoT and show that in this case, the gap between RNNs and Transformers is one-sided (Section 4.2.3).

Prior work (Arora et al., ) has shown that input-independent gating SSMs are inferior to Transformers in the task called associative recall (Willshaw et al., ; Hopfield, ; Hinton & Anderson, ). The task requires the model to recall a previously seen pattern given a partial input. They show that input-dependent gating SSMs have better performance in associative recall and also propose a hybrid architecture that combines input-independent state space machines with attention to achieve better performance. Our work differs from this work in the following ways: (1) Our work studies associative recall from a theoretical perspective and proves formal lower bounds on the memory size of RNNs necessary for solving associative recall and other retrieval tasks; (2) We also study hybrid architectures but we provide a proof that appending a single Transformer layer to RNNs can make them expressive enough; (3) Our theory applies to not only input-independent gating SSMs but also all RNNs with $o(n)$-bit memory.

Prior work (Jelassi et al., ) proves a representation gap between RNNs and Transformers in repeating a long sequence, which can be seen as a retrieval task. They show that RNNs have difficulty performing the task due to their limited memory.
Our work further proves that RNNs are limited in solving many other retrieval tasks, even with CoT. Technically, a key ingredient in their proof is a counting argument on the output sequence to show a limited memory size is not enough to produce too many different output sequences, but our proof can handle retrieval tasks that only require outputting a single token.

Notably, Sanford et al. () apply communication complexity to prove circuit size or memory size lower bounds for RNNs and Transformers on the task of sparse averaging.
Sanford et al. () extend this technique to another task called hopk, a generalization of the associative recall task. Our technique is similar to theirs since our proof is also based on communication complexity. But we consider a broader range of tasks including seemingly irrelevant reasoning tasks such as IsTree, and further explore various ways to close the representation gap.

##### Representation Theory of RNNs.

Another line of works (Li et al., , ; Alberti et al., ) studies the universal approximation power of RNNs. They show that the upper bound of the approximation power of linear RNNs will be constrained by the dimension of the hidden states. Their works on the high level are consistent with our findings but are not directly comparable because we are considering finite precision compute models with the assistance of CoT or In-context RAG.

## 3 Preliminaries

We introduce the definitions that are necessary for understanding our results
and defer other definitions to Appendix A.

##### Vocabulary and Embeddings.

A vocabulary $V$ is a finite set of tokens.
A word embedding for a token $v\in V$ is a vector $W^{(E)}_{v}\in\mathbb{R}^{d}$ that represents the token, and a position embedding for the $k$-th token in a sequence is a vector $W^{(P)}_{k}\in\mathbb{R}^{d}$ that represents the position.
Given a sequence of tokens $\mathcal{S}$, an embedding function $\textrm{Emb}(\mathcal{S})$ maps each token to a vector in $\mathbb{R}^{d}$ by mixing word and position embeddings, resulting in a sequence of vectors.
To ease our comparison between RNNs and Transformers, in this paper, we assume fixed word and position embeddings and do not learn them during training.
See Section A.3 for the formal definitions. This is common practice and has been used in many previous works studying the theory of Transformers (Li et al., ; Tian et al., ).

Many of the problems we study involve natural numbers up to $n$, where the input sequence length is linear in $n$.
For simplicity, we assume the vocabulary contains $[n]=\{1,\dots,n\}$ and the word embedding for $i$ is defined as $W^{(E)}_{i}=iw_{1}$, where $w_{1}$ is the first coordinate vector.
But in practice, the vocabulary size does not increase with $n$ and numbers may be tokenized into a few tokens according to their decimal representations.
We note that our results can be easily extended to this more practical case since our lower bounds do not rely on the specific form of the vocabulary and embeddings and for the upper bounds, our embedding can be easily represented by a few RNN or Transformer layers.

##### Numerical Precision.

We will consider computation models with fixed numerical precision in this paper. We will use $p$ to denote the precision of the number of bits to represent real numbers and use $\mathbb{R}_{p}$ to denote the set of all real numbers that can be represented by $p$-bit floating point numbers. We defer the details to Section A.2. We will assume $p=O(\log n)$ in this paper and state the constant explicitly when necessary. This is a common assumption when studying the finite precision neural networks (Feng et al., ; Merrill & Sabharwal, ).

##### Language Modeling.

We use $V^{*}$ and $V^{+}$ to denote the set of all finite sequences and all non-empty finite sequences of tokens in $V$, respectively.
We study language models that can predict the next token given a prefix of tokens.
For this, we define a language model (LM) as a function $M:V^{+}\to\mathbb{P}_{V}$ that maps a non-empty sequence to a probability distribution over the next token, where $\mathbb{P}_{V}$ is the probability simplex over $V$.
We specifically study the case where the language model is realized by deep neural networks: first map the input sequence $\mathcal{S}$ into a sequence of embeddings $\textrm{Emb}(\mathcal{S})$, and then apply a neural network, such as a Transformer or RNN, to process the embeddings and output the probability distribution. We would call a series of parameterized models with increasing input size a *family* of models.

##### Transformer.

We will first define the Transformer architecture used in the theoretical analysis in this paper.

###### Definition 3.1 (Transformer Block) .

Let $X\in\mathbb{R}^{d\times l}$ be the input matrix, where $l$ is the sequence length. The output of a Transformer block $f$ is defined as:

$$ $\displaystyle f(X)$ $\displaystyle=X+\mathcal{A}(X)+g(X+\mathcal{A}(X)),$ $\displaystyle\mathcal{A}(X)$ $\displaystyle=\sum_{h=1}^{H}W^{(V,h)}X\mathrm{softmax}\left(\frac{\left(W^{(K, h)}X\right)^{\top}W^{(Q,h)}X}{\sqrt{d}}+C\right),$ (1) $$

where $g$ is a column-wise ReGLU feed-forward network (^2^22ReGLU means $\sigma(x)=\mathrm{ReLU}(W_{1}x+b_{1})\otimes(W_{2}x+b_{2})$, this is a surrogate for the commonly used SwiGLU activation and allows the model to perform multiplication of two coordinates.) with width $0pt$ and output dimension $d$, $\mathcal{A}$ is the scaled dot-product attention, $\mathrm{softmax}$ is the column-wise softmax function, $W^{(K,h)}$, $W^{(Q,h)}$, $W^{(V,h)}$ are the learnable parameters and $H$ is the number of heads, and $C=\begin{bmatrix}0&0&\ldots&0\\
-\infty&0&\ldots&0\\
\vdots&\vdots&\vdots&\vdots\\
-\infty&-\infty&\ldots&0\end{bmatrix}\in\mathbb{R}^{l\times l}$ is a mask to prevent the attention from attending to future tokens.

In the context of language modeling, given a sequence of tokens $\mathcal{S}$,
a Transformer $T(\mathcal{S})$ is defined as:

###### Definition 3.2 (Transformer) .

Let $\mathcal{S}\in|V|^{l}$ be the tokenized input sequence, the output of a Transformer is defined as:

$$ $\displaystyle T(\mathcal{S})=\mathrm{softmax}\left(W^{(E)}\left(f_{L}\left( \ldots f_{1}\left(\textrm{Emb}\left(\mathcal{S}\right)\right)\right)\right) \right)_{:,l}.$ (2) $$

where $\mathrm{softmax}$ is the column-wise softmax function, $f_{i}$ is the $i$-th Transformer block. We will call the $i$-th Transformer block the $i$-th layer of the Transformer and denote its feed-forward layer and attention layer as $g_{i}$ and $\mathcal{A}_{i}$ respectively.

###### Definition 3.1 (Transformer Block) .

###### Definition 3.2 (Transformer) .

##### Recurrent Neural Networks

Recently there has been a lot of interest in the linear-time Transformer, which replaces the full-attention calculation with linear-time alternatives. These variants are mostly special forms of recurrent neural networks (RNNs) that are parallelizable. Here we define a general form of RNNs containing all of these variants; hence, our analysis will apply to all of them.

###### Definition 3.3 (RNN) .

An RNN architecture is characterized by two functions: state transition function $\mathbf{t}:\varTheta\to\left(\mathbb{R}_{p}^{\Lambda}\times\mathbb{R}_{p}^{d}
\to\mathbb{R}_{p}^{\Lambda}\right)$ and output function $\mathbf{o}:\varTheta\to\left(\mathbb{R}_{p}^{\Lambda}\to\mathbb{R}_{p}^{d}\right)$, where $\Lambda$ is the dimension of the state and $\varTheta$ is the parameter space. Let $\mathcal{S}\in|V|^{l}$ be the input sequence, the output of a recurrent neural network with parameter $\theta\in\varTheta$ is defined as:

$$ $\displaystyle R_{\theta}(\mathcal{S})$ $\displaystyle=\mathrm{softmax}\left(W^{(E)}\mathbf{o}_{\theta}(s_{l})\right),$ $\displaystyle\forall k\in[{l}],s_{k}$ $\displaystyle=\mathbf{t}_{\theta}(s_{k-1},\textrm{Emb}(\mathcal{S})_{:,k}),$ $$

where $s_{0}\in\mathbb{R}_{p}^{\Lambda}$ is a vector determined by $\theta$ and $W^{(E)}$ is the word embedding matrix. We will omit the subscript $\theta$ when it is clear from the context.

We can characterize the complexity of an RNN architecture with the following three measures,

- 1.
Parameter size $\mathrm{P}$: the number of bit of parameters determining $\mathbf{t}$ and $\mathbf{o}$.
- 2.
State memory size $\mathrm{M}$: the number of bits to record the state of the RNN, in this case, is $\Lambda\times p$.
- 3.
Circuit size $\mathrm{C}$: the number of bit-wise arithmetic operations needed to calculate $\mathbf{t}$ and $\mathbf{o}$.

We further constrain ourselves to RNNs with the following properties, which still contain all the modern variants of linear-time Transformers to the best of our knowledge.

definitiondefregRNN
We say that an RNN architecture is regular if $\mathrm{C}=\Theta(\mathrm{P})$, $\mathrm{P}=\Omega(\mathrm{M})$, and $\mathrm{P}=\text{Poly}(\mathrm{M})$.
We note that the above definition of RNNs is general enough to also contain many recent recurrent architecture using streaming context windows or finite KV cache (Xiao et al., ; Kuratov et al., ; Bulatov et al., ; Oren et al., ).

For our upper bound on RNNs, we will consider the following linear RNNs, which is a special case of regular RNNs.

###### Definition 3.4 (RNN block) .

A Linear RNN block is defined as follows:

$$ $\displaystyle f(X)=X+\mathrm{LU}(X)+g(X+\mathrm{LU}(X)),$ $$

where $g$ is a column-wise ReGLU feed-forward network with width $0pt$ and output dimension $d$ and $\mathrm{LU}$ is a linear unit, defined as

$$ $\displaystyle h_{0}$ $\displaystyle=0,h_{:,t}=Ah_{:,t-1}+BX_{:,t},\mathrm{LU}(X_{:,1:l})=h_{:,1:l}.$ $$

###### Definition 3.5 (Linear RNN) .

A Linear RNN is a recurrent neural network

$$ $\displaystyle R(\mathcal{S})=\mathrm{softmax}\left(W^{(E)}\left(f_{L}\left( \ldots f_{1}\left(\textrm{Emb}\left(\mathcal{S}\right)\right)\right)\right) \right)_{:,l}.$ (3) $$

where $\mathrm{softmax}$ is the column-wise softmax function, $f_{i}$ is the $i$-th Linear RNN block. We will call the $i$-th Linear RNN block the $i$-th layer of the Linear RNN and denote its feed-forward layer and linear unit layer as $g_{i}$ and $\mathrm{LU}_{i}$ respectively.

###### Definition 3.3 (RNN) .

###### Definition 3.4 (RNN block) .

###### Definition 3.5 (Linear RNN) .

##### Language Models for Algorithmic Problems.

An algorithmic problem is a problem that may be solved by an algorithm.
In this paper, we focus on algorithmic problems $f:V^{+}\to V_{\mathrm{A}}$ that
asks for computing $f(\mathcal{S}_{\mathrm{in}})$ given a sequence of tokens $\mathcal{S}_{\mathrm{in}}$ as the input, where $V_{\mathrm{A}}$ is the set of possible answers.
We say that an LM $M$ can (directly) solve an algorithmic task $f$ if, given the sequence $\mathcal{S}_{\mathrm{in}}$, the probability distribution $M(\mathcal{S}_{\mathrm{in}})$ for the next token is peaked at the correct output token $f(\mathcal{S}_{\mathrm{in}})$, i.e., $\arg\max_{j\in V}M(\mathcal{S}_{\mathrm{in}})[j]=f(\mathcal{S}_{\mathrm{in}})$.

Chain-of-Thought (CoT) reasoning is a technique that allows the LM to produce intermediate steps before the final output. We primarily concern with the ability of language models to solve algorithmic problems with CoT, since many algorithmic problems may be too challenging to solve in just one step (Feng et al., ; Li et al., ) and testing with CoT is now standard in the field (Wei et al., ; Nye et al., ; Kojima et al., ; Wang & Zhou, ).
In our context, we say that an LM $M$ can solve an algorithmic problem $f$ with CoT if the following process terminates with a sequence ended with $f(\mathcal{S}_{\mathrm{in}})$.
First, let $\mathcal{S}_{0}=\mathcal{S}_{\mathrm{in}}$.
For all $i\geq 0$, decode the next token $s^{\mathrm{next}}_{i}=\arg\max_{j\in V}M(\mathcal{S}_{i})[j]$ from $M$, and append it to the current sequence $\mathcal{S}_{i+1}=\mathcal{S}_{i}\oplus s^{\mathrm{next}}_{i}$.
If $s^{\mathrm{next}}_{i}\in V_{\mathrm{A}}$, then the process terminates with $\mathcal{S}_{i+1}$ with $i$ steps of CoT; otherwise the process continues.

It is evident that
if an LM can solve an algorithm problem with 0 0 steps of CoT,
then an LM $M$ can (directly) solve the problem. In this case, we also say that the LM can solve the problem without CoT.

## 4 Can CoT improve the Representation Power of RNNs?

In this section, we aim to understand the representation power of RNNs with CoT. We first show the positive result that RNNs with CoT can solve tasks that are impossible for RNNs without CoT fixing the state size. We then proceed to understand whether CoT can make RNNs as expressive as Transformers. We show that, even with CoT, RNNs still struggle to solve problems that explicitly require in-context retrieval and this representation gap propagates to seemingly retrieval-irrelevant reasoning tasks such as IsTree. Finally, we show that this gap is indeed one-sided: there only exist tasks where Transformers require exponentially less parameters than RNNs, but not the other way around.

### 4.1 CoT Strictly Improves RNNs

On the positive side, we show that CoT broadens the representation power of RNNs under mild complexity assumption.

[]theoremthmrnncot
Assuming $\mathrm{PSPACE}\not\subset\mathrm{P/Poly}$, there exists a task with input length $n$ that can be solved by a Linear RNN (Definition 3.5) family with $\Theta(\log n)$ bit memory with polynomial length CoT but cannot be solved by any regular RNN (Definition 3.3) family with $\Theta(\log n)$ bit memory without CoT.

We note that this result is in the same vein as the recent work on the benefit of CoT for Transformers (Feng et al., ; Li et al., ), which shows that Transformer with constant size and $O(\log n)$ or constant precision can’t simulate any polynomial size circuit family while Transformer with CoT can. Here we utilize a different complexity hypothesis, which intuitively states that not all polynomial space complexity problems ($\mathrm{PSPACE}$) can be solved by a polynomial-size circuit family ($\mathrm{P/Poly}$). We show that this hypothesis implies that RNNs can benefit from CoT in solving reasoning tasks.

##### Proof Sketch.

The intuition behind the proof is that RNNs without CoT are shallow circuits with size $\text{Poly}(\log n)$. However, RNNs with CoT can simulate a Turing machine with $O(\log n)$ space perfectly within $\exp(O(\log n))=\text{Poly}(n)$ steps. Hence, we only need to show that there exists a problem with linear space complexity that cannot be solved by a polynomial-size circuit family and this can be induced by the (seemingly weaker) assumption that $\mathrm{PSPACE}\not\subset\mathrm{P/Poly}$ (see Lemma B.25).

### 4.2 CoT Cannot Close the Representation Gap with Transformers

In this section, we aim to understand the representation gap between RNNs and Transformers. We justify that RNNs, even with CoT, struggle to solve algorithmic problems that require the capability of retrieving information from the current context, which we call In-context Retrieval for short.
This limitation is caused by the memory efficiency of RNNs: if the memory has at most $o(n)$ bits, then we can involve techniques from streaming complexity to prove impossibliity results for in-context retrieval problems.

#### 4.2.1 Simple Problems on In-Context Retrieval

First, we prove that RNNs have a significant representation gap with Transformers in solving several simple algorithmic problems that directly test the in-context retrieval capability.

###### Definition 4.1 (Index) .

Index is a problem that given a sequence of tokens with length $n$ and a query token $i\in[n]$, requires the model to output the type of the $i$-th token.

###### Definition 4.2 (Associative Recall) .

Associative Recall (AR) is a problem that given a sequence of tokens with length $n$ consisting of tokens in $[n]$ and a query token $q\in[n]$, requires the model to output the next token of $q$ in the sequence.

###### Definition 4.3 ( $c$ -gram Retrieval) .

An $c$-gram is a contiguous subsequence of $c$ tokens in a sequence of tokens. $c$-gram retrieval is a problem that given a sequence of tokens with length $n$ and a query $(c-1)$-gram that is the prefix of a $c$-gram in the sequence, requires the model to output the last token of that $c$-gram.

###### Definition 4.4 (Counting) .

Counting is a problem that given a sequence of tokens with length $n$, a query token $q\in[n]$, and a query number $t\in\mathbb{N}$, requires the model to output 0 0 or $1$ to indicate whether the number of occurrences of $q$ in the sequence is greater than $t$.

Here, Index and AR are perhaps the most basic problems in retrieval, where Index asks for retrieving a token from the input sequence viewed as a linear array of tokens, and AR asks for retrieving a token from the input sequence viewed as an associative array.
These two problems have been studied extensively by different communities. Index is a classic problem in streaming and communication complexity (Munro & Paterson, ), known to be impossible to solve with $o(n)$ bits of memory for streaming algorithms.
AR has been regarded as a fundamental problem that an artificial intelligence system should be able to solve (Willshaw et al., ; Hopfield, ; Hinton & Anderson, ; Graves et al., ; Ba et al., ). In the context of LLMs, AR has been observed to correlate with in-context learning performance (Elhage et al., ) and has also been used extensively as synthetic surrogate tasks for pretraining performance (Fu et al., ; Poli et al., ; Lutati et al., ).
Besides Index and AR, $c$-gram retrieval is a natural extension of AR to the case where the query key can contain multiple tokens: instead of retrieving a token given a single-token key, $c$-gram retrieval asks for retrieving a token when the given key is a $(c-1)$-gram. This task has been studied empirically, but not theoretically in Jelassi et al. ().
Counting is a problem that asks for the number of occurrences of a token, thereby testing the model’s capability to retrieve some statistics of relevant information from the input sequence.

The following theorems show that RNNs with $O(\log n)$ bit memory cannot solve any of the four tasks, while Transformers can solve them perfectly with a constant size and $O(\log n)$ precision.

[]theoremthmindex
For task $T\in\{\text{Index, AR, c-gram retrieval, Counting}\}$, there exists a Transformer family with constant size and $O(\log n)$ precision that can solve $T$ of size $n$. On the other hand, for any RNN family $R$ with $o(n)$ bit memory, $R$ cannot solve $T$ of size $n$ with any length of CoT for large enough $n$.

###### Definition 4.1 (Index) .

###### Definition 4.2 (Associative Recall) .

###### Definition 4.3 ( c 𝑐 c italic_c -gram Retrieval) .

###### Definition 4.4 (Counting) .

##### Proof Sketch.

The key idea of the lower bound of the proof is to put RNNs into the framework of communication complexity and use information-theoretic arguments to prove a lower bound. RNNs have the following property if party $\mathrm{A}$ simulates the RNN on the first part of the input and sends the state to party $\mathrm{B}$, then party $\mathrm{B}$ can simulate the RNN on the second part of the input with the state received from party $\mathrm{A}$ to recover the output of the RNN perfectly. Hence, in the above theorem, if the RNN can solve the problem with $o(n)$ input size, then the information about the input can be compressed to $o(n)$ bit to produce the output, which contradicts the information-theoretic lower bound.

For the upper bound, we show that the Transformer can solve the problem by utilizing an attention mechanism called $\mathrm{Match}$ that takes the query token and attends to previous keys that match the query token on certain predefined coordinates. This mechanism allows the Transformer to read its context window like a key-value dictionary and hence can solve the problems perfectly. For the counting problem, we additionally use a $\mathrm{COUNT}$ attention mechanism that counts the number of occurrences of the queried token by attending evenly to each appearance of the queried token.

#### 4.2.2 Understanding the Representation Power of RNNs Beyond Simple In-context Retrieval Problems

A natural question would be if an algorithmic problem does not directly test the in-context retrieval capability, can we hope that RNNs would have the representation power to solve it?
Do RNNs and Transformers have the same representation power in this case? We show that the limited memory size in RNNs can still be a bottleneck in solving algorithmic problems.
Even if the retrieval capability is not explicitly tested in an algorithmic problem, it may still be required implicitly for reasoning about the answer.

We demonstrate this gap on a minimal example of algorithmic problems, called $\mathrm{IsTree}$: given an undirected graph $G$ of $n$ nodes, determine whether $G$ is a tree, i.e., whether every pair of nodes is connected by exactly one simple path. A classical solution to IsTree is running Depth First Search (DFS), which takes $O(n)$ time.

In the context of language modeling, we can write the graph $G$ as a sequence of tokens, and then the task of IsTree is to determine whether $G$ is a tree by predicting a $\mathrm{YES}/\mathrm{NO}$ token with or without CoT. We use the following tokenization for the graph $G$:

$$ $\displaystyle\mathrm{Tokenize}(G)=\{\texttt{
},u_{1},\sim,v_{1},u_{2},\sim v _{2},\ldots,u_{m},\sim,v_{m}\},$ (4) $$

where 
 and $\sim$ are two special tokens representing the start of the sentence and an edge, and $u_{i},v_{i}$ are numbers denoting the nodes of the graph.

Our result states that RNN with $o(n)$ bit memory cannot solve IsTree, even with an arbitrary choice of chain of thought. On the other hand, there exists a Transformer with constant size and $O(\log n)$ precision that can generate a chain-of-thought of length $n$ following DFS and perfectly solve the same question.
{restatable}[]theoremthmistree
For any RNN family $R$ with $o(n)$ bit memory, $R$ cannot perfectly solve IsTree of size $n$ for large enough $n$, with any length of CoT. On the other hand, there exists a Transformer $T_{1}$ with constant dimension and depth, and $O(\log n)$ precision that can solve IsTree of size $n$ perfectly with Chain of Thought of length $O(n)$.

##### Proof Sketch.

The main idea of the proof is that the task of IsTree requires the model to reason about the global structure of the graph, which is beyond the capability of RNNs with limited memory. We prove the lower bound by constructing a graph from a binary sequence and showing that RNNs with $o(n)$ memory cannot solve the problem by a reduction to an information-theoretic lower bound. For the upper bound, we show that the Transformer can simulate the DFS algorithm by outputting the Euler tour of the connected components of vertex $1$ and then check the length of the Euler tour with its capability of In-context Retrieval.

The key idea of the lower bound of the proof is to again utilize the information-theoretic lower bound. This idea lies in the core of streaming complexity literature and investigation on the IsTree problem dates back to Henzinger et al. (). We hereby restate the proof for completeness. Given any binary sequence $x$ of length $n-2$ and an index $k\in[n-3]$, we can construct a graph as follows: the graph has $n$ nodes, and vertex $a$ is connected to vertex $x_{a}+n-1$ for any $a\in[n-2]$. Moreover, vertex $k$ is connected to vertex $k+1$. The graph is a tree if and only if $x_{k}\neq x_{k+1}$.

Figure: Figure 3: An example of the graph constructed from the binary sequence $x=0101$ and the index $k=2$ and the corresponding DFS tour.

Now assuming there is an RNN with $o(n)$ memory that can solve IsTree, consider two parties $\mathrm{A}$ and $\mathrm{B}$ each holding the sequence $x$ and the index $k$, they can construct two parts of the graph using their information and then $\mathrm{A}$ can simulate RNN on the first part of the graph and send the state to $\mathrm{B}$, and $\mathrm{B}$ can simulate RNN (potentially with CoT) on the second part of the graph to recover the output of the IsTree problem, which is equivalent to whether $x_{k}\neq x_{k+1}$. However, note that $k$ is never sent to $\mathrm{A}$, and hence actually $\mathrm{B}$ can get whether $x_{k}\neq x_{k+1}$ for any $k\in[n-3]$, which contradicts the information-theoretic lower bound.

Now for the upper bound, we will let the Transformer simulate the DFS algorithm by outputting the Euler tour of the connected components of vertex $1$ and then check the length of the Euler tour (see Algorithm 1). To simulate the tour, we will implement two functions through the Transformer Block:

- 1.
Given a prefix of the tour, find the parent of the last vertex in the tour. This can be implemented by copying each token’s predecessor’s type to that token and then using the $\mathrm{Match}$ mechanism to match the first occurrence of the current token in the sequence.
- 2.
Given the tokenized input graph and an edge $(u,v)$, find the next edge after $(u,v)$ containing $v$ in the input graph. We will use another attention mechanism called $\mathrm{COUNT}$ to count, for each edge $e=(a,b)$ in tokenized input graph, the number of occurrences of $a$ and $b$ up to that edge and store $1/(n_{e,a}+1)$ and $1/(n_{e,b}+1)$ in the token corresponding to the edge, where $n_{e,a}$ and $n_{e,b}$ are the corresponding counts. Then given the edge $(u,v)$, we can use the $\mathrm{Match}$ mechanism to find $1/(n_{(u,v),v}+1)$. Then we will use a feed-forward layer with gated relu activation, constant depth, and constant width to approximate $1/(n_{(u,v),v}+2)$ and then use the $\mathrm{Match}$ mechanism again to find the next edge containing $v$.

Through the above two functions, the Transformer can simulate the DFS algorithm and hence solve the IsTree problem perfectly.

#### 4.2.3 Transformers are Strictly More Expressive Than RNNs

The above theorems show the existence of tasks where Transformers require exponentially less memory than RNNs. However, they have not rule out the possibility that there exists a corresponding task where the Transformer will be more redundant and require exponentially more parameters than RNNs. However, the following theorem confirms that such a task doesn’t exist for regular RNN (Definition 3.3).

The theorem is in the same vein as the recent work on the CoT for Transformer (Li et al., ), which shows the constant size and constant precision Transformer with a polynomial-size position embedding can simulate any polynomial size circuit. The major difference of our theorem is that (1) we consider a Transformer with fixed word and position embedding, hence allowing the parameter number to be logarithmic in the input size, and (2) we consider simulating RNNs, which is a special kind of circuit family and hence we can use more succinct representation utilizing the structural property attached to the recursive process.

[]theoremthmtransrnn
Given any constant $A>0$, constant word width and number of special symbols $d,n_{S}>0$, for any $n$, precision $p=\Theta(A\log n)$ and RNN $R$ with word embedding $W^{(E)}\in\mathbb{R}_{p}^{(n+n_{S})\times d}$ such that each recurrent iteration can be calculated with a circuit with size $\mathrm{P}(n)\leq 2^{p/2}$, there exists a Transformer $T$ with $O(\mathrm{P}(n)\log\max\{\mathrm{P}(n),n\})$ bit parameter and word embedding $\begin{bmatrix}W^{(E)}&\mathbf{0}^{(n+n_{S})\times d}\end{bmatrix}$ that can simulate the RNN with at most $n^{A}$ step chain-of-thought precisely, using at most $(\mathrm{P}(n)+1)n^{A}$ step chain of thought on every input with length $n$.

##### Proof Sketch.

The key idea is to encode the RNN circuit into the Transformer’s weight and simulate the circuit gate by gate, utilizing the $\mathrm{Match}$ mechanism to fetch the input of each gate. In this manner, although the naive circuit to simulate the RNN for $n^{A}$ steps would require $O(n^{A})$ parameter, the Transformer only needs to store one instance of the RNN circuit in its weight and hence we only need $O(P(n)\log\max\{P(n),n\})$ parameter, which is at most polynomial in the parameter size of the RNN.

## 5 Enhancing the In-context Retrieval Capability Closes the Representation Gap

In Section 4.2, we show that RNNs are deficient at In-context Retrieval, hence leading to a significant representation gap with Transformers.
In this section, we aim to understand: if we enhance the In-context Retrieval capability of RNNs, do RNNs remain to have any representation gap with Transformers?
We answer this question by considering an explicit and an implicit way to enhance the In-context Retrieval capacity and showing that both ways can close the representation gap between RNNs and Transformers in solving algorithmic problems.

Figure: The retrieval augmented RNN

### 5.1 Explicit Retrieval Through Regular Expression

First, we explore the power of RNNs with Retrieval Augmented Generation (RAG),
which gives an LM the capability to retrieve relevant information to assist generation. In our context, we are specifically interested in allowing LMs to call functions to retrieve information from their context, which we call In-context Retrieval Augmented Generation (In-context RAG).

We will first show that adding function calls to associative recall is not enough to close the representation gap between RNNs and Transformers.

###### Proposition 5.1 .

For any RNN family with $O(\log n)$ bit memory and $O(\log n)$ parameter with an oracle to receive results for the AR problem (Definition 4.2) for any queries, for large enough $n$, the RNN can’t solve the index problem (Definition 4.1) with length $n$ in any CoT steps.

###### Proof.

Consider a special type of index problem where every token at the even position of the input sequence is a special token $\kappa$ and the rest of the tokens are uniformly random. Then the oracle for the AR problem can be simulated by the RNN by simply outputting the $\kappa$ when the query is not $\kappa$ and outputting the third token when the query is $\kappa$. However, following similar proof of Definition 4.4, we can show that the RNN can’t solve this special form of index problem with length $n$ in any CoT steps.
∎

In this light, we need to consider a more general form of In-context Retrieval capability. We specifically consider a special form of In-context RAG that enables an LM to perform regular expression matching because the regular expression is a flexible primitive that can be used to describe a wide range of retrieval tasks and can be implemented efficiently on modern hardware.

Given an LM $M$ with vocabulary $V$ (containing two additional special tokens, $\texttt{<}\mathrm{StartSearch}\texttt{>}$ and $\texttt{<}\mathrm{EndSearch}\texttt{>}$) and the tokenized input sequence $\mathcal{S}_{\mathrm{in}}\in|V|^{l_{0}}$, the LM $M$ with In-context RAG generates following sequence of tokenized sequence:

$$ $\displaystyle\mathcal{S}_{0}$ $\displaystyle=\mathcal{S}_{\mathrm{in}},\qquad s^{\mathrm{next}}_{i}=\arg\max_ {j\in V}M(\mathcal{S}_{i})[j],$ $\displaystyle\mathcal{S}_{i+1}$ $\displaystyle=\begin{cases}\mathcal{S}_{i}\oplus s^{\mathrm{next}}_{i},&\quad \text{if }s^{\mathrm{next}}_{i}\neq\texttt{<}\mathrm{EndSearch}\texttt{>}\\ \mathcal{S}_{i}\oplus s^{\mathrm{next}}_{i}\oplus\mathrm{RETRIEVE}\left( \mathcal{S}_{i}\right),&\quad\text{otherwise.}\end{cases}$ $$

Here $\mathrm{RETRIEVE}$ looks for the last occurrence of $\texttt{<}\mathrm{StartSearch}\texttt{>}$ at position $l_{s}$ and $\texttt{<}\mathrm{EndSearch}\texttt{>}$ in $\mathcal{S}$ at position $l_{e}$ and treat $\mathrm{Detokenize}(\mathcal{S}_{l_{s}:l_{e}})$ as a regular expression, where $\mathrm{Detokenize}$ maps the tokenized sequence back to the string, inserting a space between every pair of adjacent tokens. The algorithm then runs a regular expression matching on $\mathrm{Detokenize}(\mathcal{S}_{1:l_{s}-1})$, finds the first matching substring, and returns the first capturing group according to the regular expression (i.e., content embraced by a pair bracket in the regular expression).
While there are many grammar standards of regular expressions, we adhere to the standard specified in the re library of Python. That is, we evaluate the following Python code to get the result of the regular expression matching:

| [⬇](data:text/plain;base64,cmUuc2VhcmNoKHBhdHRlcm4sIHN0cmluZykuZ3JvdXAoMSk=)<br>re.search(pattern, string).group(1) |
| --- |

where $\mathrm{Detokenize}(\mathcal{S}_{l_{s}:l_{e}})$ is the pattern and $\mathrm{Detokenize}(\mathcal{S}_{1:l_{s}-1})$ is the string.

First, we show that In-context RAG with regular expression is powerful enough for RNNs to solve the In-context Retrieval problems in Section 4.2.1 in $O(1)$ CoT steps.

[]theoremthmindexre
For task $T\in\{\text{Index, AR, c-gram retrieval, Counting}\}$, there exists a Linear RNN family with $O(\log n)$ bit memory and $O(\log n)$ parameter, that can solve $T$ with In-context RAG in $O(1)$ CoT steps.

###### Proposition 5.1 .

###### Proof.

##### Proof Sketch.

For the Index problem, let the RNN output the regular expression ^(?:\S\s*){$a$}(\S), where ${\color[rgb]{0,0,1}\definecolor[named]{pgfstrokecolor}{rgb}{0,0,1}{a}}=k-1$. For AR, let the RNN output \b $q$ \b(\S+)\b, where $q$ is the number in query. For $c$-gram retrieval, let the RNN output \b $q_{1}$ … $q_{c-1}$ \b(\S+)\b, where $q_{i}$ is the $i$-th number in the query. For Counting, let the RNN output (\b $v$ \b){$k+1$}, where $v$ is the query token and $k$ is the query threshold.

Beyond these simple In-context Retrieval problems, in Section 4.2.2, we have shown that RNNs cannot solve IsTree due to its implicit requirement of In-context Retrieval capability. We now show that In-context RAG can help linear RNN solve IsTree in $O(n)$ CoT steps. See Section B.9 for the proof.
{restatable}theoremthmragistree
There exists a Linear RNN family with $O(\log n)$ bit memory and $O(\log n)$ parameter, that can solve IsTree of size $n$ with In-context RAG in $O(n)$ CoT steps.

To further show the power of the explicit retrieval, the following theorem further proves a general result, showing that In-context RAG empowers RNNs with $O(\log n)$ bit memory to simulate polynomial-time Turing machines.

[]theoremrnnturing
Given any constant $A,B$, for any polynomial-time Turing machine $T\in\mathrm{TIME}(n^{A})$ with $B$ states and vocabulary size $B$, there exists a retrieval augmented Linear RNN family (see Definitions A.5 and 3.5) with vocabulary of $B$ special symbol, $O(A\log n)$ bit precision and memory, and $O(AB^{2}\log n)$ bit parameters, that can simulate the result of $T$ on any input with length $n$ with a chain of thought of length $O(n^{A})$.

##### Proof Sketch.

The retrieval augmented RNN can simulate the Turing machine by maintaining the state of the Turing machine and the position of the tape head in its memory and writing down the tape in the context in the form of $c\ v\ c$ to indicate the value of the tape at position $c$ is updated to $v$. Given the input tape $\mathrm{TAPE}[1,i]$, the retrieval augmented RNN will first write down the initial tape in the previous form of $i\ \mathrm{TAPE}[1,i]\ i$ using the regular expression used in the Index problem.
The RNN can then generate the search queries with forms of $p$ (.) $p$ .*?$ with $p$ being the pointer to retrieve the information from the context.

As a final note, our focus here is to understand the representation power of RNNs given an appropriate RAG, but not to propose a method that immediately leads to practical applications.
While the above results show that In-context RAG can close the representation gap between RNNs and Transformers in solving algorithmic problems, a limitation here is that In-context RAG is not an immediate practical solution, as there is no existing training data for this In-context RAG.

### 5.2 Implicit Retrieval by Appending Just One Transformer Layer

Since Bahdanau et al. (), attention mechanisms have been understood as a form of compensation for the fixed memory size of RNNs, allowing the model to attend to the entire context. We show in this section formally that this form of implicit retrieval can close the representation gap between RNNs and Transformers in solving algorithmic problems. We consider the following hybrid architecture, which combines the RNN and the Transformer by appending a single Transformer layer to the RNN output.

###### Definition 5.2 (Hybrid RNN) .

A hybrid RNN is a model that consists of an RNN with transition and output function $\mathbf{t},\mathbf{o}$ and one Transformer layer $f$, the output of the RNN is used as the input of the Transformer layer and the output of the Transformer layer is used to produce the next token. Concretely, given the input sequence $\mathcal{S}_{\mathrm{in}}$, the output of the hybrid architecture is:

$$ $\displaystyle\mathcal{H}(\mathcal{S})$ $\displaystyle=\mathrm{softmax}\left(W^{(E)}f\left([\mathbf{o}(s_{k})]_{k\in[l] }\right)\right)_{:,l},$ $\displaystyle\forall k\in[l],s_{k}$ $\displaystyle=\mathbf{t}(s_{k-1},\textrm{Emb}(\mathcal{S})_{:,k}),$ $$

First, we show that hybrid RNNs can solve the In-context Retrieval problems in Section 4.2.1 without CoT.

[]theoremthmhybridindex
For task $T\in\{\text{Index, AR, c-gram retrieval, Counting}\}$, there exists a hybrid Linear RNN (Definitions 5.2 and 3.5) family with $O(\log n)$ bit memory and $O(\log n)$ parameter, that can solve $T$ without CoT.

###### Definition 5.2 (Hybrid RNN) .

##### Proof Sketch.

The proof is similar to the proof of Definition 4.4, using the appended Transformer layer to simulate the $\mathrm{Match}$ function and $\mathrm{COUNT}$ function in the RNN.

Similar to the situation in Section 5.1, the implicit retrieval method can enpower the hybrid linear RNN to solve IsTree with CoT. See Section B.12 for the proof.

[]theoremthmhybridistree
There exists a hybrid Linear RNN with $O(\log n)$ bit memory and $O(\log n)$ parameter, that can solve IsTree of size $n$ with a chain of thought of length $O(n\log n)$.

Further, we show that this hybrid architecture with only one attention block is powerful enough to even simulate any polynomial-time Turing machine with CoT.
{restatable}[]theoremhybridturing
Given any constant $A,B$, for any polynomial-time Turing machine $T\in\mathrm{TIME}(n^{A})$ with $B$ states and vocabulary size $B$, there exists a hybrid Linear RNN (see Definition 5.2) with vocabulary of $B$ special symbol, $O(A\log n)$ bit precision and memory, and $O(AB^{2}\log n)$ bit parameters, that can simulate the result of $T$ on any input with length $n$ in $O(n^{A})$ CoT steps.

##### Proof Sketch.

The proof is similar to the proof of Section 5.1. Instead of using regular expressions to retrieve the information from the context, the hybrid architecture can use the attention mechanism in the Transformer layer to implement the $\mathrm{Match}$ function to retrieve the information from the context.

## 6 Experiments

Figure: Figure 5: We train Mamba with one additional layer of attention with a frozen word embedding and decoding head of three different model sizes (0.5M, 1M, 2M) on IsTree with three different sizes of graph (16, 32, 64) under three different setups. Vanilla means the model directly predicts the label. COT means the model will generate a chain-of-thought process based on DFS (see Algorithm 1) before prediction. Retrieval means the model will generate the chain of search queries and reasoning before prediction (see Algorithm 2).
Refer to caption: extracted/2402.18510v3/main/istree2.png

We tested our theory on the IsTree task. To generate the graph, we follow the procedure described in the proof of Section 4.2.2 (see Figure 3). The CoT data is generated using Algorithm 1 and the retrieval data is generated using Algorithm 2. For the CoT model, we decode the reasoning path during inference time until we reach the first $\mathrm{YES}$ or $\mathrm{NO}$ up to a max token limit greater than the length of all ground truth CoT. For the data points that the model fails to give a prediction, we assume the model gets it correct with 0.5 probability. For the retrieval task, we omit the explicit format of the regular expression and only ask the model to generate the vertices and special tokens in the regular expression to shorten the length of the input sequence. The reported accuracy is calculated over a validation set of 5000 samples using the last iteration of the model.

We train three different architectures: (1) LLaMA architecture (Touvron et al., ) representing Transformers, (2) Mamba architecture (Gu & Dao, ) representing RNNs, and (3) Mamba with one additional layer of LLaMA block representing hybrid architectures. Following our theory, we freeze and weight-tie the prediction head and word embedding in all the models. For ease of training, we use a different embedding function mapping $i-$th token to $[\sin(\frac{i}{10000^{j/d}}),\cos(\frac{i}{10000^{j/d}})]_{j\in[d/2]}$ with $N$ being the number of different tokens and use standard RoPE (Su et al., ) as position embedding.
We train every model with at least 1M samples to guarantee convergence using Adam with a cosine learning rate. If the model doesn’t converge, we retrain using 5M samples. After a grid search over learning rates, we train all the Transformer models with learning rates 1e-3 and the rest of the models with learning rates 3e-4.

The results are shown in Figures 3 and 5. We observe that:

- 1.
CoT improves the performance for both Transformers and RNNs. However, the RNNs’ performance degrades sharply as the graph size increases and the Transformers consistently outperforms the RNNs.
This is consistent with our theory that CoT can improve the expressiveness of the RNN models but the expressiveness is still not enough to solve the IsTree task (see Sections 4.1 and 4.2.2).
- 2.
Retrieval Augmentation via regular expression allows all the models to reach near-perfect accuracy. This is consistent with our theory that retrieval augmentation via regular expression can improve the expressiveness of the RNN models to solve algorithmic tasks (see Sections 5.1 and 5.1).
- 3.
The hybrid model shows performance on par with the Transformer, which is consistent with our theory (see Sections 5.2 and 5.2).

## 7 Conclusion and Discussion

This paper studies the representation gap between RNNs and Transformers on algorithmic problems.
It is proved that CoT can improve RNNs but is insufficient to close the gap with Transformers:
the inability of RNNs to perform in-context retrieval is a key bottleneck.
To address this, we show that adopting In-Context RAG or appending a single Transformer layer to RNNs can enhance their in-context retrieval capabilities and close the representation gap with Transformers.

One limitation of this work is that the solution of In-Context RAG through regular expression is for the purpose of understanding the bottleneck of the representation power of RNNs, and may not be a practical method beyond $\mathrm{IsTree}$ since there is no existing training data for this type of RAG.
Effectively enabling or eliciting LLMs’ capability to perform in-context RAG or other types of RAG is an interesting direction for future work.
Second, appending a single Transformer layer to RNNs is a minimal example of making architectural changes to RNNs to improve their representation power while marginally increasing the memory cost. It is left unexplored what other architectural changes can pose a similar effect or enjoy a better trade-off between representation power and memory efficiency.
Finally, we only study the aspect of representation power, and do not analyze the training dynamics and generalization of RNNs and Transformers. We believe this is the most challenging but important direction for future work.

## Appendix A Additional Definitions

We will now define some definitions used in the proofs.

### A.1 Reasoning Tasks on Graphs.

When resaoning on graphs, without otherwise specified, we will use $n$ as the number of vertices and $m$ as the number of edges. Without loss of generality, we will assume the vertices are labeled by $[n]$.

We will focus on decision problems on graphs, which are defined as follows:

###### Definition A.1 (Decision Problem on Graphs) .

A decision problem on graphs is a function $f:\mathcal{G}\to\{\mathrm{YES},\mathrm{NO}\}$, where $\mathcal{G}$ is the set of all possible graphs.

We will use the following decision problem as our main example:

###### Definition A.2 (IsTree) .

$\mathrm{IsTree}(G)=\mathrm{YES}$ if $G$ is a tree, and $\mathrm{IsTree}(G)=\mathrm{NO}$ otherwise.

One can view IsTree as a minimal example of reasoning tasks. One of the classical solutions to IsTree is running Depth First Search and this algorithm takes $O(n)$ time.

###### Definition A.1 (Decision Problem on Graphs) .

###### Definition A.2 (IsTree) .

### A.2 More on Numeric Precisions.

We will use $\mathrm{ROUND}(x,p)$ to denote the rounding function that rounds $x$ to the nearest number in $\mathbb{R}_{p}$. We will assume $p$ is an odd number without loss of generality.

$$ $\displaystyle\mathbb{R}_{p}=\left\{(2b_{p}-1)\left(\sum_{i=1}^{p-1}b_{i}2^{(p- 1)/2-i}\right):\forall i\in[p],b_{i}\in\{0,1\}\right\}.$ (5) $$

For calculation over $\mathbb{R}_{p}$, we will assume the calculation is exact and the result is rounded to $\mathbb{R}_{p}$ at the end, that is, for operator $\oplus$, we will have

$$ $\displaystyle\mathrm{ROUND}(x,p)\oplus_{p}\mathrm{ROUND}(y,p)$ $\displaystyle=$ $\displaystyle\mathrm{ROUND}\left(\mathrm{ROUND}(x,p)\oplus\mathrm{ROUND}(y,p), p)\right).$ $$

We will additionally define $\mathbb{Z}_{p}$ as the set of all integers that can be represented by $p$-bit floating point numbers. We will define $1/[m]$ as the set of unit fractional $\{\frac{1}{i}\}_{i\in[m]}$. Further, we will define $\mathrm{ROUND}(1/[m],p)$ as the rounding of $1/[m]$ to $\mathbb{R}_{p}$. We will additionally define for any real number $x\in\{0,1\}$, $\mathrm{next}(x)=\frac{1}{m+1}$ where $m=\arg\min_{k\in\mathbb{Z}}|x-\frac{1}{k}|$.

### A.3 Models

##### Tokenization.

To tokenize a string, we will tokenize all the words separated by the space character into a sequence of tokens.
To tokenize a graph $G$, we will order its edges $\mathrm{E}=\{(u_{i},v_{i})\mid u_{i}<v_{i}\}$ randomly and tokenize it into the following string:

$$ $\displaystyle\mathrm{Tokenize}(G)=\{\texttt{
},u_{1},\sim,v_{1},\ldots,u_{m} ,\sim,v_{m}\}.$ (6) $$

We hereby assume there are constant more special tokens that are not the same as any number token, which are listed below:

- •

: the first special token, indicating the start of a sentence.
- •
$\sim$: the second special token, indicating an edge.
- •
$\mathrm{YES}$: the third special token, indicating the answer is yes.
- •
$\mathrm{NO}$: the fourth special token, indicating the answer is no.
- •
$\texttt{<}\mathrm{StartSearch}\texttt{>}$: the fifth special token, indicating the start of a search query.
- •
$\texttt{<}\mathrm{EndSearch}\texttt{>}$: the sixth special token, indicating the end of a search query.

We will denote the total number of special tokens as $n_{S}$ and the total vocabulary size as $|V|=n+n_{S}$. We will further define the detokenization function $\mathrm{Detokenize}$,

$$ $\displaystyle\mathrm{Detokenize}(\mathcal{S})=``\mathcal{S}_{1}\ \mathcal{S}_{ 2}\ \ldots\ \mathcal{S}_{l}".$ $$

Here each $\mathcal{S}_{i}$ is either a number or a special token, which we will treat as a word.

##### Embedding Functions.

We will use $d$ to denote the dimension of the embedding and $w_{i}$ to denote the $i$-th coordinate vector in $\mathbb{R}^{d}$.

We will separate the embedding function into two parts: the word embedding and the position embedding.
For the word embedding, we will use $iw_{1}\in\mathbb{R}^{d}$ to represent the embedding of the vertice $i$ in the tokenization of the graph. For the $k$-th special token, we will use $w_{2+k}$ to represent its embedding. For example, the embedding of $\sim$ is $w_{2}$. We will denote the word embedding matrix as $W^{(E)}\in\mathbb{R}^{|V|\times d}.$

For the position embedding, we will use $lw_{d}$ to represent the position embedding of the $l$-th token in the tokenization of the graph, which is a hyperparameter. The final embedding of any token sequence is the sum of the word embedding and the position embedding. We will use Emb to denote the embedding function.

This embedding function will be fixed and shared across all models we consider in this paper and will not be learned during training, hence we will not consider it as part of the model parameters.

##### Language Modeling.

In this work, we will consider the difference between Transformer and Recurrent Neural Networks on reasoning tasks, which is a special case of language modeling. We will define language modeling as follows:

###### Definition A.3 (Language Model) .

A language model is a function $M:\cup_{l=1}^{L}|V|^{l}\to\mathbb{P}_{|V|}$, where $|V|$ is the vocabulary size, $l$ is the sequence length, and $\mathbb{P}_{|V|}$ is the probability simplex over $|V|$.

###### Definition A.3 (Language Model) .

### A.4 Language Models for Reasoning.

##### Chain of Thought.

We will now define how we use language models to solve reasoning tasks utilizing the following technique called chain of thought.

###### Definition A.4 (Chain of Thought) .

Given a language model, $M$ with vocabulary $V$ and the tokenized input sequence $\mathcal{S}_{\mathrm{in}}\in|V|^{l_{0}}$, chain of thought (CoT) generates the following sequence of tokenized sequence:

$$ $\displaystyle\mathcal{S}_{0}$ $\displaystyle=\mathcal{S}_{\mathrm{in}},$ (7) $\displaystyle s^{\mathrm{next}}_{i}$ $\displaystyle=\arg\max_{j\in V}M(\mathcal{S}_{i})[j],$ (8) $\displaystyle\mathcal{S}_{i+1}$ $\displaystyle=\mathcal{S}_{i}\oplus s^{\mathrm{next}}_{i},\forall i\geq 0.$ (9) $$

The process terminates at $\mathcal{S}_{i}$ when $\arg\max_{j\in V}M(\mathcal{S}_{i})[j]$ is $\mathrm{YES}$ or $\mathrm{NO}$. The language model can solve the reasoning task within $T$ steps of CoT if the process terminates at $\mathcal{S}_{i}$ where $i\leq T$ and the final output is correct. We will call the special case where the language model solves the reasoning task within 0 0 steps of CoT as solving the reasoning task without CoT.

###### Definition A.4 (Chain of Thought) .

##### Retrieval Augmentation.

We will show in this paper that retrieval augmentation is a necessary technique to solve reasoning tasks for recurrent neural networks. We will define retrieval augmentation as follows:

###### Definition A.5 (Retrieval Augmented Generation) .

Retrieval Augmented Generation means giving the language model the capability to retrieve relevant information to assist generation. We formally described the process here. Given a language model $M$ with vocabulary $V$ (containing two additional special tokens called $\texttt{<}\mathrm{StartSearch}\texttt{>}$ and $\texttt{<}\mathrm{EndSearch}\texttt{>}$) and the tokenized input sequence $\mathcal{S}_{\mathrm{in}}\in|V|^{l_{0}}$, retrieval augmented generation generates following sequence of tokenized sequence:

$$ $\displaystyle\mathcal{S}_{0}$ $\displaystyle=\mathcal{S}_{\mathrm{in}},$ $\displaystyle s^{\mathrm{next}}_{i}$ $\displaystyle=\arg\max_{j\in V}M(\mathcal{S}_{i})[j],$ $\displaystyle\mathcal{S}_{i+1}$ $\displaystyle=\begin{cases}&\mathcal{S}_{i}\oplus s^{\mathrm{next}}_{i},s^{ \mathrm{next}}_{i}\neq\texttt{<}\mathrm{EndSearch}\texttt{>}\\ &\mathcal{S}_{i}\oplus s^{\mathrm{next}}_{i}\oplus\mathrm{RETRIEVE}\left( \mathcal{S}_{i}\right),\text{otherwise.}\end{cases}$ $$

Here $\mathrm{RETRIEVE}$ looks for the last occurrence of $\texttt{<}\mathrm{StartSearch}\texttt{>}$ at position $l_{s}$ and $\texttt{<}\mathrm{EndSearch}\texttt{>}$ in $\mathcal{S}$ at positon $l_{e}$ and treat $\mathrm{Detokenize}(\mathcal{S}_{l_{s}:l_{e}})$ as a regular expression. The algorithm then uses the regular expression on $\mathrm{Detokenize}(\mathcal{S}_{1:l_{s}-1})$. If the regular expression ever matches, the $\mathrm{RETRIEVE}$ will return the match. If the regular expression never matches, $\mathrm{RETRIEVE}$ will return a special token $\mathrm{FAILED}$.

Similar to Definition A.4, we can define the notion of solving the reasoning task within $T$ steps of retrieval augmented generation and solving the reasoning task without retrieval augmented generation.

We will note that assuming $|V|=O(n)$ and every search query and the result is of length $O(1)$, the regular expression evaluation can typically be evaluated in $O(n)$ time.

###### Definition A.5 (Retrieval Augmented Generation) .

## Appendix B Omitted Proof

### B.1 Building Blocks of FFNs Construction

We will first show some basic operations that multiple layers of feedforward neural networks with ReGLU activation can perform that will be used in the following proofs.

###### Lemma B.1 (Multiplication) .

Given two dimensions $i_{1},i_{2}$, there exists a parameter configuration of a 1-layer feedforward neural network with ReGLU activation that for any input $x\in\mathbb{R}^{d}$ and constant width, computes the product of $x_{i_{1}}$ and $x_{i_{2}}$.

$$ $\displaystyle g(x)=[x_{i_{1}}\times x_{i_{2}},0,\ldots,0]^{\top}.$ $$

###### Proof.

We can construct the following feedforward neural network with ReGLU activation:

$$ $\displaystyle W_{1}x=\begin{bmatrix}x_{i_{1}}&-x_{i_{1}}&0\ldots&0\end{bmatrix }^{\top},$ $\displaystyle W_{2}x=\begin{bmatrix}x_{i_{2}}&x_{i_{2}}&0\ldots&0\end{bmatrix} ^{\top},$ $\displaystyle W_{3}h=\begin{bmatrix}h_{1}+h_{2}&0&0&0&\ldots&0\end{bmatrix}^{ \top},$ $\displaystyle b_{1}=b_{2}=b_{3}=0.$ $\displaystyle g(x)=W_{3}(\mathrm{ReLU}(W_{1}x)\otimes\mathrm{ReLU}(W_{2}x))=[x _{i_{1}}\times x_{i_{2}},0,\ldots,0]^{\top}.$ $$

∎

###### Lemma B.2 (Linear Operations) .

Given a linear transformation $W\in\mathbb{R}^{d\times d}$, there exists a parameter configuration of a 1-layer feedforward neural network with ReGLU activation and width $0pt=d$ that for any input $x\in\mathbb{R}^{d}$, computes $Wx$.

###### Proof.

$$ $\displaystyle b_{1}=1^{0}pt,b_{2}=0,b_{3}=0,$ $\displaystyle W_{1}=0,W_{2}=W,W_{3}=I_{d\times d}$ $$

∎

###### Lemma B.3 (Indicator) .

Given a constant integer $B\leq d$ and a dimension $i$, there exists a parameter configuration of a 1-layer feedforward neural network with ReGLU activation and width $4$ that for any input $x\in\mathbb{R}^{d}$, computes the indicator function of $x_{i}=B$ when $x_{i}$ is an integer.

###### Proof.

$$ $\displaystyle b_{2}=1^{0}pt,b_{1}=[-B-0.5,-B+0.5,B-0.6,B+0.4]^{\top},b_{3}=10,$ $\displaystyle W_{2}=0,W_{1}x=[x_{i},x_{i},-x_{i},-x_{i}]^{\top},$ $\displaystyle\mathrm{ReLU}(W_{1}x+b_{1})=\begin{bmatrix}\mathrm{ReLU}(x_{i}-B- 0.5)&\mathrm{ReLU}(x_{i}-B+0.5)&\mathrm{ReLU}(B-x_{i}-0.6)&\mathrm{ReLU}(B-x_{ i}+0.4)\end{bmatrix}$ $\displaystyle W_{3}=10\begin{bmatrix}1&-1&-1&1\end{bmatrix}^{\top}.$ $$

Then,

$$ $\displaystyle g(x)$ $\displaystyle=W_{3}\mathrm{ReLU}(W_{1}x+b_{1})+b_{3}$ $\displaystyle=10\left(\mathrm{ReLU}(x_{i}-B-0.5)-\mathrm{ReLU}(x_{i}-B+0.5) \right)+10\left(\mathrm{ReLU}(B-x_{i}-0.6)-\mathrm{ReLU}(B-x_{i}+0.4)\right)+10$ $\displaystyle=\begin{cases}10\times 0+10\times-1+10=0,&\text{if }x_{i}<B,\\ 10\times-0.5+10\times-0.4+10=1,&\text{if }x_{i}=B,\\ 10\times-1+10\times 0+10=0,&\text{if }x_{i}>B.\end{cases}$ $$

∎

###### Lemma B.4 (Lookup Table) .

For constant $B$ and $k$ such that $kB\leq d$, given a lookup table which key is tuple of $k$ integers bounded by $B$, and value is a scalar in $\mathbb{R}_{p}$, there exists a parameter configuration of a $1-$ layer feedforward neural network with ReGLU activation with width $O(B^{k})$ that for any input $x\in\mathbb{R}^{d}$, computes the value of the lookup table at the key $x_{i_{1}},x_{i_{2}},\ldots,x_{i_{k}}$.

###### Proof.

We can calculate $x_{i_{1}}+B\times x_{i_{2}}+B^{2}\times x_{i_{3}}+\ldots+B^{k-1}\times x_{i_{k}}$, and then scale $B^{k}$ indicator functions to get the value of the lookup table at the key $x_{i_{1}},x_{i_{2}},\ldots,x_{i_{k}}$.
∎

###### Lemma B.5 (Threshold) .

Given any threshold $u$ and constant $\epsilon>0$, there exists a parameter configuration of a 1-layer feedforward neural network with ReGLU activation and width $2$ that for any input $x\in\mathbb{R}^{d}$, computes the indicator function $x_{i}>u$ on $x_{i}\in[-\infty,u-\epsilon]\cup[u,\infty]$.

###### Proof.

$$ $\displaystyle b_{2}=1^{0}pt,b_{1}=[-u+\epsilon,-u+0.5\epsilon]^{\top},b_{3}=0,$ $\displaystyle W_{2}=0,W_{1}x=[x_{i},x_{i}]^{\top},$ $\displaystyle\mathrm{ReLU}(W_{1}x+b_{1})=\begin{bmatrix}\mathrm{ReLU}(x_{i}-u+ \epsilon)&\mathrm{ReLU}(x_{i}-u+0.5\epsilon)\end{bmatrix}$ $\displaystyle W_{3}=\frac{2}{\epsilon}\begin{bmatrix}1&-1&-1&1\end{bmatrix}^{ \top}.$ $$

Then,

$$ $\displaystyle g(x)$ $\displaystyle=W_{3}\mathrm{ReLU}(W_{1}x+b_{1})+b_{3}$ $\displaystyle=2/\epsilon\left(\mathrm{ReLU}(x_{i}-u-\epsilon)-\mathrm{ReLU}(x_ {i}-u+0.5)\right)$ $\displaystyle=\begin{cases}0&\text{if }x_{i}<u-\epsilon,\\ 1,&\text{if }x_{i}>u,\\ \end{cases}$ $$

∎

###### Lemma B.6 (Interval) .

Given any constant $u,v$ and $\epsilon>0$, there exists a parameter configuration of a 1-layer feedforward neural network with ReGLU activation and width $4$ that for any input $x\in\mathbb{R}^{d}$, computes the indicator function $x_{i}\in[u,v]$ on $x_{i}\in[-\infty,u-\epsilon]\cup\cup[u,v]\cup[v,\infty]$.

###### Proof.

The interval function here can be written as the difference of two threshold functions. We can use the same construction as in Lemma B.5 to approximate the indicator function $x_{i}>u$ and $x_{i}>v+\epsilon$ and then take the difference.
∎

###### Lemma B.1 (Multiplication) .

###### Proof.

###### Lemma B.2 (Linear Operations) .

###### Proof.

###### Lemma B.3 (Indicator) .

###### Proof.

###### Lemma B.4 (Lookup Table) .

###### Proof.

###### Lemma B.5 (Threshold) .

###### Proof.

###### Lemma B.6 (Interval) .

###### Proof.

### B.2 Building Blocks of Transformers Construction

We will show in this section some construction for basic functionality using Transformer Blocks. This construction will be used in the following sections to prove the main results.

We will always use $X\in\mathbb{R}_{p}^{d\times l}$ as the input to the Transformer Block, where $d$ is the dimension of the input, and $l$ is the length of the sequence.
We will first outline all the building functionality and then show how to implement them.

###### Definition B.7 (Copying Function) .

For integer $s$, index set $I_{1},I_{2}\subset[d-20]$ satisfying $|I_{1}|=|I_{2}|$, a copying function $\mathrm{COPY}[s,I_{1},I_{2}]$ satisfies the following, $\forall X\in\mathbb{R}_{p}^{d\times l}$, then

$$ $\displaystyle\mathrm{COPY}[s,I_{1},I_{2}](X)_{I_{2},k}$ $\displaystyle=x_{I_{1},\max\{k-s,0\}}\quad\forall k\leq[m]$ $\displaystyle\mathrm{COPY}[s,I_{1},I_{2}](X)_{I_{2}^{c},k}$ $\displaystyle=0\quad\forall r\in[m]$ $$

###### Definition B.8 (Counting Function) .

For index set $I_{1},I_{2}\subset[d-20],|I_{1}|=|I_{2}|\leq 10$ and index $i$, a counting function $\mathrm{COUNT}[I_{1},I_{2},i]$ satisfies the following, if $\forall v\in I_{1}\cup I_{2},k\in[l],X_{v,k}\in\mathbb{Z}_{p}$ and $X_{v,k}\neq 0$, then

$$ $\displaystyle\mathrm{COUNT}[I_{1},I_{2},i](X)_{i,k}$ $\displaystyle=\frac{1}{\sum_{h=1}^{k}\mathbf{1}{\left[X_{I_{1},h}=X_{I_{2},k} \right]}+1}\quad\forall k\in[l].$ $\displaystyle\mathrm{COUNT}[I_{1},I_{2},i](X)_{i^{c},k}$ $\displaystyle=0\quad\forall k\in[l].$ $$

###### Definition B.9 (Matching Function) .

For index set $I_{1},I_{2},I_{3},I_{4}\subset[d-20],|I_{1}|=|I_{2}|\leq 10,|I_{3}|=|I_{4}|$, a matching function $\mathrm{Match}[I_{1},I_{2},I_{3},I_{4}]$ satisfies the following, if $\forall v\in I_{1}\cup I_{2},k\in[l],X_{v,k}\in\mathbb{Z}_{p}$, then

$$ $\displaystyle\mathrm{Match}[I_{1},I_{2},I_{3},I_{4}](x)_{I_{3},k}$ $\displaystyle=X_{I_{4},k^{*}}\quad\forall k\in[l]$ $\displaystyle\text{where }k^{*}$ $\displaystyle=\begin{cases}\min\{h\mid X_{I_{1},h}=X_{I_{2},k}\},\{h\mid X_{I_ {1},h}=X_{I_{2},k}\}\neq\emptyset\\ 1\text{, otherwise}\end{cases}.$ $$

###### Definition B.10 (Matching Closest Function) .

For index set $I_{1},I_{2},I_{3},I_{4}\subset[d-20],|I_{1}|=|I_{2}|\leq 10,|I_{3}|=|I_{4}|$, a matching closest function $\mathrm{Match}[I_{1},I_{2},I_{3},I_{4}]$ satisfies the following, if $\forall v\in I_{1}\cup I_{2},k\in[l],X_{v,k}\in\mathbb{Z}_{p}$, then

$$ $\displaystyle\mathrm{MatchClose}[I_{1},I_{2},I_{3},I_{4}](x)_{I_{3},k}$ $\displaystyle=X_{I_{4},k^{*}}\quad\forall k\in[l]$ $\displaystyle\text{where }k^{*}$ $\displaystyle=\begin{cases}\max\{h\mid X_{I_{1},h}=X_{I_{2},k}\},\{h\mid X_{I_ {1},h}=X_{I_{2},k}\}\neq\emptyset\\ 1\text{, otherwise}\end{cases}.$ $$

###### Definition B.11 (Matching Nearest Function) .

For index set $I_{1},I_{2},I_{3},I_{4}\subset[d-20],|I_{1}|=|I_{2}|\leq 10,|I_{3}|=|I_{4}|$ and index $i$, a matching nearest function $\mathrm{MatchNearest}[I_{1},I_{2},I_{3},I_{4},i]$ satisfies the following, if $\forall v\in I_{1}\cup I_{2},k\in[l],X_{v,k}\in\mathbb{Z}_{p}$, then

$$ $\displaystyle\mathrm{MatchNearest}[I_{1},I_{2},I_{3},I_{4}](x)_{I_{3},k}$ $\displaystyle=X_{I_{4},k^{*}}\quad\forall k\in[l]$ $\displaystyle\text{where }k^{*}$ $\displaystyle=\begin{cases}\arg\min_{h\in\{h\mid X_{I_{1},h}=X_{I_{2},k}\}}|h- X_{i,k}|,\{h\mid X_{I_{1},h}=X_{I_{2},k}\}\neq\emptyset\\ 1\text{, otherwise}\end{cases}.$ $$

###### Definition B.12 (Matching Next Function) .

Given any interger constant $A$, assuming $p>10A\log n$, for index set $I_{1},I_{2},I_{3},I_{4}\subset[d-20],|I_{1}|=|I_{2}|\leq 10,|I_{3}|=|I_{4}|$, and a special counting index $a$, a matching next function $\mathrm{MatchNext}[I_{1},I_{2},I_{3},I_{4},a]$ satisfies the following, if $X$ satisfies the following condition:

- 1.
$\forall v\in I_{1}\cup I_{2},k\in[l],X_{v,k}\in\mathbb{Z}_{p}$,
- 2.
$X_{a,k}\in\mathrm{ROUND}(1/[n^{A}],p)\cup\{0\}$,
- 3.
For any $k\in[l]$, given any $k\geq k$, the counting index multiset $S_{k}=\{X_{a,k^{\prime}}\mid X_{I_{1},k^{\prime}}=X_{I_{2},k}\}$ takes consecutive and disjoint values in $\mathrm{ROUND}(1/[n^{A}],p)$, that is, there exists $u_{k},v_{k}\in\mathrm{ROUND}(1/[n^{A}],p)$ such that $S_{k}=[u_{k},v_{k}]\cap\mathrm{ROUND}(1/[n^{A}],p)$.

then, we have

$$ $\displaystyle\mathrm{MatchNext}[I_{1},I_{2},I_{3},I_{4},a](X)_{I_{3},k}=X_{I_{ 4},k^{*}}\quad\forall k\in[l]$ $\displaystyle\text{where }k^{*}=\arg\min_{h\in\{h\mid X_{I_{1},h}=X_{I_{2},k} \}\cup\{1\}}|X_{a,h}-\mathrm{next}(X_{a,k})|.$ $$

Now we will show how to implement these functions using Transformer Blocks. The construction here is motivated by the construction in Feng et al. () with some modifications.

###### Lemma B.13 (Copying Blocks) .

For integer $s$, index set $I_{1},I_{2}\subset[d-10]$ satisfying $|I_{1}|=|I_{2}|$, a copying function $\mathrm{COPY}[s,I_{1},I_{2}]$ can be implemented with $1$ feedforward block $g$ and $1$ attention block $\mathcal{A}$ with $1$ attention head. Formally, when $X_{d,k}=k$, it holds that

$$ $\displaystyle\mathcal{A}\left(g\left(X\right)+X\right)=\mathrm{COPY}[s,I_{1},I _{2}](X).$ $$

###### Proof of Lemma B.13 .

We will use the feedforward block to calculate $X_{k,d}^{2}$ and $1$ (Lemma B.1) and have

$$ $\displaystyle\left(g(X)+X\right)_{d-1,k}$ $\displaystyle=k^{2}$ $\displaystyle\left(g(X)+X\right)_{d-2,k}$ $\displaystyle=1.$ $\displaystyle\forall i\not\in\{d-1,d-2\},\left(g(X)+X\right)_{i,k}$ $\displaystyle=X_{i,k}.$ $$

We will use $X^{\prime}$ to denote $g(X)+X$.
Then we will choose $W^{(K)},W^{(Q)}$ such that

$$ $\displaystyle W^{(K)}X_{:,k^{\prime}}^{\prime}=n\begin{bmatrix}1\\ k^{\prime}\\ k^{\prime 2}\end{bmatrix}$ $\displaystyle W^{(Q)}X^{\prime}_{:,k}=\begin{bmatrix}-(k^{2}+s^{2}-2sk)\\ 2k-2s\\ -1\end{bmatrix}$ $$

Hence

$$ $\displaystyle\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k ^{\prime},k}$ $\displaystyle=$ $\displaystyle-n\left(k^{\prime 2}-k^{\prime}(2k-2s)+k^{2}+s^{2}-2sk\right)$ $\displaystyle=$ $\displaystyle-n(k-s-k^{\prime})^{2}$ $$

Hence we have

$$ $\displaystyle\arg\max_{k^{\prime}<k}\left(\left(W^{(K)}X\right)^{\top}\left(W^ {(Q)}X\right)\right)_{k^{\prime},k}$ $\displaystyle=\max\{k-s,0\}.$ $$

Also, for any $k^{\prime}\leq k,k^{\prime}\neq\max\{k-s,0\}$, we have

$$ $\displaystyle\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k ^{\prime},k}-\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{ \max\{k-s,0\},k}$ $\displaystyle<-n.$ $$

Hence after the column-wise softmax and rounding to $p=O(\log n)$ bit, we have

$$ $\displaystyle\left(\mathrm{softmax}\left(\left(W^{(K)}X\right)^{\top}\left(W^{ (Q)}X\right)\right)\right)_{k^{\prime},k}$ $\displaystyle=\mathbf{1}{\left[k^{\prime}=\max\{k-s,0\}\right]}.$ $$

We will then choose $W^{(V)}$ such that

$$ $\displaystyle W^{(V)}X_{I_{2},k^{\prime}}^{\prime}=X^{\prime}_{I_{1},k^{\prime }}=X_{I_{1},k^{\prime}}\quad\forall k^{\prime}\in[l].$ $\displaystyle W^{(V)}X_{I_{2}^{c},k^{\prime}}^{\prime}=0\quad\forall k^{\prime }\in[l].$ $$

This then concludes that

$$ $\displaystyle\mathcal{A}\left(g\left(X\right)+X\right)=\mathrm{COPY}[s,I_{1},I _{2}](X).$ $$

The proof is complete.
∎

###### Lemma B.14 (Counting Blocks) .

For index set $I\subset[d-20]$ satisfying $|I_{1}|=|I_{2}|\leq 10$, a counting function $\mathrm{COUNT}[i,I_{1},I_{2}]$ can be approximated with $1$ feedforward block $g$ and $1$ attention block $\mathcal{A}$ with $1$ attention head. Formally, when $X_{d,k}=k$ and $X_{3,k}=\mathbf{1}{\left[k=1\right]},X_{I_{1},1}=0$, it holds that

$$ $\displaystyle\mathcal{A}\left(g\left(X\right)+X\right)_{i,k}$ $\displaystyle=\mathrm{ROUND}\left(\mathrm{COUNT}[s,I_{1},I_{2}](X)_{i,k},p \right).$ $\displaystyle\mathcal{A}\left(g\left(X\right)+X\right)_{i^{c},k}$ $\displaystyle=0.$ $$

###### Proof of Lemma B.14 .

We will use the feedforward block to calculate $X_{v,k}^{2},v\in I_{1}\cup I_{2}$ (Lemma B.1) and have

$$ $\displaystyle\left(g(X)+X\right)_{d-i,k}$ $\displaystyle=X_{I_{1}[i],k}^{2},i\in[|I|].$ $\displaystyle\left(g(X)+X\right)_{d-|I|-i,k}$ $\displaystyle=X_{I_{2}[i],k}^{2},i\in[|I|].$ $\displaystyle\left(g(X)+X\right)_{d-2|I|-1,k}$ $\displaystyle=1.$ $\displaystyle\forall i\not\in\{d-i\mid i\in[2|I|+1]\},\left(g(X)+X\right)_{i,k}$ $\displaystyle=X_{i,k}.$ $$

We will use $X^{\prime}$ to denote $g(X)+X$.
Then we will choose $W^{(K)},W^{(Q)}$ such that

$$ $\displaystyle W^{(K)}X_{:,k^{\prime}}^{\prime}$ $\displaystyle=n\begin{bmatrix}1+\mathbf{1}{\left[k^{\prime}=1\right]}\\ X_{I_{1}[i],k^{\prime}}\\ X_{I_{1}[i],k^{\prime}}^{2}\end{bmatrix}_{i\in[I]}$ $\displaystyle W^{(Q)}X^{\prime}_{:,k}$ $\displaystyle=\begin{bmatrix}X_{I_{2}[i],k}^{2}\\ -X_{I_{2}[i],k}\\ 1\end{bmatrix}_{i\in[I]}$ $$

Hence,

$$ $\displaystyle\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k ^{\prime},k}$ $\displaystyle=$ $\displaystyle-n\sum_{i=1}^{|I|}\left(X_{I_{2}[i],k^{\prime}}^{\prime 2}-X_{I_{ 1}[i],k^{\prime}}(2X_{I_{2}[i],k})+X_{I_{2}[i],k}^{2}\right)+n\mathbf{1}{\left [k^{\prime}=1\right]}\sum_{i=1}^{|I|}X_{I[i],k}^{2}.$ $\displaystyle=$ $\displaystyle-n\sum_{i=1}^{|I|}(X_{I_{1}[i],k^{\prime}}-X_{I_{2}[i],k})^{2}+n \mathbf{1}{\left[k^{\prime}=1\right]}\sum_{i=1}^{|I|}X_{I_{2}[i],k}^{2}.$ $$

Hence we have

$$ $\displaystyle\max_{k^{\prime}<k}\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q) }X\right)\right)_{k^{\prime},k}$ $\displaystyle=0.$ $$

Equality holds when $k^{\prime}=1$ or $X_{I_{1}[i],k^{\prime}}=X_{I_{2}[i],k}$ for all $i\in[|I_{1}|]$.

Also, for any $k^{\prime}\leq k,k^{\prime}\neq 1$ or $X_{I_{1}[i],k^{\prime}}\neq X_{I_{2}[i],k}$ for some $i\in[|I_{1}|]$, we have

$$ $\displaystyle\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k ^{\prime},k}$ $\displaystyle<-n.$ $$

Hence after the column-wise softmax and rounding to $p=O(\log n)$ bit, we have

$$ $\displaystyle\left(\mathrm{softmax}\left(\left(W^{(K)}X\right)^{\top}\left(W^{ (Q)}X\right)\right)\right)_{k^{\prime},k}$ $\displaystyle=\mathrm{ROUND}\left(\frac{1}{\sum_{h=1}^{k}\mathbf{1}{\left[X_{I _{1},h}=X_{I_{2},k}\right]}+1},p\right)$ $$

Here the $O\left(\frac{1}{n^{A}}\right)$ term comes from the fact that the softmax is rounded to $p=O(\log n)$ bit.

We will then choose $W^{(V)}$ such that

$$ $\displaystyle W^{(V)}X_{i,k^{\prime}}^{\prime}=X^{\prime}_{3,k^{\prime}}= \mathbf{1}{\left[k^{\prime}=1\right]}\quad\forall k^{\prime}\in[l].$ $\displaystyle W^{(V)}X_{I^{c},k^{\prime}}^{\prime}=0\quad\forall k^{\prime}\in [l].$ $$

This then concludes that

$$ $\displaystyle\mathcal{A}\left(g\left(X\right)+X\right)_{i,k}$ $\displaystyle=\mathrm{ROUND}\left(\mathrm{COUNT}[s,I_{1},I_{2}](X)_{i,k},p \right).$ $\displaystyle\mathcal{A}\left(g\left(X\right)+X\right)_{i^{c},k}$ $\displaystyle=0.$ $$

∎

###### Lemma B.15 (Matching Blocks) .

Given any constant $c$, for index set $I_{1},I_{2},I_{3},I_{4}\subset[d-20],|I_{1}|=|I_{2}|\leq 10,|I_{3}|=|I_{4}|$, a matching function $\mathrm{Match}[I_{1},I_{2},I_{3},I_{4}]$ can be implemented with $1$ feedforward block $g$ and $1$ attention block $\mathcal{A}$ with $1$ attention head. Formally, when $X_{d,k}=k,X_{3,k}=\mathbf{1}{\left[k=1\right]},X_{I_{1},1}=0$ and $k\leq n^{c}$, it holds that

$$ $\displaystyle\mathcal{A}\left(g\left(X\right)+X\right)$ $\displaystyle=\mathrm{Match}[I_{1},I_{2},I_{3},I_{4}](X)$ $$

###### Proof.

We will use the feedforward block to calculate $k^{2},X_{v,d}^{2},v\in\cup I_{1}\cup I_{2}$ as in the proof of Lemmas B.14 and B.13.

We then choose $W^{(K)},W^{(Q)}$ such that

$$ $\displaystyle\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k ^{\prime},k}$ $\displaystyle=$ $\displaystyle-n^{4c+1}\sum_{i=1}^{|I|}(X_{I_{1}[i],k^{\prime}}-X_{I_{2}[i],k}) ^{2}-nk^{\prime 2}$ $\displaystyle+\mathbf{1}{\left[k^{\prime}=1\right]}\left(n^{4c+1}\sum_{i=1}^{| I|}X_{I_{2}[i],k}^{2}+n-n^{2c+2}\right).$ $$

The detailed construction of $W^{(K)},W^{(Q)}$ is omitted here since it is similar to the proof of Lemmas B.14 and B.13.

We will discuss several cases for the distribution of $\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k^{\prime},k}$. It always holds that $\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{1,k}=-n^{2c+2}$.

- 1.
If there doesn’t exists $k^{\prime}$, such that $X_{k^{\prime},I_{1}}=X_{k,I_{2}}$, then for any $i>1$, we have $\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{i,k}<-n^{4c+1}$.
- 2.
If there exists $k^{\prime}$, such that $X_{k^{\prime},I_{1}}=X_{k,I_{2}}$, then for such $k^{\prime}$, we have $\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k^{\prime},k}=
-nk^{\prime 2}>-n^{2c+1}$. The rest of the entries are all smaller than $-n^{4c+1}$. Finally, the largest $k^{\prime}$ satisfying that $X_{k^{\prime},I_{1}}=X_{k,I_{2}}$ will corresponds to a $\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k^{\prime},k}$ that is at least $n$ larger than the second largest $\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k^{\prime},k}$, as in the proof of Lemma B.13.

Concluding the above discussion, we have after the column-wise softmax and rounding to $p=O(\log n)$ bit,

$$ $\displaystyle\left(\mathrm{softmax}\left(\left(W^{(K)}X\right)^{\top}\left(W^{ (Q)}X\right)\right)\right)_{k^{\prime},k}$ $\displaystyle=\begin{cases}\mathbf{1}{\left[k^{\prime}=\min\{h\mid X_{I_{1},h} =X_{I_{2},k}\}\right]},\{h\mid X_{I_{1},h}=X_{I_{2},k}\}\neq\emptyset\\ \mathbf{1}{\left[k^{\prime}=1\right]}\text{, otherwise}\end{cases}$ $$

Further, we will choose $W^{(V)}$ such that

$$ $\displaystyle W^{(V)}X_{I_{3},k^{\prime}}^{\prime}=X^{\prime}_{I_{4},k^{\prime }}=X_{I_{4},k^{\prime}}\quad\forall k^{\prime}\in[l].$ $\displaystyle W^{(V)}X_{I_{3}^{c},k^{\prime}}^{\prime}=0\quad\forall k^{\prime }\in[l].$ $$

This then concludes that

$$ $\displaystyle\mathcal{A}\left(g\left(X\right)+X\right)$ $\displaystyle=\mathrm{Match}[I_{1},I_{2},I_{3},I_{4}](X)$ $$

This concludes the proof.
∎

###### Lemma B.16 (Matching Closest Blocks) .

Given any constant $c$, for index set $I_{1},I_{2},I_{3},I_{4}\subset[d-20],|I_{1}|=|I_{2}|\leq 10,|I_{3}|=|I_{4}|$, a matching closest function $\mathrm{MatchClose}[I_{1},I_{2},I_{3},I_{4}]$ can be implemented with $1$ feedforward block $g$ and $1$ attention block $\mathcal{A}$ with $1$ attention head. Formally, when $X_{d,k}=k,X_{3,k}=\mathbf{1}{\left[k=1\right]},X_{I_{1},1}=0$ and $k\leq n^{c}$, it holds that

$$ $\displaystyle\mathcal{A}\left(g\left(X\right)+X\right)$ $\displaystyle=\mathrm{MatchClose}[I_{1},I_{2},I_{3},I_{4}](X)$ $$

###### Proof.

The proof is similar to the proof of Lemma B.15, and one can design the attention pattern as

$$ $\displaystyle\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k ^{\prime},k}$ $\displaystyle=$ $\displaystyle-n^{4c+1}\sum_{i=1}^{|I|}(X_{I_{1}[i],k^{\prime}}-X_{I_{2}[i],k}) ^{2}-n(k-k^{\prime})^{2}$ $\displaystyle+\mathbf{1}{\left[k^{\prime}=1\right]}\left(n^{4c+1}\sum_{i=1}^{| I|}X_{I_{2}[i],k}^{2}+n(k-1)^{2}-n^{2c+2}\right).$ $$

The rest of the proof is omitted here.
∎

###### Lemma B.17 (Matching Nearest Blocks) .

Given any constant $c$, for index set $I_{1},I_{2},I_{3},I_{4}\subset[d-20],|I_{1}|=|I_{2}|\leq 10,|I_{3}|=|I_{4}|$ and index $i$ , a matching nearest function $\mathrm{MatchNearest}[I_{1},I_{2},I_{3},I_{4},i]$ can be implemented with $1$ feedforward block $g$ and $1$ attention block $\mathcal{A}$ with $1$ attention head. Formally, when $X_{d,k}=k,X_{3,k}=\mathbf{1}{\left[k=1\right]},X_{I_{1},1}=0$ and $k\leq n^{c}$, it holds that

$$ $\displaystyle\mathcal{A}\left(g\left(X\right)+X\right)$ $\displaystyle=\mathrm{MatchNearest}[I_{1},I_{2},I_{3},I_{4},i](X)$ $$

###### Proof.

The proof is similar to the proof of Lemma B.15, and one can design the attention pattern as

$$ $\displaystyle\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k ^{\prime},k}$ $\displaystyle=$ $\displaystyle-n^{4c+1}\sum_{u=1}^{|I|}(X_{I_{1}[u],k^{\prime}}-X_{I_{2}[u],k}) ^{2}-n(X_{i,k}-k^{\prime})^{2}$ $\displaystyle+\mathbf{1}{\left[k^{\prime}=1\right]}\left(n^{4c+1}\sum_{u=1}^{| I|}X_{I_{2}[u],k}^{2}+n(1-X_{i,k})^{2}-n^{2c+2}\right).$ $$

The rest of the proof is omitted here.
∎

###### Lemma B.18 (Matching Next Blocks) .

Given any constant $A,c$, for index set $I_{1},I_{2},I_{3},I_{4}\subset[d-20],|I_{1}|=|I_{2}|\leq 10,|I_{3}|=|I_{4}|$ and a special counting index $a$, a matching next function $\mathrm{MatchNext}[I_{1},I_{2},I_{3},I_{4},a]$ can implement with $1$ feedforward block $g$ and $1$ attention block $\mathcal{A}$ with $1$ attention head. Formally, when $X_{d,k}=k,X_{3,k}=\mathbf{1}{\left[k=1\right]},X_{I_{1},1}=0$ and $k\leq n^{c}$, it holds that

$$ $\displaystyle\mathcal{A}\left(g\left(X\right)+X\right)$ $\displaystyle=\mathrm{MatchNext}[I_{1},I_{2},I_{3},I_{4},a](X)$ $$

###### Proof.

We will use the feedforward block to calculate the following $\overline{\mathrm{next}}$ function, where

$$ $\displaystyle\overline{\mathrm{next}}(x)=\begin{cases}\frac{1}{2},&x\geq\frac{ 2}{3}.\\ \frac{1}{3},&\frac{3}{5}>x>\frac{2}{5}.\\ \frac{1}{4},&\frac{7}{20}>x>\frac{3}{10}\\ x-x^{2}+x^{3},&x\leq\frac{11}{40}.\end{cases}$ $$

The value can be arbitrary for $x\in[\frac{11}{40},\frac{3}{10}]\cup[\frac{2}{5},\frac{7}{20}]\cup[\frac{3}{5}
,\frac{2}{3}]$.
This function is achievable by a feedforward block through combination of Lemmas B.1 and B.6.

The purpose of this is to approximate the $\mathrm{next}$ function for $x\in\mathrm{ROUND}(1/[n^{A}],p)$, and we have the following lemma.

###### Lemma B.19 .

For large enough $n$ and any $x\in\mathrm{ROUND}(1/[n^{A}],p)$, we have

$$ $\displaystyle|\overline{\mathrm{next}}(x)-\mathrm{next}(x)|$ $\displaystyle\leq\mathrm{next}(x)^{3}+O\left(\frac{1}{n^{10A}}\right).$ $$

###### Proof.

We always have $\mathrm{ROUND}(1/[n^{A}],p)\cap\left([\frac{11}{40},\frac{3}{10}]\cup[\frac{2}
{5},\frac{7}{20}]\cup[\frac{3}{5},\frac{2}{3}]\right)=\emptyset$.
We will discuss several cases for $x\in\mathrm{ROUND}(1/[n^{A}],p)$.

- 1.
If $x\geq\frac{3}{10}$, then $\overline{\mathrm{next}}(x)=\mathrm{next}(x)$.
- 2.
If $x\leq\frac{7}{20}$, it holds that $|x-1/m|\leq 1/n^{10A},m\geq 3$, then
$\displaystyle\overline{\mathrm{next}}(x)$
$\displaystyle=x-x^{2}=\frac{1}{m}-\frac{1}{m^{2}}+\frac{1}{m^{3}}+O\left(\frac
{1}{n^{10A}}\right)$
$\displaystyle=\frac{1}{m+1}-\frac{1}{m^{3}(m+1)}+O\left(\frac{1}{n^{10A}}\right)$

This then concludes the proof.
∎

We then choose $W^{(K)},W^{(Q)}$ such that

$$ $\displaystyle\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k ^{\prime},k}$ $\displaystyle=$ $\displaystyle-n^{4A+3}\sum_{i=1}^{|I|}(X_{I_{1}[i],k^{\prime}}-X_{I_{2}[i],k}) ^{2}-n^{4A+1}(\overline{\mathrm{next}}(X_{a,k})-X_{a,k^{\prime}})^{2}$ $\displaystyle+\mathbf{1}{\left[k^{\prime}=1\right]}\left(n^{4A+3}\sum_{i=1}^{| I|}X_{I_{2}[i],k}^{2}+n^{4A+1}X_{a,k}^{2}-n^{4A+2}\right).$ $$

Again, the detailed construction of $W^{(K)},W^{(Q)}$ is omitted here since it is similar to the proof of Lemmas B.14 and B.13.

We will discuss several cases for the distribution of $\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k^{\prime},k}$. It always holds that $\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{1,k}=-n^{4A+2}$.

- 1.
If there doesn’t exists $k^{\prime}$, such that $X_{k^{\prime},I_{1}}=X_{k,I_{2}}$, then for any $i>1$, we have $\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{i,k}<-n^{4A+3}$.
- 2.
If there exists $k^{\prime}$, such that $X_{k^{\prime},I_{1}}=X_{k,I_{2}}$, then for such $k^{\prime}$, we have $\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k^{\prime},k}=
-n^{3A}(\mathrm{next}(X_{a,k})-X_{a,k^{\prime}})^{2}>-n^{4A+1}$. The rest of the entries are all smaller than $-n^{4A+2}$.
It remains to discuss the distribution of $\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k^{\prime},k}$ for $k^{\prime}$ satisfying $X_{k^{\prime},I_{1}}=X_{k,I_{2}}$. When $X$ satisfies the condition in Definition B.12, we have that $S_{k}=\{X_{a,k^{\prime}}\mid X_{k^{\prime},I_{1}}=X_{k,I_{2}}\}$ takes consecutive and disjoint values in $\mathrm{ROUND}(1/[n^{A}],p)$. Hence, if $|S_{k}|>2$, suppose $y,z\in S_{k}$ satisfies that
$\displaystyle|y-\overline{\mathrm{next}}(X_{a,k})|$
$\displaystyle=\min_{x\in S_{k}}|x-\overline{\mathrm{next}}(X_{a,k})|$
$\displaystyle|z-\overline{\mathrm{next}}(X_{a,k})|$
$\displaystyle=\min_{x\in S_{k},x\neq y}|x-\overline{\mathrm{next}}(X_{a,k})|.$
We will discuss several cases for $y,z$.
•
If $y-\overline{\mathrm{next}}(X_{a,k})$ and $z-\overline{\mathrm{next}}(X_{a,k})$ are both negative, then $y>z$, we have,
$\displaystyle\left(y-\overline{\mathrm{next}}(X_{a,k})\right)^{2}-\left(z-
\overline{\mathrm{next}}(X_{a,k})\right)^{2}$
$\displaystyle=(y-z)(y+z-2\overline{\mathrm{next}}(X_{a,k}))$
$\displaystyle\leq-(y-z)^{2}\leq-\frac{1}{4n^{4A}}.$
.
•
If $y-\overline{\mathrm{next}}(X_{a,k})$ and $z-\overline{\mathrm{next}}(X_{a,k})$ are both positive, then $y<z$, and same as above we have
$\displaystyle\left(y-\overline{\mathrm{next}}(X_{a,k})\right)^{2}-\left(z-
\overline{\mathrm{next}}(X_{a,k})\right)^{2}$
$\displaystyle=(y-z)(y+z-2\overline{\mathrm{next}}(X_{a,k}))$
$\displaystyle\leq-(y-z)^{2}\leq-\frac{1}{4n^{4A}}.$
.
•
If $y-\overline{\mathrm{next}}(X_{a,k})$ and $z-\overline{\mathrm{next}}(X_{a,k})$ have different signs, then according to Lemma B.19, we have, $y=\mathrm{ROUND}(\mathrm{next}(X_{a,k}),p)$ because $S_{k}$ takes consecutive and disjoint values in $\mathrm{ROUND}(1/[n^{A}],p)$. This then implies that
$\displaystyle\left(y-\overline{\mathrm{next}}(X_{a,k})\right)^{2}-\left(z-
\overline{\mathrm{next}}(X_{a,k})\right)^{2}$
$\displaystyle\leq$
$\displaystyle O(\frac{1}{n^{10A}})+\frac{1}{\mathrm{next}^{6}(X_{a,k})}-\left(
\frac{1}{\mathrm{next}(X_{a,k})(\mathrm{next}(X_{a,k})+1)}\right)^{2}$
$\displaystyle\leq$
$\displaystyle-\frac{1}{4n^{4A}}.$
Concluding, we always have for any $k^{\prime\prime}\neq k^{*}=\arg\max_{k^{\prime},k}\left(\left(W^{(K)}X\right)^
{\top}\left(W^{(Q)}X\right)\right)_{k^{\prime},k}$
$\displaystyle\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k
^{\prime},k}-\left(\left(W^{(K)}X\right)^{\top}\left(W^{(Q)}X\right)\right)_{k
^{*},k}\leq-\frac{n}{4}.$

Concluding the above discussion, we have after the column-wise softmax and rounding to $p=O(\log n)$ bit,

$$ $\displaystyle\left(\mathrm{softmax}\left(\left(W^{(K)}X\right)^{\top}\left(W^{ (Q)}X\right)\right)\right)_{k^{\prime},k}$ $\displaystyle=\mathbf{1}{\left[k^{\prime}=\arg\min_{h\in\{h\mid X_{I_{1},h}=X_ {I_{2},k}\}\cup\{1\}}|X_{a,h}-\mathrm{next}(X_{a,k})|\right]}.$ $$

Further, we will choose $W^{(V)}$ such that

$$ $\displaystyle W^{(V)}X_{I_{3},k^{\prime}}^{\prime}=X^{\prime}_{I_{4},k^{\prime }}=X_{I_{4},k^{\prime}}\quad\forall k^{\prime}\in[l].$ $\displaystyle W^{(V)}X_{I_{3}^{c},k^{\prime}}^{\prime}=0\quad\forall k^{\prime }\in[l].$ $$

This then concludes the proof.
∎

###### Definition B.7 (Copying Function) .

###### Definition B.8 (Counting Function) .

###### Definition B.9 (Matching Function) .

###### Definition B.10 (Matching Closest Function) .

###### Definition B.11 (Matching Nearest Function) .

###### Definition B.12 (Matching Next Function) .

###### Lemma B.13 (Copying Blocks) .

###### Proof of Lemma B.13 .

###### Lemma B.14 (Counting Blocks) .

###### Proof of Lemma B.14 .

###### Lemma B.15 (Matching Blocks) .

###### Proof.

###### Lemma B.16 (Matching Closest Blocks) .

###### Proof.

###### Lemma B.17 (Matching Nearest Blocks) .

###### Proof.

###### Lemma B.18 (Matching Next Blocks) .

###### Proof.

###### Lemma B.19 .

###### Proof.

### B.3 Building Blocks of RNNs Construction

We will now describe the building blocks of Linear RNNs construction. We will introduce some basic operations that will be used to build more complex RNNs family.

###### Lemma B.20 (Recent Input Memorizing) .

Given any constant $k$ and $C$, there exists a parameter configuration of linear unit that maintains $C$ dimensions of last $k$ input vectors in the state.

###### Proof.

Suppose the input sequence is $x_{1:t}\in\mathbb{R}^{d}$, and the dimensions that the state should memorize are $d_{1},d_{2},\ldots,d_{C}$. We can construct the following linear unit:

$$ $\displaystyle h_{t}=\begin{bmatrix}x_{t-1,d_{1}}&\ldots&x_{t-1,d_{C}}&h_{t-1,1 }&\ldots&h_{t-1,C\times(k-1)}&h_{t-1,C\times k+1}&\ldots&h_{t-1,d}\end{bmatrix}.$ $$

∎

###### Lemma B.21 (Summation) .

Given any constant $k$ and $C$, there exists a parameter configuration of linear unit that maintains the sum of one dimension of the last $k$ input vectors in the state.

###### Proof.

Suppose WLOG the input sequence is $x_{1:t}\in\mathbb{R}^{d}$, and the dimension that the state should memorize is $1$. We can construct the following linear unit:

$$ $\displaystyle h_{t}=\begin{bmatrix}x_{t-1,1}+h_{t-1,1}&h_{t-1,2}\ldots&h_{t-1, d}\end{bmatrix}.$ $$

∎

###### Lemma B.22 (Special Position Memorizing) .

Given any constant $k$ and $C$, there exists a parameter configuration of linear unit and a FFN with $Ck$ width that maintains the $C$ dimensions of the input vector at position $1$ to $k$ in the state.

###### Proof.

This is a direct combination of Lemma B.21 and Lemmas B.4 and B.1. The FFN can assign all the input vectors with position greater than $k$ to 0 0, and permute the corresponding dimensions of first $k$ input vectors to the first $Ck$ dimensions of the state. The linear unit can then maintain the state.
∎

###### Lemma B.23 (Recite Fixed Sequence) .

Given any constant integer $k$ and $C$, there exists a FFN with width $kC$ that can output fixed sequence of scalars that takes values in $[C]$ on a fixed set of positions $l_{1},\ldots l_{k}$.

###### Proof.

This is a direct consequence of Lemma B.4.
∎

###### Lemma B.20 (Recent Input Memorizing) .

###### Proof.

###### Lemma B.21 (Summation) .

###### Proof.

###### Lemma B.22 (Special Position Memorizing) .

###### Proof.

###### Lemma B.23 (Recite Fixed Sequence) .

###### Proof.

### B.4 Proof of Definition 4.4

We will first restate the theorem for clarity.

*

###### Proof.

We will discuss by cases.

When $T$ is Index, we will first show why RNN cannot solve the Index question without $\Omega(n)$ memory. The key observation is that the recurrent form of RNNs allowed the algorithm to be run in a streaming fashion with $o(n)$ bit memory. Here streaming means that the algorithm gets to look at each bit of the memory sequentially and can only update a constant size of memory.

###### Lemma B.24 .

Consider the following two-party game, where Alice receives string $x\in\{0,1\}^{n}$ and Bob receives an integer $k$, and Bob wants to know the value of $x_{k}$. If only Alice is allowed to send a signal to Bob, then $\Omega(n)$ bit communication complexity is required.

###### Proof of Lemma B.24 .

Suppose there exists a communication protocol where $B$ only receives $o(n)$ bit and can perfectly decide $x_{k}$. Because Alice doesn’t know $k$, the protocol must send the same message to Bob for all $k$. Hence Bob can reconstruct the whole string $x$ with $n$ bit with $o(n)$ bit communication. This is a contradiction.
∎

Now if RNN can solve the Index problem with $o(n)$ bit memory, then it can also solve the Index problem with $o(n)$ bit communication complexity. This is because Alice can simply run the RNN on input $x$ and send the hidden state to Bob. Then Bob can run the RNN with the hidden state and $k$ to get the answer. This is a contradiction to Lemma B.24. Hence RNN cannot solve the Index problem with $o(n)$ bit memory.

On the other hand, we will show that Transformers can solve the Index problem with $O(\log n)$ bit parameters. This is because using 2 layers of Transformer, we will implement a Match Block (Lemma B.15) that can match the last query token with the position of the previous token and retrieve the type of the matched token to the query token.

When $T$ is AR, wthout loss of generality, we assume that $n$ is even. The proof is similar to the proof of the proof of theIndex problem. As there are $n$ different types of tokens, we can label them as $[n]$. Now for any boolean sequence $x\in\{0,1\}^{n/2}$, solving AR for the following input is equivalent to solving the Index problem for $x$:

$$ $\displaystyle\mathcal{S}_{\mathrm{in}}=\texttt{
},1,x_{1}+n/2,2,x_{2}+n/2, \ldots,n/2,x_{n/2}+n/2,k$ $$

This then implies that RNN cannot solve AR with $o(n)$ bit memory. Transformers, on the other hand, can still solve AR with $O(\log n)$ bit parameters, we will use one layer of copying function to copy each token’s previous token’s type to it. Then we can use the Match Block to match the last query token with the position of the previous token and retrieve the type of the matched token to the query token.

When $T$ is $c$-gram retrieval, without loss of generality, we assume that $n$ is a multiple of $c$. The proof is similar to the proof of Definition 4.4. As there are $n$ different types of tokens, we can label them as $[n]$. Now for any boolean sequence $x\in\{0,1\}^{n/2}$, solving AR for the following input is equivalent to solving the Index problem for $x$:

$$ $\displaystyle\mathcal{S}_{\mathrm{in}}=\texttt{
},\underbrace{1,\ldots,1}_{c -1},x_{1}+n/c,\underbrace{2,\ldots,2}_{c-1},x_{2}+n/c,\ldots,\underbrace{n/c, \ldots,n/c}_{c-1},x_{n/c}+n/c,\underbrace{k,\ldots,k}_{c-1}$ $$

This then implies that RNN cannot solve $c$-gram retrieval with $o(n)$ bit memory. Transformers, on the other hand, can still solve $c$-gram retrieval with $O(\log n)$ bit parameters, we will use one layer of copying function to copy each token’s previous $c-1$ tokens’ type to it. Then we can use the Match Block to match the last query token with the position of the previous token and retrieve the type of the matched token to the query token.

When $T$ is Counting, we will first show why RNN cannot solve the Counting question without $\Omega(n)$ memory. Consider the following setup, given any $x\in\{0,1\}^{n}$, the input string is $j_{1}j_{2}\ldots j_{k}$ where $\{j_{i}\ldots j_{k}\}=\{j\mid x_{j}=1\}$, then solving the Counting question for this input string for queried threshold $1$ is equivalent to solving the Index problem for $x$. This then implies that RNN cannot solve the Counting question with $o(n)$ bit memory.

On the other hand, we will show that Transformers can solve the Counting question with $O(\log n)$ bit parameters. This is because using 2 layers of Transformer, we can first use a $\mathrm{COPY}$ block to copy the last query token to the token corresponds to the threshold, and then use a $\mathrm{COUNT}$ block (Lemma B.14) that can count the number $m$ of the appearance of the last query token in the input sequence, and then write $1/(m+1)$ to one of the dimension. Finally, we can use the Feed Forward Network on the last layer to multiply threshold $+1$ with this value and compare the result to $1$ to get the answer.
∎

###### Proof.

###### Lemma B.24 .

###### Proof of Lemma B.24 .

### B.5 Proof of Section 4.1

We will restate the theorem for clarity.

*

###### Proof.

We will first prove a lemma,

###### Lemma B.25 .

If $\mathrm{PSPACE}\not\subset\mathrm{P/Poly}$, then there exists a Turing machine $M$ with linear space complexity that cannot be simulated by a polynomial-size circuit family.

###### Proof.

We will prove this by contradiction. Assuming for every Turing machine $M$ with linear space complexity, there exists a polynomial-size circuit family $\{C_{n}\}$ that can simulate $M$. We will construct a polynomial-size circuit family $\{C^{\prime}_{n}\}$ that can decide $\mathrm{PSPACE}$, which contradicts the assumption that $\mathrm{PSPACE}\not\subset\mathrm{P/Poly}$.

Given any language $L\in\mathrm{PSPACE}$, we can decide $L$ by a Turing machine $M_{L}$ with space $O(n^{k})$ for some constant $k$. We can consider another language $L^{\prime}=\{x1^{|x|^{k}}\mid x\in L\}$. We can decide $L^{\prime}$ by a Turing machine $M_{L^{\prime}}$ with linear space complexity by checking the length of the input and then simulating $M_{L}$. By the assumption, there exists a polynomial-size circuit family $\{C_{n}\}$ that can simulate $M_{L^{\prime}}$. We can then construct a polynomial-size circuit family $\{C^{\prime}_{n}\}$ that can decide $L$ by first counting the length of the input and then simulating $C_{n}$ on the extended input. This contradicts the assumption that $\mathrm{PSPACE}\not\subset\mathrm{P/Poly}$.
∎

By Lemma B.25, we know that if $\mathrm{PSPACE}\not\subset\mathrm{P/Poly}$, then there exists a Turing machine $M$ with linear space complexity that cannot be simulated by a polynomial-size circuit family. We will use this result to prove Section 4.1.

We will design the task as follows, for any $n$, let $m=\lfloor\log_{2}n\rfloor$, for any boolean input $x$ of length $m$, we will choose input sequence as $\mathcal{S}_{\mathrm{in}}=\overline{0^{n-m}x}$ and the label as $y=\mathrm{YES}$ if $M(x)=1$ and $y=\mathrm{NO}$ otherwise.

Because we are considering regular RNN with $O(m)$ memory, we know that we can compute the result of RNN without CoT through a circuit family with size $\text{Poly}(m)$. However, we know that $M$ cannot be simulated by a polynomial-size circuit family. Hence, no RNN family with $O(m)$ memory can solve the task for all $n$.

On the other hand, we can simulate $M$ by the RNN by maintaining the state, the pointer, and the tape of the $M$ inside the state of the RNN. The RNN can then maintain the transition function of the Turing machine in its output function as a lookup table Lemma B.4 and write down the updated state, the direction of the pointer movement, and the updated cell value at the pointer in its context. Paired with the ability to memorize the recent input Lemma B.20, the RNN can then simulate the running of the Turing machine.

Because the space complexity of $M$ is linear in $m$, the time complexity of $M$ is $\exp(O(m))$ which is polynomial in $n$. Hence, we can solve the task by an RNN with CoT and $O(m)$ memory and polynomial-size circuit family.
∎

###### Proof.

###### Lemma B.25 .

###### Proof.

### B.6 Proof of Section 4.2.2

We will now proceed to prove our main theorem, which states that Transformers with chain-of-thought can solve IsTree perfectly, while RNNs cannot. We will first restate the theorem here.

*

###### Proof of Section 4.2.2 .

We will prove this theorem by proving the following lemmas.

###### Lemma B.26 .

For any $n$ and RNN $R$ with $o(n)$ memory, $R$ can’t perfectly solve IsTree of size $n$.

###### Lemma B.27 .

There exists a Transformer $T$ with constant depth and width, and $O(\log n)$ precision, that can solve IsTree of size $n$ perfectly with Chain of Thought.

This proof is a direct combination of Lemmas B.26 and B.27.
∎

###### Proof of Section 4.2.2 .

###### Lemma B.26 .

###### Lemma B.27 .

#### B.6.1 Proof of Lemma B.26

###### Proof of Lemma B.26 .

We will first reduce another problem in communication complexity to IsTree.

###### Lemma B.28 .

Consider the following two-party game, where Alice receives string $x\in\{0,1\}^{n}$ and Bob receives an integer $k$, and Bob wants to know whether $x_{k}=x_{k-1}$. If only Alice is allowed to send information, $\Omega(n)$ bit communication complexity is required.

###### Proof of Lemma B.28 .

We can reduce this problem to the problem in Lemma B.24. Considering the game in Lemma B.24, given any $x\in\{0,1\}^{n}$, we can construct $\tilde{x}_{i}=\sum_{j=1}^{i}x_{i}\mod 2$. Then $\tilde{x}$ is a string of length $n$ with only 0 0 and $1$. Moreover, $x_{k}=x_{k-1}$ if and only if $\tilde{x}_{k}=\tilde{x}_{k-1}$. Hence, if Bob can solve the problem in Lemma B.28 with $o(n)$ bit, he can solve the problem in Lemma B.24. This is a contradiction.
∎

Now suppose that we have a streaming algorithm for IsTree with only $o(n)$ memory. We shall prove Alice and Bob in Lemma B.28 can use it to solve the original question with $o(n)$ memory.

Consider the following graph with $n+2$ nodes. There is a node $i$ corresponding to each $x_{i}$ for $i\in[n]$ and two special nodes $n+1,n+2$. Node $i$ will be connected to $n+1$ if $x_{i}=0$ and to $n+2$ if $x_{i}=1$. Moreover, $k-1$ and $k$ will be connected. Now the original answer Bob wants is False if and only if the graph is a Tree. Hence, given access to the streaming algorithm, Alice can run it on the edges that she knows exist and send the memory to Bob. Bob can then run it on the edges that he knows exist. Combining they will be able to solve the original problem. This is a contradiction.

Moreover, as RNN with chain-of-thought is also a streaming algorithm, it also requires $\Omega(n)$ memory.
∎

###### Proof of Lemma B.26 .

###### Lemma B.28 .

###### Proof of Lemma B.28 .

#### B.6.2 Proof of Lemma B.27

###### Proof of Lemma B.27 .

The proof is two-folded. We will first define an algorithm that can solve IsTree by generating a sequence of vertices of length $O(n)$, and then we will show that this sequence can be generated by a Transformer with constant depth and width, and $O(\log n)$ precision as a Chain of Thought.

Figure: Algorithm 1 Depth-First Search Algorithm

###### Proof of Lemma B.27 .

##### Algorithm for IsTree.

We define Algorithm 1 as a depth-first search algorithm that can generate a sequence of vertices of length $O(n)$ that can be used to solve IsTree. We will use two stacks $S_{1},S_{2}$ to store the vertices. $S_{1}$ will be used to store the vertices that are not yet visited, and $S_{2}$ will be used to store the vertices that are already visited. The algorithm will start with $S_{1}=[v_{1}]$ and $S_{2}=\emptyset$. At each step, the algorithm will pop the top of $S_{1}$ and push it to $S_{2}$. Then it will push all the neighbors of the popped vertex that are not in $S_{1}\cup S_{2}$ to $S_{1}$. The algorithm will terminate when $S_{1}$ is empty. We will denote the yielded vertice sequence for $G$ as $\mathcal{A}(G)$. The following lemma shows the connection between the result of the algorithm and the IsTree problem.

###### Lemma B.29 .

For any graph $G$, $\mathcal{A}(G)$ is a tree traversal of a spanning tree of the connected component of $G$ containing $v_{1}$. Hence $\mathrm{IsTree}(G)$ is True if and only if $G$ has $n-1$ edges and $\mathcal{A}(G)$ contains $2n-1$ vertices.

###### Proof of Lemma B.29 .

First, every vertex in the connected component of $G$ containing $v_{1}$ will be visited. This is because the algorithm will always push all the neighbors of the popped vertex that are not in $S_{1}\cup S_{2}$ to $S_{1}$. Hence, the algorithm will terminate when all the vertices in the connected component of $G$ containing $v_{1}$ are visited.

Second, every two consecutive vertices in the yielded sequence will be connected by an edge. This is because the algorithm will always push one of the neighbors of the popped vertex that is not in $S_{1}\cup S_{2}$ to $S_{1}$. Hence, every two consecutive vertices in the yielded sequence will be connected by an edge. On the other hand, the combination of these edges will form a tree because the algorithm will never push a vertex that is already in $S_{1}\cup S_{2}$ to $S_{1}$. Hence, the yielded sequence is a tree traversal of a spanning tree of the connected component of $G$ containing $v_{1}$.
∎

###### Lemma B.29 .

###### Proof of Lemma B.29 .

##### Construction of Transformer.

We will now show that the yielded sequence of Algorithm 1 can be generated by a Transformer with constant depth and width, and $O(\log n)$ precision as a Chain of Thought. The Transformer will generate a valid yielded sequence but can terminate early if the graph is not a tree. We will now describe the Transformer in detail. We will assume the input token sequence $\mathcal{S}$ is as follows,

$$ $\mathcal{S}=\mathrm{Tokenize}(G),v_{1},\ldots v_{r}$ (10) $$

for some $r\geq 0$ and $v_{1}\ldots v_{r}$ is a valid yielded sequence. The length of $\mathrm{Tokenize}(G)$ is $3n-2$ with $3$ tokens for each edges and $1$ special token 
. We will further denote the input to the first layer $X$ as $\textrm{Emb}(\mathcal{S})$. We will similarly denote the input to layer $\ell$ as $X^{(\ell)}$. We will also denote the output of the last layer as $X^{out}$.

- 1.
Layer 1 and Layer 2 Attention. The attention at Layer 1 will output zero and the FFN at Layer 1 and Attention at Layer 2 will implement a counting function (Definition B.8) to count the number of vertices $n$ appears in the previous token sequence and write $\mathrm{ROUND}\left(\frac{1}{n},p\right)$ in a new dimension $i_{1}$ as a result.
- 2.
Layer 2 FFN and Layer 3 Attention. The FFN at Layer 2 and Attention at Layer 3 will implement a copying function (Definition B.7) copying the first dimension and the counting dimension $i_{1}$ of each token to its successor at two new dimensions $i_{2}$ and $i_{3}$. For each edge, this moves the type of the first vertice and the number of times the first vertice appears to $\sim$. For every vertice in the chain of thought, this moves the type of the previous vertice to them.
- 3.
Layer 3 FFN and Layer 4 Attention. The FFN at Layer 3 and Attention at Layer 4 will implement another copying function, copying the dimensions $i_{2}$ and $i_{3}$ of each token to its successor at two new dimensions $i_{4}$ and $i_{5}$. Especially, for each edge, this moves the type of the first vertice and the number of times the first vertice appears to the position corresponding to the second vertices.
- 4.
Layer 4 FFN.
This FFN will process the information gathered from the previous layer and prepare for the next layer. It will make sure the following properties hold for $X^{(5)}$,
•
For every token, the position number, its square, and $1$ will be kept in the last three dimensions.
•
For the first vertices in each edges, $\sim$ and 
 The rest dimension will be zero.
•
For the second vertices of each edges $(a,b)$, there will be four dimensions $i_{6},i_{7},i_{8},i_{9}$ with value $a,b$ and $n_{a,e},n_{b,e}$, where $n_{a,e}=\mathrm{ROUND}(\frac{1}{1+\#a\text{appears up to current edge}},1)$.
•
For vertice $v_{l}$ in $v_{1},\ldots,v_{r}$, there will be four dimensions $i_{10},i_{11},i_{12},i_{13}$ with value $v_{l},v_{l-1}$ and $v_{l}^{2},v_{l-1}^{2}$ ($v_{0}=0$).
- 5.
Layer 5 Attention. Combining with the previous Layer 4 FFN layer, we will implement two match functions with two attention heads matching $(i_{10},i_{11})$ or $(i_{11},i_{10})$ with $(i_{6},i_{7})$ at Layer 5 Attention, i.e. finding the edge in input for each step in the chain of thought, we will then copy $n_{v_{l},(v_{l},v_{l-1})}$ to dimensions $i_{8}$ and $i_{9}$.
- 6.
Layer 6. We will use Layer 5 FFN and Layer 6 Attention to implement the match function that matches dimension $i_{10}$ of the current token to $i_{10}$ in the previous token. This will match $v_{l}$ to the first appearance of $v_{l}$ in the chain of thought and we will copy $i_{11}$ of the matched token to $i_{22}$. This dimension will be the first predecessor of $v_{l}$ in the chain of thought (0 for $v_{1}$). We will denote this predecessor of $v_{l}$ to be $f(v_{l})$ as it is the father of $v_{l}$ in the tree. Now we will need to split into two cases depending on whether $v_{l-1}$ is $f(v_{l})$. If $v_{l-1}=f(v_{l})$ or $v_{l-1}=0$ (for $v_{1}$), we will set dimension $i_{8}$ to be $1$ and $i_{9}$ to be 0 0. Otherwise, we will keep dimension $i_{8}$ and $i_{9}$ as $n_{v_{l},(v_{l},v_{l-1})}$.
- 7.
Layer 7. Now we will use Layer 6 FFN and Layer 7 Attention with two attention heads to implement two $\mathrm{MatchNext}$ functions(^3^33The constant $A$ here in Definition B.12is $1$) (Definition B.12) which use $i_{8}$ or $i_{9}$ as the counting index, and match $v_{l}$ at $i_{10}$ to $i_{6}$ or $i_{7}$ respectively. We will then copy dimensions $i_{6}$ to $i_{9}$ of the matched tokens to $i_{14}$ to $i_{21}$ (because there will be two of them).
The match next function will be able to retrieve the first edge containing $v_{1}$. For any $i\geq 2$, one of the matches next function will be able to retrieve the next edge containing $v_{i}$ after $(v_{i},v_{i+1})$ if it exists. If it doesn’t exist, the corresponding counting dimension will either be zero or no smaller than $n_{v_{l},(v_{l},v_{l-1})}$. We will use Layer 6 FFN to decide whether the next edge exists and set dimension $i_{14}$ of the output of Layer 6 to be the other edge in the next edge if it exists, or 0 0 otherwise, and $i_{15},i_{16}$ of the output of layer $6$ to be the counting dimension of the next edge if it exists, or 0 0 otherwise. For each edge in the original input, we will also set dimension $i_{15},i_{16}$ to be the counting dimension of the edge.
- 8.
Layer 8 Attention We will grab the next edge again, in the same manner as Layer 6, but this time using dimension $i_{15}$ and $i_{16}$. The necessity of this step is that the next edge containing $(v_{i-1},v_{i})$ in the original graph can be the same as the $(f(v_{l}),v_{i})$ and in such case we need to check whether the next edge after this edge.
- 9.
Layer 8 FFN. We now have, at each position corresponding to $v_{l}$, the first edge $(f(v_{l}),v_{l})$ in the yielded sequence containing $v_{l}$ and the other vertex in the edge containing $v_{l}$ that hasn’t been visited if it exists. If they don’t exist, the corresponding dimension will be zero. This allows us to use Layer 8s FFN to decide the next vertex in the yielded sequence, which is exactly the first vertex different with $f(v_{l})$ in the two edges if they exist, or $f(v_{l})$ otherwise. We will use Layer 8 FFN to calculate the potential next vertex and put it in dimension $i_{23}$ and its square in $i_{24}$.
- 10.
Layer 9 Attention. Combining with Layer 8 FFN, we will match $i_{23}$ of the current token to $i_{10}$ of the previous token to find the first appearance of the potential next vertex in the chain of thought. We will then copy dimension $d$ of the matched token to $i_{25}$. This value being $1$ will imply this vertex has never appeared in the chain of thought before and any other value will imply the vertex has appeared before.
- 11.
Layer 9 FFN. We can now check several cases,
•
If the potential next vertex $v$ is either $f(v_{r})\neq 0$ or never appears in the chain-of-thought sequence, then Layer 9 will output $n[-v,1,-1,\ldots-1]$, which will decodes to $v$.
•
If the potential next vertex $v$ is not $f(v_{r})$ and appears in the chain-of-thought sequence, then Layer 9 will output $nw_{6}$, which will decodes to $\mathrm{NO}$, because the chain of thought has already visited $v$ and hence the graph is not a tree.
•
If $v_{r}=1$ and the potential next vertex $v$ is $f(v_{r})=0$, this means the chain of thought has finished. In this case, layer 9 FFN will check whether the position is $3n-2+2n-1=5n-3$ and output $nw_{5}$ if it is, or output $nw_{6}$ otherwise, which will decode to $\mathrm{YES}$ and $\mathrm{NO}$ respectively.

This concludes the construction of the Transformer.
∎

### B.7 Proof of Section 4.2.3

We will now prove Section 4.2.3. We will first restate the theorem for convenience.

*

###### Proof.

The proof is motivated by the Theorem B.30 from Li et al. ().

###### Theorem B.30 (Theorem 3.3 of Li et al. ( 2024 ) ) .

For any $n$ and any circuit with size $T(n)$ and input size $n$, there exists a Transformer with constant depth and precision, $O(\log n)$ width, and a position embedding with size $O(T(n)\log n)$, such that for any input $\mathcal{S}$ of length $n$, the Transformer computes the output of the circuit on $\mathcal{S}$ using $T(n)$ steps.

However, direct utilization of Theorem B.30 is not feasible because we are interested in (1) $O(\log n)$ precision Transformer, and (2) simulating the RNN for $n^{A}$ step, which would correspond to a circuit with $n^{A}\mathrm{P}(n)$ in size. However, as the calculation is recurrent, we can encode the RNN circuit in $O(\mathrm{P}(n))$ parameter instead.

To do so, we will unroll the circuit of each recurrent step of the RNN into $\mathrm{P}(n)$ gates. We will then assign each gate a unique id in $[\mathrm{P}(n)]$ and assume the circuit is calculated in the order of the gate id in the following manner.

- 1.
Each gate has a type $t(i)$, which is either a constant gate outputting $1$, an input gate, a hidden state gate, an AND gate, or an XOR gate.
- 2.
Each gate $i$’s output depends on two values $l(i)$ and $r(i)$. If $t(i)$ is a constant gate, then $l(i)$ and $r(i)$ are assigned to be 0 0. When it is an input gate, $l(i)$ will be assigned to be the coordinate of the input embedding and $r(i)$ will be assigned to be the index of the bit of the value at $l(i)$ coordinate. When it is a hidden state gate, $l(i)$ will be assigned to be the coordinate of the hidden state embedding, and $r(i)$ will be assigned to be the index of the bit of the value at $l(i)$ coordinate. If it is an AND gate or an XOR gate, $l(i)$ and $r(i)$ will be assigned to be the id of the two gates that it depends on.

We will further assume without loss of generality that the hidden state gate is the first $p\Lambda$ gate. The output of the last $p\Lambda$ gate will be the next hidden state. We will also assume that the last $p(\Lambda+d)$ to $p\Lambda-1$ gates are the output gates.
We will now first describe the chain of thought that the Transformer will output and then construct the Transformer.

###### Proof.

###### Theorem B.30 (Theorem 3.3 of Li et al. ( 2024 ) ) .

##### Chain of thought

Taking any input $\mathcal{S}$ with length $n$, the Transformer will output a sequence of 0 0 and $1$ tokens. The first $n$ tokens will be the same as the input sequence. For each $a\geq 0$ and $b\in[\mathrm{P}(n)+1]$, the $n+a\left(\mathrm{P}(n)+1\right)+b$ token is

- 1.
the output of gate $b$ when RNN circuit is calculating the output at $a$ position plus $1$, if $b\leq\mathrm{P}(n)$.
- 2.
the $n+a+1$ token in the RNN chain of thought, if $b=\mathrm{P}(n)+1$.

##### Construction of the Transformer.

- 1.
Layer 1. The first attention layer will output zero and the first FFN layer will be of width $O(\mathrm{P}(n))$, encoding all the gate information. The output of the first layer at position $n+a\left(\mathrm{P}(n)+1\right)+b$ will have the following coordinate:
•
The input $i$ will be encoded in the first dimensions.
•
$a,a^{2},b,b^{2}$ will be encoded in four different dimensions.
•
The gate type $t(s(b))$ will be encoded in the next dimension, where $s(b)=(b+1)\mod(\mathrm{P}(n)+1)$ If $b=\mathrm{P}(n)-1$, then the gate type will be encoded as 0 0.
•
The necessary dependence $l(s(b)),l^{2}(s(b))$ and $r(s(b)),r^{2}(s(b))$ will be encoded in the next two dimensions.
•
A constant $1$ will be encoded in the next dimension.
- 2.
Layer 2 Attention. Together with the Layer 1 FFN, the Layer 2 Attention will implement two match functions (Definition B.9) to copy the output of gate $l(b+1)$ and $r(b+1)$ when RNN circuit is calculating the output at $a$ position. When the type of gate $b+1$ is not AND or XOR, the result will not be used in the following calculation.
- 3.
Layer 2 FFN Layer 2 FFN will be of width $O(1)$. The output of the layer will be
•
When $b<\mathrm{P}(n)$ and $t(s(b))$ is AND or XOR or constant, one dimension of the output will be the output of gate $b+1$ when RNN circuit is calculating the output at $a$ position.
•
When $b<\mathrm{P}(n)$ and $t(s(b))$ is an input or hidden state gate or $b=\mathrm{P}(n)+1$, one dimension of the output will be the position in the current chain of thought where the input bit or hidden state bit is copied from and the other dimension will be the square of that position
•
When $b=\mathrm{P}(n)$, the output remains the same as the input to Layer 2 FFN.
- 4.
Layer 3 Attention. Layer 3 Attention will be of width $O(1)$. Together with Layer 2 FFN, Layer 3 Attention will implement $\mathrm{Match}$ heads (Definition B.9) to copy the output at the position where the input bit or hidden state bit is copied from. When the type of gate $b+1$ is not input or hidden state gate, the result will not be used in the following calculation.
- 5.
Layer 3 FFN Layer 3 FFN will be of width $O(1)$. The output of the layer will be
•
When $b\neq\mathrm{P}(n)$, one dimension of the output will be output of gate $s(b)$ when RNN circuit is calculating the output at $a+\mathbf{1}{\left[b=\mathrm{P}(n)+1\right]}$ position.
•
When $b=\mathrm{P}(n)$, the output remains the same as the input to Layer 3 FFN.
- 6.
Layer 4 Layer 4 Attention will have $p-1$ heads and each head will be of width $O(1)$. Head $h\in[p-1]$ will copy the first dimension of the output of Layer 3 FFN at position $n+a(P(n)+1)+b-(p-h)$ and weight each of them by $2^{-h+(p-1)/2}$ and add them in one dimension. The Layer 4 FFN will calculate $r$ when the first dimension of the input is $1$ and $-r$ otherwise. Hence, for each $a\geq 0$, the $n+a\left(\mathrm{P}(n)+1\right)-hp,h\in[\Lambda:\Lambda+d]$ token contains a dimension $i_{1}$ which is the $k-\Lambda$ dimension of the output of the RNN at position $a$.
- 7.
Layer 5 Layer 5 Attention will have $d+1$ heads and each head will be of width $O(1)$. Head $h\in[d+1]$ will copy the dimension $i_{1}$ of the output of Layer 4 FFN at position $n+a(P(n)+1)+b-(h+\Lambda)p$ to a disjoint dimension $i_{h+1}$. The Layer 5 FFN will then make sure the output of Layer 5 satisfies the following:
•
When $b\neq\mathrm{P}(n)$, one dimension of the output will be $n$ times the output of gate $s(b)$ when RNN circuit is calculating the output at $a+\mathbf{1}{\left[b=\mathrm{P}(n)+1\right]}$ position plus $1$, which will decode to the corresponding value.
•
When $b=\mathrm{P}(n)$, the first $d$ dimension of the output will be the same as the output of the RNN at position $a$, and the rest dimension will be 0 0, which will decode to the same token as the chain of thought of the RNN at position $a+1$.

This concludes the construction of the Transformer.
∎

### B.8 Proof of Proposition 5.1

We will first restate the theorem for clarity.

*

###### Proof.

When task $T$ is Index, given an input sequence that ends with a query token $k$, RNN will generate the following search query sequence:$\texttt{<}\mathrm{StartSearch}\texttt{>}$, ^(?:\S\s*){$k-1$}(\S), $\texttt{<}\mathrm{EndSearch}\texttt{>}$.

Then the regular expression will match the $k$-th token in the input sequence. The RNN needs to recite the format of the query, remember the index $k$ and calculate $k-1$ to generate the regular expression. As we have shown in Lemmas B.23 and B.20, RNN can recite a fixed sequence at fixed position and memorize the recent input, the above sequence can be generated by an RNN. The explicit construction of the RNN is omitted here.

When task $T$ is AR, given an input sequence that ends with a query token $k$, RNN will generate the following search query sequence: $\texttt{<}\mathrm{StartSearch}\texttt{>}$, \b $k$ \b(\S+)\b, $\texttt{<}\mathrm{EndSearch}\texttt{>}$.

Then the regular expression will match the next token after the first occurrence of $k$ in the input sequence. The RNN needs to recite the format of the query and remember the query token $k$ to generate the regular expression. The explicit construction of the RNN is omitted here.

When task $T$ is c-gram retrieval, given an input sequence that ends with query tokens, RNN will generate the following search query sequence: $\texttt{<}\mathrm{StartSearch}\texttt{>}$, \b $k_{1}\ldots k_{c-1}$ \b(\S+)\b, $\texttt{<}\mathrm{EndSearch}\texttt{>}$.

Then the regular expression will match the next token after the first occurrence of $k_{1},\ldots k_{c-1}$ in the input sequence. The RNN needs to recite the format of the query and remember the query tokens $k_{1},\ldots k_{c-1}$ to generate the regular expression. The explicit construction of the RNN is omitted here.

When task $T$ is Counting, given an input sequence that ends with a query token $v$ and a query threshold $k$, RNN will generate the following search query sequence $\texttt{<}\mathrm{StartSearch}\texttt{>}$,(\b $v$ \b){$k+1$}, $\texttt{<}\mathrm{EndSearch}\texttt{>}$.

Then the regular expression will match the $k$-th occurrence of $v$ in the input sequence. The RNN needs to recite the format of the query and remember the query token $v$ and the threshold $k$ to generate the regular expression. The RNN can then check whether the retrieval result is $\mathrm{FAILED}$ to determine if the count is less than $k$. The explicit construction of the RNN is omitted here.

∎

###### Proof.

### B.9 Proof of Section 5.1

In this section, we will prove Section 5.1. We will first restate the theorem for convenience.

*

###### Proof of Section 5.1 .

We will first define the sequence that the retrieval-augmented RNN will generate and then construct an RNN that can generate such a sequence.

###### Proof of Section 5.1 .

##### Sequence Generation.

We will use a variant of Algorithm 1 to generate the sequence and we will use the concatenation of the tokenization of the sequence returned by Algorithm 2 as the sequence that the retrieval augmented RNN will generate.

Figure: Algorithm 2 Depth-First Search Algorithm with Retrieving

##### RNN Construction.

We can use similar proof in Section 5.1 by having the RNN memorize local sequences and determine the phase of Algorithm 2 it is in. The RNN will maintain the length of $S_{2}$ (Lemma B.21) and the top of $S_{1}$ in the state (Lemma B.20) and it is easy to check that the retrieval function will retrieve the correct result for each search query. The way to determine the next vertex in the stack is the same as in the proof of Lemma B.27. We will omit the simple but tedious detailed construction here.
∎

### B.10 Proof of Section 5.1

In this section, we will prove Section 5.1. We will first restate the theorem for convenience.

*

###### Proof of Section 5.1 .

We will denote the state of $T$ as $1,\ldots,B$ (we will use $1$ as the initial state) and the vocabulary of $T$ as $1,\ldots,B$. We will assume $T$ operates on an infinite tape $\mathrm{TAPE}$, which is a sequence of cells indexed by $\mathbb{Z}$. We will also assume that the tape is initialized with all cells being 0 0 except for the $n$ cell starting at $1$. The Turing machine also has a pointer $p$ that points to a cell in the tape. The pointer is initialized to $1$. At each time step, the Turing machine reads the cell pointed by $\mathrm{POINTER}$ and updates the cell pointed by $\mathrm{POINTER}$ and the pointer $p$ according to the transition function $\delta:[B+1]\times[B]\to[B]\times[B]\times\{-1,1\}$, which takes the current state and the current cell value (could be empty, which corresponds to $B+1$) as input and outputs the new state, the new cell value and the direction to move the pointer. The Turing machine halts when the state is $B$. Because $T\in\mathrm{TIME}(n^{A})$, the Turing machine will always halt in $n^{A}$ steps. We will use $\mathrm{TAPE}[t,i]$ as the value on the $i$-th cell on $\mathrm{TAPE}$ before the $t$ timestep. We will use $\mathrm{POINTER}[t]$ as the value of the pointer before the $t$ timestep and $\mathrm{State}[t]$ as the state of the Turing machine before the $t$ timestep. We will further use $\mathrm{Direction}[t]$ as the direction of the pointer movement before the $t$ timestep.

We will first define the sequence that the retrieval-augmented RNN will generate and then construct an RNN that can generate such a sequence.

###### Proof of Section 5.1 .

##### Sequence generation.

The input token sequence $\mathcal{S}_{\mathrm{in}}$ will be as followed,

$$ $\mathcal{S}_{\mathrm{in}}=\texttt{
},\mathrm{TAPE}[1,1],\mathrm{TAPE}[1,2], \ldots,\mathrm{TAPE}[1,n]$ $$

Here all the symbols on the tape are represented by one special symbol in the vocabulary. Given this input token sequence, the retrieval augmented RNN will generate the following output token sequence,

$$ $\displaystyle\mathcal{S}=\mathcal{S}_{\mathrm{in}},$ $\displaystyle\texttt{<}\mathrm{StartSearch}\texttt{>},\texttt{^(?:\S \s *).\{{ \color[rgb]{0,0,1}\definecolor[named]{pgfstrokecolor}{rgb}{0,0,1}{1}}\}({\S }) },\texttt{<}\mathrm{EndSearch}\texttt{>},$ $\displaystyle{\color[rgb]{1,0,0}\definecolor[named]{pgfstrokecolor}{rgb}{1,0,0 }{{\mathrm{TAPE}[1,1]}}}$ $\displaystyle 1,\mathrm{TAPE}[1,1],1,$ $\displaystyle\ldots$ $\displaystyle\texttt{<}\mathrm{StartSearch}\texttt{>},\texttt{^(?:\S \s *).\{{ \color[rgb]{0,0,1}\definecolor[named]{pgfstrokecolor}{rgb}{0,0,1}{n}}\}({\S }) },\texttt{<}\mathrm{EndSearch}\texttt{>},$ $\displaystyle{\color[rgb]{1,0,0}\definecolor[named]{pgfstrokecolor}{rgb}{1,0,0 }{{\mathrm{TAPE}[1,n]}}}$ $\displaystyle n,\mathrm{TAPE}[1,n],n,$ $\displaystyle\texttt{<}\mathrm{StartSearch}\texttt{>},((\text{{\color[rgb]{ 0,0,1}\definecolor[named]{pgfstrokecolor}{rgb}{0,0,1}{$\mathrm{POINTER}[1]$}} \ (.) \ {\color[rgb]{0,0,1}\definecolor[named]{pgfstrokecolor}{rgb}{0,0,1}{$ \mathrm{POINTER}[1]$}}\ {.*?\$}})),\texttt{<}\mathrm{EndSearch}\texttt{>},$ $\displaystyle{\color[rgb]{1,0,0}\definecolor[named]{pgfstrokecolor}{rgb}{1,0,0 }{{\mathrm{SearchResult}(1)}}},$ $\displaystyle\mathrm{POINTER}[1],\mathrm{TAPE}[2,\mathrm{POINTER}[1]],\mathrm{ POINTER}[1]$ $\displaystyle\mathrm{State}[2],\mathrm{Direction}[2],$ $\displaystyle\ldots$ $\displaystyle\texttt{<}\mathrm{StartSearch}\texttt{>},(\text{{\color[rgb]{ 0,0,1}\definecolor[named]{pgfstrokecolor}{rgb}{0,0,1}{$\mathrm{POINTER}[t]$}} \ (.) \ {\color[rgb]{0,0,1}\definecolor[named]{pgfstrokecolor}{rgb}{0,0,1}{$ \mathrm{POINTER}[t]$}}\ {.*?\$}}),\texttt{<}\mathrm{EndSearch}\texttt{>},$ $\displaystyle{\color[rgb]{1,0,0}\definecolor[named]{pgfstrokecolor}{rgb}{1,0,0 }{{\mathrm{SearchResult}(t)}}}$ $\displaystyle\mathrm{POINTER}[t],\mathrm{TAPE}[t+1,\mathrm{POINTER}[t]], \mathrm{POINTER}[t],$ $\displaystyle\mathrm{State}[t+1],\mathrm{Direction}[t+1],$ $$

Here $\mathrm{SearchResult}(t)$ is defined as

$$ $\displaystyle\mathrm{SearchResult}(t)=\begin{cases}&\mathrm{FAILED};\text{if } \mathrm{POINTER}[t]\text{ is empty cell before }t\\ &\mathrm{TAPE}[t,\mathrm{POINTER}[t]]\text{;otherwise}\\ \end{cases}$ $$

The output token sequence simulates the Turing machine $T$ on the tape $\mathrm{TAPE}$ due to the following lemma.

###### Lemma B.31 .

Given any $t\in[n^{A}]$ and $i\in[n^{A}]$, the last string in $\mathcal{S}$ that contains $i,i$ as a substring is $i,\mathrm{TAPE}[t,i],i$ if $\mathrm{TAPE}[t,i]$ is not empty and is the empty string otherwise.

###### Proof.

The proof is by induction, for $t=1$, the result holds. For any $t\geq 2$, we only need to notice that $\mathrm{POINTER}[t-1]$ is the only cell that can be updated at time $t-1$.
∎

###### Lemma B.31 .

###### Proof.

##### Construction of RNN

Given the input, the RNN can first iterate over $1$ to $n$ and generate the first $n$ search queries and results by maintaining a counter in its state and memorizing the most recent search result (Lemma B.20). Then it is easy to see that the retrieval oracle will generate the correct $\mathrm{SearchResult}(t)$ given the input $\mathcal{S}$. Therefore, we will only need to construct an RNN that can generate the rest part of $\mathcal{S}$.

We will assume the RNN maintains the state and pointer of the Turing machine in its state and show that they can be updated.

Based on Lemma B.20, the RNN can maintain constant recent token types in its state, we will assume the RNN memorize the last tokens up to the most recent $\texttt{<}\mathrm{StartSearch}\texttt{>}$ and also calculate the position relative to the most recent $\texttt{<}\mathrm{StartSearch}\texttt{>}$. By a lookup table in the FFN Lemma B.4, the RNN can output the fixed format of the search query. Similarly, RNN can output the $\mathrm{POINTER}[t]$. To generate the update $\mathrm{TAPE}[t+1,\mathrm{POINTER}[t]],\mathrm{State}[t],\mathrm{Direction}[t]$, the RNN can use a FFN with $O(B^{2})$ width to memorize the transition function of the Turing machine and output the update. Then, the RNN can use the memorized recent input to update the state and the pointer of the Turing machine at the next $\texttt{<}\mathrm{StartSearch}\texttt{>}$. The proof is then complete.

∎

### B.11 Proof of Definition 5.2

*

###### Proof.

The proof here is essentially the same as the construction of the Transformer in Definition 4.4. We would use the same Transformer layer to solve $T$. The only difference is that we would use the output of the RNN, instead of FFN, as the input of the Transformer layer.Also for Counting, instead of using a $\mathrm{COPY}$ function, we write the query token in the state of the RNN (Lemma B.20).
∎

###### Proof.

### B.12 Proof of Section 5.2

*

###### Proof.

The proof is similar to the proof of Section 5.1. However, instead of using regular expressions to retrieve the next neighbor and parent, we will need to use the Transformer layer. The Transformer layer can retrieve the parent through an attention head implementing the match closest head (Lemma B.16) if the RNN part maintains the predecessor of each node in the chain of thought.

Retrieving the next neighbor is more complicated and we will use $O(\log n)$ steps of the chain of thought to do that. Given an edge $(v,v^{\prime})$, we will first use one match head to retrieve the position $p$ of $(v,v^{\prime})$ in the input sequence and write it to the chain of thought. Then we will use two $\mathrm{MatchClose}$ heads to retrieve the edge that contains $v$ and is closest to $p+2^{i}$ for $i=0,1,\ldots,\log_{2}n$ iteratively until the heads return an edge that is not $(v,v^{\prime})$ or $i$ reaches $\log_{2}n$. Here $2^{i}$ can be computed through doubling one of the dimensions in the state of the RNN and reset that dimension to $1$ after termination.
We will then compare the retrieved edge with the father of $v$ to check if it is the same. If it is the same, we will search the next neighbor of $v$ after the parent of $v$ in the same way. The other part of the proof is similar to the proof of Section 5.1.
∎

###### Proof.

### B.13 Proof of Section 5.2

*

###### Proof.

Sequence Generation. Under the same formulation of proof of the Section 5.1. The hybrid RNN will output the following sequence.

$$ $\displaystyle\mathcal{S}=\mathcal{S}_{\mathrm{in}},$ $\displaystyle\mathrm{POINTER}[1],\mathrm{TAPE}[2,\mathrm{POINTER}[1]],\mathrm{ POINTER}[1]$ $\displaystyle\mathrm{State}[2],\mathrm{Direction}[2],$ $\displaystyle\ldots$ $\displaystyle\mathrm{POINTER}[t],\mathrm{TAPE}[t+1,\mathrm{POINTER}[t]], \mathrm{POINTER}[t],$ $\displaystyle\mathrm{State}[t+1],\mathrm{Direction}[t+1],$ $$

Note that Lemma B.31 still holds. We only need to prove that the hybrid architecture can generate the above sequence.

Hybrid Construction. The way RNN maintains the pointers and the states is the same as the proof of Section 5.1. Given each pointer value, we can retrieve the last value of the cell at the pointer through the one layer of attention by implementing a match closest head (Lemma B.16).
∎

###### Proof.

## Peer Reviews

confidence: 4
contribution: 1
initial_rating: 3
presentation: 3
questions: Please address points from weaknesses as questions if authors do not agree.
soundness: 2
strengths: - The paper is well-structured and clearly written, main ideas and results are easy to follow.
    
- The paper constructs building blocks for Transformers that can be used to solve various tasks, including indexing, associative recall, c-gram retrieval, and counting. It provides detailed analyses of how these problems can be addressed using different architectures and shows their limitations.
    
- The authors support their theoretical results with empirical evaluations on tasks such as IsTree and HotPotQA, thus strengthening their claims.
    
- Show that in-context retrieval, similar to attention mechanism, is sufficient to close the gap between RNNs and Transformers with CoT, and suggest how to build more expressive RNN architectures.
summary: This paper investigates the representational capabilities of RNNs and Transformers, both with and without Chain-of-Thought (CoT). Assuming a limited RNN state size of O(log n), the authors show that RNNs augmented with CoT have greater representational power than standard RNNs, but are still less powerful than Transformers with CoT. To bridge this gap, the authors propose adding in-context retrieval mechanisms to RNNs, showing that this extension enables RNNs with CoT to solve polynomial-time problems, effectively matching the capabilities of Transformers with CoT.


Informally, the relationships can be summarized as:
- Standard RNN < RNN + CoT < Transformer + CoT
- RNN + In-Context Retrieval + CoT ~= Transformer + CoT
weaknesses: 1. The authors compare RNNs and Transformers under different assumptions regarding their state sizes. Specifically, they limit RNNs to a limited hidden state size of O(log n), while allowing Transformers to have an effective state size that grows linearly with the input length due to the accumulation of key-value pairs in self-attention. A decoder-only Transformer can be viewed as an RNN with a recurrent state that increases linearly -- adding each processed input to the key-value cache:  in transformer h_t = Transformer(x_t, [h_1, …, h_{t-1}]), in RNN h_{t}  = RNN(x_t, h_{t-1}); In other words, the results obtained are equivalent to comparing RNNs with limited state sizes to RNNs with linearly growing state sizes. There is nothing inherently specific to Transformers in this comparison. The key differentiator in this comparison is the state size rather than any unique property of Transformers. Therefore, concluding that models with larger state sizes have more expressive power than those with limited state sizes is unsurprising.
    
2. Violation of initial assumptions with in-context retrieval. By introducing in-context retrieval to RNNs, the model effectively needs to store the entire input sequence, resulting in O(n) state size requirements - similar to Transformers. This breaks the initial assumption of a limited-size RNN state O(log n) used to state RNNs limitations. As a result, the proposed solution does not address the original problem under the same constraints.
    
3. The proposed solution of adding in-context retrieval to RNNs essentially increases the state size linearly, effectively transforming the RNN into an RNN with linearly growing state size, which resembles a decoder-only Transformer. This leads to the somewhat tautological conclusion that to close the gap between RNNs and Transformers, one should make a Transformer-like model out of an RNN.
    
4. Overlap with existing concepts is not discussed. The relationship between RNNs with CoT and Adaptive Computation Time (ACT) mechanisms is not fully explored. Since CoT can be seen as a form of ACT, and RNNs with ACT have been previously studied, the novelty of the findings may be limited. A deeper exploration of how this work builds upon or differs from existing research on RNNs with ACT could strengthen the contribution.

---

confidence: 3
contribution: 3
initial_rating: 6
presentation: 2
questions: Why in-context retrieval can not be deemed as a part of COT?
Since I tried RWKV6 -7B and it can retrieve in context words without the help of regular express function but with the help of CoT. 
I tried api on https://huggingface.co/spaces/BlinkDL/RWKV-Gradio-2
My input is: 

Assistant: ”How can we craft an engaging story featuring vampires on Mars? Let's think step by step and provide an expert response. “ What's the word between 'featuring' and 'on'. Think step by step.

The result is unstable but primarily consist of 'vampire' and 'mars‘， which means RWKV has somewhat retrieval ability.
soundness: 3
strengths: Originality:
The paper tackles a unique question—whether RNNs can be enhanced to achieve Transformer-like in-context retrieval. By applying communication complexity and information-theoretic methods, it provides a fresh perspective on RNN limitations. Proposing hybrid architectures and Retrieval-Augmented Generation (RAG) is an innovative approach to bridging this gap.

Quality:
The paper is theoretically rigorous, with strong proofs demonstrating RNN limitations. The authors conduct well-designed empirical experiments that validate the theoretical claims on both synthetic and real-world tasks, making the results robust and credible.

Clarity:
The structure is logical, with clear definitions and effective visuals that aid understanding. Complex proofs could benefit from simplified explanations, but overall, the writing is accessible and well-organized.

Significance:
This work addresses a critical issue in model efficiency, with practical implications for memory-constrained applications. It lays a solid theoretical foundation for future research on enhancing RNNs, potentially impacting both low-resource applications and the development of hybrid model architectures.
summary: This paper investigates the representational limitations of Recurrent Neural Networks (RNNs) compared to Transformers in handling sequence modeling tasks. It particularly examines RNNs’ in-context retrieval capabilities, which are essential for tasks requiring memory and context comprehension. Through theoretical analysis and empirical validation, the authors identify the inherent weaknesses of RNNs in retrieving information across long sequences and propose potential methods to bridge the performance gap between RNNs and Transformers.
weaknesses: 1.This paper spends a large section explaining the well-known concepts in AI community like Language Models and CoT while ignoring the explanation of communication complexity and other less-known concepts that are also important in the proof. It would be better to shift the focus of concept explanation. 

2. In some cases, with enough training, RNN may have the ability to better organize information based on the original O(n) bit. For example, based on the case of in-context retrieval, it may be possible to finetune the RNN to enable it to achieve the information retrieval process with the deepening of layers. e.g. the hidden states in shallow layers store the raw information and the deeper layers add organized information (like the regular expression for retrieval ) into the hidden state.  Please disprove this possibility.

---

confidence: 2
contribution: 3
initial_rating: 8
presentation: 3
questions: 1. Why were the word embedding and decoding heads frozen in the experiments for Figures 2 and 4?
2. How do the author's define a "RNN with o(n)-bit memory"?
soundness: 3
strengths: 1. This is a well-designed study with a focused question, along with theoretical and empirical support for its conclusions.
2. This paper’s theoretical analysis is a strong contribution, given the numerous prior works that empirically find hybridization and RAG help close the performance gap between transformers and RNNs.
summary: This paper compares the performance differences between RNNs and Transformers when enhanced with chain of thought (CoT) reasoning. The term RNN is used in a broad sense to refer to the class of architectures that are linear time and constant memory with respect to sequence length. This paper demonstrates, both theoretically and empirically, that while CoT enhances the performance of RNNs, it is not sufficient to close the gap between RNNs and Transformers. To close this gap, the authors demonstrate that retrieval-augmented generation or adding a single transformer layer is sufficient.
weaknesses: 1. Perhaps this is a misunderstanding, but line 297 seems to contradict Arora et al. (2023), who observe that while transformers outperform RNNs on associative recall, certain RNNs can perform associative recall. Claiming that “constant-size RNNs cannot solve any of the four tasks” seems too strong, given that Theorem 4.6 is only true for “large enough $n$.” This paper would benefit from the addition of a paragraph reconciling this claim with the empirical results of prior work.
2. The use of IsTree as a synthetic task could be better motivated. As currently written, it seems that this task was chosen simply because it does not obviously require retrieval to solve which is not unique to IsTree. It would be helpful to expand on the authors' rationale.

---

confidence: 3
contribution: 3
initial_rating: 5
presentation: 2
questions: None
soundness: 3
strengths: A key strength of this paper is its use of theoretical lower bounds to rigorously show that RNNs lack the expressiveness needed for certain in-context retrieval tasks, unlike Transformers. This analytical approach confirms that RNN limitations are inherent, not merely practical, underscoring the need for hybrid architectures or additional layers to bridge the performance gap. Additionally, the paper validates these findings through empirical experiments across various tasks (such as associative recall and graph recognition), demonstrating that the theoretical limitations are evident in real applications as well.
The authors also provide practical techniques—like Retrieval-Augmented Generation (RAG) and hybrid architectures involving Transformer layers—that make their findings actionable and relevant for architecture design in large language models (LLMs). By identifying specific ways to improve RNNs, the paper offers a path toward resource-efficient alternatives to Transformers, valuable for applications in memory-constrained or mobile environments. Moreover, their analysis sheds light on the mechanisms behind in-context learning, clarifying what capabilities make Transformers excel and highlighting specific limitations in RNNs. This deeper understanding could inform future research on in-context retrieval and neural network architecture design.
summary: This paper investigates the gap in representation power between Transformers and Recurrent Neural Networks (RNNs) and examines whether RNNs, with techniques like Chain-of-Thought (CoT) prompting, can achieve comparable performance. Through theoretical analysis, the authors demonstrate that RNNs lack the expressiveness needed to handle in-context retrieval tasks, such as associative recall and graph recognition, that Transformers handle with ease. They further support these findings with empirical evidence from both synthetic tasks and natural language experiments, showing that while CoT prompting improves RNNs, it alone is insufficient to bridge the gap.
The authors propose that enhancing RNNs with Retrieval-Augmented Generation (RAG) or adding a Transformer layer can significantly close this performance gap, bringing RNNs closer to Transformer-level capabilities. However, they also acknowledge that practical challenges, such as computational costs and scalability, may arise when implementing these improvements, suggesting a trade-off between efficiency and retrieval effectiveness.
weaknesses: Readability and Structure: The paper is hard to follow due to poor language flow, making the argument difficult to track.
Redundant content, especially on Chain of Thought (CoT) prompting, appears in both the Related Work and Preliminaries sections. Consolidating these would improve readability. Some definitions are introduced but not utilized in the main text, adding unnecessary complexity.

Depth of Discussion and Empirical Validation: The paper discusses two main ideas: first, that Chain of Thought (CoT) prompting can improve RNNs but is insufficient to fully bridge the representation gap with Transformers; and second, that enhancing the in-context retrieval capability of RNNs can potentially close this gap. However, each of these points would benefit from deeper discussion and more practical experiments. The empirical validation is insufficient; model comparisons are not “apple-to-apple, limiting the clarity and robustness of results.

Meta-review: This paper explores the representation capacity of memory efficient sequence models, and the effect resulting from the use of chain-of-thought techniques. Namely, authors show that RNNs cannot match the performance of Transformers even under chain-of-thought prompting. However, they do show that chain-of-thought strictly increases representation power. It's also shown that addressing the lack of ability of RNNs to perform in-context retrieval can close the gap with respect to transformers under chain-of-thought, which is done via hybrid architectures that include a transformer layer in a recurrent model.

The reviewers do not raise correctness concerns nor do they point out limitations with the tooling used for the analysis, so we assume conclusions hold. Instead, the discussion centered around fairness of comparisons in terms of equivalence of the architectures in parameter and state sizes, and on the relevance of the conclusions. To the first point, authors rebutted by arguing that the two architectures are fundamentally distinct and cannot be made completely equivalent, so they chose to match parameter sizes, as is commonly done in concurrent work. As per the relevance of the results, that boils down to opinion. I'd argue that highlighting fundamental limitations in RNNs is of great relevance to our community. There's been a recent surge in papers seeking to address known limitations in RNNs that would enable them reaching the level of performance we observe for quadratic self-attention models, and this work shows that this can only be achieved for long contexts if we somehow address the lack of ability of RNNs to perform in-context retrieval. While I do agree with some of the reviewers that claimed that the solution of adding linearly growing states to RNNs defeats the purpose of using an RNN in the first place, I see significant value in the negative results contributed by this work, which will help inform future work.

Decision: Accept (Poster)