Training Nonlinear Transformers for Chain-of-Thought Inference: A Theoretical Generalization Analysis

## Abstract

Abstract Chain-of-Thought (CoT) is an efficient prompting method that enables the reasoning ability of large language models by augmenting the query using multiple examples with multiple intermediate steps. Despite the empirical success, the theoretical understanding of how to train a Transformer to achieve the CoT ability remains less explored. This is primarily due to the technical challenges involved in analyzing the nonconvex optimization on nonlinear attention models. To the best of our knowledge, this work provides the first theoretical study of training Transformers with nonlinear attention to obtain the CoT generalization capability so that the resulting model can inference on unseen tasks when the input is augmented by examples of the new task. We first quantify the required training samples and iterations to train a Transformer model towards CoT ability. We then prove the success of its CoT generalization on unseen tasks with distribution-shifted testing data. Moreover, we theoretically characterize the conditions for an accurate reasoning output by CoT even when the provided reasoning examples contain noises and are not always accurate.
In contrast, in-context learning (ICL), which can be viewed as one-step CoT without intermediate steps, may fail to provide an accurate output when CoT does. These theoretical findings are justified through experiments.

## 1 Introduction

Transformer-based large-scale foundation models, such as GPT-3 (Brown et al., ), GPT-4 (OpenAI, ), LLaMa (Touvron et al., , ), and Sora (Liu et al., ), have demonstrated remarkable success across various tasks, including natural language processing (Brown et al., ; Touvron et al., ), multimodal learning (OpenAI, ; Radford et al., ), and image/video generation (OpenAI, ; Liu et al., ).
What is more surprising is that large language models (LLMs) demonstrate reasoning ability through the so-called “Chain-of-Thought” (CoT) method (Wei et al., ). The objective is to let a pre-trained LLM generate $K$ steps of reasoning given input query ${\bm{x}}_{query}$ without any fine-tuning. To achieve that, the input ${\bm{x}}_{query}$ is augumented with $l$ examples $\{{\bm{x}}_{i},\{{\bm{y}}_{i,j}\}_{j=1}^{K}\}_{i=1}^{l}$ of a certain $K$-step reasoning task, where each ${\bm{x}}_{i}$ is the input with ${\bm{y}}_{i,j}$ as the $j$-th reasoning step, and ${\bm{y}}_{i,K}$ is the final output.
A pre-trained model then takes the resulting augmented input, referred to as a prompt, and outputs the corresponding reasoning steps $\{{\bm{z}}_{j}\}_{j=1}^{K}$ for ${\bm{x}}_{query}$, or simply outputs ${\bm{z}}_{K}$. CoT can be viewed as an extended and more intelligent method than the previous in-context learning (ICL) method, where only input-label pairs $\{{\bm{x}}_{i},{\bm{y}}_{i,K}\}_{i=1}^{l}$ are augmented in the prompt to predict ${\bm{z}}_{K}$ with the pre-trained model.

Inspired by the outstanding empirical performance of CoT in arithmetic reasoning (Wang et al., ; Zhang et al., ; Wang and Zhou, ), symbolic reasoning (Zhang et al., ; Zhou et al., ), and commonsense reasoning (Wang et al., ; Wang and Zhou, ), there have been some recent works (Li et al., ; Feng et al., ; Li et al., ; Yang et al., ; Wen et al., ) on the theoretical understanding of CoT. These works investigate CoT from the perspective of expressive power, i.e., they construct the Transformer architecture that is proven to have the CoT ability. They also demonstrate empirically that supervised training on pairs of CoT prompts and corresponding outputs can lead to models with CoT ability.
However, none of these results theoretically address the question of why a Transformer can obtain generalization-guaranteed CoT ability by training from data with gradient-based methods.
Meanwhile, another line of research (Zhang et al., ; Huang et al., ; Wu et al., ; Li et al., ) aims to unveil the reasons behind the ICL ability of Transformers through characterizing the training dynamics of a Transformer in the supervised setting. These analyses are specifically applicable to ICL.
Therefore, a theoretical question still remains less explored, i.e.,

Why can a Transformer be trained to generalize on multi-step reasoning tasks via CoT?

### 1.1 Major Contributions

Following Li et al. (); Feng et al. (); Li et al. (); Yang et al. (); Wen et al. (),
we train the model in a supervised setting using prompt and label pairs. This paper provides the first theoretical analysis of the training dynamics of nonlinear Transformers to achieve CoT ability. We prove that the learned model has guaranteed CoT ability for new tasks with distribution shifts from the training tasks, even when there exist noisy and erroneous context examples in the prompt. We theoretically characterize the required number of training samples and iterations needed to train a desirable model and the number of context examples required for successful CoT reasoning with a generalization guarantee. Moreover, we provide a theoretical explanation for why CoT outperforms ICL in some cases. Our main technical contributions are as follows:

1. A quantitative analysis of how the training can enable the CoT ability: We theoretically analyze the training dynamics on a one-layer single-head attention-only Transformer and quantify the required number of context examples in each training sample, the total number of training samples, and the number of training iterations needed to acquire CoT ability. We illustrate that the CoT ability results from the property that the attention values of the learned model are concentrated on testing context examples with the same input patterns as the testing query during each reasoning step.

2. A quantitative analysis of how context examples affect CoT performance: We characterize the required number of context examples in the testing prompt for successful CoT reasoning when noise and error exist in contexts. Our quantitative bounds are consistent with the intuition that more accurate context examples and more similar examples to the query improve CoT accuracy.

3. A theoretical characterization of why CoT outperforms ICL: We provide a quantitative analysis of the requirements for successful ICL reasoning with our studied trained model. We show that successful ICL requires an additional condition that the prompt has a dominant number of correct input-label examples, while the success of CoT does not depend on this condition. This can be viewed as one of the possible reasons why CoT outperforms ICL.

### 1.2 Related works

Expressive power of CoT Li et al. () proves the existence of a Transformer that can learn a multi-layer perceptron (MLP). They interpret CoT as first filtering important tokens and then making predictions by ICL. They also establish the required number of context examples for a desired prediction with the constructed Transformer. Feng et al. (); Li et al. (); Merrill and Sabharwal () show that Transformers with CoT are more expressive than Transformers without CoT. Yang et al. (); Wen et al. () show the superiority of standard Transformers in some reasoning tasks compared with recurrent neural networks and linear Transformers.

Theoretical analysis of ICL As a simplified one-step version of CoT, ICL has gained much attention from the theoretical community. Garg et al. (); Akyürek et al. (); Bai et al. (); Guo et al. () demonstrate that Transformers are expressive to conduct many machine learning algorithms in context. Akyürek et al. (); Von Oswald et al. (); Ahn et al. (); Cheng et al. (); Ding et al. () especially show the existence of Transformers to implement gradient descent and its variants with different input prompts. Zhang et al. (); Huang et al. (); Wu et al. (); Li et al. () explore the training dynamics and generalization of ICL on single-attention Transformers. Cui et al. (); Chen et al. () provably show the superiority of multi-head attention over single-head attention to achieve ICL ability.

Training and Generalization of Transformers There have been several recent works about the optimization and generalization analysis of Transformers. Jelassi et al. (); Li et al. (); Oymak et al. (); Li et al. (, , ); Luo (); Huang et al. (); Zhang et al. () study the generalization of one-layer Transformers by assuming spatial association, semantic/contextual structure, or the majority voting of tokens in the data. Oymak et al. (); Tarzanagh et al. (, ); Tian et al. (, ); Li et al. (); Ildiz et al. (); Nichani et al. (); Makkuva et al. () investigate the training dynamics or loss landscape of Transformers for the next token prediction by assuming infinitely long input sequences, causal structure/Markov Chain of data, or a proper prediction head. Deora et al. (); Chen and Li () analyze the optimization and generalization of multi-head attention networks.

## 2 Problem Formulation

We study the problem of learning and generalization of $K$-steps reasoning tasks. Each task $f=f_{K}\circ\cdots f_{2}\circ f_{1}$ is a composition of functions $\{f_{i}\}_{i=1}^{K}$ and outputs labels ${\bm{z}}_{1},{\bm{z}}_{2},\cdots,{\bm{z}}_{K}$ for the input ${\bm{x}}_{query}$. During the $k$-th reasoning step, $k\in[K]$, the label is ${\bm{z}}_{k}=f_{k}({\bm{z}}_{k-1})$, where ${\bm{z}}_{0}:={\bm{x}}_{query}$.

### 2.1 Training to acquire the Chain-of-Thought ability

Following theoretical analysis (Feng et al., ; Li et al., ; Wen et al., ) and empirical works like process supervision (Lightman et al., ), we first investigate the training on a Transformer model to obtain the CoT ability in evaluating new data and tasks. It is a supervised learning setting on pairs of prompts and labels. Different from the testing prompt that includes examples and only ${\bm{x}}_{query}$, the training prompt includes multiple $K$-steps reasoning examples and a $(k-1)$-step reasoning of ${\bm{x}}_{query}$ for any $k$ in $[K]$, and the label for this prompt is ${\bm{z}}_{k}$. Specifically,

Training Prompt and Label for CoT. For every prompt and output pair from a task $f=f_{K}\circ\cdots f_{2}\circ f_{1}$, we construct a prompt ${\bm{P}}$ that include the query input ${\bm{z}}_{k-1}$ by prepending $l_{tr}$ reasoning examples and the first $k-1$ steps of the reasoning query. The prompt ${\bm{P}}$ of the query input ${\bm{z}}_{k-1}$ is formulated as:

$$ $\displaystyle{\bm{P}}=$ $\displaystyle\begin{pmatrix}{\bm{E}}_{1},{\bm{E}}_{2},\cdots,{\bm{E}}_{l_{tr}} ,{\bm{Q}}_{k}\end{pmatrix}\in\mathbb{R}^{2d_{\mathcal{X}}\times(l_{tr}K+k)},$ (1) $\displaystyle\text{where }{\bm{E}}_{i}=$ $\displaystyle\begin{pmatrix}{\bm{x}}_{i}&{\bm{y}}_{i,1}&\cdots&{\bm{y}}_{i,K-1 }\\ {\bm{y}}_{i,1}&{\bm{y}}_{i,2}&\cdots&{\bm{y}}_{i,K}\\ \end{pmatrix},\ {\bm{Q}}_{k}=\begin{pmatrix}{\bm{z}}_{0}&{\bm{z}}_{1}&\cdots&{ \bm{z}}_{k-2}&{\bm{z}}_{k-1}\\ {\bm{z}}_{1}&{\bm{z}}_{2}&\cdots&{\bm{z}}_{k-1}&\bm{0}\\ \end{pmatrix},i\in[l_{tr}],$ $$

where ${\bm{E}}_{i}$ is the $i$-th context example, and ${\bm{Q}}_{k}$ is the first $k$ steps of the reasoning query for any $k$ in $[K]$. We have ${\bm{y}}_{i,k}=f_{k}({\bm{y}}_{i,k-1})$ and ${\bm{z}}_{k}=f_{k}({\bm{z}}_{k-1})$ for $i\in[l_{tr}]$, $k\in[K]$ with a notation ${\bm{y}}_{i,0}:={\bm{x}}_{i}$. Let ${\bm{p}}_{s}$ and ${\bm{p}}_{query}$ be the $s$-th column and the last column of ${\bm{P}}$, respectively, for $s\in[l_{tr}K+k-1]$. ${\bm{x}}_{i},{\bm{y}}_{i,k},{\bm{z}}_{j}\in\mathbb{R}^{d_{\mathcal{X}}}$ for $i\in[l_{tr}]$ and $j,k\in[K]$. We respectively call ${\bm{x}}_{i}$ and ${\bm{y}}_{i,k}$ context inputs and outputs of the $k$-th step of the $i$th context example. For simplicity of presentation, we denote ${\bm{z}}$ as the label of ${\bm{P}}$, which is indeed ${\bm{z}}_{k}$ for (1). All the notations are summarized in Table 1 in Appendix.

The learning model is a single-head, one-layer attention-only Transformer. We consider positional encoding $\{{\bm{c}}_{k}\}_{k=1}^{K}\in\mathbb{R}^{2d_{\mathcal{X}}}$. Following theoretical works (Jelassi et al., ; Huang et al., ; Ildiz et al., ), we add the positional encoding to each ${\bm{p}}_{i}$ by $\tilde{{\bm{p}}}_{i}={\bm{p}}_{i}+{\bm{c}}_{(i\mod K)}$, $i\in[K(l_{tr}+1)]$. $\tilde{{\bm{p}}}_{query}$ is also defined by adding the corresponding ${\bm{c}}_{k}$ to ${\bm{p}}_{query}$. Mathematically, given a prompt ${\bm{P}}$ defined in (1) with $\text{len}(P)$ (which is at most $K(l_{tr}+1)$) denoting the number of columns, it can be written as

$$ $\displaystyle F(\Psi;{\bm{P}})=\sum_{i=1}^{\text{len}(P)-1}{\bm{W}}_{V}\tilde{ {\bm{p}}}_{i}\cdot\text{softmax}(({\bm{W}}_{K}\tilde{{\bm{p}}}_{i})^{\top}{\bm {W}}_{Q}\tilde{{\bm{p}}}_{query}),$ (2) $$

where ${\bm{W}}_{Q},{\bm{W}}_{K}\in\mathbb{R}^{m\times(2d_{\mathcal{X}})}$, ${\bm{W}}_{V}\in\mathbb{R}^{d_{\mathcal{X}}\times(2d_{\mathcal{X}})}$ are the embedding matrices for queries, keys, and values, respectively. $\Psi:=\{{\bm{W}}_{Q}$, ${\bm{W}}_{K},{\bm{W}}_{V}\}$ is the set of all model weights(^1^11We focus on a one-layer single-head Transformer motivated by recent advancements and current state in Transformer and CoT analysis. Please see Appendix A.1 for discussion.). Typically, $m>2d_{\mathcal{X}}$.

The training problem to enhance the reasoning capability solves the empirical risk minimization,

$$ $\min_{\Psi}R_{N}(\Psi):=\frac{1}{N}\sum_{n=1}^{N}\ell(\Psi;{\bm{P}}^{n},{\bm{z }}^{n}),$ (3) $$

using $N$ prompt and label pairs $\{{\bm{P}}^{n},{\bm{z}}^{n}\}_{n=1}^{N}$.
For the $n$-th sample, ${\bm{x}}_{query}^{n}$ and the context input ${\bm{x}}_{i}^{n}$ are all sampled from an unknown distribution $\mathcal{D}$, the training task $f^{n}$ is sampled from $\mathcal{T}$, $k$ is randomly selected from 1 to $K$, and ${\bm{P}}^{n}$ is constructed following (1). The loss function is squared loss, i.e., $\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})=1/2\cdot\|{\bm{z}}^{n}-F(\Psi;{\bm{P}}^{n
})\|^{2}$, where $F(\Psi;{\bm{P}}^{n})$ is defined in (2).

### 2.2 Training Algorithm

For simplicity of analysis, we let ${\bm{W}}={\bm{W}}_{K}^{\top}{\bm{W}}_{Q}$ and ${\bm{W}}_{V}=(\bm{0}_{d_{\mathcal{X}}\times d_{\mathcal{X}}}\ {\bm{I}}_{d_{
\mathcal{X}}})\in\mathbb{R}^{d_{\mathcal{X}}\times(2d_{\mathcal{X}})}$ as (Jelassi et al., ; Huang et al., ; Zhang et al., ; Huang et al., ). Let $\{{\bm{c}}_{k}\}_{k=1}^{K}$ be a set of orthonormal vectors. The model is trained using stochastic gradient descent (SGD) with step size $\eta$ with batch size $B$, summarized in Algorithm 1 in Appendix B. Each entry of ${\bm{W}}^{(0)}$ is generated from $\mathcal{N}(0,\xi^{2})$ for a tiny $\xi>0$. ${\bm{W}}_{V}$ is fixed during the training. The fraction of prompts with ${\bm{z}}_{k-1}$ as the query input is $1/K$ by uniform sampling for any $k\in[K]$ in each batch.

### 2.3 Chain-of-Thought Inference

We then consider another $K$-steps reasoning task $f\in\mathcal{T}^{\prime}$, whose target is to predict labels $\{{\bm{z}}_{k}\}_{k=1}^{K}$ given the input query ${\bm{x}}_{query}$. $\mathcal{T}^{\prime}$ is the set of testing tasks, and $\mathcal{T}^{\prime}\neq\mathcal{T}$.

Testing Prompt for CoT. The testing prompt ${\bm{P}}$ is composed of $l_{ts}\ (\leq l_{tr})$ context examples of $K$ steps plus a query, which is constructed as

$$ ${\bm{P}}=({\bm{E}}_{1},{\bm{E}}_{2},\cdots,{\bm{E}}_{l_{ts}},{\bm{p}}_{query}) \in\mathbb{R}^{(2d_{\mathcal{X}})\times(l_{ts}K+1)},{\bm{p}}_{query}=({\bm{x}} _{query}^{\top},\bm{0}^{\top})^{\top},$ (4) $$

where ${\bm{E}}_{i}$ follows the form in (1) for $i\in[l_{ts}]$.

We follow the CoT-I/O scheme formulated in (Li et al., ; Feng et al., ; Li et al., ; Yang et al., ; Park et al., ) as the inference method. Specifically, for a $K$-step CoT with $l_{ts}$ examples on a certain $f\in\mathcal{T}^{\prime}$, given the testing prompt ${\bm{P}}$ defined in (4), let ${\bm{P}}_{1}={\bm{P}}$ and ${\bm{P}}_{0}$ be the first $K\cdot l_{ts}$ columns of ${\bm{P}}$. When we use CoT prompting for prediction in the $k$-th step, we first generate the output ${\bm{v}}_{k}$, $k\in[K]$ via greedy decoding by feeding the $k$-th step prompt ${\bm{P}}_{k}$ to the trained model $\Psi$ obtained from (3). The greedy decoding scheme means outputting the most probable token from the discrete set $\mathcal{Y}$ of all possible outputs, as stated in (5).

$$ ${\bm{v}}_{k}=\arg\min_{{\bm{u}}\in\mathcal{Y}}\frac{1}{2}\|F(\Psi;{\bm{P}}_{k} )-{\bm{u}}\|^{2},\text{ (greedy decoding)}$ (5) $$

Then, we use the output ${\bm{v}}_{k}$ to update ${\bm{P}}_{k}$ and use ${\bm{v}}_{k}$ as the query input to form the input prompt ${\bm{P}}_{k+1}$ for the next step, which is computed as

$$ $\displaystyle{\bm{P}}_{k}=\begin{pmatrix}{\bm{P}}_{k-1}\ {\bm{q}}_{k}\end{ pmatrix}\in\mathbb{R}^{(2d_{\mathcal{X}})\times(Kl_{ts}+k)},\ {\bm{P}}_{k+1}= \begin{pmatrix}{\bm{P}}_{k}\ {\bm{q}}_{k+1}\end{pmatrix}\in\mathbb{R}^{(2d_{ \mathcal{X}})\times(Kl_{ts}+k+1)},$ (6) $\displaystyle\text{where }{\bm{q}}_{k}=\begin{pmatrix}{\bm{v}}_{k-1}^{\top}\ { \bm{v}}_{k}^{\top}\end{pmatrix}^{\top},\ {\bm{q}}_{k+1}=\begin{pmatrix}{\bm{v} }_{k}^{\top}\ \bm{0}^{\top}\end{pmatrix}^{\top},$ $$

where ${\bm{q}}_{k}$ is the $k$-th step reasoning column for the query. The model finally outputs ${\bm{v}}_{1},\cdots,{\bm{v}}_{K}$ as CoT result for query ${\bm{x}}_{query}$ by (5). The CoT process is summarized in Algorithm 2 of Appendix B.

When $K\geq 2$, following (Li et al., ; Feng et al., ; Li et al., ; Yang et al., ), the CoT generalization error given the testing query ${\bm{x}}_{query}$, the testing data distribution $\mathcal{D}^{\prime}$, and the labels $\{{\bm{z}}_{k}\}_{k=1}^{K}$ on a $K$-steps testing task $f\in\mathcal{T}^{\prime}$ is defined as

$$ $\bar{R}_{CoT,{\bm{x}}_{query}\sim\mathcal{D}^{\prime},f\in\mathcal{T}^{\prime} }^{f}(\Psi)=\mathbb{E}_{{\bm{x}}_{query}\sim\mathcal{D}^{\prime}}\left[\frac{1 }{K}\sum_{k=1}^{K}\mathbbm{1}[{\bm{z}}_{k}\neq{\bm{v}}_{k}]\right],$ (7) $$

which measures the average error between the output and the label of each reasoning step. A zero CoT generalization error indicates correct generations in all $K$ steps.

### 2.4 In-Context Learning Inference

The ICL inference on a $K$-steps reasoning task $f\in\mathcal{T}^{\prime}$ only predicts the final-step label by perpending examples of input and label pairs before the query. ICL can be viewed as a one-step CoT without intermediate steps. Here, we evaluate the ICL performance of the trained model.

Testing Prompt for ICL. Mathematically, ICL is implemented by constructing a prompt ${\bm{P}}$ as below,

$$ ${\bm{P}}=({\bm{E}}_{1},\cdots,{\bm{E}}_{l_{ts}},{\bm{p}}_{query}),\text{where }{\bm{p}}_{query}=\begin{pmatrix}{\bm{x}}_{query}\\ \bm{0}\end{pmatrix},{\bm{E}}_{i}=\begin{pmatrix}{\bm{x}}_{i}&\bm{0}&\cdots&\bm {0}\\ {\bm{y}}_{i,K}&\bm{0}&\cdots&\bm{0}\\ \end{pmatrix}$ (8) $$

${\bm{P}}\in\mathbb{R}^{(2d_{\mathcal{X}})\times(l_{ts}K+1)}$, ${\bm{E}}_{i}\in\mathbb{R}^{(2d_{\mathcal{X}})\times K}$ for $i\in[l_{ts}]$. Note that in the ICL setting, ${\bm{E}}_{i}$ only has input ${\bm{x}}_{i}$ and the $K$-step output ${\bm{y}}_{i,K}$ but does not include any intermediate labels. We pad zeros in ${\bm{E}}_{i}$ so that its dimension is the same as ${\bm{E}}_{i}$ in (1) for the inference with the same model as for CoT. The ICL output is ${\bm{v}}=\arg\min_{{\bm{u}}\in\mathcal{Y}}\frac{1}{2}\|F(\Psi;{\bm{P}})-{\bm{u
}}\|^{2}$, following (5). The ICL generalization error is

$$ $\bar{R}_{ICL,{\bm{x}}_{query}\sim\mathcal{D}^{\prime},f\in\mathcal{T}^{\prime} }^{f}(\Psi)=\mathbb{E}_{{\bm{x}}_{query}\sim\mathcal{D}^{\prime}}\left[ \mathbbm{1}[{\bm{z}}_{K}\neq{\bm{v}}]\right],$ (9) $$

which measures the error between the one-step reasoning output and the final step label.

## 3 Theoretical Results

We first summarize the main theoretical insights in Section 3.1. Then, we introduce the formulation of data and tasks in Section 3.2. Sections 3.3, 3.4, and 3.5, respectively characterize the training analysis of the Transformer and generalization using CoT and ICL with the trained model.

### 3.1 Main Theoretical Insights

We consider the setup that the model is trained using samples generated from tasks in $\mathcal{T}$ that operate on $M$ orthonormal training-relevant (TRR) patterns, while both CoT and ICL are evaluated on tasks in $\mathcal{T}^{\prime}$ that operate on $M^{\prime}$ orthonormal testing-relevant (TSR) patterns that belong to the span of TRR patterns. We consider the general setup that the context examples in the prompt for CoT and ICL testing are both noisy, i.e., TSR patterns with additive noise, and partially inaccurate, i.e., the reasoning in some examples contains incorrect steps. Our main insights are as follows.

P1. Training Dynamics of Nonlinear Transformer towards CoT. We theoretically analyze the training dynamics on a one-layer single-head attention-only Transformer to acquire the CoT generalization ability and characterize the required number of training samples and iterations. Theorem 1 shows that to learn a model with guaranteed CoT ability, the required number of context examples in each training sample and the total number of training samples/iterations are linear in $\alpha^{-1}$ and $\alpha^{-2}$, respectively, where $\alpha$ is the fraction of context examples with inputs that share the same TRR patterns as the query. This is consistent with the intuition that the CoT performance is enhanced if more context examples are similar to the query.
Moreover, the attention values of the learned model are proved to be concentrated on testing context examples that share similar input TSR patterns as the testing query during each of the reasoning steps (Proposition 1), which is an important property that leads to the success of the CoT generalization.

P2. Guaranteed CoT Generalization. To achieve zero CoT error on tasks in $\mathcal{T}^{\prime}$ with the learned model, Theorem 2 shows that the required number of context examples, where noise and errors are present, for task $f$ in the testing prompt is proportional to ${(\alpha^{\prime}\tau^{f}\rho^{f})}^{-2}$, where $\alpha^{\prime}$ is the fraction of context examples with inputs that share the same TSR patterns as the query, the constant $\tau^{f}$ in $(0,1)$ measures the fraction of accurate context examples, and a larger constant $\rho^{f}$ in $(0,1)$ reflects a higher reasoning accuracy in each step of the examples. This result formally characterizes the intuition that more accurate context examples and more similar examples to the query improve the CoT accuracy.

P3. CoT outperforms ICL. In Theorem 3, We theoretically show that the required number of testing context examples for ICL to be successful has a similar form to that for CoT in Theorem 2, but with an additional requirement (Condition 1) that the fraction of correct input-label examples in the testing prompt must be dominant. Because not all testing cases satisfy this requirement, our result provides one explanation for why CoT sometimes outperforms ICL.

### 3.2 The Formulation of Data and Tasks

Training data and tasks: Consider $M$ training-relevant (TRR) patterns ${\bm{\mu}}_{1},{\bm{\mu}}_{2},\cdots,{\bm{\mu}}_{M}$, which form an orthonormal set $\mathcal{M}=\{{\bm{\mu}}_{i}\}_{i=1}^{M}$. $M=\Theta(d),M\leq d$. $({\bm{\mu}}_{i}^{\top},0_{d_{\mathcal{X}}}^{\top})^{\top}\perp{\bm{c}}_{k}$ for $i\in[M^{\prime}],k\in[K]$.

Every training prompt ${\bm{P}}$ in (1) contains the query and training examples from the same training task $f$ in the set of training tasks $\mathcal{T}$.
Specifically, each training task $f$ is a composition of $K$ functions $f=f_{K}\circ\cdots\circ f_{2}\circ f_{1}$ where each function $f_{k}$ belongs to a function set $\mathcal{F}$.
The $k$-th step label of the query is ${\bm{z}}_{k}=f_{k}({\bm{z}}_{k-1})$ given the $k$-th step input ${\bm{z}}_{k-1}$ with ${\bm{z}}_{k}\in\mathcal{M}$, $k\in[K]$. Moreover, the $k$-th step label of the $i$-th ($i\in[l_{tr}]$) context example is ${\bm{y}}_{i,k}=f_{k}({\bm{y}}_{i,k-1})$ given the $k-1$th step input ${\bm{y}}_{i,k-1},k\in[K]$ with ${\bm{x}}_{i},{\bm{y}}_{i,k}\in\mathcal{M}$, where ${\bm{y}}_{i,0}:={\bm{x}}_{i}$(^2^22The formulation of $f$ is motivated by recent theoretical works on model training or ICL with Transformers. Please see Appendix A.2 for details.).We assume that $f_{k}({\bm{x}})\neq f_{k^{\prime}}({\bm{x}}^{\prime})$ if and only if either ${\bm{x}}\neq{\bm{x}}^{\prime}$ or $f_{k}\neq f_{k^{\prime}}$.

Training prompt:
Consider a training prompt ${\bm{P}}$ on task $f\in\mathcal{T}$ defined in (1) with the query input ${\bm{z}}_{k-1},k\in[K]$. Let $\alpha\in(0,1-c]$ for some constant $c>0$(^3^33This is to prevent the trivial case that the model only learns the positional encoding but not the TRR patterns when $\alpha$ becomes arbitrarily close to $1$.) denote the fraction of context examples with input sharing the same TRR pattern as the query input.

Testing task and query: Consider $M^{\prime}$ testing-relevant (TSR) patterns ${\bm{\mu}}_{1}^{\prime},{\bm{\mu}}_{2}^{\prime},\cdots,{\bm{\mu}}_{M}^{\prime}$, which form an orthonormal set $\mathcal{M}^{\prime}=\{{\bm{\mu}}_{i}^{\prime}\}_{i=1}^{M^{\prime}}$. $M^{\prime}\leq M$. We also have ${\bm{\mu}}_{i}^{\prime}\perp{\bm{c}}_{k}$ for $i\in[M^{\prime}],k\in[K]$. Let $\mathcal{T}^{\prime}$ denote the set of testing tasks, which all operate on patterns in $\mathcal{M}^{\prime}$ rather than $\mathcal{M}$ in training tasks in $\mathcal{T}$.
Every testing task $f=f_{K}\circ\cdots f_{2}\circ f_{1}\in\mathcal{T}^{\prime}$ is a composition of $K$ functions. The reasoning for the testing query is considered to be noiseless and accurate. That means,

${\bm{z}}_{k}\in\mathcal{M}^{\prime}$ for all $k\in\{0\}\cup[K]$, and ${\bm{z}}_{k}=f_{k}({\bm{z}}_{k-1})$, ${\bm{z}}_{0}={\bm{x}}_{query}$.

Testing prompt: We consider the general setup that testing examples are noisy and erroneous. By noisy examples, we mean all inputs and outputs of each step are noisy versions of TSR patterns, i.e.,

$$ ${\bm{x}}_{i},{\bm{y}}_{i,k}\in\{{\bm{b}}\in\mathbb{R}^{d}|{\bm{b}}={\bm{\mu}}_ {j}^{\prime}+{\bm{\delta}},j\in[M^{\prime}],{\bm{\delta}}\perp\mathcal{M}^{ \prime},\|{\bm{\delta}}\|\leq\sqrt{2}/2\},$ (10) $$

with noise ${\bm{\delta}}\neq 0$ for $i\in[Kl_{ts}^{f}]$, $k\in[K]$. Denote $\text{TSR}:\mathbb{R}^{d}\mapsto\mathbb{Z}^{+}$ as a function that outputs the index of the TSR pattern of the noisy input. We consider the case that at least an $\alpha^{\prime}$ fraction of context examples where the TSR pattern of the input ${\bm{y}}_{s,1},s\in[l_{ts}^{f}]$ is the same as ${\bm{x}}_{query}$.

By erroneous examples, we mean that the reasoning steps in test examples may contain errors. To formally model this, we define the step-wise transition matrices
$\{{\bm{A}}_{k}^{f}\}_{k=1}^{K}\in\mathbb{R}^{M^{\prime}\times M^{\prime}}$ such that ${\bm{A}}_{k}^{f}$ represents the reasoning probabilities of step $k$ in test examples. Specifically, there exists some constant $\rho^{f}$ in $(0,1)$ such that
for all $s\in[l_{ts}^{f}]$, $k\in[K]$, the $i,j$-th entry of ${\bm{A}}_{k}^{f}$ satisfies

$$ $\displaystyle A^{f}_{k(i,j)}=\Pr(\text{TSR}({\bm{y}}_{s,k})=j|\text{TSR}({\bm{ y}}_{s,k-1})=i),$ (11) $\displaystyle\textrm{ and }A^{f}_{k(i,j^{*})}$ $\displaystyle\geq 1/(1-\rho^{f})\cdot A^{f}_{k(i,j)},\forall j\in[M^{\prime}], \textrm{ where }{\bm{\mu}}_{j^{*}}^{\prime}=f_{k}({\bm{\mu}}_{i}^{\prime}),$ $$

Figure: Figure 1: An example of a two-step inference
Refer to caption: x1.png

Note that (11)
characterizes a general case in inference that for any given $k$, in the $k$-th reasoning step of the test example, the $k$-th step output is a noisy version of the true label with the highest probability, which guarantees that the examples are overall informative in the $k$-th step. This requirement is intuitive because otherwise, these examples would overall provide inaccurate information on the $k$-th step reasoning. Moreover, (11) models the general case that, with some probability, the $k$-step reasoning is inaccurate in the examples. $\rho^{f}$ is referred to as the primacy of the step-wise transition matrices. $\rho^{f}$ reflects the difference in the probability of correct reasoning and incorrect reasoning in each step, and a larger $\rho^{f}$ indicates a larger probability of accurate reasoning.

Let ${\bm{B}}^{f}=\prod_{k=1}^{K}{\bm{A}}^{f}_{k}$ be the $K$-step transition matrix. Then ${\bm{B}}^{f}_{(i,j)}$ is the probability that the $K$-th step output is a noisy version of ${\bm{\mu}}_{j}^{\prime}$, when the input is a noisy version of ${\bm{\mu}}_{i}^{\prime}$ in the testing example. We similarly define $\rho_{o}^{f}$ in $(0,1)$ as the primacy of ${\bm{B}}^{f}$, where

$$ $B^{f}_{(i,j^{*})}\geq 1/(1-\rho_{o}^{f})\cdot B^{f}_{(i,j)},\ \forall j\in[M^{ \prime}],\ j^{*}=\arg\max_{j\in[M^{\prime}]}B^{f}_{(i,j)}.$ (12) $$

###### Example 1 .

Consider a simple two-step inference example with $K=2$, ${\bm{\mu}}_{1}^{\prime}$, ${\bm{\mu}}_{2}^{\prime}$ as the TSR pattern, and ${\bm{\delta}}=0$ in inputs and outputs of every step, as shown in Figure 1. The black solid arrows denote the correct inference process, where $f_{1}({\bm{\mu}}_{1}^{\prime})={\bm{\mu}}_{1}^{\prime}$, $f_{1}({\bm{\mu}}_{2}^{\prime})={\bm{\mu}}_{2}^{\prime}$, $f_{2}({\bm{\mu}}_{1}^{\prime})={\bm{\mu}}_{2}^{\prime}$, and $f_{1}({\bm{\mu}}_{2}^{\prime})={\bm{\mu}}_{1}^{\prime}$. Hence, ${\bm{\mu}}_{1}^{\prime}\rightarrow{\bm{\mu}}_{1}^{\prime}\rightarrow{\bm{\mu}}
_{2}^{\prime}$ and ${\bm{\mu}}_{2}^{\prime}\rightarrow{\bm{\mu}}_{2}^{\prime}\rightarrow{\bm{\mu}}
_{1}^{\prime}$ are two inference sequences under the function $f$. The testing examples contain errors and follow the transition matrices ${\bm{A}}_{1}^{f}$ and ${\bm{A}}_{2}^{f}$ (brown dashed arrows). We let ${\bm{A}}_{1}^{f}=\begin{pmatrix}0.6&0.4\\
0.4&0.6\end{pmatrix}$, ${\bm{A}}_{2}^{f}=\begin{pmatrix}0.4&0.6\\
0.8&0.2\end{pmatrix}$, which results in ${\bm{B}}^{f}=\begin{pmatrix}0.56&0.44\\
0.64&0.36\end{pmatrix}$.

###### Example 1 .

### 3.3 The Sample Complexity Analysis of the Training Stage

We first characterize the convergence and the testing performance of the model during the training stage with sample complexity analysis in Theorem 1.

###### Theorem 1 .

For any $\epsilon>0$, when (i) the number of context examples in every training sample is

$$ $l_{tr}\geq\Omega(\alpha^{-1}),$ (13) $$

(ii) the number of iterations satisfies

$$ $T\geq\Omega(\eta^{-1}\alpha^{-2}K^{3}\log\frac{K}{\epsilon}+\eta^{-1}MK(\alpha ^{-1}+\epsilon^{-1})),$ (14) $$

and (iii) the training tasks and samples are selected such that every TRR pattern is equally likely in every inference step and in each training batch(^4^44Our analysis assumes that the whole set of $\mathcal{M}$ is achievable uniformly in each step and training batch. This condition is to ensure a balanced gradient update among all TRR patterns, as used in (Li et al., ) for ICL.) with batch size $B\geq\Omega(\max\{\epsilon^{-2},M\}\cdot\log M)$, the step size $\eta<1$ and $N=BT$ samples, then with a high probability, the returned model guarantees

$$ $\mathbb{E}_{{\bm{x}}_{query}\in\mathcal{M},f\in\mathcal{T}}\left[\ell(\Psi;{ \bm{P}},{\bm{z}})\right]\leq\mathcal{O}(\epsilon).$ (15) $$

Theorem 1 indicates that with long enough training prompts and a sufficient number of iterations and samples for training, a one-layer Transformer can achieve a diminishing loss of $\mathcal{O}(\epsilon)$ on data following the same distribution as training examples. The results indicate that (i) the required number of context examples is proportional to $\alpha^{-1}$; (ii) the required number of iterations and samples increases as $M$ and $\alpha^{-2}$ increases. As a sanity check, these bounds are consistent with the intuition that it will make the training stage more time- and sample-consuming if the number of TRR patterns increases or the fraction of prompt examples that share the same TRR pattern as the query decreases.

###### Theorem 1 .

### 3.4 CoT generalization guarantee

In this section, we first define two quantities, $\tau^{f}$, and $\tau_{o}^{f}$ for each testing task $f\in\mathcal{T}^{\prime}$ based on the formulation of testing data and tasks in Section 3.2. These two quantities are used to characterize the CoT and ICL generalization in Theorems 2 and 3, respectively.

###### Definition 1 .

For $f=f_{K}\circ\cdots f_{1}\in\mathcal{T}^{\prime}$, we define the min-max trajectory transition probability as:

$$ $\tau^{f}=\underset{i\in[M^{\prime}]}{\min}\prod_{k=1}^{K}A_{k(\text{TSR}(f_{k- 1}\circ\cdots f_{0}({\bm{\mu}}_{i}^{\prime})),\text{TSR}(f_{k}\circ\cdots f_{0 }({\bm{\mu}}_{i}^{\prime})))}^{f},\text{ where }f_{0}({\bm{\mu}}_{i}^{\prime}) :={\bm{\mu}}_{i}^{\prime},\forall\ i\in[M^{\prime}],$ (16) $$

which measures the minimum probability of the most probable $K$-step reasoning trajectory over the initial TSR pattern.
We also define the min-max input-label transition probability as

$$ $\tau_{o}^{f}=\underset{i\in[M^{\prime}]}{\min}\underset{j\in[M^{\prime}]}{\max }B_{i,j}^{f},$ (17) $$

which measures the minimum probability of the most probable output over the initial TSR pattern.

For instance, in Example 1 after (12), $\tau^{f}=\min\{0.36,0.48\}=0.36$, $\tau_{o}^{f}=\min\{0.56,0.64\}=0.56$.

###### Theorem 2 (CoT generalization) .

Given a trained model that satisfies conditions (i) to (iii) in Theorem 1, as long as (iv)

$$ ${\bm{\mu}}_{j}^{\prime}\in\text{span}({\bm{\mu}}_{1},{\bm{\mu}}_{2},\cdots,{ \bm{\mu}}_{M}),$ (18) $$

for $j\in[M^{\prime}]$, and (v) the number of testing examples for every task $f\in\mathcal{T}^{\prime}$ is

$$ $l_{ts}^{f}\geq\Omega((\alpha^{\prime}{\tau^{f}}\rho^{f})^{-2}\log M),$ (19) $$

we have $\bar{R}_{CoT,{\bm{x}}_{query}\in\mathcal{M}^{\prime},f\in\mathcal{T}^{\prime}}
^{f}(\Psi)=0$.

###### Remark 1 .

Theorem 2 characterizes the sufficient conditions for a trained one-layer Transformer to generate all $K$-steps reasoning correctly by CoT for a task $f$ in $\mathcal{T}^{\prime}$. First, the TSR patterns of a new task in $\mathcal{T^{\prime}}$ should be linear combinations of TRR patterns in the training tasks in $\mathcal{T}$. Second, the number of context examples should be in the order of ${\alpha^{\prime}}^{-2}$, ${\rho_{s}^{f}}^{-2}$, and ${\tau^{f}}^{-2}$. One can equivalently interpret the decrease in the number of required context examples to achieve zero CoT error as an improvement of the CoT accuracy with fixed context length. Then, when the fraction $\alpha^{\prime}$ of contexts where the TSR pattern of the first step input is the same as the query increases, the contexts become more informative for the query. Thus, the CoT accuracy increases. When $\rho^{f}$ and $\tau^{f}$ increase, the reasoning labels in the context examples are more likely to be accurate based on their definitions in (11) and (16), then the CoT accuracy is improved.

###### Definition 1 .

###### Theorem 2 (CoT generalization) .

###### Remark 1 .

### 3.5 ICL Generalization and Comparison with CoT

Because only input-label pairs are used as context examples without intermediate reasoning steps for ICL, then the input-label pairs in context examples should be accurate on average. Otherwise, the context examples are not informative about the task and will lead to the failure of ICL. We formulate this requirement as Condition 1.

###### Condition 1 .

For the testing task $f=f_{K}\circ\cdots\circ f_{1}\in\mathcal{T}^{\prime}$, we have that for any $i\in[M^{\prime}]$,

$$ $\text{TSR}(f({\bm{\mu}}_{i}^{\prime}))=\arg\max_{j\in[M^{\prime}]}B_{(i,j)}^{f}.$ (20) $$

Condition 1 requires that in a context example, if the input TSR is ${\bm{\mu}}_{i}^{\prime}$, then the output TSR needs to be $f({\bm{\mu}}_{i}^{\prime})$ with the largest probability over all other TSR patterns. It is intuitive that the success of ICL requires this condition. Note that although (11) indicates that, $A_{k}^{f}(i,j^{*})$ achieves the largest value for all $j$ when ${\bm{\mu}}_{j^{*}}^{\prime}=f_{k}({\bm{\mu}}_{i}^{\prime})$ for every $k$ and $i$, (11) does not always lead to (20). Example 1 demonstrates a case where Condition 1 does not hold. Given the input ${\bm{\mu}}_{1}^{\prime}$, the correct inference output shall be ${\bm{\mu}}_{2}^{\prime}$, but ${\bm{B}}^{f}$ indicates that the most probable output is ${\bm{\mu}}_{1}^{\prime}$.

Our result of the ICL generalization is stated as follows.

###### Theorem 3 (ICL generalization) .

Given a trained model that satisfies conditions (i) to (iii) of Theorem 1 and (18), for the testing task $f\in\mathcal{T}^{\prime}$,

- a.
if Condition 1 does not hold, then $\bar{R}_{ICL,{\bm{x}}_{query}\in\mathcal{M}^{\prime},f\in\mathcal{T}^{\prime}}
^{f}(\Psi)\geq\Omega(1)$;
- b.
if Condition 1 holds, we have $\bar{R}_{ICL,{\bm{x}}_{query}\in\mathcal{M}^{\prime},f\in\mathcal{T}^{\prime}}
^{f}(\Psi)=0$, as long as the number of testing examples is
$l_{ts}^{f}\geq\Omega((\alpha^{\prime}{\tau_{o}^{f}}\rho_{o}^{f})^{-2}\log M).$
(21)

###### Remark 2 (Comparison between CoT and ICL) .

Theorem 3(a) formally states that, Condition 1 is necessary for a successful ICL generalization. Because Condition 1 is not required for CoT generalization, CoT performs better than ICL if Condition 1 fails(^5^55Our insight of the comparison between CoT and ICL still holds when we evaluate CoT generalization only by the final step output. This is because a successful CoT generalization in Theorem 2 on all reasoning steps already ensures a satisfactory CoT generalization on the final step.). Theorem 3(b) characterizes that when Condition 1 holds, a desired ICL generalization needs a testing prompt length linear in ${\alpha^{\prime}}^{-2}$, ${\rho_{o}^{f}}^{-2}$, and ${\tau_{o}^{f}}^{-2}$ for the testing task $f\in\mathcal{T}^{\prime}$.
This result is the counterpart of the requirement (19) for the CoT generalization, indicating that more context examples with the same TSR pattern as the query and more accurate context examples improve ICL generalization.

Ref. Li et al. () also shows the advantage of CoT over ICL to learn MLP functions, but in a different setting from ours, where our studied tasks operate on patterns. More importantly, this paper characterizes the CoT and ICL performance theoretically when the testing task has a distribution shift from training tasks (TRR patterns to TSR patterns), and the testing examples contain errors, while Li et al. () only empirically evaluates the CoT and ICL performance with noisy examples.

###### Condition 1 .

###### Theorem 3 (ICL generalization) .

###### Remark 2 (Comparison between CoT and ICL) .

## 4 The Mechanism of CoT and the Proof Sketch

### 4.1 Transformers implement CoT by Attending to the Most Similar Examples Every Step

In this section, we characterize the key mechanism of a properly trained one-layer Transformer to implement CoT on a $K$-steps reasoning task via training dynamics analysis of the attention layer, as demonstrated in Figure 2. This is different from the mechanism study in (Li et al., ; Feng et al., ) by constructing a model that can conduct CoT. We have the following proposition for the trained model.

###### Proposition 1 .

Let $\mathcal{S}_{k}^{*}$ denote the index set of the context columns of the testing prompt ${\bm{P}}$ in (4) that (a) correspond to the $k$-th step in a context example and (b) share the same TSR pattern in the $k$-th input as the $k$-th input ${\bm{v}}_{k-1}$ of the query,
$k\in[K]$. Given a trained model that satisfies conditions (i) to (iii) of Theorem 1 and (18) and (19) after $T$ iterations, we have

$$ $\sum_{i\in\mathcal{S}_{k}^{*}}\text{softmax}(\tilde{{\bm{p}}}_{i}^{\top}{\bm{W }}^{(T)}\tilde{{\bm{q}}}_{k})\geq 1-\epsilon,\text{ where }\tilde{{\bm{p}}}_{i }={\bm{p}}_{i}+{\bm{c}}_{(i\mod K)},\tilde{{\bm{q}}}_{k}={\bm{q}}_{k}+{\bm{c}} _{k},$ (22) $$

with ${\bm{q}}_{k}$ defined in (6). Moreover, for any $f\in\mathcal{T}^{\prime}$, the $k$-th step output ${\bm{v}}_{k}$ given ${\bm{x}}_{query}={\bm{\mu}}_{i}^{\prime}$ satisfies,

$$ ${\bm{v}}_{k}=f_{k}\circ\cdots\circ f_{1}({\bm{\mu}}_{i}^{\prime}).$ (23) $$

Figure: Figure 2: Concentration of attention weights for CoT inference.
Refer to caption: x2.png

Proposition 1 first illustrates that, when conducting the $k$-th step reasoning of the query for any $k\in[K^{\prime}]$, the trained model assigns dominant attention weights on the prompt columns that are also the $k$-th step reasoning of examples and share the same TSR pattern in the $k$-th step input as the query. Then, given a sufficient number of testing context examples by (19), it is ensured that the fraction of the correct TSR pattern is the largest in the output of each step by (11). Subsequently, the generation by greedy decoding (5) is correct in each step, leading to a successful CoT generalization.

###### Proposition 1 .

### 4.2 An Overview of the Proof

The technical challenges of the proof are concentrated on Theorem 1, where the property of the trained model is derived. The proof of Theorem 1 is built upon three Lemmas, which characterize the two stages of the training dynamics. Specifically, Lemmas 3 and 4 show that if a training prompt ${\bm{P}}$ includes the first $k$ steps of the reasoning query, then the attention weights on columns of ${\bm{P}}$ with a different step from the query decrease to be close to zero in the first stage. Lemma 5 computes the gradient updates in the second stage, where the attention weights on columns in ${\bm{P}}$ that correspond to step $k$ and have the same TRR pattern as the query
gradually become dominant. Theorem 1 unveils this training process by showing the required number of training iterations and sample complexity.

To prove Theorem 2, we first compute the required number of context examples for the new task $f\in\mathcal{T}^{\prime}$ so that by concentration inequalities, the number of context examples with accurate TSR is larger than examples with inaccurate TSR patterns in all $K$ reasoning steps with high probability. Then, due to the linear correlation between TSR and TRR patterns (18), we also show that the trained Transformer can attend to context columns with the same TSR pattern as the query. Therefore, the model can make the correct generation in each step. Theorem 3 follows a similar proof idea to Theorem 2, with the difference that the trained model predicts output directly from the input query following ${\bm{B}}^{f}$ instead of using $K$ reasoning steps following ${\bm{A}}_{k}^{f},k\in[K]$ in CoT. Therefore, Condition 1 is required for the success of ICL generalization.

## 5 Numerical Experiments

Data Generation and Model setup. We use synthetic data generated following Sections 2 and 3.2. Let $d_{\mathcal{X}}=30$, $M=20$, $M^{\prime}=10$, $\alpha=0.4$. We consider $3$-steps tasks for training and testing, i.e., $K=3$. A reasoning task $f$ is generated by first sampling a set of numbers of permutations $\{p_{i}\}_{i=1}^{M}$ with $p_{i}\in[M]$ and then let $f_{k}({\bm{\mu}}_{p_{i}})={\bm{\mu}}_{p_{((i+k)\mod M)}}$ for $i\in[M],k,j\in[K]$. The testing noise level is set to be $0.2$ for any examples and $f\in\mathcal{T}^{\prime}$. The learning model is a one-layer single-head Transformer defined in (2) or a three-layer two-head Transformer. We set $\tau^{f}=0.5$, $\rho^{f}=0.8$, $\alpha^{\prime}=0.8$ for CoT testing if not otherwise specified.

Experiments on the generalization of CoT. We first verify the required number of context examples for a desired CoT generalization on a one-layer Transformer. We investigate the impact of $\alpha^{\prime}$, $\tau^{f}$, and $\rho^{f}$ by varying one and fixing the other two. Figure 3 illustrates that more testing examples are needed when $\alpha^{\prime}$, $\tau^{f}$, or $\rho^{f}$ is small, which verifies the trend of the lower bound of $l_{ts}^{f}$ in (19).

Figure: Figure 3: CoT testing error with different (A) $\alpha^{\prime}$ (B) $\tau^{f}$ (C) $\rho^{f}$.
Refer to caption: x3.png

Experiments on the generalization of ICL and a comparison with CoT. We then verify the ICL generalization with the trained model. We vary $\tau_{o}^{f}$ and $\rho_{o}^{f}$ by changing $\tau^{f}$ and $\rho^{f}$. Figure 3 indicates that more testing examples are required when $\alpha^{\prime}$, $\tau_{o}^{f}$, or $\rho_{o}^{f}$ is small, which is consistent with our bound in (21). We then consider the case where $\tau_{o}^{f}=0.4$ and $\rho_{o}^{f}=0.1$ so that the generated testing prompt may not satisfy Condition 1 depending on the specific choices of $A_{k}^{f}$’s. Figure 6 shows that when Condition 1 holds, the ICL testing error decreases if the number of contexts increases. However, when Condition 1 fails, the ICL testing error remains large, irrespective of the number of contexts.

Figure: Figure 4: ICL testing error with different (A) $\alpha^{\prime}$ (B) $\tau_{o}^{f}$ (C) $\rho_{o}^{f}$.
Refer to caption: x6.png

Figure: Figure 5: Comparison between CoT and ICL w./w.o. Condition 1
Refer to caption: x9.png

Experiments on the training dynamics of CoT.
In Figure 6, we compute the total attention weights on four types of testing context columns along the training, which are contexts with the same (or different) TSR pattern and in the same (or different) step as the query. The result shows that the attention weights on contexts that share the same TSR pattern and in the same step as the query increase along the training and converge to around $1$. This verifies the mechanism formulated in (22). Meanwhile, Figure 6 also justifies the two-stage training dynamics proposed in Section 4.2, where we add a black vertical dashed line to demonstrate the stage transition boundary. We observe that the attention weights on context columns with a different step, i.e., the red and yellow curves, decrease to zero in the first stage. Then, the attention weights on contexts with the same TSR pattern and the same step as the query, i.e., the blue curve, increase to $1$ in the second stage. We also justify the attention mechanism of CoT on a three-layer two-head Transformer with a two-step reasoning task. Figure 7 shows that there exists at least one head in each layer of the Transformer that implements CoT as characterized in Proposition 1. This indicates that the CoT mechanism we characterize on one-layer Transformers can be extended to multi-layer multi-head Transformers.

Figure: Figure 7: Training dynamics of Transformers. (A) Layer 1, Head 2 (B) Layer 2 Head 2 (C) Layer 3 Head 2.
Refer to caption: x11.png

## 6 Conclusion, Limitations, and Future Works

This paper theoretically analyzes the training dynamics of Transformers with nonlinear attention, together with the CoT generalization ability of the resulting model on new tasks with noisy and partially inaccurate context examples. We quantitatively characterize and compare the required conditions for the success of CoT and ICL. Although based on a simplified Transformer model and reasoning tasks operating on patterns, this work deepens the theoretical understanding of the CoT mechanism. Future directions include designing efficient prompt-generating methods for CoT and analyzing LLM reasoning on a more complicated data model.

## Appendix A Additional Discussions

### A.1 The motivation to study one-layer single-head Transformers

The reasons we study one-layer single-head attention-only nonlinear Transformers in this work are as follows.

First, it is much more challenging to theoretically analyze the training dynamics and generalization of multi-layer/head Transformers. This is because the loss landscape for multi-layer/head Transformers is highly nonlinear and non-convex due to the interactions between multiple nonlinear functions. The simplified data helps to characterize the gradient updates in different directions for different patterns and steps. Non-orthogonal data make the updates less separable for different inputs, which is more challenging to analyze.

Second, the state-of-the-art theoretical works (Li et al., , ; Huang et al., ; Makkuva et al., ; Ildiz et al., ) on optimization and generalization also focus mainly on one-layer Transformers. No existing works study the optimization and generalization of CoT even for one-layer Transformers. Therefore, we plan to focus on the one-layer analysis to obtain more theoretical insights. We leave the theoretical analysis of the multi-layer case as future works.

Third, although we admit the gap between theory and practice, our theory still makes contributions under our settings. Our work is the first one to investigate the optimization and generalization of CoT and characterize the conditions when CoT is better than ICL. We establish the required number of context examples for a successful CoT in terms of how informative and erroneous the prompt is.

We also implement experiments on the attention mechanism for three-layer two-head Transformers on two-step reasoning tasks. Please see Figure 7 for details. The findings of all three layers are generally consistent with Proposition 1 for the single-layer single-head case, which indicates that the CoT mechanism we characterize on one-layer Transformers can be extended to multi-layer multi-head Transformers.

### A.2 The motivation of the data and task formulation

There are several reasons for using such data formulation.

First, our data formulation of orthogonal patterns, on which the function
is based, is widely used in the state-of-the-art theoretical study of model training or ICL on language and sequential data [(Tian et al., ; Huang et al., ; Li et al., ; Chen et al., ). For example, (Huang et al., ; Li et al., ) study ICL on regression or classification tasks, which also use orthogonal patterns as data. Sections 2.1 and 2.2 in (Chen et al., ) consider learning n-gram data in ICL by formulating transitions between orthogonal patterns. Section 3 of (Tian et al., ) also assume orthogonal patterns in Transformer model training, and the generation comes from the orthogonal pattern set. The data formulation we use is consistent with the existing theoretical works.

Second, based on this formulation, one can characterize the gradient updates in different directions for different patterns and steps. This enables us to distinguish the impact of different patterns and steps in the convergence analysis of CoT using Transformers. Non-orthogonal data make the model updates less separable for different inputs, which is more challenging to analyze. Moreover, we would like to mention that during the inference, the tokens in testing prompts contain noises as defined in Equation 10. This makes the tokens of different TSR patterns not orthogonal to each other and relaxes our orthogonality condition to some degree.

### A.3 The discussion of positional encoding

The positional encoding (PE) we use is simplified for theoretical analysis. The formulation of PE we use is motivated by (Huang et al., ; Nichani et al., ), where each token is added with a PE represented by orthogonal vectors. These works formulate the distribution of the PE to be related to the structure of the data, such as patch-wise association (Huang et al., ), and sparse token selection (Nichani et al., ). Likewise, we follow their intuition to make the PE vary in different steps of our reasoning tasks so that the Transformer can distinguish different steps when making inferences for the query.

Our analysis can be extended to study more general PEs with additional technical work in the future. One possible direction is studying the family of periodic and separable PE. For example, the absolute PE proposed by (Vaswani et al., ) considers PE as a sinusoid, which is periodic. Such analysis can be made by relaxing the “orthogonality” of PE vectors to a certain “separability” between PE vectors.

We also conduct experiments on a three-layer single-head Transformer with the standard PE proposed in Section 3.5 of (Vaswani et al., ) for our problem. Figure shows that the blue curve increases to be the largest along the training, which means the attention weights on example steps that share the same TSR pattern and the same step as the query. This indicates that the CoT mechanism of using standard PE is the same as the one proposed in Proposition 1 in our paper. One might note that the scores of the blue curve are not as high as Figure 6 in our paper. We guess the reason why the distinction in attention values is more significant in our PE may be the additional orthogonality of our PE and the property that its period
is the same as the reasoning length. Nevertheless, the strong similarity between the results on standard PE and our used PE shows the practical significance of our analysis.

Figure: Figure 8: CoT mechanism with standard PE of (A) Layer 1 (B) Layer 2 (C) Layer 3.
Refer to caption: x14.png

## Appendix B Algorithms

We first present the training algorithm introduced in Section 2.2.

Figure: Algorithm 1 Training with Stochastic Gradient Descent (SGD)

We then summarize the algorithm of the CoT inference introduced in Section 2.3 as follows.

Figure: Algorithm 2 Inference with Chain-of-Thought (CoT)

## Appendix C Preliminaries

We first summarize the notations we use in this paper in Table 1.

**Table 1: Summary of Notations**
| Notations | Annotation |
| --- | --- |
| ${\bm{x}}_{i}$, ${\bm{y}}_{i,k}$, ${\bm{x}}_{query}$, ${\bm{z}}_{k}$ | ${\bm{x}}_{i}$ is the input to the first step of a reasoning example. ${\bm{y}}_{i,k}$ is the $k$-th step output label of ${\bm{x}}_{i}$. ${\bm{x}}_{query}$ is the query input. ${\bm{z}}_{k}$ the $k$-th step output label of ${\bm{x}}_{query}$. $k\in[K]$. |
| ${\bm{P}}$, ${\bm{p}}_{query}$, ${\bm{E}}_{i}$, ${\bm{Q}}_{k}$, ${\bm{v}}_{k}$ | ${\bm{P}}$ is a training or testing prompt that consists of multiple training or testing examples and a query. The last column of ${\bm{P}}$ is denoted by ${\bm{p}}_{query}^{n}$, which is the query of ${\bm{P}}$. ${\bm{E}}_{i}$ is the $i$-th context example of ${\bm{P}}$. ${\bm{Q}}_{k}$ is the first $k$ steps of the reasoning query. $k\in[K]$. ${\bm{v}}_{k}$ is the $k$-th step generation by CoT. $k\in[K]$. |
| ${\bm{c}}_{i}$, $\tilde{{\bm{p}}}_{i}$, $\tilde{{\bm{p}}}_{query}$ | ${\bm{c}}_{i}$ is the positional encoding for the $i$-th column of the input sequence. $\tilde{{\bm{p}}}_{i}={\bm{p}}_{i}+{\bm{c}}_{i}$, where ${\bm{p}}_{i}$ is the $i$-th column of ${\bm{P}}$. $\tilde{{\bm{p}}}_{query}$ is the ${\bm{p}}_{i}$ of the query column. |
| $F(\Psi;{\bm{P}})$, $\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})$ | $F(\Psi;{\bm{P}}^{n})$ is the Transformer output for ${\bm{P}}$ with $\Psi$ as the parameter. $\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})$ is the loss function value given ${\bm{P}}^{n}$ and the corresponding label ${\bm{z}}^{n}$. |
| ${\bm{\mu}}_{i}\in\mathcal{M}$, ${\bm{\mu}}_{i}^{\prime}\in\mathcal{M}^{\prime}$, $\text{TSR}(\cdot)$ | ${\bm{\mu}}_{i}$ is the $i$-th training-relevant (TRR) pattern for $i\in[M]$. ${\bm{\mu}}_{i}^{\prime}$ is the $i$-th testing-relevant (TSR) pattern for $i\in[M^{\prime}]$. $\mathcal{M}$ and $\mathcal{M}^{\prime}$ are the set of TRR and TSR patterns, respectively. $\text{TSR}(\cdot)$ is a function that outputs the index of the TSR pattern of the noisy input. |
| $f_{k}$, $f$ | $f$ is the task function with $f=f_{K}\circ\cdots f_{2}\circ f_{1}$ for a $K$-steps reasoning. $f_{k}$ is the $k$-th step task function. |
| $\mathcal{T}$, $\mathcal{T}^{\prime}$, $\mathcal{D}$, $\mathcal{D}^{\prime}$ | $\mathcal{T}$ is the distribution of training tasks, while $\mathcal{T}^{\prime}$ is the distribution of testing tasks. $\mathcal{D}$ is the training data distribution. $\mathcal{D}^{\prime}$ is the testing data distribution. |
| $\alpha$, $\alpha^{\prime}$ | $\alpha$ (or $\alpha^{\prime}$) is the fraction of context examples with input sharing the same TRR (or TSR) pattern as the query. |
| ${\bm{A}}_{k}^{f}$, ${\bm{B}}_{k}^{f}$ | ${\bm{A}}_{k}^{f}$ is the step-wise transition matrix at the $k$-th step for the task $f$, $k\in[K]$. ${\bm{B}}_{k}^{f}$ is the $K$-steps transition matrix of the task $f$. |
| $\tau^{f}$, $\tau_{o}^{f}$, $\rho^{f}$, $\rho_{o}^{f}$ | $\tau^{f}$ is the min-max trajectory transition probability for task $f$. $\tau_{o}^{f}$ is the min-max input-label transition probability for task $f$. $\rho^{f}$ and $\rho_{o}^{f}$ are primacy of the step-wise transition matrices and the $K$-steps transition matrix, respectively. |
| $\mathcal{S}_{k}^{*}$ | The index set of context columns of the prompt that correspond to the $k$-th step of the example and share the same TSR pattern in the $(k-1)$-th output as the $(k-1)$-th output ${\bm{v}}_{k-1}$ of the query. |
| $p_{n}(t)$ | $p_{n}(t)$ is the summation of attention weights on context columns that share the same TRR/TSR pattern and in the same step as the query. |
| $\mathcal{B}_{b}$ | $\mathcal{B}_{b}$ is the SGD batch at the $b$-th iteration. |
| $l_{tr}$ | $l_{tr}$ is the universal number of training context examples. |
| $l_{ts}^{f}$ | $l_{ts}$ is the number of testing context examples of the task $f$. |
| $\mathcal{O}()$, $\Omega()$, $\Theta()$ | We follow the convention that $f(x)=O(g(x))$ (or $\Omega(g(x))$, $\Theta(g(x)))$) means that $f(x)$ increases at most, at least, or in the order of $g(x)$, respectively. |
| $\gtrsim$, $\lesssim$ | $f(x)\gtrsim g(x)$ (or $f(x)\lesssim g(x)$ ) means that $f(x)\geq\Omega(g(x))$ (or $f(x)\lesssim\mathcal{O}(g(x))$). |

###### Lemma 1 (Multiplicative Chernoff bounds, Theorem D.4 of (Mohri et al., 2018 ) ) .

Let $X_{1}$, $\cdots$, ${\bm{X}}_{m}$ be independent random variables drawn according to some distribution $\mathcal{D}$ with mean $p$ and support included in $[0,1]$. Then, for any $\gamma\in[0,\frac{1}{p}-1]$, the following inequality holds for $\hat{p}=\frac{1}{m}\sum_{i=1}^{m}X_{i}$:

$$ $\Pr(\hat{p}\geq(1+\gamma)p)\leq e^{-\frac{mp\gamma^{2}}{3}},$ (26) $$

$$ $\Pr(\hat{p}\leq(1-\gamma)p)\leq e^{-\frac{mp\gamma^{2}}{2}}.$ (27) $$

###### Definition 2 ( (Vershynin, 2010 ) ) .

We say $X$ is a sub-Gaussian random variable with sub-Gaussian norm $K>0$, if $(\mathbb{E}|X|^{p})^{\frac{1}{p}}\leq K\sqrt{p}$ for all $p\geq 1$. In addition, the sub-Gaussian norm of X, denoted $\|X\|_{\psi_{2}}$, is defined as $\|X\|_{\psi_{2}}=\sup_{p\geq 1}p^{-\frac{1}{2}}(\mathbb{E}|X|^{p})^{\frac{1}{p}}$.

###### Lemma 2 ( Vershynin ( 2010 ) Proposition 5.1, Hoeffding’s inequality) .

Let $X_{1},X_{2},\cdots,X_{N}$ be independent centered sub-gaussian random variables, and let $K=\max_{i}\|{\bm{X}}_{i}\|_{\psi_{2}}$. Then for every ${\bm{a}}=(a_{1},\cdots,a_{N})\in\mathbb{R}^{N}$ and every $t\geq 0$, we have

$$ $\Pr\Big{(}\Big{|}\sum_{i=1}^{N}a_{i}X_{i}\Big{|}\geq t\Big{)}\leq e\cdot\exp \left(-\frac{ct^{2}}{K^{2}\|{\bm{a}}\|^{2}}\right),$ (28) $$

where $c>0$ is an absolute constant.

###### Definition 3 .

Define that for $\tilde{{\bm{p}}_{i}}$ that shares the same TRR/TSR pattern and in the same step as the query,

$$ $p_{n}(t)=\sum_{i}\text{softmax}(\tilde{{\bm{p}}_{i}^{n}}^{\top}{\bm{W}}^{(t)} \tilde{{\bm{p}}}_{query}^{n}).$ (29) $$

###### Lemma 3 .

Given the SGD training scheme described in Section 2.2, $B\geq\Omega(M\log M)$, and $l_{tr}\geq\Omega(\alpha^{-1})$, we have the following results. When $\mathcal{O}(\eta^{-1}\alpha^{-2}K^{3}\log\frac{K}{\epsilon})\geq t\geq 1$, for any ${\bm{p}}$ as a column of context examples in (1), we have

$$ $\displaystyle\tilde{{\bm{p}}}^{\top}\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}} \frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}^{(t)}} \tilde{{\bm{p}}}$ (30) $\displaystyle\leq$ $\displaystyle\frac{\eta}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(1-p_{n}(t)) ^{2}(-4p_{n}(t)(1+\frac{\alpha^{2}}{K^{2}})+\frac{\alpha^{2}}{K^{2}}(1+\frac{2 (K-1)}{K}))-\frac{\alpha^{2}}{K^{3}}(1-p_{n}(t))^{2}).$ $$

For any $\tilde{{\bm{p}}}^{\prime}$ that shares the same TRR pattern and a different positional encoding as $\tilde{{\bm{p}}}$, we have

$$ $\displaystyle\frac{\eta}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(-4-(3K-2)(1 -p_{n}(t))(1+\frac{\alpha^{2}}{K^{2}}))p_{n}(t)(1-p_{n}(t))+\frac{\alpha^{2}}{ K^{3}}(1-p_{n}(t))^{2})$ (31) $\displaystyle\leq$ $\displaystyle{\tilde{{\bm{p}}^{\prime}}}^{\top}\eta\frac{1}{B}\sum_{n\in \mathcal{B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{ \bm{W}}^{(t)}}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle\frac{\eta}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(-4-(3K-2)(1 -p_{n}(t))(1+\frac{\alpha^{2}}{K^{2}}))p_{n}(t)(1-p_{n}(t))+\frac{1}{K}p_{n}(t )(1-p_{n}(t))^{2}$ $\displaystyle\cdot(1+\frac{\alpha^{2}}{K^{2}})).$ $$

For any $\tilde{{\bm{p}}^{\prime}}$ that shares a different TRR pattern but the same positional encoding as $\tilde{{\bm{p}}}$, we have

$$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(- \frac{\alpha^{2}}{K^{2}}+(K-1+\frac{(2K-1)\alpha^{2}}{K^{2}})p_{n}(t))(1-p_{n} (t))^{2}-(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{3}}$ (32) $\displaystyle+\frac{1}{K}\cdot(1-p_{n}(t))^{2}(-p_{n}(t)+(1-p_{n}(t))\frac{ \alpha^{2}}{K^{2}}))$ $\displaystyle\leq$ $\displaystyle{\tilde{{\bm{p}}^{\prime}}}^{\top}\eta\frac{1}{B}\sum_{n\in \mathcal{B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{ \bm{W}}^{(t)}}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(- \frac{\alpha^{2}}{K^{2}}+(K-1+\frac{(2K-1)\alpha^{2}}{K^{2}})p_{n}(t))(1-p_{n} (t))^{2}-(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{3}}).$ $$

For any $\tilde{{\bm{p}}^{\prime}}$ that shares a different TRR pattern and a different positional encoding from $\tilde{{\bm{p}}}$, we have

$$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}p_{n}( t)(1-p_{n}(t))^{2}(1+\frac{(2-K)\alpha^{2}}{K^{2}})+(1-p_{n}(t))^{2}\cdot\frac {\alpha^{2}}{K^{3}})$ (33) $\displaystyle\leq$ $\displaystyle{\tilde{{\bm{p}}^{\prime}}}^{\top}\eta\frac{1}{B}\sum_{n\in \mathcal{B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{ \bm{W}}^{(t)}}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}p_{n}( t)(1-p_{n}(t))^{2}(2-K+\frac{(2-K)\alpha^{2}}{K^{2}})+(1-p_{n}(t))^{2}p_{n}(t) (1+\frac{\alpha^{2}}{K^{2}})\cdot\frac{1}{K}).$ $$

###### Lemma 4 .

Given the SGD training scheme described in Section 2.2, $B\geq\Omega(M\log M)$, and $l_{tr}\geq\Omega(\alpha^{-1})$, and

$$ $t\gtrsim T_{1}:=\eta^{-1}\alpha^{-2}K^{3}\log\frac{K}{\epsilon},$ (34) $$

we have that if ${\bm{p}}_{query}$ is in the $k$-th step,

$$ $\sum_{i\in\mathcal{S}_{[K]\backslash{k}}}\text{softmax}(\tilde{{\bm{p}}}_{i}^{ \top}{\bm{W}}^{(t)}\tilde{{\bm{p}}}_{query})\leq\epsilon$ (35) $$

where $\mathcal{S}_{[K]\backslash{k}}$ means the index set of context columns that are not in the $k$-th step.

###### Lemma 5 .

Given the SGD training scheme described in Section 2.2, $B\geq\Omega(M\log M)$, and $l_{tr}\geq\Omega(\alpha^{-1})$, we have the following results. When $t\geq T_{1}=\eta^{-1}\alpha^{-2}K^{3}\log\frac{K}{\epsilon}$, for any ${\bm{p}}$ as a column of context examples in (1), we have

$$ $\displaystyle\tilde{{\bm{p}}}^{\top}\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}} \frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}^{(t)}} \tilde{{\bm{p}}}\leq-\frac{\eta}{2MB}\sum_{n\in\mathcal{B}_{b}}4p_{n}(t)(1-p_{ n}(t))^{2}.$ (36) $$

For any $\tilde{{\bm{p}}^{\prime}}$ that shares the same TRR pattern and a different positional encoding as $\tilde{{\bm{p}}}$, we have

$$ $\displaystyle\left|{\tilde{{\bm{p}}^{\prime}}}^{\top}\eta\frac{1}{B}\sum_{n\in \mathcal{B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{ \bm{W}}^{(t)}}\tilde{{\bm{p}}}\right|\leq\eta\epsilon.$ (37) $$

For any $\tilde{{\bm{p}}^{\prime}}$ that shares a different TRR pattern but the same positional encoding as $\tilde{{\bm{p}}}$, we have

$$ $\displaystyle\left|{\tilde{{\bm{p}}^{\prime}}}^{\top}\eta\frac{1}{B}\sum_{n\in \mathcal{B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{ \bm{W}}^{(t)}}\tilde{{\bm{p}}}\right|\leq\frac{\eta}{2BM}\sum_{n\in\mathcal{B} _{b}}p_{n}(b)(1-p_{n}(b))^{2}.$ (38) $$

For any $\tilde{{\bm{p}}^{\prime}}$ that shares a different TRR pattern and a different positional encoding from $\tilde{{\bm{p}}}$, we have

$$ $\displaystyle\left|{\tilde{{\bm{p}}^{\prime}}}^{\top}\eta\frac{1}{B}\sum_{n\in \mathcal{B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{ \bm{W}}^{(t)}}\tilde{{\bm{p}}}\right|\leq\eta\epsilon.$ (39) $$

###### Lemma 1 (Multiplicative Chernoff bounds, Theorem D.4 of (Mohri et al., 2018 ) ) .

###### Definition 2 ( (Vershynin, 2010 ) ) .

###### Lemma 2 ( Vershynin ( 2010 ) Proposition 5.1, Hoeffding’s inequality) .

###### Definition 3 .

###### Lemma 3 .

###### Lemma 4 .

###### Lemma 5 .

## Appendix D Proof of Main Theorems

### D.1 Proof of Theorem 1

###### Proof.

By the condition in Lemma 3, we have that

$$ $B\geq\Omega(M\log M).$ (40) $$

We know that there exists gradient noise caused by imbalanced TRR patterns in each batch. Then, by Hoeffding’s inequality (28),

$$ $\displaystyle\Pr\left(\Big{\|}\frac{1}{|\mathcal{B}_{b}|}\sum_{n\in\mathcal{B} _{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},z^{n})}{\partial{\bm{W}}}-\mathbb{E} \left[\frac{\partial\ell(\Psi;{\bm{P}}^{n},z^{n})}{\partial{\bm{W}}}\right] \Big{\|}\geq\Big{|}\mathbb{E}\left[\frac{\partial\ell(\Psi;{\bm{P}}^{n},z^{n}) }{\partial{\bm{W}}}\right]\epsilon\right)$ (41) $\displaystyle\leq$ $\displaystyle e^{-B\epsilon^{2}}\leq M^{-C},$ $$

if $B\gtrsim\epsilon^{-2}\log M$.
Therefore, we require

$$ $B\gtrsim\max\{\epsilon^{-2},M\}\log M.$ (42) $$

By Lemma 5 and Definition 3, for $\tilde{{\bm{p}}_{i}^{n}}$ that share the same TRR pattern and the same positional encoding of $\tilde{{\bm{p}}}_{query}^{n}$,

$$ $\displaystyle\frac{p_{n}(t+1)}{|\mathcal{S}_{1}^{n}|}=\text{softmax}({\tilde{{ \bm{p}}_{i}^{n}}}^{\top}{\bm{W}}^{(t+1)}\tilde{{\bm{p}}}_{query}^{n})\geq$ $\displaystyle\frac{1}{l}\cdot\frac{1}{\frac{\alpha}{K}+(1-\frac{1}{K})\cdot \epsilon+(\frac{1}{K}-\frac{\alpha}{K})e^{-u}},$ (43) $$

where by (159),

$$ $u\gtrsim\frac{\eta}{KM}\sum_{b=0}^{t}(1-p_{n}(b))^{2}p_{n}(b).$ (44) $$

For $\tilde{{\bm{p}}_{i}^{n}}$ that only share the same positional encoding of $\tilde{{\bm{p}}}_{query}^{n}$,

$$ $\displaystyle\text{softmax}({\tilde{{\bm{p}}_{i}^{n}}}^{\top}{\bm{W}}^{(t+1)} \tilde{{\bm{p}}}_{query}^{n})\geq$ $\displaystyle\frac{1}{l}\cdot\frac{1}{\frac{\alpha}{K}e^{u}+(1-\frac{1}{K}) \cdot\epsilon+(\frac{1}{K}-\frac{\alpha}{K})}.$ (45) $$

Therefore, to make the attention weights between $\tilde{{\bm{p}}}_{query}^{n}$ and $\tilde{{\bm{p}}_{i}^{n}}$ that share the same TRR pattern and the same positional encoding dominant, we need a large enough $u$.
When $1-p_{n}(b)\geq\Omega(1)$, we have

$$ $t\leq T_{2}:=\eta^{-1}KM\alpha^{-1}.$ (46) $$

When $1-p_{n}(b)\leq O(1)$,

$$ $p_{n}(t+1)=\frac{e^{u}}{e^{u}+\frac{1-\alpha}{\alpha}}\gtrsim 1-\frac{1-\alpha }{\alpha}e^{-u},$ (47) $$

and

$$ $1-p_{n}(t+1)\geq\frac{1-\alpha}{\alpha e^{u}+(1-\alpha)}\gtrsim\frac{1-\alpha} {\alpha}e^{-u}.$ (48) $$

Then, we prove that when $t$ is large enough, $u(t)\geq\frac{1}{2}\log\frac{\eta(1-\alpha)^{2}t}{\alpha^{2}M}$. We show it by induction. Suppose that the conclusion holds when $t=t_{0}$, then

$$ $\displaystyle u(t)\geq$ $\displaystyle\frac{\eta}{KM}\sum_{b=0}^{t_{0}}(1-p_{n}(b))^{2}p_{n}(b)+\frac{ \eta}{KM}(1-p_{n}(t))^{2}p_{n}(t)$ (49) $\displaystyle\geq$ $\displaystyle\frac{1}{2}\log\frac{(1-\alpha)^{2}t}{2\alpha^{2}KM}+\frac{\eta}{ KM}(1-p_{n}(t))^{2}p_{n}(t)$ $\displaystyle\geq$ $\displaystyle\frac{1}{2}\log\frac{\eta(1-\alpha)^{2}(t+1)}{\alpha^{2}KM},$ $$

where the last step is by

$$ $\frac{1}{2}\log(1+\frac{1}{t})\leq\frac{1}{2t}\leq\frac{\eta}{KM}\cdot(\frac{1 -\alpha}{\alpha})^{2}e^{-\log\frac{\eta(1-\alpha)^{2}t}{\alpha^{2}KM}}.$ (50) $$

To make $(1-p_{n}(t))^{2}<\epsilon$, we need

$$ $(\frac{1-\alpha}{\alpha})^{2}e^{-2u}\leq\epsilon.$ (51) $$

Then, we get

$$ $u\geq\frac{1}{2}\log\frac{1}{\epsilon}+\log\frac{1-\alpha}{\alpha}.$ (52) $$

Therefore, by

$$ $\frac{1}{2}\log\frac{\eta t}{KM}+\log\frac{1-\alpha}{\alpha}\geq\frac{1}{2} \log\frac{1}{\epsilon}+\log\frac{1-\alpha}{\alpha},$ (53) $$

we finally obtain

$$ $t\geq T_{3}:=\eta^{-1}\epsilon^{-1}KM.$ (54) $$

For $\tilde{{\bm{p}}_{i}^{n}}$ that shares the same TSR pattern as the query, we have that when $t=T_{1}$,

$$ ${\tilde{{\bm{p}}_{i}^{n}}}^{\top}{\bm{W}}^{(t)}\tilde{{\bm{p}}}_{query}^{n} \geq\log\frac{K}{\epsilon}.$ (55) $$

When $t=T_{1}+T_{2}+T_{3}$,

$$ ${\tilde{{\bm{p}}_{i}^{n}}}^{\top}{\bm{W}}^{(t)}\tilde{{\bm{p}}}_{query}^{n} \geq\Theta(1)\cdot\log\frac{K}{\epsilon}=\Theta(\log\frac{K}{\epsilon}).$ (56) $$

Then,

$$ $\displaystyle T:=$ $\displaystyle T_{1}+T_{2}+T_{3}$ (57) $\displaystyle=$ $\displaystyle\Theta(\eta^{-1}\alpha^{-2}K^{3}\log\frac{K}{\epsilon}+\eta^{-1} MK(\alpha^{-1}+\epsilon^{-1})).$ $$

Therefore,

$$ $\mathbb{E}_{{\bm{x}}_{query}\sim\mathcal{D},f\in\mathcal{T}}\left[\ell(\Psi;{ \bm{P}},{\bm{z}})\right]\leq\mathcal{O}(\epsilon).$ (58) $$

∎

###### Proof.

### D.2 Proof of Theorem 2

###### Proof.

We know that $\alpha^{\prime}$ is the fraction of examples that share the same TSR pattern as the query. We need that in each step, the number of examples that share the same TSR pattern as the current step of the query is at least $1$. Note that the probability of examples where each reasoning step produces the most probable output is

$$ $\prod_{k=1}^{K}A_{k(\text{TSR}(f_{k-1}\circ\cdots f_{0}({\bm{\mu}}_{i}^{\prime })),\text{TSR}(f_{k}\circ\cdots f_{0}({\bm{\mu}}_{i}^{\prime})))}^{f},\text{ where }f_{0}({\bm{\mu}}_{i}^{\prime}):={\bm{\mu}}_{i}^{\prime},\forall\ i\in[M ^{\prime}],$ (59) $$

where the input to the first step has the TSR pattern ${\bm{\mu}}_{i}^{\prime}$.
Define $m_{k(i)}$ as the TSR pattern in the $k$-th step output of the $i$-th context example by the transition matrix defined in 11. Consider that the TSR pattern of the $k$-th step label of the testing query is ${\bm{\mu}}_{q_{k}}^{\prime}$, which is also the most probable $k$-th step output of the $k$-th step of a certain ${\bm{x}}_{i}$ with $\text{TSR}({\bm{x}}_{i})=\text{TSR}({\bm{x}}_{query})=q_{0}$. Let the TSR pattern of another reasoning process, where for a certain first-step input ${\bm{x}}_{i}$ with $\text{TSR}({\bm{x}})=\text{TSR}({\bm{x}}_{query})=q_{0}$, the $k$-th step output is the most probable for $k\in[K^{\prime}]\backslash\{h\}$, while the $h$-th step output is the second probable. Denote the TSR pattern of the $k$-th step output of ${\bm{x}}_{i}$ following this process as ${\bm{\mu}}_{u_{k}}^{\prime}$ with $u_{0}=q_{0}$. By the Chernoff bound of Bernoulli distribution in Lemma 1, we can obtain

$$ $\displaystyle\Pr\left(\frac{1}{l_{ts}}\sum_{i=1}^{l_{ts}}\mathbbm{1}[m_{k(i)}= {\bm{\mu}}_{q_{k}}^{\prime},\forall k\in[K^{\prime}]]\leq(1-\rho_{s}^{f}/2) \alpha^{\prime}\prod_{k=1}^{K^{\prime}}A_{k(q_{k-1},q_{k})}^{f}\right)$ (60) $\displaystyle\leq$ $\displaystyle e^{-l_{ts}(\rho_{s}^{f})^{2}\alpha^{\prime}\prod_{k=1}^{K^{ \prime}}A_{k(q_{k-1},q_{k}}^{f}}=M^{-C},$ $$

and by Lemma 2,

$$ $\displaystyle\Pr\left(\frac{1}{l_{ts}}\sum_{i=1}^{l_{ts}}\mathbbm{1}[m_{k(i)}= {\bm{\mu}}_{u_{k}}^{\prime},\forall k\in[K^{\prime}]]\geq(1-\rho_{s}^{f}/2) \alpha^{\prime}\prod_{k=1}^{K^{\prime}}A_{k(q_{k-1},q_{k})}^{f}\right)$ (61) $\displaystyle\leq$ $\displaystyle\Pr\left(\frac{1}{l_{ts}}\sum_{i=1}^{l_{ts}}\mathbbm{1}[m_{k(i)}= {\bm{\mu}}_{u_{k}}^{\prime},\forall k\in[K^{\prime}]]\geq\alpha^{\prime}\prod_ {k=1}^{K^{\prime}}A_{k(u_{k-1},u_{k})}^{f}+t_{0}\right)$ $\displaystyle\leq$ $\displaystyle e^{-l_{ts}t_{0}^{2}}=M^{-C},$ $$

for some $c\in(0,1)$ and $C>0$, where the first step is by the definition of $\rho_{s}^{f}$ in (11), and

$$ $t_{0}\lesssim\rho_{s}^{f}\alpha^{\prime}\prod_{k=1}^{K^{\prime}}A_{k(q_{k-1},q _{k})}^{f}.$ (62) $$

Hence, with a high probability,

$$ $\displaystyle l_{ts}\gtrsim$ $\displaystyle\max\{({\rho_{s}^{f}}^{2}\alpha^{\prime}\prod_{k=1}^{K^{\prime}}A _{k(q_{k-1},q_{k})}^{f})^{-1}\log M,(\rho_{s}^{f}\alpha^{\prime}\prod_{k=1}^{K ^{\prime}}A_{k(q_{k-1},q_{k})}^{f})^{-2}\log M\}$ (63) $\displaystyle\gtrsim$ $\displaystyle(\rho_{s}^{f}\alpha^{\prime}\prod_{k=1}^{K^{\prime}}A_{k(q_{k-1}, q_{k})}^{f})^{-2}\log M,$ $$

such that the number of examples with the same TSR pattern as the query in each of the total $K$ steps is at least $1$. To make the above condition hold for any TSR pattern of the intermediate step of the query, we need

$$ $\displaystyle l_{ts}\gtrsim$ $\displaystyle\max_{q_{k}\in[M^{\prime}]}({\rho_{s}^{f}}\alpha^{\prime}\prod_{k =1}^{K^{\prime}}A_{k(q_{k-1},q_{k})}^{f})^{-2}\log M$ (64) $\displaystyle=$ $\displaystyle\max_{i\in[M^{\prime}]}({\rho_{s}^{f}}\alpha^{\prime}\prod_{k=1}^ {K^{\prime}}A_{k(\text{TSR}(f_{k-1}\circ\cdots f_{0}({\bm{\mu}}_{i}^{\prime})) ,\text{TSR}(f_{k}\circ\cdots f_{0}({\bm{\mu}}_{i}^{\prime})))}^{f})^{-2}\log M$ $\displaystyle=$ $\displaystyle(\rho_{s}^{f}\alpha^{\prime}\tau_{s}^{f})^{-2}\log M.$ $$

Then, we show the CoT testing error is zero by induction. In the first step, consider ${\bm{x}}_{i}={\bm{\mu}}_{j}+{\bm{\delta}}_{i}$ such that

$$ $\tilde{{\bm{p}}_{i}}=\begin{pmatrix}{\bm{\mu}}_{j}^{\prime}\\ {\bm{y}}_{i}\\ \end{pmatrix}+\begin{pmatrix}{\bm{\delta}}_{i}\\ \bm{0}\\ \end{pmatrix}+{\bm{c}}_{i}.$ (65) $$

Since that

$$ $({\bm{\delta}}_{i}^{\top},0^{\top}){\bm{W}}^{(0)}\tilde{{\bm{p}}_{i}}\lesssim\xi,$ (66) $$

by that each entry of ${\bm{W}}^{(0)}$ follows $\mathcal{N}(0,\xi^{2})$, and

$$ $({\bm{\delta}}_{i}^{\top},0^{\top})\frac{\eta}{B}\sum_{n\in\mathcal{B}_{b}} \sum_{b=0}^{T-1}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{ \bm{W}}^{(b)}}\tilde{{\bm{p}}}_{query}=0,$ (67) $$

we have that for $\tilde{{\bm{p}}_{i}}$ that shares the same TSR pattern as the query,

$$ $\displaystyle\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}^{(T)}\tilde{{\bm{p}}}_{query}$ (68) $\displaystyle=$ $\displaystyle\tilde{{\bm{p}}_{i}}^{\top}({\bm{W}}^{(0)}+\frac{\eta}{B}\sum_{n \in\mathcal{B}_{b}}\sum_{b=0}^{T-1}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z }}^{n})}{\partial{\bm{W}}^{(b)}})\tilde{{\bm{p}}}_{query}$ $\displaystyle=$ $\displaystyle(({{\bm{\mu}}_{j}^{\prime}}^{\top},{\bm{y}}_{i}^{\top})+{\bm{c}}_ {i}^{\top}))({\bm{W}}^{(0)}+\frac{\eta}{B}\sum_{n\in\mathcal{B}_{b}}\sum_{b=0} ^{T-1}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}^{(b )}})\tilde{{\bm{p}}}_{query}.$ $$

Let ${\bm{\mu}}_{j}^{\prime}=\sum_{i=1}^{M^{\prime}}\lambda_{j,i}{\bm{\mu}}_{i}$. Then, we have

$$ $\displaystyle\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}^{(T)}\tilde{{\bm{p}}}_{query}$ (69) $\displaystyle=$ $\displaystyle((\sum_{i=1}^{M^{\prime}}\lambda_{j,i}{\bm{\mu}}_{i}^{\top},{\bm{ y}}_{i}^{\top})+{\bm{c}}_{i}^{\top})({\bm{W}}^{(0)}+\frac{\eta}{B}\sum_{n\in \mathcal{B}_{b}}\sum_{b=0}^{T-1}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^ {n})}{\partial{\bm{W}}^{(b)}})((\sum_{i=1}^{M^{\prime}}\lambda_{j,i}{\bm{\mu}} _{i}^{\top},\bm{0}^{\top})+{\bm{c}}_{1})^{\top}$ $\displaystyle=$ $\displaystyle\sum_{i=1}^{M^{\prime}}\lambda_{j,i}^{2}(({\bm{\mu}}_{i}^{\top},{ \bm{y}}_{i}^{\top})+{\bm{c}}_{i}^{\top})({\bm{W}}^{(0)}+\frac{\eta}{B}\sum_{n \in\mathcal{B}_{b}}\sum_{b=0}^{T-1}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z }}^{n})}{\partial{\bm{W}}^{(b)}})(({\bm{\mu}}_{i}^{\top},\bm{0}^{\top})+{\bm{c }}_{1})^{\top}$ $\displaystyle+\sum_{i\neq i^{\prime}}\lambda_{j,i}\lambda_{j,i^{\prime}}({\bm{ \mu}}_{i}^{\top},{\bm{y}}_{i}^{\top},{\bm{c}}_{i}^{\top})({\bm{W}}^{(0)}+\frac {\eta}{B}\sum_{n\in\mathcal{B}_{b}}\sum_{b=0}^{T-1}\frac{\partial\ell(\Psi;{ \bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}^{(b)}})(({\bm{\mu}}_{i^{\prime}}^{ \top},\bm{0}^{\top})+{\bm{c}}_{1})^{\top}$ $\displaystyle\geq$ $\displaystyle\Theta(\log\frac{K}{\epsilon})-\epsilon$ $\displaystyle=$ $\displaystyle\Theta(\log\frac{K}{\epsilon}),$ $$

where the second to last step is by Theorem 1. Since the gradient updates for different TRR patterns are very close to each other, we have that if $\sum_{i=1}^{M^{\prime}}\lambda_{j,i}\lambda_{k,i}=0$,

$$ $\displaystyle\sum_{i=1}^{M^{\prime}}\lambda_{j,i}\lambda_{j,i^{\prime}}(({\bm{ \mu}}_{i}^{\top},{\bm{y}}_{i}^{\top})+{\bm{c}}_{i}^{\top})({\bm{W}}^{(0)}+ \frac{\eta}{B}\sum_{n\in\mathcal{B}_{b}}\sum_{b=0}^{T-1}\frac{\partial\ell( \Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}^{(b)}})(({\bm{\mu}}_{i}^{ \top},\bm{0}^{\top})+{\bm{c}}_{1})^{\top}$ (70) $\displaystyle\lesssim$ $\displaystyle\epsilon\log\frac{K}{\epsilon}.$ $$

Hence, for $\tilde{{\bm{p}}_{i}}$ that shares a different TSR pattern with $\tilde{{\bm{p}}_{i}}$,

$$ $\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}^{(T)}\tilde{{\bm{p}}}_{query}\lesssim \epsilon\log\frac{K}{\epsilon}.$ (71) $$

Therefore, we can derive that

$$ $\sum_{i\in\mathcal{S}_{1}^{*}}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W }}^{(T)}\tilde{{\bm{p}}}_{query})\geq 1-\epsilon,$ (72) $$

where $\mathcal{S}^{*}_{1}$ is the set of the first step of examples that share the same TSR pattern as the query. Then, the first step leads to a correct prediction with zero testing error, since that $\max_{j\in[M^{\prime}]}A_{k(q_{0},j)}$ is the largest to make the correct prediction for ${\bm{x}}_{query}$ if ${\bm{x}}_{query}={\bm{\mu}}_{q_{0}}^{\prime}$, i.e.,

$$ ${\bm{v}}_{1}=f_{1}({\bm{\mu}}_{q_{0}}^{\prime}).$ (73) $$

Suppose that the $k$-th step generates a zero testing error. Then, for the $k+1$-th step, we know that there exists ${\bm{p}}_{j}$ that shares the same TSR pattern as ${\bm{v}}_{k}$. Then, we can also derive that

$$ $\displaystyle\tilde{{\bm{p}}}_{j}^{\top}{\bm{W}}^{(T)}(({\bm{v}}_{k}^{\top}, \bm{0}^{\top})^{\top}+{\bm{c}}_{k}^{\top})^{\top}=\Theta(\log\frac{K}{\epsilon }),$ (74) $$

and

$$ $\sum_{j\in\mathcal{S}_{k}^{*}}\text{softmax}(\tilde{{\bm{p}}}_{j}^{\top}{\bm{W }}^{(T)}(({\bm{v}}_{k-1}^{\top}\ {\bm{v}}_{k}^{\top})^{\top}+{\bm{c}}_{k}^{ \top})^{\top})\geq 1-\epsilon.$ (75) $$

Hence, the $k+1$-th also makes the correct prediction, i.e.,

$$ ${\bm{v}}_{k+1}=f_{k+1}\circ\cdots f_{1}({\bm{\mu}}_{q_{0}}^{\prime}),$ (76) $$

where ${\bm{\mu}}_{q_{k+1}}^{\prime}$ is the TSR pattern of the $k+1$-th step input. Therefore, we show that CoT makes the correct prediction in each step as well as in the final prediction, such that

$$ $\bar{R}_{CoT,{\bm{x}}\in\mathcal{M}^{\prime},f\in\mathcal{T}^{\prime}}^{f}( \Psi)=0.$ (77) $$

∎

###### Proof.

### D.3 Proof of Theorem 3

###### Proof.

We know that the positional encodings are the same for the ICL inference in all examples. Hence, similar to (72), we can derive that

$$ $\sum_{i\in\mathcal{S}_{K}^{*}}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W }}^{(T)}\tilde{{\bm{p}}}_{query})\geq 1-\epsilon,$ (78) $$

where $\mathcal{S}^{*}_{K}$ is the set of the last step output of examples that share the same TSR pattern as the last step output of the query. For ${\bm{x}}_{query}={\bm{\mu}}_{q}^{\prime}$, $q\in[K^{\prime}]$, we know that the distribution of the corresponding label ${\bm{y}}$ of ${\bm{x}}$ with $\text{TSR}({\bm{x}})=q$ follows the $q$-th row the $K$-steps transition matrix $B^{f}$. Let $F(\Psi;{\bm{P}})=\sum_{i=1}^{M^{\prime}}\lambda_{i}^{{\bm{P}}}{\bm{\mu}}_{i}^{\prime}$.
Hence, based on the output scheme of ICL as stated in Section 2.3, we have that

$$ ${\bm{v}}=\arg\min_{{\bm{y}}\in\mathcal{M}^{\prime}}\frac{1}{2}\|F(\Psi;{\bm{P} })-{\bm{y}}\|^{2}={\bm{\mu}}_{\arg\max_{i\in[M^{\prime}]}\lambda_{i}^{{\bm{P}} }}.$ (79) $$

Note that the probability of examples with the most probable final output with ${\bm{\mu}}_{q}^{\prime}$ as the TSR pattern of the input is

$$ $B_{(q,\text{TSR}(f({\bm{\mu}}_{q}^{\prime})))}.$ (80) $$

To ensure that the number of examples with the same TSR pattern as the query that generates the most probable output is at least $1$, we compute the following,

$$ $\displaystyle\Pr\left(\frac{1}{l_{ts}}\sum_{i=1}^{l_{ts}}\mathbbm{1}[m_{i}={ \bm{\mu}}_{q_{1}}^{\prime}]\leq(1-\rho_{o}^{f}/2)\alpha^{\prime}B_{(q,\text{ TSR}(f({\bm{\mu}}_{q}^{\prime})))}\right)$ (81) $\displaystyle\leq$ $\displaystyle e^{-l_{ts}{\rho_{o}^{f}}^{2}\alpha^{\prime}B_{(q,\text{TSR}(f({ \bm{\mu}}_{q}^{\prime})))}}=M^{-C},$ $$

for some $c\in(0,1)$ and $C>0$ by the Chernoff bound of Bernoulli distribution in Lemma 1. Here, $m_{i}$ is defined as the TSR pattern in the final output of the $i$-th context example by the $K$-steps transition matrix defined in 12. The TSR pattern of the most probable output of the testing query is ${\bm{\mu}}_{q_{1}}^{\prime}$. Similarly, let the TSR pattern of the second most probable output of the testing query be ${\bm{\mu}}_{q_{2}}^{\prime}$. We also have

$$ $\displaystyle\Pr\left(\frac{1}{l_{ts}}\sum_{i=1}^{l_{ts}}\mathbbm{1}[m_{i}={ \bm{\mu}}_{q_{2}}^{\prime}]\geq(1-\rho_{o}^{f}/2)\alpha^{\prime}B_{(q,q_{1})}^ {f}\right)$ (82) $\displaystyle\leq$ $\displaystyle\Pr\left(\frac{1}{l_{ts}}\sum_{i=1}^{l_{ts}}\mathbbm{1}[m_{i}={ \bm{\mu}}_{q_{2}}^{\prime}]\geq\alpha^{\prime}B_{(q,q_{2})}+c\cdot\rho_{o}^{f} \alpha^{\prime}B_{(q,q_{1})}^{f}\right)$ $\displaystyle\leq$ $\displaystyle e^{-l_{ts}{\rho_{o}^{f}}^{2}c^{2}\alpha^{\prime}B_{(q,q_{1})}}=M ^{-C},$ $$

by Lemma 2 and (12) for some constant $c>0$. Therefore, to make the number of examples with the same TSR pattern in the output as the label of the query be at least $1$ for any TSR pattern of the query and the output be the most probable one, we need

$$ $\displaystyle l_{ts}^{f}\gtrsim$ $\displaystyle\max\{({\rho_{o}^{f}}^{2}\alpha^{\prime}\min_{i\in[M^{\prime}]}B_ {(i,\text{TSR}(f({\bm{\mu}}_{i}^{\prime}))})^{-1}\log M,({\rho_{o}^{f}}\alpha^ {\prime}\min_{i\in[M^{\prime}]}B_{(i,\text{TSR}(f({\bm{\mu}}_{i}^{\prime}))})^ {-2}\log M\}$ (83) $\displaystyle=$ $\displaystyle(\rho_{o}^{f}\alpha^{\prime}\tau_{o}^{f})^{-2}\log M\}.$ $$

In addition, if Condition 1 holds such that the most probable output is the actual label, we can derive

$$ $\bar{R}_{ICL,{\bm{x}}\in\mathcal{M}^{\prime},f\in\mathcal{T}^{\prime}}^{f}( \Psi)=0.$ (84) $$

When (83) holds but Condition 1 does not, we know that ICL still always produces the most probable output by the $K$-steps transition matrix, but such an output is not the label since Condition 1 fails. Hence,

$$ $\bar{R}_{ICL,{\bm{x}}\in\mathcal{M}^{\prime},f\in\mathcal{T}^{\prime}}^{f}( \Psi)\geq\Omega(1).$ (85) $$

When both Condition 1 and (83) do not hold, ICL can produce multiple possible outputs with a non-trivial probability, which is decided by the distribution of the prompt instead of the $K$-steps transition matrix. This can be seen from that (81) and (82) both do not hold since (83) fails. Then, ICL can produce both the most probable and the second most probable output with a constant probability. Let the TSR pattern of the $r$-th most probable output of the testing query be ${\bm{\mu}}_{r}^{\prime}$. Recall that $F(\Psi;{\bm{P}})=\sum_{i=1}^{M^{\prime}}\lambda_{i}^{{\bm{P}}}{\bm{\mu}}_{i}^{\prime}$, we then have that for some small $\epsilon>0$,

$$ $\lambda_{r(q)}^{{\bm{P}}}=\frac{|\{i\in[l_{ts}^{f}]:{\bm{y}}_{i}={\bm{\mu}}_{r }^{\prime}\text{ in }{\bm{P}}\}|}{l_{ts}^{f}}\pm\epsilon.$ (86) $$

Then, by (79), the output of the query is ${\bm{\mu}}_{\arg\max_{r\in[M^{\prime}]}\lambda_{r}}$. Since that (83) does not hold, there exists at least a constant probability of the prompt ${\bm{P}}^{\prime}$ with the same query as ${\bm{P}}$ such that

$$ $\lambda_{r}^{{\bm{P}}^{\prime}}=\frac{|\{i\in[l_{ts}^{f}]:{\bm{y}}_{i}={\bm{ \mu}}_{r}^{\prime}\text{ in }{\bm{P}}^{\prime}\}|}{l_{ts}^{f}}\pm\epsilon\neq \lambda_{r}^{{\bm{P}}},$ (87) $$

for some $r\in[M^{\prime}]$. Therefore, with a constant probability, the output for the same testing query and the same testing task $f$ varies. This leads to

$$ $\bar{R}_{ICL,{\bm{x}}\in\mathcal{M}^{\prime},f\in\mathcal{T}^{\prime}}^{f}( \Psi)\geq\Omega(1).$ (88) $$

∎

###### Proof.

### D.4 Proof of Proposition 1

###### Proof.

This proposition is derived from the proof of Theorem 2. (22) comes from (75), while (23) comes from (76), both by induction.
∎

###### Proof.

## Appendix E Proof of Lemmas

### E.1 Proof of Lemma 3

###### Proof.

$$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}\frac{\partial\ell(\Psi; {\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}}$ (89) $\displaystyle=$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}\frac{\partial\ell(\Psi; {\bm{P}}^{n},{\bm{z}}^{n})}{\partial F(\Psi;{\bm{P}})}\frac{\partial F(\Psi;{ \bm{P}})}{\partial{\bm{W}}}$ $\displaystyle=$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(F(\Psi;{\bm{P}})-{\bm{z }}^{n})^{\top}\sum_{i=1}^{l}{\bm{W}}_{V}\tilde{{\bm{p}}_{i}}\text{softmax}( \tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})$ $\displaystyle\cdot(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text{softmax}(\tilde{{ \bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}_{i}})\tilde{{\bm{p}}_{r}})\tilde{{ \bm{p}}}_{query}^{\top}.$ $$

When $t=0$, we know that each entry of ${\bm{W}}^{(0)}$ is generated from the Gaussian distribution $\mathcal{N}(0,\xi^{2})$. Then,

$$ $|\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}^{(0)}\tilde{{\bm{p}}}_{query}|=|\sum_{k,j }p_{i,k}p_{query,j}W^{(0)}_{k,j}|\lesssim\xi.$ (90) $$

Hence,

$$ $\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}^{(0)}\tilde{{\bm{p}}}_{ query})\geq\frac{e^{-\Theta(\xi)}}{l\cdot e^{\Theta(\xi)}}=\frac{1}{l}\cdot e^ {-\Theta(\xi)},$ (91) $$

$$ $\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}^{(0)}\tilde{{\bm{p}}}_{ query})\leq\frac{e^{-\Theta(\xi)}}{l\cdot e^{\Theta(\xi)}}=\frac{1}{l}\cdot e^ {-\Theta(\xi)}.$ (92) $$

We can obtain

$$ $F(\Psi;{\bm{P}})=\sum_{i=1}^{l}\frac{e^{-\Theta(\xi)}}{l}{\bm{W}}_{V}{\bm{p}}_ {i}.$ (93) $$

Since that $\text{PE}(\cdot)$, and $\text{TRR}(\cdot)$ denote the positional encoding, and the TSR pattern of the input, respectively, we have that for ${\bm{p}}$,

$$ $\tilde{{\bm{p}}}^{\top}\tilde{{\bm{p}}}_{query}=\mathbbm{1}[\text{TRR}(\tilde{ {\bm{p}}})=\text{TRR}(\tilde{{\bm{p}}}_{query})]+\mathbbm{1}[\text{PE}(\tilde{ {\bm{p}}})=\text{PE}(\tilde{\tilde{{\bm{p}}_{i}}})].$ (94) $$

Given $\text{lab}(\cdot)$ is the label embedding of the context as the input, we have that for ${\bm{p}}$,

$$ $\tilde{{\bm{p}}}^{\top}\tilde{{\bm{p}}_{i}}=\mathbbm{1}[\text{TRR}(\tilde{{\bm {p}}})=\text{TRR}(\tilde{{\bm{p}}_{i}})]+\mathbbm{1}[\text{lab}(\tilde{{\bm{p} }})=\text{lab}(\tilde{{\bm{p}}_{i}})]+\mathbbm{1}[\text{PE}(\tilde{{\bm{p}}})= \text{PE}(\tilde{{\bm{p}}_{i}})],$ (95) $$

$$ $({\bm{W}}_{V}\tilde{{\bm{p}}})^{\top}{\bm{W}}_{V}\tilde{{\bm{p}}_{i}}=\mathbbm {1}[\text{lab}(\tilde{{\bm{p}}})=\text{lab}(\tilde{{\bm{p}}_{i}})].$ (96) $$

When $t\geq 1$, we first consider the case where $\tilde{{\bm{p}}}$ shares the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}}_{query}$.
If $\tilde{{\bm{p}}}$ and $\tilde{{\bm{p}}}_{query}$ share the same TRR pattern, label pattern, and the positional encoding,

$$ $\displaystyle\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text{ softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq$ $\displaystyle 2\cdot(3-3p_{n}(t)-(1-p_{n}(t)))$ (97) $\displaystyle=$ $\displaystyle 4(1-p_{n}(t)),$ $$

and

$$ $\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text{softmax}( \tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{\bm{p}}_{r }})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\leq 2\cdot(3-3p_{n}(t))=6(1 -p_{n}(t)).$ (98) $$

When $\tilde{{\bm{p}}}$ and $\tilde{{\bm{p}}}_{query}$ only share the same positional encoding or the same TRR pattern,

$$ $2-6p_{n}(t)\geq\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l} \text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-4p_{n }(t).$ (99) $$

When $\tilde{{\bm{p}}}$ and $\tilde{{\bm{p}}}_{query}$ share both different positional encodings and TRR patterns,

$$ $-6p_{n}(t)\geq\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}_{qeury}}^{\top}\tilde{{\bm{p}}}\geq-2-4p_{n}(t).$ (100) $$

Then, we consider the case where $\tilde{{\bm{p}}}$ only shares the same TRR pattern or the same positional encoding as $\tilde{{\bm{p}}_{i}}$.
If $\tilde{{\bm{p}}}$ and $\tilde{{\bm{p}}}_{query}$ share the same TRR pattern, label pattern, and the positional encoding,

$$ $\displaystyle 3-p_{n}(t)\geq\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_ {r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq$ $\displaystyle 1\cdot(3-p_{n}(t)-(1-p_{n}(t)))$ (101) $\displaystyle=$ $\displaystyle 2.$ $$

When $\tilde{{\bm{p}}}$ and $\tilde{{\bm{p}}}_{query}$ only share the same positional encoding or the same TRR pattern,

$$ $1-p_{n}(t)\geq\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq 0.$ (102) $$

When $\tilde{{\bm{p}}}$ and $\tilde{{\bm{p}}}_{query}$ only share both different positional encodings and TRR patterns,

$$ $-p_{n}(t)\geq\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text{ softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-1.$ (103) $$

Note that $-(1-p_{n}(t))p_{n}(t)+(1-p_{n}(t))^{2}\alpha^{2}/K^{2}<0$ for $p_{n}(t)\in[\alpha/K,\alpha]$. Then, when $l\geq\Omega(\alpha^{-1})$ and $\tilde{{\bm{p}}}$ shares the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}_{i}}$,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (104) $\displaystyle\cdot\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l} \text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle-4p_{n}(t)(1-p_{n}(t))^{2}-4p_{n}(t)(1-p_{n}(t))^{2}\cdot\frac{ \alpha^{2}}{K^{2}}$ $\displaystyle+\frac{1}{l}(\frac{1}{K}-\frac{\alpha}{K})(-4p_{n}(t))+\frac{1}{l }(\frac{1}{K}-\frac{\alpha}{K})(1-p_{n}(t))(-2-4p_{n}(t))(K-1)$ $\displaystyle=$ $\displaystyle-4p_{n}(t)(1-p_{n}(t))^{2}(1+\frac{\alpha^{2}}{K^{2}})+\frac{2}{ lK}(1-\alpha)(-(K-1)-(K+1)p_{n}(t)+2p_{n}(t)^{2}(K-1)).$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same TRR pattern and the different positional encoding as $\tilde{{\bm{p}}}_{query}$. Note that

$$ $\frac{2}{Kl}\cdot(1-\alpha)\cdot K(1-p_{n}(t))\lesssim|(-(1-p_{n}(t))p_{n}(t)+ (1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{2}})(1-p_{n}(t))|,$ (105) $$

if $l\geq\Omega(\alpha^{-1})$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (106) $\displaystyle\cdot\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l} \text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle-0\cdot p_{n}(t)(1-p_{n}(t))+(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^ {2}}\cdot(+2)+\frac{1}{l}(\frac{1}{K}-\frac{\alpha}{K})(-(K-1))$ $\displaystyle=$ $\displaystyle 2(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{2}}-\frac{K-1}{l}(\frac{1} {K}-\frac{\alpha}{K}).$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same positional encoding and the different TRR pattern as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (107) $\displaystyle\cdot\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l} \text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}_{i}}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle 0-(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{2}}+\frac{1}{l}(\frac{1}{ K}-\frac{\alpha}{K})(-(K-1))$ $\displaystyle=$ $\displaystyle-(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{2}}-\frac{K-1}{l}(\frac{1}{ K}-\frac{\alpha}{K}).$ $$

Therefore, as long as

$$ $l\geq\Omega(\alpha^{-1}),$ (108) $$

we have

$$ $\displaystyle\tilde{{\bm{p}}}^{\top}\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}} \frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}}{\bm{p}}$ (109) $\displaystyle=$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(F(\Psi;{\bm{P}})-{\bm{z }}^{n})^{\top}\sum_{i=1}^{l}{\bm{W}}_{V}\tilde{{\bm{p}}_{i}}\text{softmax}( \tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})$ $\displaystyle\cdot\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l} \text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}_{i}}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(1-p_{n}(t) )^{2}(-4p_{n}(t)(1+\frac{\alpha^{2}}{K^{2}})+\frac{2(K-1)\alpha^{2}}{K^{2}})$ $\displaystyle\cdot+(\frac{1}{K}-\frac{1}{M})(-(1-p_{n}(t))^{2}\frac{\alpha^{2} }{K^{2}}))$ $\displaystyle=$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(1-p_{ n}(t))^{2}(-4p_{n}(t)(1+\frac{\alpha^{2}}{K^{2}})+\frac{\alpha^{2}}{K}(1+\frac {2(K-1)}{K}))-\frac{\alpha^{2}}{K^{3}}(1-p_{n}(t))^{2}).$ $$

We then consider the case where $\tilde{{\bm{p}}}^{\prime}$ shares a different positional encoding and the same TRR pattern as $\tilde{{\bm{p}}}$. Let $\tilde{{\bm{p}}}$ share the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}}_{query}$.
If $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ share the same TRR pattern, label pattern, and the positional encoding,

$$ $\displaystyle 2(3-p_{n}(t))\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p} }_{i}}-\sum_{r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{ {\bm{p}}}_{query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{ \bm{p}}}\geq$ $\displaystyle 2\cdot(3-p_{n}(t)-(1-p_{n}(t)))$ (110) $\displaystyle=$ $\displaystyle 4.$ $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}}_{query}$ only share the same positional encoding or the same TRR pattern,

$$ $2(1-p_{n}(t))\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r =1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}} \geq 0.$ (111) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share both different positional encodings and TRR patterns,

$$ $-2p_{n}(t)\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1} ^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query} )\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-2.$ (112) $$

Then, we consider the case where $\tilde{{\bm{p}}}$ only shares the same TRR pattern as $\tilde{{\bm{p}}}_{query}$. If $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ share the same TRR pattern, label pattern, and the positional encoding,

$$ $\displaystyle 3-p_{n}(t))\geq$ $\displaystyle\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^ {l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ (113) $\displaystyle\geq$ $\displaystyle 1\cdot(3-3p_{n}(t)-(1-p_{n}(t)))=2(1-p_{n}(t)).$ $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share the same positional encoding or the same TRR pattern,

$$ $1-p_{n}(t)\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1} ^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query} )\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-2p_{ n}(t).$ (114) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share both different positional encodings and TRR patterns,

$$ $-p_{n}(t)\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^ {l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-1-2p_ {n}(t).$ (115) $$

Next, we consider the case where $\tilde{{\bm{p}}}$ only shares the same positional encoding as $\tilde{{\bm{p}}}_{query}$. If $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ share the same TRR pattern, label pattern, and the positional encoding,

$$ $3\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq 1\cdot(3-(1-p _{n}(t)))=2+p_{n}(t).$ (116) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share the same positional encoding or the same TRR pattern,

$$ $1\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq p_{n}(t).$ (117) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share both different positional encodings and TRR patterns,

$$ $0\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-1+p_{n}(t).$ (118) $$

Then, when $l\geq\Omega(\alpha^{-1})$ and $\tilde{{\bm{p}}}$ shares the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}}_{query}$,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (119) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle-4p_{n}(t)(1-p_{n}(t))+\frac{1}{l}(\frac{1}{K}-\frac{\alpha}{K})( -2K).$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same TRR pattern and the different positional encoding as $\tilde{{\bm{p}}}_{query}$.
Then, by (105),

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (120) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle-2p_{n}(t)(1-p_{n}(t))^{2}-2p_{n}(t)(1-p_{n}(t))^{2}\cdot\frac{ \alpha^{2}}{K^{2}}+\frac{1}{l}(\frac{1}{K}-\frac{\alpha}{K})((-1-2p_{n}(t))K)$ $\displaystyle=$ $\displaystyle-2p_{n}(t)(1-p_{n}(t))^{2}(1+\frac{\alpha^{2}}{K^{2}})+\frac{1}{l }(1-\alpha)(-1-2p_{n}(t)).$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same positional encoding and the different TRR pattern as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (121) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle p_{n}(t)(1-p_{n}(t))^{2}+p_{n}(t)(1-p_{n}(t))^{2}\frac{\alpha^{2 }}{K^{2}}+\frac{1}{l}(1-\alpha)(-1-2p_{n}(t))$ $\displaystyle=$ $\displaystyle p_{n}(t)(1-p_{n}(t))^{2}(1+\frac{\alpha^{2}}{K^{2}})-\frac{1}{l} (1-\alpha)(1+2p_{n}(t)).$ $$

Therefore, as long as

$$ $l\geq\Omega(\alpha^{-1}),$ (122) $$

we have

$$ $\displaystyle\tilde{{\bm{p}}^{\prime}}^{\top}\eta\frac{1}{B}\sum_{n\in\mathcal {B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}} \tilde{{\bm{p}}}$ (123) $\displaystyle=$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(F(\Psi;{\bm{P}})-{\bm{z }}^{n})^{\top}\sum_{i=1}^{l}{\bm{W}}_{V}\tilde{{\bm{p}}_{i}}\text{softmax}( \tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{\bm{p}}}^{ \top}(\tilde{{\bm{p}}_{i}}$ $\displaystyle-\sum_{r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top} \tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(-4-2(K-1)( 1-p_{n}(t))(1+\frac{\alpha^{2}}{K^{2}}))p_{n}(t)(1-p_{n}(t))$ $\displaystyle+(\frac{1}{K}-\frac{1}{M})p_{n}(t)(1-p_{n}(t))^{2}(1+\frac{\alpha ^{2}}{K^{2}}))$ $\displaystyle=$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(-4-(3 K-2)(1-p_{n}(t))(1+\frac{\alpha^{2}}{K^{2}}))p_{n}(t)(1-p_{n}(t))$ $\displaystyle+\frac{1}{K}p_{n}(t)(1-p_{n}(t))^{2}(1+\frac{\alpha^{2}}{K^{2}})),$ $$

and

$$ $\displaystyle\tilde{{\bm{p}}^{\prime}}^{\top}\eta\frac{1}{B}\sum_{n\in\mathcal {B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}} \tilde{{\bm{p}}}$ (124) $\displaystyle\geq$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(-4-(3K-2)( 1-p_{n}(t))(1+\frac{\alpha^{2}}{K^{2}}))p_{n}(t)(1-p_{n}(t))$ $\displaystyle+\frac{1}{K}p_{n}(t)(1-p_{n}(t))^{2}(1+\frac{\alpha^{2}}{K^{2}})+ \frac{1}{K}\cdot(1-p_{n}(t))^{2}(-p_{n}(t)+(1-p_{n}(t))\frac{\alpha^{2}}{K^{2} }))$ $\displaystyle=$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(-4-(3 K-2)(1-p_{n}(t))(1+\frac{\alpha^{2}}{K^{2}}))p_{n}(t)(1-p_{n}(t))+\frac{\alpha ^{2}}{K^{3}}(1-p_{n}(t))^{2}).$ $$

We next consider the case where $\tilde{{\bm{p}}}^{\prime}$ shares a different TRR pattern and the same positional encoding as $\tilde{{\bm{p}}}$. Let $\tilde{{\bm{p}}}$ share the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}}_{query}$.
If $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ share the same TRR pattern, label pattern, and positional encoding,

$$ $\displaystyle 2(3-p_{n}(t))\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p} }_{i}}-\sum_{r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{ {\bm{p}}}_{query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{ \bm{p}}}\geq$ $\displaystyle 2\cdot(3-p_{n}(t)-(1-p_{n}(t)))$ (125) $\displaystyle=$ $\displaystyle 4.$ $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share the same positional encoding or the same TRR pattern,

$$ $2(1-p_{n}(t))\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r =1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}} \geq 0.$ (126) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share both different positional encodings and TRR patterns,

$$ $-2p_{n}(t)\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1} ^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query} )\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-2.$ (127) $$

Then, we consider the case where $\tilde{{\bm{p}}}$ only shares the same TRR pattern as $\tilde{{\bm{p}}}_{query}$. If $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ share the same TRR pattern, label pattern, and the positional encoding,

$$ $3\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq 1\cdot(3-(1-p _{n}(t)))=2+p_{n}(t).$ (128) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share the same positional encoding or the same TRR pattern,

$$ $1\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq p_{n}(t).$ (129) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share both different positional encodings and TRR patterns,

$$ $0\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-1+p_{n}(t).$ (130) $$

Next, we consider the case where $\tilde{{\bm{p}}}$ only shares the same positional encoding as $\tilde{{\bm{p}}}_{query}$. If $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ share the same TRR pattern, label pattern, and the positional encoding,

$$ $3-p_{n}(t)\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1} ^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query} )\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq 1 \cdot(3-p_{n}(t)-(1-p_{n}(t)))=2.$ (131) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share the same positional encoding or the same TRR pattern,

$$ $1-p_{n}(t)\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1} ^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query} )\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq 0.$ (132) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share both different positional encodings and TRR patterns,

$$ $-p_{n}(t)\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^ {l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-1.$ (133) $$

Then, when $l\geq\Omega(\alpha^{-1})$, and when $\tilde{{\bm{p}}}$ shares the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}}_{query}$, by (105),

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (134) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle 0-2(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{2}}+\frac{1}{l}(\frac{1} {K}-\frac{\alpha}{K})(-2(K-1)).$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same TRR pattern and the different positional encoding as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (135) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle-p_{n}(t)(1-p_{n}(t))(-1+p_{n}(t))+p_{n}(t)(1-p_{n}(t))^{2}\cdot \frac{\alpha^{2}}{K^{2}}+\frac{1}{l}(\frac{1}{K}-\frac{\alpha}{K})K(-1+p_{n}(t))$ $\displaystyle=$ $\displaystyle p_{n}(t)(1-p_{n}(t))^{2}(\frac{\alpha^{2}}{K^{2}}+1)+\frac{1}{l} (1-\alpha)(-1+p_{n}(t)).$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same positional encoding and the different TRR pattern as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (136) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle-(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{2}}-0+\frac{1}{l}(\frac{1}{ K}-\frac{\alpha}{K})(-K+1)$ $\displaystyle=$ $\displaystyle-(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{2}}-\frac{K-1}{Kl}(1-\alpha).$ $$

Therefore, as long as

$$ $l\geq\Omega(\alpha^{-1}),$ (137) $$

we have

$$ $\displaystyle\tilde{{\bm{p}}^{\prime}}^{\top}\eta\frac{1}{B}\sum_{n\in\mathcal {B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}}{ \bm{p}}$ (138) $\displaystyle=$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(F(\Psi;{\bm{P}})-{\bm{z }}^{n})^{\top}\sum_{i=1}^{l}{\bm{W}}_{V}\tilde{{\bm{p}}_{i}}\text{softmax}( \tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})$ $\displaystyle\cdot\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l} \text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(-\frac{ \alpha^{2}}{K^{2}}+(K-1)(1+\frac{\alpha^{2}}{K^{2}})p_{n}(t))(1-p_{n}(t))^{2}- (\frac{1}{K}$ $\displaystyle-\frac{1}{M})(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{2}}))$ $\displaystyle=$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(- \frac{\alpha^{2}}{K^{2}}+(K-1+\frac{(2K-1)\alpha^{2}}{K^{2}})p_{n}(t))(1-p_{n} (t))^{2}-(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{3}}).$ $$

and

$$ $\displaystyle\tilde{{\bm{p}}^{\prime}}^{\top}\eta\frac{1}{B}\sum_{n\in\mathcal {B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}}{ \bm{p}}$ (139) $\displaystyle\geq$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(- \frac{\alpha^{2}}{K^{2}}+(K-1+\frac{(2K-1)\alpha^{2}}{K^{2}})p_{n}(t))(1-p_{n} (t))^{2}-(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{3}}$ $\displaystyle+\frac{1}{K}\cdot(1-p_{n}(t))^{2}(-p_{n}(t)+(1-p_{n}(t))\frac{ \alpha^{2}}{K^{2}})).$ $$

We next consider the case where $\tilde{{\bm{p}}}^{\prime}$ shares a different TRR pattern and a different positional encoding as $\tilde{{\bm{p}}}$. Let $\tilde{{\bm{p}}}$ share the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}}_{query}$.
If $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ share the same TRR pattern, label pattern, and the positional encoding,

$$ $6\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq 2\cdot(3-(1-p _{n}(t)))=4+2p_{n}(t).$ (140) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share the same positional encoding or the same TRR pattern,

$$ $2\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq 2p_{n}(t).$ (141) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share both different positional encodings and TRR patterns,

$$ $0\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-2+2p_{n}(t).$ (142) $$

Then, we consider the case where $\tilde{{\bm{p}}}$ only shares the same TRR pattern as $\tilde{{\bm{p}}}_{query}$. If $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ share the same TRR pattern, label pattern, and the positional encoding,

$$ $3\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq 1\cdot(3-p_{n }(t)-(1-p_{n}(t)))=2.$ (143) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share the same positional encoding or the same TRR pattern,

$$ $1\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq 0.$ (144) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share both different positional encodings and TRR patterns,

$$ $0\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-1.$ (145) $$

Next, we consider the case where $\tilde{{\bm{p}}}$ only shares the same positional encoding as $\tilde{{\bm{p}}}_{query}$. If $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ share the same TRR pattern, label pattern, and the positional encoding,

$$ $3\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq 1\cdot(3-(1-p _{n}(t)))=2+p_{n}(t).$ (146) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share the same positional encoding or the same TRR pattern,

$$ $1\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq p_{n}(t).$ (147) $$

When $\tilde{{\bm{p}}}^{\prime}$ and $\tilde{{\bm{p}}_{i}}$ only share both different positional encodings and TRR patterns,

$$ $0\geq\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text {softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\geq-1+p_{n}(t).$ (148) $$

Then, when $l\geq\Omega(\alpha^{-1})$, and when $\tilde{{\bm{p}}}$ shares the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}}_{query}$,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (149) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle-p_{n}(t)(1-p_{n}(t))(-2+2p_{n}(t))+(1-p_{n}(t))^{2}\frac{\alpha^ {2}}{K^{2}}\cdot 2p_{n}(t)+\frac{1}{l}(1-\alpha)(-2+2p_{n}(t)).$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same TRR pattern and the different positional encoding as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (150) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle 0+p_{n}(t)(1-p_{n}(t))^{2}\cdot\frac{\alpha^{2}}{K^{2}}\cdot(-1) +\frac{1}{l}(\frac{1}{K}-\frac{\alpha}{K})(-K)$ $\displaystyle=$ $\displaystyle-p_{n}(t)(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{2}}+\frac{1}{l}(1- \alpha)(-1).$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same positional encoding and the different TRR pattern as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (151) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle-(1-p_{n}(t))p_{n}(t)(-1+p_{n}(t))+p_{n}(t)(1-p_{n}(t))^{2}\frac{ \alpha^{2}}{K^{2}}+\frac{1}{l}(\frac{1}{K}-\frac{\alpha}{K})(-1+p_{n}(t))K$ $\displaystyle=$ $\displaystyle(1-p_{n}(t))^{2}p_{n}(t)(1+\frac{\alpha^{2}}{K^{2}})+\frac{1}{l}( 1-\alpha)(-1+p_{n}(t)).$ $$

Therefore, as long as

$$ $l\geq\Omega(\alpha^{-1}),$ (152) $$

we have

$$ $\displaystyle\tilde{{\bm{p}}^{\prime}}^{\top}\eta\frac{1}{B}\sum_{n\in\mathcal {B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}}{ \bm{p}}$ (153) $\displaystyle=$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(F(\Psi;{\bm{P}})-{\bm{z }}^{n})^{\top}\sum_{i=1}^{l}{\bm{W}}_{V}\tilde{{\bm{p}}_{i}}\text{softmax}( \tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})$ $\displaystyle\cdot\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l} \text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}(-p_{n}(t)( 1-p_{n}(t))(-2+2p_{n}(t))+(3-K)(1-p_{n}(t))^{2}\frac{\alpha^{2}}{K^{2}}\cdot p _{n}(t))$ $\displaystyle+(\frac{1}{K}-\frac{1}{M})(1-p_{n}(t))^{2}p_{n}(t)(1+\frac{\alpha ^{2}}{K^{2}}))$ $\displaystyle=$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}p_{n}( t)(1-p_{n}(t))^{2}(2-K+\frac{(2-K)\alpha^{2}}{K^{2}})$ $\displaystyle+(1-p_{n}(t))^{2}p_{n}(t)(1+\frac{\alpha^{2}}{K^{2}})\cdot\frac{1 }{K}),$ $$

and

$$ $\displaystyle\tilde{{\bm{p}}^{\prime}}^{\top}\eta\frac{1}{B}\sum_{n\in\mathcal {B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}}{ \bm{p}}$ (154) $\displaystyle\geq$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}p_{n}( t)(1-p_{n}(t))^{2}(1+\frac{(2-K)\alpha^{2}}{K^{2}})+(1-p_{n}(t))^{2}p_{n}(t)(1 +\frac{\alpha^{2}}{K^{2}})\cdot\frac{1}{K}$ $\displaystyle+\frac{1}{K}\cdot(1-p_{n}(t))^{2}(-p_{n}(t)+(1-p_{n}(t))\frac{ \alpha^{2}}{K^{2}}))$ $\displaystyle=$ $\displaystyle\eta\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{KM}p_{n}( t)(1-p_{n}(t))^{2}(1+\frac{(2-K)\alpha^{2}}{K^{2}})+(1-p_{n}(t))^{2}\cdot\frac {\alpha^{2}}{K^{3}}).$ $$

∎

###### Proof.

### E.2 Proof of Lemma 4

###### Proof.

We can derive that when $1-p_{n}(t)\geq\Omega(1)$, $\tilde{{\bm{p}}^{\prime}}^{\top}{\bm{W}}^{(t)}\tilde{{\bm{p}}}$ increases if $\tilde{{\bm{p}}}$ and $\tilde{{\bm{p}}}^{\prime}$ share the same positional encoding. Otherwise, $\tilde{{\bm{p}}^{\prime}}^{\top}{\bm{W}}^{(t)}\tilde{{\bm{p}}}$ decreases. We know that $p_{n}(t)\geq\frac{\alpha}{2}$. Combining the results in Lemma 3, we can derive that when $t\geq 1$,

$$ ${\bm{W}}^{(t+1)}={\bm{W}}^{(t)}-\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}\frac {\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}^{(t)}}.$ (155) $$

Then, for $\tilde{{\bm{p}}_{i}}^{n}$ that share the same TRR pattern and the same positional encoding of $\tilde{{\bm{p}}}_{query}^{n}$,

$$ $\displaystyle\frac{p_{n}(t+1)}{|\mathcal{S}_{1}^{n}|}=\text{softmax}({{\bm{p}} ^{n}_{i}}^{\top}{\bm{W}}^{(t+1)}\tilde{{\bm{p}}}_{query}^{n})$ (156) $\displaystyle\geq$ $\displaystyle\frac{1}{l}\cdot\frac{1}{\frac{\alpha}{K}+\frac{(K-1)\alpha}{K} \cdot e^{-s_{1}}+(\frac{1}{K}-\frac{\alpha}{K})((K-1)e^{-s_{2}}+e^{-s_{3}})},$ $$

where

$$ $\displaystyle s_{1}\geq$ $\displaystyle\eta\sum_{b=0}^{t}((1-p_{n}(b))^{2}\frac{\alpha^{2}}{K^{3}}+\frac {\alpha^{2}}{K^{3}}(1-p_{n}(b))^{2})=\eta\sum_{b=0}^{t}(1-p_{n}(b))^{2}\frac{2 \alpha^{2}}{K^{3}},$ (157) $$

$$ $\displaystyle s_{2}\geq$ $\displaystyle\sum_{b=0}^{t}(1-p_{n}(b))^{2}\cdot\frac{2\eta\alpha^{2}}{K^{3}},$ (158) $$

$$ $\displaystyle s_{3}\geq$ $\displaystyle-\frac{\eta}{KM}\sum_{b=0}^{t}(1-p_{n}(b)^{2}(-4p_{n}(b)(1+\frac{ \alpha^{2}}{K^{2}})+\frac{\alpha^{2}}{K}(1+\frac{2(K-1)}{K})+\frac{\alpha^{2}} {K^{2}}$ (159) $\displaystyle-(K-1+\frac{2K-1}{K^{2}}\alpha^{2})p_{n}(b)))$ $\displaystyle\geq$ $\displaystyle\frac{\eta}{KM}\sum_{b=0}^{t}(1-p_{n}(b))^{2}(p_{n}(b)(3+\frac{ \alpha^{2}}{K^{2}})(4+\frac{2K-1}{K^{2}})),$ $$

where the last step is by $Kp_{n}(b)\geq 4\alpha^{2}/K^{2}$ when $p_{n}(b)\geq\alpha/K$.
For $\tilde{{\bm{p}}_{i}}^{n}$ that share the same TRR pattern and a different positional encoding of $\tilde{{\bm{p}}}_{query}^{n}$,

$$ $\displaystyle\text{softmax}({\tilde{{\bm{p}}_{i}^{n}}}^{\top}{\bm{W}}^{(t+1)} \tilde{{\bm{p}}}_{query}^{n})=$ $\displaystyle\frac{1}{l}\cdot\frac{1}{\frac{\alpha}{K}e^{s_{1}}+\frac{(K-1) \alpha}{K}+(\frac{1}{K}-\frac{\alpha}{K})((K-1)e^{-s_{4}}+e^{s_{5}})},$ (160) $$

where

$$ $\displaystyle s_{4}\geq$ $\displaystyle-\sum_{b=0}^{t}\frac{\eta}{M}((-4-(3K-2)(1-p_{n}(b))(1+\frac{ \alpha^{2}}{K^{2}}))p_{n}(b)(1-p_{n}(b))$ (161) $\displaystyle-(2-K)(1+\frac{\alpha^{2}}{K^{2}})p_{n}(b)(1-p_{n}(b))^{2})$ $\displaystyle=$ $\displaystyle\sum_{b=0}^{t}\frac{\eta}{M}(4+2K(1-p_{n}(b))(1+\frac{\alpha^{2}} {K^{2}}))p_{n}(b)(1-p_{n}(b)),$ $$

$$ $\displaystyle s_{5}\geq$ $\displaystyle\sum_{b=0}^{t}(1-p_{n}(b))^{2}\cdot\frac{2\eta\alpha^{2}}{K^{3}}.$ (162) $$

When $M\geq\Omega(K^{4}\alpha^{-1})$ and $t\geq\Omega(\eta^{-1}K^{3}\log K\alpha^{-2})$,

$$ $(K-1)e^{-s_{4}}+e^{s_{5}}>K.$ (163) $$

If $M\geq\Omega(K^{4}\alpha^{-1})$ and $t\leq O(\eta^{-1}K^{3}\log K\alpha^{-2})$, we cannot ensure

$$ $(K-1)e^{-s_{4}}+e^{s_{5}}>K.$ (164) $$

For $\tilde{{\bm{p}}_{i}^{n}}$ that share a different TRR pattern and the same positional encoding of $\tilde{{\bm{p}}}_{query}^{n}$,

$$ $\displaystyle\text{softmax}({\tilde{{\bm{p}}_{i}^{n}}}^{\top}{\bm{W}}^{(t+1)} \tilde{{\bm{p}}}_{query}^{n})=$ $\displaystyle\frac{1}{l}\cdot\frac{1}{\frac{\alpha}{K}e^{s_{3}}+\frac{\alpha}{ K}\cdot e^{-s_{4}}+(\frac{1}{K}-\frac{\alpha}{K})(1+(K-1)e^{-s_{6}})},$ (165) $$

where

$$ $s_{6}\geq\eta\sum_{b=0}^{t}\frac{2\alpha^{2}}{K^{3}}(1-p_{n}(b))^{2}.$ (166) $$

For $\tilde{{\bm{p}}_{i}^{n}}$ that share a different TRR pattern and a different positional encoding of $\tilde{{\bm{p}}}_{query}^{n}$,

$$ $\displaystyle\text{softmax}({\tilde{{\bm{p}}_{i}^{n}}}^{\top}{\bm{W}}^{(t+1)} \tilde{{\bm{p}}}_{query}^{n})=$ $\displaystyle\frac{1}{l}\cdot\frac{1}{\frac{\alpha}{K}e^{s_{2}}+(\frac{1}{K}- \frac{\alpha}{K})(K-1+e^{s_{6}})+\frac{\alpha}{K}e^{s_{4}}}.$ (167) $$

Note that when $t\lesssim\eta^{-1}\alpha^{-2}K^{3}$, for ${\bm{p}}_{query}^{n}$ in the $k$-th step, we have

$$ $\sum_{i\in\mathcal{S}_{[K]\backslash\{k\}}}\text{softmax}({\tilde{{\bm{p}}_{i} ^{n}}}^{\top}{\bm{W}}^{(t+1)}\tilde{{\bm{p}}}_{query}^{n})\geq\Omega(1),$ (168) $$

for $\tilde{{\bm{p}}_{i}^{n}}$ that share a different positional encoding from $\tilde{{\bm{p}}}_{query}^{n}$. To make the total softmax values on contexts that share a different positional encoding and a different TRR pattern from the query smaller than $\epsilon$, we need

$$ $s_{1},s_{2},s_{6}\gtrsim\log\frac{K}{\epsilon}.$ (169) $$

When $t$ further increases to be larger than $\Omega(\eta^{-1}\alpha^{-2}K^{3}\log\frac{K}{\epsilon})$, we also have that the total softmax values on contexts that share a different positional encoding and the same TRR pattern from the query smaller than $\epsilon$.
Therefore,

$$ $t\gtrsim T_{1}:=\eta^{-1}\alpha^{-2}K^{3}\log\frac{K}{\epsilon}.$ (170) $$

∎

###### Proof.

### E.3 Proof of Lemma 5

###### Proof.

We consider the case when $t\geq T_{1}$ given Lemma 4. When $l\geq\Omega(\alpha^{-1})$, and when $\tilde{{\bm{p}}}$ shares the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}}_{query}$,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (171) $\displaystyle\cdot\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l} \text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\leq$ $\displaystyle-4p_{n}(t)(1-p_{n}(t))^{2}+\epsilon$ $\displaystyle\lesssim$ $\displaystyle-4p_{n}(t)(1-p_{n}(t))^{2}.$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same TRR pattern and the different positional encoding as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (172) $\displaystyle\cdot\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l} \text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\lesssim$ $\displaystyle-0\cdot p_{n}(t)(1-p_{n}(t))+\epsilon$ $\displaystyle\lesssim$ $\displaystyle\epsilon.$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same positional encoding and the different TRR pattern as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (173) $\displaystyle\cdot\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l} \text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\lesssim$ $\displaystyle\epsilon.$ $$

Therefore,

$$ $\displaystyle\tilde{{\bm{p}}}^{\top}\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}} \frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}}{\bm{p}}$ (174) $\displaystyle=$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(F(\Psi;{\bm{P}})-{\bm{z }}^{n})^{\top}\sum_{i=1}^{l}{\bm{W}}_{V}\tilde{{\bm{p}}_{i}}\text{softmax}( \tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})$ $\displaystyle\cdot\tilde{{\bm{p}}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l} \text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}) \tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\lesssim$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(\frac{1}{2M}(-4p_{n}(t) (1-p_{n}(t))^{2})+(\frac{1}{2}-\frac{1}{M})\cdot\epsilon$ $\displaystyle=$ $\displaystyle-\eta\cdot\frac{1}{2M}\cdot\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}4 p_{n}(t)(1-p_{n}(t))^{2}.$ $$

We then discuss if $\tilde{{\bm{p}}}$ and $\tilde{{\bm{p}}}^{\prime}$ only share the same TRR pattern. When $l\geq\Omega(\alpha^{-1})$, and when $\tilde{{\bm{p}}}$ shares the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}}_{query}$, we can obtain

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (175) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\gtrsim$ $\displaystyle-2(1-p_{n}(t))^{2}p_{n}(t).$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same TRR pattern and the different positional encoding as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (176) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\gtrsim$ $\displaystyle-(1-p_{n}(t))(1-p_{n}(t))p_{n}(t).$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same positional encoding and the different TRR pattern as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle\Big{|}(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{ \bm{W}}\tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n}) ^{\top}\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{ {\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (177) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}} \Big{|}$ $\displaystyle\lesssim$ $\displaystyle\epsilon.$ $$

Therefore,

$$ $\displaystyle\left|\tilde{{\bm{p}}^{\prime}}^{\top}\eta\frac{1}{B}\sum_{n\in \mathcal{B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{ \bm{W}}}{\bm{p}}\right|$ (178) $\displaystyle=$ $\displaystyle\Big{|}\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(F(\Psi;{\bm{P}}) -{\bm{z}}^{n})^{\top}\sum_{i=1}^{l}{\bm{W}}_{V}\tilde{{\bm{p}}_{i}}\text{ softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}}^{\top}$ $\displaystyle\cdot(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text{softmax}(\tilde{{ \bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{\bm{p}}_{r}}) \tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}\Big{|}$ $\displaystyle\leq$ $\displaystyle\eta\epsilon.$ $$

We next discuss when $\tilde{{\bm{p}}}$ only shares the same positional encoding as $\tilde{{\bm{p}}}^{\prime}$. When $l\geq\Omega(\alpha^{-1})$, and when $\tilde{{\bm{p}}}$ shares the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}}_{query}$,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (179) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\lesssim$ $\displaystyle\epsilon.$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same TRR pattern and the different positional encoding as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (180) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\lesssim$ $\displaystyle-p_{n}(t)(1-p_{n}(t))(-1+p_{n}(t))+\frac{1}{M}$ $\displaystyle\lesssim$ $\displaystyle p_{n}(t)(1-p_{n}(t))^{2}.$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same positional encoding and the different TRR pattern as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (181) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\lesssim$ $\displaystyle\epsilon.$ $$

Therefore,

$$ $\displaystyle\tilde{{\bm{p}}^{\prime}}^{\top}\eta\frac{1}{B}\sum_{n\in\mathcal {B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{\bm{W}}}{ \bm{p}}$ (182) $\displaystyle=$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(F(\Psi;{\bm{P}})-{\bm{z }}^{n})^{\top}\sum_{i=1}^{l}{\bm{W}}_{V}\tilde{{\bm{p}}_{i}}\text{softmax}( \tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{\bm{p}}}^{\top}$ $\displaystyle\cdot(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text{softmax}(\tilde{{ \bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{\bm{p}}_{r}}) \tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\lesssim$ $\displaystyle\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}\frac{1}{2M}\cdot p_{n}( b)(1-p_{n}(b))^{2}.$ $$

We then consider if $\tilde{{\bm{p}}}$ shares a different TRR pattern and a different positional encoding as $\tilde{{\bm{p}}}^{\prime}$. When $l\geq\Omega(\alpha^{-1})$, and when $\tilde{{\bm{p}}}$ shares the same TRR pattern and the positional encoding as $\tilde{{\bm{p}}}_{query}$,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (183) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\gtrsim$ $\displaystyle\epsilon.$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same TRR pattern and the different positional encoding as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}} \tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n})^{\top} \sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p} }}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (184) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}}$ $\displaystyle\gtrsim$ $\displaystyle-(1-p_{n}(t))p_{n}(t).$ $$

We next consider the case where $\tilde{{\bm{p}}}$ shares the same positional encoding and the different TRR pattern as $\tilde{{\bm{p}}}_{query}$.
Then,

$$ $\displaystyle\Big{|}(\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{ \bm{W}}\tilde{{\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}-{\bm{z}}^{n}) ^{\top}\sum_{i=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{ {\bm{p}}}_{query}){\bm{W}}_{V}\tilde{{\bm{p}}_{i}}$ (185) $\displaystyle\cdot\tilde{{\bm{p}}^{\prime}}^{\top}(\tilde{{\bm{p}}_{i}}-\sum_{ r=1}^{l}\text{softmax}(\tilde{{\bm{p}}_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{ query})\tilde{{\bm{p}}_{r}})\tilde{{\bm{p}}}_{query}^{\top}\tilde{{\bm{p}}} \Big{|}$ $\displaystyle\lesssim$ $\displaystyle\epsilon.$ $$

Therefore,

$$ $\displaystyle\left|\tilde{{\bm{p}}^{\prime}}^{\top}\eta\frac{1}{B}\sum_{n\in \mathcal{B}_{b}}\frac{\partial\ell(\Psi;{\bm{P}}^{n},{\bm{z}}^{n})}{\partial{ \bm{W}}}{\bm{p}}\right|$ (186) $\displaystyle=$ $\displaystyle\Big{|}\eta\frac{1}{B}\sum_{n\in\mathcal{B}_{b}}(F(\Psi;{\bm{P}}) -{\bm{z}}^{n})^{\top}\sum_{i=1}^{l}{\bm{W}}_{V}\tilde{{\bm{p}}_{i}}\text{ softmax}(\tilde{{\bm{p}}_{i}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query})\tilde{{ \bm{p}}}^{\top}$ $\displaystyle(\tilde{{\bm{p}}_{i}}-\sum_{r=1}^{l}\text{softmax}(\tilde{{\bm{p} }_{r}}^{\top}{\bm{W}}\tilde{{\bm{p}}}_{query}){\bm{p}}_{r})\tilde{{\bm{p}}}_{ query}^{\top}\tilde{{\bm{p}}}\Big{|}$ $\displaystyle\lesssim$ $\displaystyle\eta\epsilon.$ $$

∎

###### Proof.

## Peer Reviews

confidence: 3
contribution: 3
initial_rating: 5
presentation: 3
questions: 1. For the CoT training set, this work constructed it with examples + query + (K-1)-reasoning steps, and the K-th step as the label. Why did not adopted more later steps as the label or more flexible label strategies?

2. In the 3rd line of Example 1, should the last affinity function be ?

3. How well do the theoretical results extend to more complex Transformer models with multiple layers and heads?

4. Have the authors considered validating their theoretical findings with real-world datasets, and if so, what are the challenges and potential modifications needed?

5. Could the authors elaborate on how the model's performance is affected by different levels of noise and inaccuracy in the context examples?
soundness: 2
strengths: 1. The paper provided a novel theoretical framework for understanding how a simplified Transformer can be trained to perform CoT reasoning, which is important for Transformers-based model reasoning.

2. The authors offered a detailed quantitative analysis of the training process, including the number of context examples, training samples, and iterations required for successful CoT training. This level of detail helps in understanding the complexity and feasibility of training similar models.

3. The paper addresses the generalization of CoT ability to new tasks with distribution shifts, which is a practical concern for real-world applications where training and testing data may differ.
summary: This work presented a theoretical study on training Transformers to achieve Chain-of-Thought (CoT) reasoning capabilities. The authors explored the training dynamics of nonlinear Transformers and provided a quantitative analysis of the required training samples and iterations for developing CoT ability. They analyzed the model's success in generalizing CoT on new tasks with O.O.D. test data, even in noisy and inaccurate reasoning examples, furthermore, contrasted CoT with in-context learning (ICL) and theoretically characterizes scenarios where CoT outperforms ICL.
weaknesses: 1. The paper idealized the Transformer as a simplified one-layer single-head architecture. While such a simplification can aid in theoretical analysis, it may limit the generalizability of more complex Transformer architectures.

2. The assumptions made about the data and tasks, such as the orthonormality of training-relevant and testing-relevant patterns, may not hold in all real-world scenarios, which could affect the applicability of the results.

---

confidence: 2
contribution: 3
initial_rating: 8
presentation: 3
questions: Are the results applicable, or can they be easily extended to the case where the reasoning steps $z_i, f(z_i)$ are not necessarily of the same sequence length? In other words, $z_1 \in R^{1\times d}$, $f(z_1) \in \mathbb R^{2\times d}$, $z_2 \in \mathbb R^{2 \times d}$, $f(z_2) \in \mathbb R^{1\times d}$. Such a setting might be of greater interest because the steps are typically multi-token in applications involving natural language.
soundness: 4
strengths: **Originality and Significance**: While previous papers analyze ICL (with and without CoT), their assumptions do not match this paper's. Specifically, this paper does the analysis using a transformer with non-linear attention, with test data coming from a different distribution than the training data, making the setting more general and useful.

**Quality and Clarity**: The theoretical results are presented clearly and are supported by relevant experiments. The discussion and remarks help with understanding the implications of the results.
summary: The paper provides a theoretical characterization of three aspects of in-context learning with examples consisting of chain-of-thought using a simplified data model and a simplified single-layer transformer with non-linear attention. The central result of the paper is a quantitative characterization of the training dynamics that leads to CoT ability. Using this, the authors characterize how the noise and relevance of the in-context examples affect the CoT performance. The final result compares the error rates for ICL with CoT and ICL without CoT with the main conclusion that while the rates are similar, ICL without CoT needs the examples to be dominantly correct, while CoT does not need this. Experiments on synthetic data support the theoretical results.
weaknesses: It would increase the impact of the paper if the authors could include experiments on some real datasets.

---

confidence: 3
contribution: 3
initial_rating: 6
presentation: 3
questions: See the section on weakness. We will increase the score based on the answer to the question.
soundness: 3
strengths: 1. Theoretical Advancement: The paper makes a significant theoretical contribution by providing the first analysis of training dynamics for Transformers aimed at achieving chain-of-thought (CoT) generalization capability. This addresses a critical gap in the literature, as prior works have not fully explored how Transformers can be trained to generalize on multi-step reasoning tasks via CoT.
﻿2. Deep Quantitative Analysis: The authors offer a detailed quantitative analysis of the training process, including the number of context examples required for successful CoT reasoning and the conditions under which CoT outperforms in-context learning (ICL). This level of detail is commendable and provides actionable insights for practitioners looking to implement CoT in Transformers.
summary: The paper provides a theoretical study on training Transformers with nonlinear attention to obtain CoT generalization capability, allowing the model to infer unseen tasks when the input is augmented with examples of the new task. They quantify the required training samples and iterations needed to train a Transformer model towards CoT ability and prove the success of its CoT generalization on unseen tasks with distribution-shifted testing data. The paper's major contributions include a quantitative analysis of training dynamics on a one-layer single-head attention-only Transformer, characterizing the conditions for accurate reasoning output by CoT even in the presence of noisy and inaccurate examples. It also provides a theoretical explanation for why CoT outperforms in-context learning (ICL) in certain cases, highlighting the robustness of CoT against noise in reasoning examples. Through experiments, the authors justify their theoretical findings, demonstrating the practical relevance of their theoretical analysis. This work advances the understanding of Transformer models' reasoning abilities and their potential applications in solving complex reasoning tasks.
weaknesses: 1. **Limitations in Model Complexity**: The paper primarily analyzes a single-head attention Transformer, which may not encapsulate the full complexity and performance characteristics of multi-layer and multi-head attention models. The validation was confined to binary classification tasks, thereby restricting the generalizability of the theoretical findings.
2. **Formula Accuracy**: The paper requires a meticulous review of its mathematical expressions. For instance, in Example 1, the function should be denoted as \( f_2(\mu_2') = \mu_1' \) instead of \( f_1(\mu_2') = \mu_1' \), and the second matrix \( A_1^f \) should be corrected to \( A_2^f \). It is essential to verify all formulas throughout the text to ensure accuracy.
3. **Theorem Validity and Clarification**: The theorems presented in the article should be scrutinized for their validity, particularly the sections that substantiate reasoning, as they may induce some ambiguity. Reading the appendix is mandatory for a comprehensive understanding of the article; otherwise, it might lead to misconceptions.
4. **Originality Concerns**: The article's reasoning and writing logic bear similarities to those found in "How Do Nonlinear Transformers Learn and Generalize in In-Context Learning." It raises the question of whether this work is merely an extension of the previous study or if it introduces novel contributions.

---

confidence: 2
contribution: 3
initial_rating: 6
presentation: 3
questions: No further issues.
soundness: 3
strengths: ● This paper is one of the first to theoretically explore training dynamics of nonlinear Transformers with CoT capabilities, providing important insights into sample complexity, required iterations, and generalization guarantees.

● The comparison between CoT and ICL is theoretically grounded, with a clear quantitative explanation of why CoT can succeed in contexts where ICL may fail.

● This work addresses an understudied area in Transformer models, specifically how they generalize CoT reasoning abilities across unseen tasks, making it a relevant contribution to understanding advanced prompting methods in LLMs.
summary: This paper presents a theoretical analysis of training Transformers with nonlinear attention mechanisms to develop Chain-of-Thought (CoT) reasoning abilities. The authors focus on quantifying the requirements (such as sample complexity and iterations) for training a Transformer model capable of CoT generalization on unseen tasks, even under conditions where examples might contain noise and inaccuracies. The study also compares CoT with In-Context Learning (ICL), showing that CoT can outperform ICL when intermediate reasoning steps are essential. The paper provides both theoretical insights and empirical evidence supporting the model's behavior during training and testing.
weaknesses: ● The authors analyze attention-only models, aligning with previous work by [1] Olsson et al. (2022). Both studies observe that trained models’ attention values tend to focus on context examples with input patterns similar to the test query. While Olsson et al. attribute this to ICL (In-Context Learning) ability, the authors link it to CoT ability and explore how training may facilitate this behavior. A more detailed comparison of contributions between this study and Olsson et al. would be valuable. 

  ○ [1] In-context Learning and Induction Heads.  2022. Olsson et al.

● The paper acknowledges the gap between the one-layer model studied and multi-layer Transformer models. As practical LLMs are complex, the findings might not directly extend to real-world Transformer architectures. Please discuss and analyze the applicability of these conclusions in more complex scenarios.

● The theoretical results assume a linear span between training-relevant (TRR) and testing-relevant (TSR) patterns, limiting applicability to cases with greater diversity in test distributions.

Meta-review: Summary: The paper investigates how nonlinear Transformers can be trained to perform Chain-of-Thought (CoT) reasoning, providing a theoretical foundation for this capability. It examines the training dynamics required to develop CoT abilities and demonstrates that Transformers trained in this way can generalize robustly to unseen tasks. Additionally, a comparison with In-Context Learning (ICL) highlights CoT's advantages, particularly in scenarios where ICL struggles due to its dependency on accurate input-label pairs. Simple experiments are used to corroborate theoretical results.

Strengths:
- All the reviewers leaning positive
- Analysis of training dynamics for CoT capability in transformers - an important problem
- Detailed quantitative analysis of sample complexity, iterations, and generalization bounds
- Theoretical framework comparing CoT vs ICL performance

Weakness:
- Analysis limited to simplified one-layer single-head transformer architecture
- Assumptions about orthogonal patterns may not fully represent real-world scenarios
- Experiments primarily on synthetic data rather than real-world applications
- Some presentation issues including typos and formula accuracy that needed correction

Decision:
All the reviewers were in consensus that the paper represents an important step forward in understanding transformer capabilities and limitations, providing both theoretical insights and practical implications for future work. It merits publication at ICLR 2025, with a few minor edits: the contribution section can softened and simplified: currently repitive and over-claiming.

Decision: Accept (Poster)