Looped Transformers for Length Generalization

## Abstract

Abstract Recent work has shown that Transformers trained from scratch can successfully solve various arithmetic and algorithmic tasks, such as adding numbers and computing parity. While these Transformers generalize well on unseen inputs of the same length, they struggle with length generalization, i.e., handling inputs of unseen lengths. In this work, we demonstrate that looped Transformers with an adaptive number of steps significantly improve length generalization. We focus on tasks with a known iterative solution, involving multiple iterations of a RASP-L operation—a length-generalizable operation that can be expressed by a finite-sized Transformer. We train looped Transformers using our proposed learning algorithm and observe that they learn highly length-generalizable solutions for various tasks.

## 1 Introduction

Most algorithmic tasks such as coding, writing mathematical proofs, and reasoning are defined with inputs of variable *length*.
The length of an input often correlates with the difficulty of the problem instance. For example, the longer the input, the more difficult the problem tends to be.
We say a model perfectly *length-generalizes* if it can solve an algorithmic task on inputs of any length, even if it was only trained on data with inputs up to a finite length [].
Generally, it is hard to expect models to be trained on inputs with all possible lengths, and we need to rely on length generalization.
Also, if a model can length-generalize, it means the model has truly learned the correct algorithmic solution to the task, not just a spurious solution that works only for a certain range of input lengths.

Recently, many works on Large Language Models (LLMs) have shown that we can get more powerful AI models by scaling both compute and data at training time.
This scaling approach has indeed succeeded in improving accuracies on various benchmarks.
However, even the largest and latest LLMs like [] which have been trained on much of the existing text on the Internet, still struggle with length generalization [, , ].
One possible cause is the particular computing model.
LLMs are built based mostly on the Transformer architecture [].
While Transformers can accept a variable length of inputs (that can be processed in parallel), they usually have a fixed depth. This might be sufficient for certain tasks, but not always.

To learn a model that can effectively generalize to longer problems, it is important to consider architectures that can adaptively adjust the computational budget to the difficulty of the tasks [, , ]. One approach to achieve this is to explicitly generate intermediate output tokens, similar to writing down a scratchpad, which improves LLMs’ capability for solving harder problems[]. In theory, LLMs may generate more scratchpad tokens representing intermediate computation when solving a more difficult task, indicating that they can allocate elastic computation according to the length and difficulty of the given instance. This approach can be learned by explicitly training a model on data with intermediate computation steps [, ]. Alternatively, it can be achieved via Chain-of-Thought (CoT) reasoning with few-shot examples [] or even in a zero-shot manner []. Notice that these approaches still use
fixed-depth models.
While these approaches help solve more complex reasoning tasks, they are still far from achieving near-perfect length generalization for simple algorithmic tasks.
For instance, Lee et al. applied CoT for arithmetic tasks but observed that Transformers cannot length generalize even for simple addition tasks [].

Recently, there has been growing interest in using recurrent architectures for reasoning [, , , ].
Unlike standard RNN-type architectures that process different parts of the input sequence incrementally, one can consider a recurrent architecture that processes the entire input sequence multiple times. This architecture passes the intermediate processing output to the next iteration’s input, possibly along with the original input.
In particular, if the base model used in each iteration is a Transformer, this model is called a Looped Transformer [].

Figure: Figure 1: Method Overview. During training, we supervise the output of the model to match the target data only after a certain number of steps of applying the same decoder block, helping the model learn intermediate steps that can be reused and can handle input of arbitrary lengths. All grey blocks share the same parameters. Examples are from the Copy task with $n$ symbols. “#” indicates EOS, “*” indicates ignored output, and “>” indicates the end of the query (EOQ).
Refer to caption: x1.png

Looped Transformer can naturally break the limitation of the fixed depth in the standard Transformer architecture: *One can adjust the number of looped steps based on the computational complexity of the underlying algorithmic solution*. Consider a problem set with the following properties: 1) The problems can be solved by a loop of one RASP-L [] program(^1^11Here we consider a more general way to loop, i.e., predicting all missing tokens at the end of the loop, not necessarily in the way of predicting the single next token at a time. See more discussions in Section 2.2.), i.e., each step in the loop can be performed by a decoder-only Transformer with a fixed depth; 2) The number of steps needed in the loop depends on the problem’s complexity, i.e., more difficult problems could potentially require more steps to solve. Under the length generalization scheme, we consider the number of steps depending on the problem length, and define this problem set as $n$-RASP-L problems. For $n$-RASP-L problems, if we can learn these length-independent steps, we can utilize an adaptive number of steps to achieve length generalization.

Inspired by this observation, we study training Looped Transformers models for length generalization. Specifically, we consider a training setup where we do not require any intermediate supervision data (such as reasoning steps or scratchpad). We only assume access to end-to-end supervision (input and output) and the number of steps needed. Depending on the number of steps, we iteratively apply the same decoder block and then decode the final answer; See Figure 1 for illustration. At inference time, the model could either decide when to stop with predefined stopping criteria or stop when reaching the ground-truth number of steps. Empirically, we show that looped Transformers with an adaptive number of steps can successfully length-generalize to longer lengths simply by appropriately adapting the number of loops at inference time, indicating that our approach encourages the model to implicitly learn the necessary steps to solve a task.

Our contributions can be summarized as follows: (1) We first formally define $n$-RASP-L problems, and provide examples of $n$-RASP-L solutions to the Copy, Parity, and Addition tasks (Section 3); (2) We propose to learn $n$-RASP-L problems with Looped Transformers where we supervise the final answer in a step-dependent way, which enables us to use an adaptive number of steps depending on the problem complexity (Section 4); (3) Empirically, we show that our proposed method outperforms the baseline approaches in terms of length generalization performance (Section 6).

## 2 Background

### 2.1 RASP-L

A decoder-only Transformer is a type of Transformer architecture that consists of only the decoder part of the original Transformer model introduced by [], where a causal mask is applied to the attention weights to prevent the model from attending to future tokens.

RASP (Restricted Access Sequence Processing) [] is a computational model for the Transformer architecture in the form of a programming language.
RASP-L [], where ‘L’ stands for learnable, is a learnable subset of the RASP language.
Some key points about RASP-L are:

- •
RASP-L programs accept an input sequence and return an output sequence of the same length for an *arbitrary length*, like decoder-only Transformers.
- •
The core operations in RASP-L include element-wise operations on sequences and a specific type of non-elementwise operation called kqv, which simulates a causal attention layer.
- •
RASP-L has restrictions on the allowed operations to ensure learnability: It does not allow arbitrary index arithmetic, and restricts operations on token indices to order comparisons and computing successor/predecessor.
- •
RASP-L *does not allow control flow* statements like branching or loops. Programs must be straight-line code, with each line being a call to a core function or another RASP-L program.

In [], the authors show that algorithmic tasks that can be written as a RASP-L program can be easily learned by a Transformer in a length-generalizable way with next-token prediction. The length-generalizable tasks include counting, finding the mode, copying the input sequence (consisting of unique tokens), and sorting. However, they also showed that for algorithmic tasks whose RASP-L program representation is not known to exist, such as addition, parity, and copying the input sequence, it is hard to learn in a length-generalizable way. In other words, once the Transformer is trained on in-distribution data up to a particular length, it fails to generalize to unseen lengths.

### 2.2 Next-token prediction and full-answer prediction

Figure: Figure 2: Visualization of the next-token prediction (NTP) and full-answer prediction (FAP) schemes. “#" indicates EOS, “*" indicates ignored output, and “>" indicates the end of the query (EOQ).
Refer to caption: x2.png

Decoder-only Transformers are naturally convenient for next-token prediction (NTP) which could be efficiently trained in parallel. In [], their setup and RASP-L solutions are both constrained to predicting the single next token: During training, the full sequence (both the query and the answer) is provided as input and the output is expected to be the shifted sequence. During inference, only the query part is provided, and the model continues to output the next token and append the token to the current sequence until the output token is EOS. The output locations before the end of query (EOQ) sign are ignored. See (a) in Figure 2 for illustration.

On the other hand, we can also consider a more general way of predicting the answer: full-answer prediction (FAP). During both training and inference time, the input given is just the query part, and the rest of the locations are filled with multiple EOS tokens to keep the input and the output to be the same length. The model is supposed to output the answer with a shifted location, and the output locations before the EOQ sign are ignored; see (b) in Figure 2. Notice that in FAP, the model is not forced to predict token-by-token as NTP. Instead, the model is expected to predict all missing tokens after all internal processing steps.

## 3 n 𝑛 n italic_n -RASP-L

Recall that RASP-L programs do not allow loops. If we consider the next-token prediction (NTP) scheme, it means that we need to find the same RASP-L program (which can be represented with a fixed-depth decoder-only Transformer) to predict the next token given any possible prefix in the answer sequence. Such solutions might not always exist for all problems: there is no known RASP-L program for addition, parity, and copy under the NTP scheme [].

On the other hand, architectures such as the Looped Transformer have external loops embedded in the architecture which naturally provides adaptive depth. Thus, a natural question is: what kind of algorithmic tasks can we represent with a decoder-only Transformer in a loop? Specifically, what if we also allow the number of iterations to explicitly depend on the input length, say $n$? Moreover, what if we are not constrained by the NTP scheme, but a more general FAP scheme?

Inspired by these questions, we define the following class of algorithmic tasks:

###### Definition 3.1 ( $n$ -RASP-L) .

A program $P$ is called an $n$-RASP-L program if
(1) there exist $T(n):\mathbb{N}\rightarrow\mathbb{N}$, and (2) $P$ can be decomposed to a sequential application of $P^{\prime}$ for $T(n)$ steps.

We show that $n$-digit addition, $n$-bit parity, copying $n$ symbols indeed have $n$-RASP-L solutions.

###### Proposition 3.2 .

(Parity.) There exists a n-RASP-L program with $T(n)=n$ that solves the $n$-bit parity check task:

$$ $\underbrace{\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4. 52083pt][1.50694pt]{$x_{1}$}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{ \raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\ldots}}}}}\mathbin{\vbox{\hbox{ \scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{$x_{n}$}}}}}} _{\text{$n$ tokens}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.602 75pt}[4.52083pt][1.50694pt]{>}}}}}\underbrace{\mathbin{\vbox{\hbox{\scriptsize \framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{ \hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\ldots} }}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt] [1.50694pt]{\#}}}}}}_{\text{$n^{\prime}$ tokens, $n^{\prime}\geq 0$}} \Rightarrow\underbrace{\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0. 60275pt}[4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox {\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\ldots}}}}}\mathbin{\vbox{\hbox{ \scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}}_{ \text{$n$ tokens}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275 pt}[4.52083pt][1.50694pt]{y}}}}}\underbrace{\mathbin{\vbox{\hbox{\scriptsize \framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{ \hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\ldots} }}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt] [1.50694pt]{\#}}}}}}_{\text{$n^{\prime}$ tokens}},$ $$

where $y$ is the parity check result for the arbitrary binary input sequence $\{x_{i}\}$.

###### Proof.

See Listing LABEL:lst:parity in Appendix A,
where the number of steps required in parity_loop is $T(n)=n$ for the input query with $n$ bits.
∎

###### Proposition 3.3 .

Figure: Figure 3: Visualization of the $n$-RASP-L solutions for Copy, Parity, and Addition with $n=2$. Copy is implemented by $n$ iterations of shifting; Parity is implemented by $n$ iterations of shifting and XOR; Addition is implemented by $n+1$ iterations of shifted XOR and AND; The inputs are preprocessed. See details in Section 3.
Refer to caption: x3.png

(Copy.) There exists a n-RASP-L program with $T(n)=n$ that solves the $n$-symbol copy task:

$$ $\underbrace{\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4. 52083pt][1.50694pt]{$x_{1}$}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{ \raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\ldots}}}}}\mathbin{\vbox{\hbox{ \scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{$x_{n}$}}}}}} _{\text{$n$ tokens}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.602 75pt}[4.52083pt][1.50694pt]{>}}}}}\underbrace{\mathbin{\vbox{\hbox{\scriptsize \framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{ \hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\ldots} }}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt] [1.50694pt]{\#}}}}}}_{\text{$n^{\prime}$ tokens, $n^{\prime}\geq n-1$}} \Rightarrow\underbrace{\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0. 60275pt}[4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox {\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\ldots}}}}}\mathbin{\vbox{\hbox{ \scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}}_{ \text{$n$ tokens}}\underbrace{\mathbin{\vbox{\hbox{\scriptsize\framebox{ \raisebox{-0.60275pt}[4.52083pt][1.50694pt]{$x_{1}$}}}}}\mathbin{\vbox{\hbox{ \scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\ldots}}}}} \mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5 0694pt]{$x_{n}$}}}}}}_{\text{$n$ tokens}}\underbrace{\mathbin{\vbox{\hbox{ \scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}} \mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5 0694pt]{\ldots}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.6027 5pt}[4.52083pt][1.50694pt]{\#}}}}}}_{\text{$n^{\prime}-n+1$ tokens}},$ $$

where $\{x_{i}\}$ is an arbitrary binary input symbols.

###### Proof.

See Listing LABEL:lst:copy in Appendix A,
where the number of steps required in copy_loop is $T(n)=n$ for the input query with $n$ symbols.
∎

###### Proposition 3.4 .

(Addition.) There exists a n-RASP-L program with $T(n)=n+1$ that solves the $n$-digit addition task:

$$ $\underbrace{\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4. 52083pt][1.50694pt]{$x_{1}$}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{ \raisebox{-0.60275pt}[4.52083pt][1.50694pt]{$\dots$}}}}}\mathbin{\vbox{\hbox{ \scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{$x_{n}$}}}}}} _{\text{$n$ tokens}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.602 75pt}[4.52083pt][1.50694pt]{+}}}}}\underbrace{\mathbin{\vbox{\hbox{\scriptsize \framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{$y_{1}$}}}}}\mathbin{ \vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{$ \dots$}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.5 2083pt][1.50694pt]{$y_{n}$}}}}}}_{\text{$n$ tokens}}\mathbin{\vbox{\hbox{ \scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{>}}}}} \underbrace{\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4. 52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox {-0.60275pt}[4.52083pt][1.50694pt]{\ldots}}}}}\mathbin{\vbox{\hbox{\scriptsize \framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}}_{\text{$n^{ \prime}$ tokens, $n^{\prime}\geq n$}}\Rightarrow\underbrace{\mathbin{\vbox{ \hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}} \mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5 0694pt]{\ldots}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.6027 5pt}[4.52083pt][1.50694pt]{*}}}}}}_{\text{$2n+1$ tokens}}\underbrace{\mathbin{ \vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{$ z_{1}$}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.5 2083pt][1.50694pt]{\ldots}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{ \raisebox{-0.60275pt}[4.52083pt][1.50694pt]{$z_{n+1}$}}}}}}_{\text{$n+1$ tokens}}\underbrace{\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.602 75pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{ \raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\ldots}}}}}\mathbin{\vbox{\hbox{ \scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}}_{ \text{$n^{\prime}-n$ tokens}},$ $$

where $\{x_{i}\}$, $\{y_{i}\}$ are arbitrary binary summands and $\{z_{i}\}$ is the result of adding $\{x_{i}\}$ and $\{y_{i}\}$(^2^22For simplicity, we include the leading 0’s to keep the same length of the output for all possible inputs.).

###### Proof.

See Listing LABEL:lst:add in Appendix A, where the number of steps required in addition_loop is $T(n)=n+1$ for the input summands with $n$ digits each.
∎

We present visualizations of the intermediate steps in the loops of our n-RASP-L solutions in Figure 3: For the parity task, $P^{\prime}_{\text{parity}}$ is to shift the input sequence to the right by 1 and calculate XOR of the answer sequence and the input sequence; For the copy task, $P^{\prime}_{\text{copy}}$ is to shift the input sequence to the right by 1; For the addition task $P^{\prime}_{\text{addition}}$ is to calculate the XOR of two sequences and shift the results to the right by 1 position as the partial answer, and calculate the AND of two sequences as the carry-on sequence(^3^33Here we omit the pre-processing and post-processing steps like handling EOS (“#”) and EOQ (“¿”) tokens which can be done by fixed-depth attention layers outside of the loop (see Listings LABEL:lst:parity, LABEL:lst:copy, LABEL:lst:add).).

###### Definition 3.1 ( n 𝑛 n italic_n -RASP-L) .

###### Proposition 3.2 .

###### Proof.

###### Proposition 3.3 .

###### Proof.

###### Proposition 3.4 .

###### Proof.

## 4 Learning n 𝑛 n italic_n -RASP-L problems with looped Transformers

Consider a task solvable by an $n$-RASP-L program. It is straightforward to learn the looped Transformer model with the supervision of the ground truth intermediate outputs: One can use a fixed-depth TF block and simply supervise the input and output for each step. However, such intermediate supervision can be difficult to get, just like collecting helping CoT steps could be difficult. Therefore, a more interesting setup we consider is to learn looped Transformers in an end-to-end manner *without* intermediate-step supervision.

Here we present a novel framework for length generalization: In the absence of ground truth CoT data/intermediate output, we propose to leverage the inherent structure of the problem with the help of “knowing when to stop”. We present the setup for training data in Section 4.1, the model architecture and training algorithm in Section 4.2, and the inference algorithm in Section 4.3.

### 4.1 End-to-end supervised data without intermediate step supervision

We consider the following settings for the training data and the tasks:

- •
There exists an $n$-RASP-L program that solves the given task.
- •
Training data consists only of $(x,y)$ pairs, but not intermediate steps.
That is, we do not have access to $P^{\prime}(x),P^{\prime}(P^{\prime}(x)),\ldots$.
- •
$T(n)$, i.e., the ground truth number of iterations to solve the problem (with some $P^{\prime}$) is available in the training data(^4^44This assumption is to provide supervision for when to stop during training; for inference, we can either use the ground truth steps or leverage the confidence of the output as a stopping criterion (see Section 4.3 for details.)).
- •
The length $n$ is diversely distributed in the dataset, e.g., $n\in\{1,\ldots,n_{\text{max}}\}$ where $n_{\text{max}}$ is the maximum number of lengths in the dataset; The ground truth number of steps needed $T(n)$ is also diversely distributed in the dataset, e.g., $T(n)\in\{1,,\ldots,T(n_{\text{max}})\}$ where $T(n_{\text{max}})$ is the maximum number of steps in the dataset(^5^55The length of the problem is not necessarily the same as the actual length of the input due to EOS and EOQ tokens; see Section 6.1.1 for the definition of the length of the specific tasks.).

### 4.2 Looped training with step supervision

#### 4.2.1 Architecture of the looped Transformers

We present the architecture for Looped Transformer model in Figure 1. The key characteristics are:

Recurrence: Instead of having a simple stack of blocks, the Looped Transformer is recurrent (like [] but with decoder-only structure) in the sense that we reuse the same decoder block (which consists of a certain number of layers) for a number of looped steps, and we can adjust the number of looped steps at will.

Input injection: For each step, the original input sequence is injected together with the output from the previous decoder block, i.e. the input embeddings are added to the output embeddings of the previous step as the input of the current step. With input injection, the model can maintain a strong connection to the original input, preventing information loss with improved performance [, ].

##### Positional embedding:

Notice that there is no positional encoding in the RASP-L operations []. To follow our $n$-RASP-L assumption and test the effect of the looped training only, we use NoPE [] in decoder-only Transformers to avoid the impact from different positional embeddings(^6^66NoPE is shown to inherently learn to use relative positional embeddings in practice [].).

#### 4.2.2 Training algorithm

Given a dataset $D=\{(\{(x_{l})_{l=1}^{L_{i}}\}_{i},\{(y_{l})_{l=1}^{L_{i}}\}_{i},T_{i},L_{i})
\}_{i=1}^{N}$, where $\{(\{(x_{l})_{l=1}^{L_{i}}\}_{i}$ is the input with $L_{i}$ tokens, $\{(y_{l})_{l=1}^{L_{i}}\}_{i}$ is the output with $L_{i}$ tokens, and $T_{i}$ is ground truth number of steps of sample $i$. We aim to learn the transformer model $M_{\theta}$(^7^77$M_{\theta}$ only handles the embedding space and we use greedy decoding to get the decoded output.) by minimizing the following loss:

$$ $\displaystyle\mathbb{E}_{D}[\mathcal{L}\left(f_{T_{i}}(M_{\theta},\{(\{(x_{l}) _{l=1}^{L_{i}}\}_{i}),\{(y_{l})_{l=1}^{L_{i}}\}_{i}\right)],$ (1) $$

where $\mathcal{L}$ is the cross entropy loss and $f_{T_{i}}(M_{\theta},\{(\{(x_{l})_{l=1}^{L_{i}}\}_{i})=\underbrace{M_{\theta}(
M_{\theta}(\cdot\cdot\cdot M_{\theta}}_{\text{$T_{i}$ iterations}}(\{(\{(x_{l}
)_{l=1}^{L_{i}}\}_{i})))$.

How would our training help to learn more length-generalizable steps without intermediate supervision? Consider that for some input, we want to match the target after $T$ steps, $T\geq 1$. Although we do not have intermediate supervision for this input, there could exist some other input where we match the output after $T-1$ steps as shown in Figure 1, which means the model also receives supervision from $T-1$ steps with a different input length. The same argument applies to other $T$ values covered in the training set, so the same decoder block receives supervision from not only variable lengths, but also a variable number of steps. As a result, we expect the model to learn length-generalizable intermediate steps that could potentially generalize to more steps at test time.

### 4.3 Adaptive inference

Recall that looped Transformers can use adaptive depth at inference time, so we need certain rules to decide when to stop. In this paper, we consider two rules: 1) *Oracle*: We can assume that the number of steps needed is given; 2) *Maximum confidence*: We can use confidence base rules to decide when to stop, i.e., stop when we are confident about the output at the current step. More specifically, for (2), given a test sequence $\{(\{(x_{l})_{l=1}^{L}\}$ and a trained model $M_{\theta}$, we can get the number of steps $T$ from Equation (2):

$$ $T={\arg\max}_{t\in[1,T_{\text{max}}]}\mathcal{L}\left(f_{t}(M_{\theta},\{(\{(x _{l})_{l=1}^{L}\}),\{(\hat{y_{l}})_{l=1}^{L}\}^{t}\right),$ (2) $$

where $\{(\hat{y_{l}})_{l=1}^{L}\}^{t}$ is the decoded sequence from $f_{t}(M_{\theta},\{(\{(x_{l})_{l=1}^{L}\})$ at step $t$, $T_{\text{max}}$ is a threshold for the maximum number of steps.

## 5 Related work

##### Positional embedding for length generalization.

Positional embeddings have been shown to greatly affect Transformers’ ability to generalize to longer lengths [, , , , , , , , ]. By designing positional embedding schemes that better capture relative positional information with techniques such as randomization and functional representations, researchers have made significant progress in improving length generalization. Especially, [] and [] are tailor-made positional embeddings for some arithmetic problems without potential generality. (^8^88In [], they show that models with weight-tied layers (but with a fixed depth) can improve the generalization ability when comparing with the variants of the same positional embedding, but they do not find adaptive depths to be helpful since they do not perform the step-specific training as our method, while the key to our method is to use models with adaptive depths. To also compare with this baseline, we add NTP-Loop in Section 6.1.2.) This direction is orthogonal to our work since there is no positional encoding in RASP-L operations. We choose no positional embedding in our experiments, but other positional embeddings could further be synergistically applied with our approach. However, they might not be expressed as RASP-L operations. We leave further investigation with different positional embeddings to future work.

##### RNNs and Chomsky Hierarchy.

[] conducts an extensive empirical study to investigate the limits of the generalization performance of different neural network structures, demonstrate that grouping tasks according to the Chomsky hierarchy allows forecasting whether certain architectures will be able to generalize to out-of-distribution inputs. Their results show that RNNs and Transformers fail to generalize on non-regular tasks, LSTMs can solve regular and counter-language tasks, and only networks augmented with structured memory (such as a stack or memory tape) can successfully generalize on some context-free and context-sensitive tasks. In our paper, the Looped Transformer architecture also has augmented memory and the recurrent structure but is potentially more powerful since each iteration contains an operation of the whole sequence.

##### Universal Transformers and other looped models.

Our method is an adaptation of Universal Transformer [] to solve n-RASP-L problems. Compared with Universal TF and its follow-up work [], our training uses decoder-only models and step-dependent supervision data, without next-token prediction, dynamic halting, and timestep embeddings. Moreover, in looped Transformers, the original input is constantly fed into every iteration’s input along with the previous iteration’s output with input injection. Besides, [] studies learning recurrent networks to generalize to harder maze problems than seen during training, but with a focus on CNN architectures.

##### Input representation.

Recall that adding two numbers of length $n$ could not be solved by a RASP-L program where the difficulty mainly comes from indexing operations []. It could be solved by reformatting the input so that each digit is presented to the model with “index hints” in []. Such reformatting enables a simple RASP-L program for addition. Similarly, representing the answer in reversed order also helps because the corresponding RASP-L program gets much shorter, providing a concrete justification of the empirical observation made in []. However, such input representations are highly dependent on the specific problems and might not necessarily exist in general.

##### COT.

Scratchpad or CoT reasoning [, , , , ] is also useful for length generalization as it could simplify the next-token prediction task with intermediate results presented to the input layer.
There are also potential drawbacks and limitations to CoT reasoning. First, CoT training data might be hard to collect. Training and inference with pause tokens [] has been proposed to learn implicit CoT steps without CoT data, but pause tokens only increase horizontal compute, not sequential compute. Second, not all CoT steps are helpful. If CoT steps introduce additional complexity or require operations not easily expressible in RASP-L, then CoT may hinder length generalization, as shown in [].
Moreover, CoT steps that could convert the next token prediction task to RASP-L programs might not always exist. Besides, CoT is normally constrained to fixed-depth models, while in our paper we study a more general and powerful way to use adaptive compute at inference time.

## 6 Experiments

In this section, we evaluate the efficacy of looped Transformers in solving tasks that require length generalization. We introduce the experimental setup in Section 6.1, present length generalization results in Section 6.2 and ablation studies in Section 6.3, and visualize the stopping criterion in Section 6.4. Code is available at [https://github.com/UW-Madison-Lee-Lab/looped-tf](https://github.com/UW-Madison-Lee-Lab/looped-tf).

### 6.1 Experimental setup

#### 6.1.1 Tasks

Figure: Figure 4: Length Generalization Performance. Our looped Transformer model with adaptive depth generalized better than NTP methods across studied tasks, including the variants with pause tokens and weight-tied layers. The vertical dashed line indicates the maximum training length.
Refer to caption: x4.png

Here we consider tasks with $n$-RASP-L solutions presented in Section 3: Parity, Copy, and Addition, together with more tasks like calculating the sum, multiplication, and calculating the unique set.

##### Parity.

Checking the parity of the binary string. Example input: $\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{0}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{0}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{0}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{1}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{>}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}$, example output: $\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{0}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}$. We define the length of the problem to be the number of the digits, set $T$ (the number of steps needed) to be the same as the length, and train with length $[1,20)$.

##### Copy (with repeated tokens).

Copying the binary string. Example input: $\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{0}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{1}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{>}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{\#}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}
[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}$, example output: $\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{1}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{0}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}$. We define the length of the problem to be the number of the binary digits to copy, set $T$ to be the same as the problem length, and train with length $[1,20)$. It has been shown that copy with unique tokens could be easily solved by inductive head [], but copy with repeated tokens (e.g., binary) does not length-generalize with vanilla NTP training [].

##### Binary Addition.

Performing binary addition of two binary numbers with the same number of digits, and the output has one more digit (without removing leading 0 if it appears). Example input: $\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{0}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{+}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{1}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{>}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{\#}}}}}$, example output: $\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{0}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{1}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{\#}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}
[4.52083pt][1.50694pt]{\#}}}}}$. We highlight that we do not reverse the output like recent works [, , ]. We define the length of the problem to be the number of digits to be added, set $T$ to be the same as the problem length, and train with length $[1,20)$. It has been shown that binary addition without index hint is hard to generalize in vanilla NTP [].

##### Binary Sum.

Calculating the sum of the binary string in the binary form (in reversed order).
Example input: $\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{0}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{1}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{1}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{>}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{\#}}}}}$,
example output:$\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{\#}}}}}$.
We define the length of the problem to be the number of binary digits to be added, set $T$ to be the same as the problem length, and train with length $[1,20)$.

##### Binary Multiplication.

Multiplying two binary numbers, while the first number has up to two digits. The output is in reversed order and the length is the sum of the lengths of two numbers, without removing leading 0. Example input:
$\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{$\times$}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{1}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{0}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{>}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{\#}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}
[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}$,
example output: $\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{0}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{1}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{0}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{0}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{1}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{0}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{\#}}}}}$. We define the problem length to be the number of the second digits, and set $T$ to be the product of the lengths of two digits, and train with length $[1,12)$.

##### Unique Set.

Calculating the unique set with the first occurrence order with an alphabet of 50 tokens. Example input: $\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{1}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{4}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{2}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{2}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{4}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{3}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{>}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{\#}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}
[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}$, example output: $\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{*}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{*}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{1}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{4}}}}}
\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.5
0694pt]{2}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{\raisebox{-0.60275pt}[
4.52083pt][1.50694pt]{3}}}}}\mathbin{\vbox{\hbox{\scriptsize\framebox{
\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}\mathbin{\vbox{\hbox{
\scriptsize\framebox{\raisebox{-0.60275pt}[4.52083pt][1.50694pt]{\#}}}}}$. We define the length of the problem to be the number of digits to be calculated, set $T$ to be the same as problem length, and train with length $[1,20)$.

#### 6.1.2 Baseline methods

##### Vanilla NTP.

We use vanilla next-token prediction as one of our baselines, which is referred to as “NTP” in Figure 4. To ensure that the baseline method uses a maximum effective depth comparable to our method during training, we train the transformer model with a depth 20 times the depth of the looped block in our approach.

##### NTP with pause tokens.

Training and inference with pause tokens [] is a way to implicitly learn implicit CoT steps without CoT data by enabling extra compute pathways before outputting the answer in NTP. We use it as a baseline with the same depth as in vanilla NTP which is referred to as “NTP-Pause” in Figure 4. We include a visual illustration of NTP-Pause in Figure 7 in Appendix B.

##### NTP with weight-tied layers.

Using weight-tied layers but with a fixed number of overall depths in NTP is also shown to improve the performance in []. Here we fix the number of looped steps as 20, use the same depth as the decoder block of our looped model, and train the model with NTP as another baseline which is referred to as “NTP-Loop” in Figure 4.

#### 6.1.3 Training and evaluation setup

For training, we use a decoder-only Transformer block in GPT-2 architecture []. We adopt a curriculum learning strategy for all methods that starts from the smallest length and incrementally increases the length during training till it reaches the maximum length as in [].

For evaluation, we measure the exact match accuracy for the whole output sequence. For our looped inference, we test two possible stopping criterion discussed in Section 4.3: 1) *Oracle*: Adopt the same rule when generating the dataset as the number of steps to perform, 2) *Maximum confidence*: Run a maximum number of steps, and choose the step using Equation (2)(^9^99Another option is to set a threshold for the cross-entropy loss and stop when the threshold is first met. This will also succeed if the maximum confidence rule works.). We report test results from 1) in Section 6.2 and 6.3, and we also find 2) to be an effective stopping criterion in Section 6.4.

Full details of training and evaluation are in Appendix E.

### 6.2 Length generalization results

We present the generalization performance on various reasoning tasks in Figure 4.

##### Looped Transformers help with length generalization.

Our looped training significantly improves the length generalization performance. For example, for Parity, it can generalize to more than 50 digits near perfectly(^10^1010It still maintains accuracy higher than 0.95 when tested with 100 digits, which is not included in the graph.) when only trained with up to 20 digits. Moreover, for tasks like addition and copy, where the next token prediction failed when tested on maximum training length $+10$, our looped model can still perform nearly perfectly.
All of the models are only trained with a relatively small number of lengths, and the looped model generalizes surprisingly well.

##### Variants of NTP could improve generalization but not as effectively as our adaptive-depth model.

Compared with vanilla NTP, we observe that NTP-Loop could lead to improved generalization in tasks like Addition, Copy and Multiplication. Similarly, NTP-pause could introduce slight improvement in Parity and Unique Set. However, they all fall behind compared with our method. Besides, NTP-Loop suffers from lower in-distribution accuracy in Parity, possibly because using a fixed-depth model with weight-tied layers for NTP with all lengths might be too constrained for the task.

Figure: Figure 5: Ablation study. Our looped Transformer model with adaptive depth generalized better than FAP variants across studied tasks, including the variant of our method without input injection, and FAP with pause tokens. The vertical dashed line indicates the maximum training length.
Refer to caption: x5.png

### 6.3 Ablation studies

In Section 6.2, we compare with NTP baselines while the efficacy of components in our architecture design remains unclear. In this section, we compare with FAP variants of our model: “FAP-Loop-Adaptive-WO” indicates our method but without input injection; “FAP-Pause” indicates FAP with pause tokens(^11^1111Visual illustration of FAP-Pause is in Figure 8 in Appendix B.); “FAP” indicates vanilla FAP, without weight-tied layers or adaptive depth.

##### Effect of input injection.

We observe that the generalization performance with input injection is generally better than without it, which aligns with the findings in [, ]. The effect of input injection is more visible in tasks like Addition, Binary Sum, and Unique Set.

##### Comparison with pause tokens and vanilla FAP.

We find that training with pause tokens in FAP could boost the generalization performance compared to vanilla FAP, but not as effective as our method with looped steps and adaptive depth. As discussed in  [], pause tokens mainly introduce parallel but not sequential compute, which is less powerful than adaptive depth. Besides, we find worse in-distribution accuracy for both FAP and FAP-Pause in Addition, which mainly comes from the difficulty in training a deep model (20$\times$ the depths of the decoder block used in the looped model) in FAP. It further highlights the importance of supervision with variant depths used in our training.

### 6.4 The stopping criterion and visualizations

In this section, we visualize the accuracy and the cross-entropy loss w.r.t. the decoded output in each iterative step across tasks in Figure 6, with the test length to be the maximum length in Figure 4.
We also provide more visualizations from other test lengths in Appendix C.

##### Convergence in Addition, Copy, Multiplication, and Unique Set.

We notice that for Addition, Copy, Multiplication, and Unique Set, the looped model somehow learns to converge for a certain number of steps after solving the task, even though we do not explicitly train the model to converge. The loss curves for these tasks are also smoother than those without convergence behaviors.

##### The maximum confidence stopping criterion chooses the step with near-perfect accuracy.

In Figure 6, the cross-entropy loss reaches the lowest when the generalization performance is near perfect, which indicates the maximum confidence rule chooses the right time to exit. By training with the ground truth number of iterations in the loop, we learn both the length-generalizable iterative steps and when to stop, which is important for looped models.

Figure: (a) Parity (test length=50)
Refer to caption: x6.png

## 7 Limitations and conclusion

Our work has several limitations. Direct looped training could be computationally demanding when the number of looped steps is too large. A possible workaround for more efficient training could be stopping the gradient tracking for earlier steps like [], but there might be a trade-off in performance and computation.
We only train the looped Transformers for a limited number of steps and lengths due to a lack of computing resources. With more diverse training data, the looped model has the potential to generalize to even longer test lengths. We use NoPE for simplicity, and an orthogonal direction is to use more delicate positional embedding to further improve length generalization performance. Moreover, our step-dependent supervision requires the ground-truth number of steps in the training data, which is an additional requirement compared with normal end-to-end training. However, we still require fewer assumptions than CoT training.

In conclusion, we show that $n$-RASP-L problems can be learned by looped Transformer with step-dependent supervision on the final answer, and can be applied with an adaptive number of steps during inference time to improve generalization. Note that $n$-RASP-L, as a challenging algorithmic problem set, could cover more challenging reasoning problems than presented in the paper, and we believe that our method has the potential to generalize to more challenging tasks.

## Appendix A n 𝑛 n italic_n -RASP-L programs

Here we provide the $n$-RASP-L programs for our Parity, Addition and Copy tasks in Listings LABEL:lst:parity, LABEL:lst:copy, LABEL:lst:add.
We also present the RASP-L library functions we use in Listing LABEL:lst:lib, which is partially taken from [].

Figure: Listing 1: Parity.

Figure: Listing 2: Copy.

Figure: Listing 3: Addition (in forward order).

Figure: Listing 4: Library functions from [].

## Appendix B Visualization of using pause tokens in NTP and FAP

We visualize NTP-Pause in Figure 7 and FAP-Pause in Figure 8 respectively, where we add a fixed number of pause tokens (3 in the figures, 20 in our experiments) before outputting the final answer during both training and inference.

Figure: Figure 7: NTP-Pause visualization. Examples are from the Copy task. “…” indicates the pause token.
Refer to caption: x12.png

Figure: Figure 8: FAP-Pause visualization. Examples are from the Copy task. “…” indicates the pause token.
Refer to caption: x13.png

## Appendix C (More) visualizations of the stopping criterion

Here we present more visualization of the stopping criterion in Figure 9 when tested with different lengths from Section 6.4. We can still see similar patterns of convergence in Addition, Copy, Multiplication, and Unique Set. Moreover, the maximum confidence stopping criterion chooses the step with near-perfect accuracy.

Figure: (a) Parity (test length=40)
Refer to caption: x14.png

## Appendix D Inference time complexity

Here we present the inference time complexity for our method, vanilla NTP and vanilla FAP.

Assume that the maximum length of the training set is $n$, the number of steps needed is $T(n)$, and the number of layers in each step is $k$. Assume that NTP and FAP are using a fixed number of layers $C$. And we test on length $n^{\prime}$.

For the first stopping criterion where we know $a(n^{\prime})$, our inference time would be $O(ka(n^{\prime})n^{\prime 2})$, and NTP (with KV cache) and FAP will be $O(Cn^{\prime 2})$. For the second criterion, we need to specify the maximum number of steps in order to find the step with maximum confidence. So our inference time would be $O(kN^{\prime}n^{\prime 2})$, where $N^{\prime}$ is the maximum number of steps.

In NTP and FAP, we use some $C\approx kT(n)$ in our experiments such that they use similar compute during training. Our inference time is then slightly longer than NTP with KV cache and FAP since we use more steps than the fixed-depth models.

Moreover, we provide the inference time (in seconds) in Table 1, where we test on length 50 for Parity with batch size 64. Ours (1) and (2) indicate our first and the second stopping criterion respectively.

**Table 1: Inference time from Parity.**
| Ours (1) | Ours (2) | FAP | FAP-pause | NTP | NTP-pause | NTP (weight-tied) |
| --- | --- | --- | --- | --- | --- | --- |
| 0.1967s | 0.2190s | 0.1117s | 0.1262s | 0.1229s | 0.1315 | 0.1527s |

## Appendix E Experimental details

We use the decoder-only GPT-2 model with NoPE, 8 heads, 256 embedding dimensions as the basic block for the looped iterations with task specific depth in Table 2. We convert the input to the embedding space, perform the loop in the embedding space, and decode the final output after the loop stops. We use a curriculum to gradually increase the maximum training length (see Table 2 for the specific setup for each task). We use AdamW optimizer with a cosine learning rate decay schedule from $10^{-4}$ to 0 after reaching the maximum training length, and train up to 100K gradient steps with batch size 64 for all tasks. For the training distribution, we adopt the online training scheme following [] where each batch is i.i.d. sampled. Given any length, the probability of each possible character is evenly distributed instead of from a finite train set to avoid over-fitting, and the length is also evenly distributed. For input injection, we use a similar technique as [] that adds the original input embedding to each looped block as part of the input. For vanilla NTP, we adopt the same training scheme, but trained with autoregressive loss instead. For NTP-Pause and FAP-Pause, we add 20 pause tokens before outputting the final answer. Each training run takes about 4-6h on NVIDIA A100 40 GB GPU, depending on the maximum training lengths of the problems.

For evaluation, we use $100\times$ the batch size number of samples in Figure 4, 5, 6, 9, and report the mean exact match accuracy and standard error from five training runs with different random seeds.

**Table 2: Task-specific experimental hyperparameters. “Incremental Interval” denotes the number of training steps between successive increases in the input sequence length.**
| Task | Depth of the Decoder Block | Incremental Interval |
| --- | --- | --- |
| Parity | 1 | 1000 |
| Copy | 2 | 1000 |
| Addition | 3 | 1600 |
| Multiplication | 4 | 500 |
| Binary Sum | 3 | 1000 |
| Unique Set | 3 | 1000 |

## Peer Reviews

confidence: 4
contribution: 3
initial_rating: 6
presentation: 4
questions: **Q1. Question on the visualization in Figure 3**

- Why don’t the illustrations in the figure contain any “#” (EOS) tokens? Is it due to the pre-processing?

**Q2. Do the trained Looped Transformers simulate the $n$-RASP-L program?**

- Although it might be difficult to reverse-engineer a trained transformer model to figure out what algorithm it actually simulates or implements, it might be interesting if we can observe any kind of similarity between it and the $n$-RASP-L program.
soundness: 4
strengths: S1. The paper is written and organized well. Overall, the presentation of the methodology and empirical results is clear and easy to follow.

S2. The idea behind the proposed method is neat and plausible. It is natural to think about adaptively scaling the depth of the model according to the problem length or the problem complexity. This paper successfully implements this idea to solve various interesting algorithmic tasks with the power of Looped Transformers. Also, $n$-RASP-L is an interesting but intuitive generalization of the RASP-L framework by allowing the loops. 

S3. The proposed answer-generation framework called FAP is also an interesting component of this work. It might be of separate interest to study.

S4. The paper presents extensive ablation studies on several components of the proposed method. Also, the empirical results (length generalization performances) are impressive enough to convince the readers about the proposed method’s efficacy.
summary: - This work studies the efficacy of Looped Transformers for Length Generalization of several algorithmic tasks whose computation complexity is known (as a function of the query length).
- The paper proposes the definition of $n$-RASP-L, a generalization of the RASP-L computation model allowing the loop of RASP-L programs. It is shown, under a general framework called full-answer prediction (FAP), that some tasks (Copying binary sequence (allowing duplicates), Parity, and Binary Addition) have their own $n$-RASP-L program with a linear number of steps in problem length.
- The authors propose training Looped Transformers (with input injection and curriculum learning) to learn $n$-RASP-L-programmable tasks, where the ground-truth number of steps is known for each task during training. They also propose two variants of inference methods: either we retain the knowledge about the number of steps at inference time (*Oracle*), or we adaptively decide the number of iterations based on the confidence of FAP (*Maximum confidence*).
- The proposed method is tested on several algorithmic tasks.
weaknesses: **W1. The definition of $n$-RASP-L (Definition 3.1) can be improved.**

- I think the equation “$T(n): \mathbb{N} \rightarrow \mathbb{N}$” should be corrected to “$T: \mathbb{N} \rightarrow \mathbb{N}$” because $T$ (instead of $T(n)$) is a function of input length $n$ representing the number of steps inside a task-solving $n$-RASP-L program.
- In (2), I guess $P’$ should be a RASP-L program, which is unspecified in the definition.
- Should $P$ be decomposed to a sequential application of $P’$, i.e., $P = (P’)^{T(n)}$? I don’t think this is exactly true because there are pre-/post-processing parts inside the proposed $n$-RASP-L programs (in Appendix A). Can the same RASP-L program $P’$ handle such parts? (It might be true because of the experimental results, but I cannot fully understand this part.) If not, I guess the definition should be modified to include the pre-/post-processing parts. For example, $P = P_{\tt pre} \circ (P’)^{T(n)} \circ P_{\tt post}$.

**W2. “Ground truth” number of steps?**

- According to Definition 3.1, a program $P$ suffices to be an $n$-RASP-L if a corresponding $T(n)$ exists. Indeed, Propositions 3.2, 3.3, and 3.4 claim and prove the existence of $T(n)$ for the Parity, Copy (with duplicates), and Binary Addition tasks, respectively.
- My question is about the uniqueness or optimality of such $T(n)$’s. There might be a clever way to construct another RASP-L program $\tilde{P}$ so that $P$ can be implemented with $\tilde{T}(n)$ steps of applying $\tilde{P}$, where $\tilde{T}(n)$ is much smaller than the previously known $T(n)$ (e.g., $\tilde{T}(n) \in o(T(n))$). It may happen since there is no uniqueness guarantee or lower bound result on $T(n)$.
    - If I venture a guess, I would say it might be possible to implement an $O(\log n)$-step $n$-RASP-L solution for the Parity task by using the parallelism of the transformer architecture. Please correct me if I am wrong. Also, I understand if it is impossible to show whether this bold guess is true. If you are interested, there are some (probably) useful references about logarithmic-depth transformers [1,2].
- However, the authors keep using the phrase “ground truth number of steps” throughout the paper, which may lead to misunderstanding that the only way to implement the given $n$-RASP-L program is by using a loop of length $T(n)$.
- If two different $T(n)$’s can be applied to a single $n$-RASP-L-programmable task, it might be interesting to observe whether the model’s performance changes depending on the choice of $T(n)$.
- Furthermore, if multiple choices of $T(n)$’s exist for a given task, does knowing only one of them suffice to train reasonably performant Looped Transformers? If we know more than one, how should we choose $T(n)$ when we train the model?

**W3. Shouldn’t we consider the input injection when implementing an $n$-RASP-L program for the given task?**

- The input injection seems to be an important component of their experiments. Since it changes the input vectors of each layer, I guess the task-solving algorithm under input injection might be different from that without it.
- However, I can’t see that the $n$-RASP-L programs provided in Appendix A reflect the input injection. As I inspect inside the loop of each program, every iteration only reuses the calculation(s) from the previous iteration right before the current one.
- Shouldn’t we consider the very first input sequence and the result from the previous iteration when implementing the loops? Or is it a valid implementation of input injection? Getting even further, Is there any way to embed the input injection into the $n$-RASP-L programs?

**W4. The proposed training method requires prior knowledge of the task’s structure.**

- The proposed method is limited in that it requires a prior understanding of the structure (e.g., $T(n)$) of the task where we want to train a model. This is because it hinders fully end-to-end training.
- Are Looped Transformers still useful for achieving length generalization even when we don’t (or cannot) know the exact expression of $T(n)$?
- Besides, it seems that the depth of the decoder block is determined based on the complexity/difficulty of the subroutine $P’$ at each step inside the loop (Appendix F). How are they actually chosen? Or, how should we decide the size of the repeating decoder block?

**W5. Some experimental details seem missing or wrong.**

- I guess Equation (2) has a typo: shouldn’t it be arg-main instead of arg-max?
- In Binary Addition, it seems that $T$ is chosen to be $n$ (the length of each operand). However, Proposition 3.4 claims that $T(n)=n+1$ for the same task. Why is there a discrepancy between theory and experiment?
- In Binary Multiplication, I guess some words are used in a wrong way. In Lines 417-418, I think it should be: “We define the problem length to be the **length** of the second **number**, and set $T$ to be the product of the lengths of two **numbers**.”
- In Section 6.1.2, are input injections also applied to NTP-based methods? Also, I’m not sure why it is fair to compare their method (based on FAP) to NTP methods with the architectural setting “…with a depth 20 times the depth of the looped block” because such depth might be suboptimal for NTP-based methods.
- Although the paper indirectly showcases that their adaptive decision of the number of steps works quite well via Figure 5, it would be better to display similar performance plots to Figure 4 (plots based on the “Oracle” inference) but using the adaptive confidence-based method instead, at least in their appendix.

**W6. Minor writing issues**

- Section 4.1, fourth bullet point: I guess $T(n) \in \\{T(1), \ldots, T(n_{\rm max})\\}$ is correct ($T(1)$ instead of $1$).
- Equations (1) and (2) have several weird-looking brackets (too many open brackets etc.)
- Line 510: Use *less* abbreviations like “w.r.t.”

---

**References**

[1] Sanford, Clayton, et al. "Transformers, parallel computation, and logarithmic depth." ICML 2024.

[2] Sanford, Clayton, et al. "Understanding transformer reasoning capabilities via graph algorithms." NeurIPS 2024.

---

confidence: 3
contribution: 3
initial_rating: 6
presentation: 3
questions: Q1. In Figure 5, why do some tasks perform well even when exceeding the step count, while others degrade immediately? For instance, the performance of the parity task and the binary sum task immediately drops when executed with additional steps, whereas the addition, multiplication, and copy tasks retain accuracy to some extent.
- Particularly for the copy task, the selected step count is significantly higher than the actual number of steps required, which seems unusual to me.

Q2. Are there any tasks whose T(n) is nonlinear (e.g. sqrt(n), n^2) to the length of the input sequence? It would be interesting to see experimental results for such tasks.

Q3. Why is the output reversed for binary multiplication (but not for binary addition)?
soundness: 3
strengths: - The paper is well-structured and clearly written.
- The introduction of Looped Transformers is well-motivated and effectively argued.
- The results are strong and solid. They do not require the use of a scratchpad. Also, the prediction is conducted using an end-to-end, full-answer prediction setup, which is a more general way than the conventional next-token prediction setup.
- The paper clearly illustrates that the model can determine the number of steps to take on its own and does not require T(n) in the test time.
summary: This paper investigates the length generalization problem of Transformer models, which refers to the inability of the model to deal with longer samples than encountered during the training phase. While recent literature has focused on modifying the positional embeddings and the input formats, this paper proposes to use Looped Transformers, which can dynamically adjust their computation steps according to the problem length. The authors define n-RASP-L problems to figure out which problems can be solved by Looped Transformers. Then, they train the models on these tasks (parity, copy, binary addition, binary sum, binary multiplication, unique set) under a full-answer prediction setup. Empirically, the trained models could successfully length-generalize to longer lengths by appropriately adapting the number of loops at inference time.
weaknesses: Weakness 1: Applicability Limited to n-RASP-L Tasks

- The approach is limited to tasks that belong to n-RASP-L categories, as it requires the ground-truth number of steps in the training data.

Weakness 2: Insufficient Experimentation.

- ***Effect of Curriculum Learning.*** How does the model perform without curriculum learning? Is the use of curriculum learning necessary?

- ***Tolerance to Step Counts.*** I am curious whether this method will still perform well with different choices of T(n). For example, for tasks like parity, would the model maintain its performance if T(n) were set to n+1 rather than n? What about 2n instead of n? This question stems from the possibility that there might be more efficient solutions to n-RASP-L problems than human-designed ones, which could work with fewer steps. Testing whether the model is robust under overestimated T(n) values could help verify the robustness of this approach.

- Overall, the paper requires more ablation studies.

---

confidence: 4
contribution: 3
initial_rating: 8
presentation: 3
questions: I listed my questions in the weaknesses section.
soundness: 3
strengths: Overall, I really liked the paper, I think that using a looped transformer to achieve length generalization is an interesting idea that was not studied in the past to my knowledge. This paper complements all the other techniques (universal transformers, different types of position emebedding, etc.) that were used in the past for length generalization The paper is well-written and well-explained. This is why I advocate for acceptance of this paper.
summary: This paper examines how looped transformers perform in terms of length generalization. The focus is on n-RASP-L problems, which are problems that can be tackled using a loop of a single RASP-L program. The concept is that Transformers can learn steps that are independent of length, employing a flexible number of iterations in the looped transformer to achieve length generalization. The authors first demonstrate that n-digit addition, n-bit parity, and copying n symbols can be addressed with n-RASP-L solutions. They then reveal that when utilizing the looped transformer with adaptive stopping time, the results exhibit significantly stronger length generalization compared to next token prediction (NTP) and other methods like using pause tokens or NTP-loop with a fixed stopping time.
weaknesses: I would like to raise the following weaknesses/questions regarding this paper: 

- **Lack of other baselines**: What would happen if you have a very deep universal transformer? Universal transformers also have shared parameters and looks equivalent to the loop transformer. The depth may play the role of the number of loops. Would this be equivalent to the fixed loop NTP? It would be interesting to run the same experiments with a universal transformer.

- **Comparison with other methods**: Where would you position the looped transformers in the list of all the tricks for length generalization? Are the effects similar or complementary to change of the input (index hinting, reverse order of operands, etc.) ? Changes of positional encoding? Chain of Thought? It would be interesting to understand this by making combinations of the tricks with looped transformers with other tricks and analyze the performance differences.

-  What is the depth of the encoder block in the loop transformer? I think this information is important to put in the main paper. 

- **Adaptive inference time**: I think one weak point of the method is actually coming up with an adaptive inference time. The methods that are proposed are nice but may look a bit hacky. Do you think one could learn this adaptive inference time?

- In Figure 2, which adaptive inference time method is used for FAP-Loop-Adaptive?

- Lastly, this is a wild question: have you tried your method on problems where there is no n-RASP-L solutions?  Would it still work better than just doing NTP?

---

confidence: 4
contribution: 2
initial_rating: 6
presentation: 3
questions: 1. Are the quantities reported in Figure 5 indeed for a single training example? When using the maximum confidence criterion, how do the results compare to the ones reported in Figure 4 with access to the ground truth number of steps?

2. In Bansal et al. 2022, they avoid the need for knowing the exact number of steps during training and inference. Have you tried using similar heuristics?
soundness: 4
strengths: 1. The paper is mostly well-written and easy to follow.

2. Demonstrates that, given knowledge of the number of steps required to perform a given task, a certain looped Transformer, which jointly predicts the full output sequence, tends to learn a length-generalizing solution. The length-generalizing capabilities of this looped Transformer are shown to surpass baselines that use next-token prediction.
summary: Empirically explores the ability of looped Transformers, i.e. Transformers that repeatedly apply the same block of layers, to length-generalize on several algorithmic tasks, including copy, parity, and addition. First, the authors manually derive length-generalizing solutions to the considered tasks through a variant of the RASP language, which they term n-RASP-L. Then, based on these ground truth solutions, they show that looped Transformers length-generalize well when trained with access to the true number of steps required to compute the output for a given input.
weaknesses: 1. The main weakness of the current paper is that the significance of the results is somewhat limited. In particular, I find that it falls somewhere in between works that are practically relevant and those that may not be practically relevant, but improve our understanding of certain phenomena. On the one hand, this work shows empirically that, in some algorithmic tasks for which we already know how to write explicitly a length-generalizing solution (in terms of looped Transformer weights), looped Transformers generalize well to longer lengths, if they have access during training to the number of steps required for solving the task for a given input. Consequently, the practical relevance is limited since the proposed method requires that we already know how to manually write a length-generalizing solution, in which case there is arguably no point in learning. On the other hand, this work does not provide much in terms of understanding why or how looped Transformers are able to length-generalize.

    Note that one may consider the demonstration of such length generalization to be possible as a main contribution. Yet, the ability to extrapolate through recurrence of layers has been demonstrated in the past, albeit for other architectures (see Bansal et al. 2022 [1], which notably do not require knowing the ground truth number of steps in training).

2. A related issue is the usage of ground truth stopping time during inference. The quantities reported in Figure 5 seem to be for a single training example, yet it is not entirely clear. If so, then how does the maximum confidence stopping criterion fair across the dataset? It would be useful to report results similar to those of Figure 4 but when using the proposed stopping criterion as opposed to the ground truth stopping time, which should be unknown.

Overall, my assessment of the paper tends towards the positive side, yet it is not a clear accept due to the substantial limitations mentioned above. Specifically, the significance of the contributions can be greatly improved if it would be possible to remove the dependence on knowing the ground truth number of steps required to solve the task for a given input during training (and by how it seems from the current results, during test time as well).


Additional (more minor) comments:
- In Definition 3.1, it seems that the intention is for $P’$ to be some RASP-L program, as opposed to just a program. Otherwise, trivially any program $P$ is an n-RASP-L program by choosing $P’ = P$ and $T(n) = 1$.
- In Equation (2), I believe that the criterion should be an argmin over the cross entropy loss instead of an argmax.


[1] Bansal, A., Schwarzschild, A., Borgnia, E., Emam, Z., Huang, F., Goldblum, M., & Goldstein, T. (2022). End-to-end algorithm synthesis with recurrent networks: Extrapolation without overthinking. Advances in Neural Information Processing Systems, 35, 20232-20242.

Meta-review: This paper studies length generalization in Transformers through the lens of looped architectures, showing that Transformers with repeated layers can effectively generalize to longer sequences when trained appropriately. The reviewers appreciated the paper's clear writing, empirical evaluation, and theoretical framing of n-RASP-L.
While some reviewers raised concerns about the requirement of knowing the number of steps T(n) during training, the authors adequately addressed this through additional experiments and discussion.
I recommend acceptance, as the reviewers found the paper insightful. For the camera-ready version, I strongly suggest incorporating feedback from the discussion (e.g. clarifying the role of T(n), adding the adaptive-stopping-time results, etc).

Decision: Accept (Poster)