Towards Better Understanding of In-Context Learning Ability from In-Context Uncertainty Quantification

## Abstract

Abstract Predicting simple function classes has been widely used as a testbed for developing theory and understanding of the trained Transformer’s in-context learning (ICL) ability. In this paper, we revisit the training of Transformers on linear regression tasks, and different from all the existing literature, we consider a bi-objective prediction task of predicting both the conditional expectation 𝔼 ⁢ [ Y | X ] 𝔼 delimited-[] conditional 𝑌 𝑋 \mathbb{E}[Y|X] blackboard_E [ italic_Y | italic_X ] and the conditional variance Var ( Y | X ) conditional 𝑌 𝑋 (Y|X) ( italic_Y | italic_X ) . This additional uncertainty quantification objective provides a handle to (i) better design out-of-distribution experiments to distinguish ICL from in-weight learning (IWL) and (ii) make a better separation between the algorithms with and without using the prior information of the training distribution. Theoretically, we show that the trained Transformer reaches near Bayes-optimum, suggesting the usage of the information of the training distribution. Our method can be extended to other cases. Specifically, with the Transformer’s context window S 𝑆 S italic_S , we prove a generalization bound of 𝒪 ~ ⁢ ( min ⁡ { S , T } / ( n ⁢ T ) ) ~ 𝒪 𝑆 𝑇 𝑛 𝑇 \tilde{\mathcal{O}}(\sqrt{\min\{S,T\}/(nT)}) over~ start_ARG caligraphic_O end_ARG ( square-root start_ARG roman_min { italic_S , italic_T } / ( italic_n italic_T ) end_ARG ) on n 𝑛 n italic_n tasks with sequences of length T 𝑇 T italic_T , providing sharper analysis compared to previous results of 𝒪 ~ ⁢ ( 1 / n ) ~ 𝒪 1 𝑛 \tilde{\mathcal{O}}(\sqrt{1/n}) over~ start_ARG caligraphic_O end_ARG ( square-root start_ARG 1 / italic_n end_ARG ) . Empirically, we illustrate that while the trained Transformer behaves as the Bayes-optimal solution as a natural consequence of supervised training in distribution, it does not necessarily perform a Bayesian inference when facing task shifts, in contrast to the equivalence between these two proposed in many existing literature. We also demonstrate the trained Transformer’s ICL ability over covariates shift and prompt-length shift and interpret them as a generalization over a meta distribution.

## 1 Introduction

A particularly remarkable characteristic of Large Language Models (LLMs) is their ability to perform in-context learning (ICL) (Brown et al., ). Once pretrained on a vast corpus of data, LLMs can solve newly encountered tasks when provided with just a few training examples, without any updates to LLMs’ parameters. ICL has significantly advanced the technique known as prompt engineering (Ekin, ), which has achieved widespread success in various aspects of daily life (Oppenlaender et al., ; Heston and Khun, ; Li et al., ). Behind the empirical success of ICL, this method has captured the attention of the theoretical machine learning community, leading to considerable efforts into understanding ICL from different theoretical perspectives (Xie et al., ; Akyürek et al., ; Von Oswald et al., ; Zhang et al., ).

This work aims to enhance the theoretical understanding of ICL by examining the Transformer’s context window and showing its effects on the approximation-estimation tradeoff. Although we obtain the results for the case of uncertainty quantification where the model is asked to predict both the mean value and the uncertainty of its prediction, our analysis is applicable across various ICL tasks and provides sharper bounds compared to previous works. In addition to developing theories, we empirically demonstrate the effectiveness of Transformers to in-context predict the mean and quantify the variance of regression tasks. We design a series of out-of-distribution (OOD) experiments, which have generated significant interest within the community (Garg et al. (); Raventós et al. (); Singh et al. ()). These experiments provide insights in designing the pretraining process and understanding the ICL capabilities of transformers.

Our contributions are as follows:

- We theoretically analyze the problem of in-context uncertainty quantification. We consider the case when Transformers can only process the contexts within a context window capacity $S$ and derive a generalization bound of $\tilde{\mathcal{O}}(\sqrt{\min\{S,T\}/{nT}})$ for pretraining over $n$ tasks with sequences of length $T$ (Theorem 3.2). Our result can be easily extended to other cases under the assumption of almost surely bounded and Lipschitz loss functions. As far as we know, our generalization bound is the first of its kind and provides a tighter bound compared to the existing analyses (Li et al., ; Zhang et al., ) when $S<T$. In particular, we use the context-window structure to establish a Markov chain over the prompt sequence and construct an upper bound for its mixing time. We also examine the extra approximation error term due to a finite context window $S$ (Section B.2). Combining those discussions together, we quantify the convergence of the trained Transformer’s risk to the Bayes-optimal risk. Moreover, we note that all the theoretical results only show that the trained Transformer achieves a near-optimal in-distribution risk compared to that of the Bayes-optimal predictor. It is incorrect to draw (from the theory or the in-distribution numerical results) either of the conclusions that (i) the Transformer that achieves the near-optimal risk exhibits a similar structure as the Bayes-optimal predictor by performing Bayesian inference (Zhang et al., ; Panwar et al., ) or (ii) the Transformer performs as the Bayes-optimal predictor for out-of-distribution tasks.

- Numerically, for the uncertainty quantification problem, we provide a comprehensive study of the in-context learning ability of the trained Transformer under three scenarios of distribution shifts: task shift (Section 4.1), covariates shift (Section 4.2), and prompt length shift (Section 4.3).
We find that transformers are capable of in-context learning of both mean and uncertainty predictions, even under a moderate amount of task distribution shift, provided that the task diversity in the training data is relatively large. Additionally, we find that increasing the task diversity with a meta-learning approach helps the transformer learn in context robustly under covariates shift. Lastly, we observe that removing positional encoding from the embedding vector massively helps the generalization ability, enabling it to better learn tasks in context with unseen prompt length.

We defer more discussions on the related literature to Section A.

## 2 Problem Setup

Consider training a Transformer for some regression task $f\colon\mathcal{X}\rightarrow\mathcal{Y}$ from a function class $\mathcal{F}$. The covariates $x\in\mathcal{X}\subset\mathbb{R}^{d}$ are generated from a distribution $\mathcal{P}_{\mathcal{X}}$, and the output variable $y=f(x)+\sigma\cdot\epsilon$ for some function $f\in\mathcal{F}$, noise level $\sigma$, and some random noise $\epsilon$ with $\mathbb{E}[\epsilon]=0$ and Var$(\epsilon)=1$. The Transformer performs a sequential prediction task over the following sequence

$$ $(x_{1},y_{1},...,x_{T},y_{T})$ $$

where $T$ is the total number of (in-context) samples. For a Transformer model with parameters $\theta\in\Theta$, we denote it as $\texttt{TF}_{\theta}$. At each time $t=1,...,T$, the model $\texttt{TF}_{\theta}$ observes $H_{t}\coloneqq(x_{1},y_{1},...,x_{t-1},y_{t-1},x_{t})$ (which is called history or prompt) and makes a bi-objective prediction of $y_{t}$ to both predict the mean with $\hat{y}_{\theta}(H_{t})$ and quantify the uncertainty of the prediction with $\hat{\sigma}_{\theta}(H_{t})$. With a slight abuse of notations, we denote the output of the model by $\texttt{TF}_{\theta}(H_{t})\coloneqq(\hat{y}_{\theta}(H_{t}),\hat{\sigma}_{
\theta}(H_{t}))$. The pretraining dataset consists of $n$ sample sequences

$$ $\mathcal{D}\coloneqq\left\{\left(x_{1}^{(i)},y_{1}^{(i)},x_{2}^{(i)},y_{2}^{(i )},...,x_{T}^{(i)},y_{T}^{(i)}\right)\right\}_{i=1}^{n}.$ $$

To generate each sample sequence in $\mathcal{D}$, a function $f_{i}$ is sampled from a distribution $\mathcal{P}_{\mathcal{F}}$ supported on $\mathcal{F}$ and a noise level $\sigma_{i}$ is sampled from a distribution $\mathcal{P}_{\sigma}$ supported on $[0,\bar{\sigma}]\subset\mathbb{R}$. Then each $x_{t}^{(i)}$ and $y_{t}^{(i)}$ is generated pairwise by

$$ $x_{t}^{(i)}\overset{\text{i.i.d.}}{\sim}\mathcal{P}_{\mathcal{X}},\ \ y_{t}^{( i)}=f_{i}\left(x_{t}^{(i)}\right)+\sigma_{i}\cdot\epsilon_{t}^{(i)},\ \ \epsilon_{t}^{(i)}\overset{\text{i.i.d.}}{\sim}\mathcal{P}_{\mathcal{\epsilon}}$ $$

where $\epsilon_{t}^{(i)}$’s are i.i.d. noise of mean zero and unit variance.

The Transformer is trained by minimizing the following empirical loss

$$ $\hat{\theta}^{\text{ERM}}\coloneqq\operatorname*{arg\,min}_{\theta\in\Theta} \frac{1}{nT}\sum_{i=1}^{n}\sum_{t=1}^{T}\ell\left(\texttt{TF}_{\theta}\left(H_ {t}^{(i)}\right),y_{t}^{(i)}\right)$ (1) $$

where $H_{t}^{(i)}=(x_{1}^{(i)},y_{1}^{(i)},...,x_{t-1}^{(i)},y_{t-1}^{(i)},x_{t}^{(i
)})$ and $l((\cdot,\cdot),\cdot):(\mathbb{R}\times\mathbb{R}^{+})\times\mathbb{R}
\rightarrow\mathbb{R}$ denotes the loss function. We use $x_{t}^{(i)},y_{t}^{(i)},H_{t}^{(i)}$ to denote the samples in the training dataset and $x_{t},y_{t},H_{t}$ to denote an arbitrary feature, label, and history. Throughout this paper, we assume that each probability distribution is continuous and has a probability density function (p.d.f.), and we also assume the conditional distribution of $y_{t}$ on observing $H_{t}$ exists almost surely.

The loss function is accordingly defined by

$$ $\ell\left((\hat{y},\hat{\sigma}),y\right)\vcentcolon=\log\hat{\sigma}+\dfrac{( y-\hat{y})^{2}}{2\hat{\sigma}^{2}}.$ $$

###### Definition 2.1 (Bayes-optimal predictor) .

The Bayes-optimal predictor under the distributions $\mathcal{P}_{\mathcal{F}}$, $\mathcal{P}_{\mathcal{X}}$, $\mathcal{P}_{\sigma}$ and $\mathcal{P}_{\epsilon}$ is defined by

$$ $(y_{t}^{*}(\cdot),\sigma_{t}^{*}(\cdot))\coloneqq\operatorname*{arg\,min}_{(y( \cdot),\sigma(\cdot))\in\mathcal{G}_{t}\times\mathcal{G}_{t}}\mathbb{E}\left[ \ell\Big{(}\big{(}y(H_{t}),\sigma(H_{t})\big{)},y_{t}\Big{)}\right]$ (2) $$

where $\mathcal{G}_{t}$ is the class of all measurable functions of $H_{t}\in\mathcal{H}_{t}$. The expectation is taken with respect to the following dynamics: $x_{t}\sim\mathcal{P}_{\mathcal{X}}$, $\epsilon_{t}\sim\mathcal{P}_{\mathcal{\epsilon}}$, $f\sim\mathcal{P}_{\mathcal{F}}$, $\sigma\sim\mathcal{P}_{\sigma}$, $y_{t}=f(x_{t})+\sigma\cdot\epsilon_{t}$ and $H_{t}=(x_{1},y_{1},\dots,x_{t})$.

The loss on the right-hand-side of (2) is the expectation of the empirical loss (1). With a rich enough function class and an infinite amount of training samples, the trained Transformer $\texttt{TF}_{\hat{\theta}_{\text{ERM}}}$ converges to $(y_{t}^{*},\sigma_{t}^{*})$ as will be shown in Theorem 3.2.

###### Definition 2.1 (Bayes-optimal predictor) .

### 2.1 Motivation for the uncertainty quantification objective

We first give a semi-formal definition for in-context learning and in-weight learning of the bi-objective linear regression task considered in this paper.

In-context learning refers to that the Transformer gains the ability to learn from the in-context samples (samples in $H_{t}$) and behave as an algorithm. For example, the Transformer exhibits in-context learning when it behaves like a ridge regression model when performing the linear regression task. The in-context learning ability should persist under the out-of-distribution setting such as shifting $\mathcal{P}_{\mathcal{X}}$, $\mathcal{P}_{\mathcal{F}}$, and $\mathcal{P}_{\sigma}.$

In-weight learning, for the NLP tasks, generally refers to that the Transformer relies on the information stored in its weights to make predictions, rather than the in-context samples. In this light, it memorizes the training samples and uses the memorization to make predictions. This memorization mechanism is more aligned with supervised learning setting; in particular, the mechanism is sensitive to distribution shifts and thus is not considered a desirable outcome of training Transformers.

Then the question is when we train the Transformer according to the objective (1), does it exhibit in-context learning or in-weight learning? For the classic single-objective linear regression task, we note two facts: (a) the trained Transformer is near the Bayes-optimal predictor (Xie et al., ; Zhang et al., ; Panwar et al., ); (b) under a Gaussian prior, the Bayes-optimal predictor is exactly a ridge regression model (with proper choice of the regularization parameter, (Wu et al., )). With these two facts, it is tempting to draw the conclusion that the Transformer gains in-context learning ability and behaves as a ridge regression model, but this has to be done with caution or may even lead to a wrong conclusion, given that the trained Transformer does not exhibit out-of-distribution ability for full generality as noted in the numerical experiments (Garg et al., ). This motivates us to consider the bi-objective tasks. First, for the uncertainty quantification objective, when the number of in-context samples is fewer than the dimension of $x_{t}$’s, a near Bayes-optimal predictor must utilize the information in the training procedure, whereas there is no algorithm that can optimally do this via only in-context samples. Thus it well distinguishes the Bayes-optimal predictor from any possible algorithm and therefore gives a clearer picture of whether the trained Transformer indeed behaves as an algorithm. Second, the uncertainty quantification objective provides us an easy handle to designing numerical experiments, such as the flipped experiments (Wei et al., ; Singh et al., ), to distinguish in-context learning from in-weight learning. Third, the uncertainty quantification objective is of independent interest as it indicates whether the trained Transformer knows its uncertainty or not. Furthermore, through this bi-objective task, we illustrate that while the trained Transformer is near the Bayes-optimal predictor, it does not necessarily perform Bayesian inference when making predictions, which contradicts the arguments in (Zhang et al., ; Panwar et al., ; Jeon et al., ).

## 3 In-Context Learning when In-Distribution

In this section, we focus on the in-distribution property of the trained Transformer. We provide a finite-sample analysis of how trained Transformers reach near Bayes-optimum.
While our analysis is made on the case of uncertainty quantification, it can be easily adapted to other loss functions such as mean squared error. To proceed, we first provide the exact form of the Bayes-optimal predictor defined in (2) for the mean and uncertainty prediction.

###### Proposition 3.1 (Bayes-optimal predictor for mean and uncertainty prediction) .

The Bayes-optimal predictor of the step-wise population risk defined in (2) is given by

$$ $y_{t}^{*}(H_{t})=\mathbb{E}[y_{t}|H_{t}],\ \ \sigma_{t}^{*2}(H_{t})=\mathbb{E} [(y_{t}-y_{t}^{*}(H_{t}))^{2}|H_{t}]=\mathbb{E}[(f(x_{t})-y_{t}^{*}(H_{t}))^{2 }|H_{t}]+\mathbb{E}[\sigma^{2}|H_{t}].$ $$

The optimal mean predictor shares the same form as the Bayes-optimal predictor for a single-objective mean prediction task. The additional uncertainty prediction task does not change the nature of the mean prediction part. The two terms in the optimal uncertainty predictor can be interpreted as follows. The first term is epistemic uncertainty, which indicates the uncertainty (of identifying the $f$ that governs the history $H_{t}$) due to lack of information. The term decreases as the samples accumulate, i.e., as the number of in-context samples $t$ increases. The second term is aleatoric uncertainty also known as intrinsic uncertainty.

Figure: (a) Mean prediction
Refer to caption: extracted/5616625/fig/exp_1_L2_compare.png

Recall that the empirical risk estimator is defined by (1). Now we define the population risk as

$$ $R(\texttt{TF}_{\theta})\coloneqq\frac{1}{T}\mathbb{E}_{H_{t}}\left[\sum_{t=1}^ {T}\ell\big{(}\texttt{TF}_{\theta}(H_{t}),y_{t}\big{)}\right],$ $$

where $H_{t}$ is another sampled sequence that is independently and identically distributed as $H_{t}^{(i)}$’s in the training data. We denote the population risk minimizer as $\theta^{*}$:

$$ $\theta^{*}\in\operatorname*{arg\,min}_{\theta\in\Theta}R(\texttt{TF}_{\theta}).$ (3) $$

Now we present our main theoretical result.

###### Theorem 3.2 .

Let $\hat{\theta}^{\text{ERM}}$ denote the ERM estimator as defined in (1) over the function class of the $L$-layer, $M$-heads Transformer models. Suppose that at each time $t$, the Transformer has a context window of making predictions based on $x_{t}$ and previous $S$ pairs of $(x_{s},y_{s})$ for $s=\max\{1,t-S\},\dots,t-1$. Then under some boundedness assumptions of the Transformer’s parameters (Assumption B.5 and B.6), we have with probability at least $1-\delta$,

$$ $R(\texttt{TF}_{\hat{\theta}^{\text{ERM}}})-R(\texttt{TF}_{\theta^{*}})\leq \tilde{\mathcal{O}}\left(\sqrt{\min\{S,T\}/(nT)}\right).$ $$

where $\tilde{\mathcal{O}}$ omits poly-logarithmic terms that depend on $n,T,1/\delta$ and boundedness parameters.

Proof sketch. First, we prove that (a slightly redefined version of) the truncated history forms up a Markov chain conditioned on observing the full hidden information $f^{(i)}$ and $\sigma^{(i)}$, and upper bound the mixing time by $\min\{S,T\}$ to enable the concentration arguments. Second, we prove that the loss function is almost surely bounded (Lemma E.3) in preparation for McDiarmid-type concentration inequalities (Lemma F.2, (Paulin, )). Third, we show that the loss is almost surely Lipschitz to control the difference between loss functions with respect to the change of the parameter (Lemma E.7). Fourth, we prove that there exist two distributions $\rho_{\hat{\theta}^{\text{ERM}}}$ and $\pi$ over parameter space $\Theta$, satisfying a number of properties as constructed in Lemma E.11. Lastly, we use standard PAC-Bayes arguments over $\rho_{\hat{\theta}^{\text{ERM}}}$ and $\pi$ and conclude the proof. The detailed proofs are deferred to Section D.2.

Comparison with previous results. There are also other theoretical results that characterize the outcomes of the (pre-)training on Transformer models (Zhang et al., ; Wu et al., ; Xie et al., ; Li et al., ; Bai et al., ; Zhang et al., ; Lin et al., ). Our analysis differs from theirs in terms of both the conclusion and the techniques. One stream of results examines the property of the gradient flow (or gradient descent) over the loss function for linear regression problems. The exact quantification of the gradient flow entails a simplification of the Transformer’s architecture to the case of a single-layer attention mechanism under linear activation or even simpler settings (Zhang et al., ; Wu et al., ). While their analyses provide insights into the learning dynamics of Transformer models, the learning of the single-layer attention Transformer can be very different from multiple-layer Transformers (Olsson et al., ; Reddy, ). Another major line of research uses statistical learning arguments (Xie et al., ; Li et al., ; Bai et al., ; Zhang et al., ; Lin et al., ) such as algorithm stability, chaining, or PAC-Bayes arguments. Bai et al. () focus on making predictions after observing a fixed length of variables under the i.i.d. setting (which is more aligned with the standard supervised learning setting), which differs from the more practical setting of making predictions at every position as in Theorem 3.2. Xie et al. () prove the convergence between the Bayesian inference and the true underlying distribution rather than the trained model and the Bayesian inference. Lin et al. () consider a sequential decision-making problem and use covering arguments to derive generalization bounds, while their analysis does not adopt the concentration arguments inside each sequence, resulting in an $\tilde{\mathcal{O}}(\sqrt{1/n})$ upper bound for the average regret. The most related works to ours are Li et al. (); Zhang et al. (). The major difference is that they all consider the only case of $S\geq T$. Li et al. () use the algorithm stability arguments to give a generalization bound over $|R-r|$ of order $\tilde{O}(\sqrt{1/(nT)})$. They prove the loss difference caused by perturbing one input pair over a history of length $t$ is controlled by $\mathcal{O}(1/t)$. Averaging those differences leads to a $\mathcal{O}(\log(T)/T)=\tilde{\mathcal{O}}(1/T)$ inside each sequence (see their equation (15) in their Appendix C), which appears in the Azuma-Hoeffding argument to prove that the loss per sequence is $\tilde{\mathcal{O}}(T^{-1/2})$-sub-Gaussian. However, in the case of $S\ll T$ (which is more often the case in practice), the algorithm stability term is of $\mathcal{O}(1/S)$. Averaging these terms inside each sequence leads to a difference of order $\mathcal{O}(1/S)$. If we stick to the original Azuma-Hoeffding arguments, the sum of squares of these terms is of $\mathcal{O}(T/S^{2})$, leading to a far worse sub-Gaussian norm of $\mathcal{O}(T^{1/2}S^{-1})$, resulting in a final generalization bound of order $\tilde{O}(T^{1/2}S^{-1}n^{-1/2})$ that is clearly suboptimal compared to our $\tilde{O}(\sqrt{S/(nT)})$. Besides, such a bound also grows with $T$, which is undesirable. Similar to ours, Zhang et al. () also use a concentration argument for Markov chains. However, their Theorem 5.3 has two limitations: The first is that their result is of the order $\tilde{\mathcal{O}}(\sqrt{\tau_{\min}/(nT)})$ but they do not specify $\tau_{\min}$. Since they do not consider the truncated history but the full history, the Markov chain (which is not verified by them) will never mix inside each task sequence (see our discussions in Section D.2). Thus, the term $\tau_{\min}$ in their result is actually $T$, leading to an order of $\tilde{\mathcal{O}}(\sqrt{1/n})$, which is suboptimal compared to our $\tilde{\mathcal{O}}(\sqrt{S/(nT)})$ when the context window $S\ll T$. The second limitation is that their error decomposition is not tight: their excessive risk bound (measured by the total variation distance between the distribution induced by $\hat{\theta}$ and that by $\theta^{*}$) has a term $D_{\mathrm{kl}}(\mathcal{P}_{\text{true}},\mathcal{P}_{\theta^{*}})-\mathrm{TV
}(\mathcal{P}_{\text{true}},\mathcal{P}_{\theta^{*}})$, which means their result has an extra term of the approximation error since the Kullback-Leibler divergence is stronger than the total variation distance (Polyanskiy and Wu, ). Our work is the first theoretical analysis showing the effects of the context window $S$ on the performance of the Transformer up to our knowledge. The construction of the truncated history serves two-fold: not only does the truncation fit the practical model of finite context window but it also gives an upper bound on the mixing time. Concentration inside each sequence makes it possible to analyze the training dynamics broader than fixed-length sequences and prove the convergence to near Bayes-optimum. The context window $S$ also captures a novel dimension of the approximation-estimation tradeoff in the Transformer model.

Extension of Theorem 3.2 to other problems. We remark that the result and its derivation do not pertain to the uncertainty quantification setting, but hold for more general loss functions and are of independent interests. In particular, our analysis still holds as long as the loss function is almost surely bounded and Lipschitz with respect to the change of parameter $\theta$, as we can see from the proof sketch. We note here that to enable the Markov chain’s concentration arguments, the almost surely bounded loss requirement cannot be relaxed to other tail properties such as sub-Gaussian (see the counter example in Theorem 4 of Fan et al. ()).

We defer discussions on the approximation error to Section B.2.

###### Proposition 3.1 (Bayes-optimal predictor for mean and uncertainty prediction) .

###### Theorem 3.2 .

## 4 In-Context Learning under Distribution Shifts

In Section 2, we describe in-context learning ability as algorithm-like that predicts based on the learning from in-context samples, and such an ability should be generalizable to an out-of-distribution (OOD) environment. In this section, we differentiate the OOD scenarios into task shift, covariate shift and length shift, and examine the Transformer’s in-context learning ability in each scenario. As far as we know, we provide the first comprehensive group of numerical experiments (for the linear regression task) that demonstrates the Transformer’s ability to handle these three types of distribution shifts. We provide preliminary theoretical discussions for such abilities and hope this points directions for future theoretical research.

### 4.1 Task shift

When the trained Transformer performs well on the OOD data, it means that the Transformer gains an algorithmic ability that learns to make predictions based on the in-context samples, because such an ability is not restricted to the distribution of the inputs. Comparatively, the mere observation that the Transformer works well on the in-distribution data does not demonstrate its in-context learning ability as a traditional supervised learning model also has such ability and generalization performance over in-distribution data.

In the previous section, when we show the in-distribution performance of the Transformer, the variance parameter $\sigma^{2}$ is generated by the prior of the inverse-Gamma distribution
$\sigma^{2}\sim\text{Inv-Gamma}(\underline{\tau},\bar{\tau})$
with parameters $\underline{\tau}$ and $\bar{\tau}$. The details of the other generation distributions are deferred to Section G.1. For the in-distribution setting, we set $\underline{\tau}=\bar{\tau}=20$ which leads to a prior mean around $1$. Now we consider three out-of-distribution (OOD) settings for the

- •
S-OOD (small OOD): $\underline{\tau}=80,\bar{\tau}=20$. The prior mean of $\sigma$ is around $0.5$.
- •
M-OOD (medium OOD): $\underline{\tau}=100,\bar{\tau}=400$. The prior mean of $\sigma$ is around $2$.
- •
L-OOD (large OOD): $\underline{\tau}=100,\bar{\tau}=1600$. The prior mean of $\sigma$ is around $4$.

Figure: (a) Transformer trained w/ small pool size
Refer to caption: extracted/5616625/fig/exp_2_OOD_Std_changePrior_smallpool.png

We make following observations based on Figure 2: First, the Bayes-optimal predictor predicts well. We note that the Bayes-optimal is computed based on the in-distribution prior distribution (with respect to $\sigma^{2}$). Thus when the Bayes-optimal predictor is tested under the OOD environment as in Figure 2, the prior used by the Bayes-optimal predictor is wrong. But we note from Figure 2 that the Bayes-optimal predictor has the OOD ability to correct the prior as the in-context samples accumulate (noting that the three Bayes-optimal curves converging to the correct mean of 0.5, 2, and 4). This is also known as the washing out of priors in Bayesian statistics. Second, Transformers deviate from the Bayes-optimal on these OOD tasks. For both plots in Figure 2, we note that the predicted values from the Transformers deviate from those of the Bayes-optimal predictor when the OOD intensity is large. This tells that the trained Transformer does not conduct Bayesian inference under task shift. In other words, it is incorrect to conclude that the trained Transformer behaves as the Bayes-optimal predictor just from the matching in-distribution loss (as Figure 1). Moreover, the Transformer achieves a near-optimal loss for in-distribution tasks (as Figure 1) but it does so via a different avenue than the Bayes-optimal predictor (as Figure 2). This is in contrast with the findings/claims in the previous papers (Zhang et al., ; Panwar et al., ). Third, the deviation of the trained Transformer from the Bayes-optimal is smaller when the task diversity is large or the OOD intensity is small. This is aligned with the findings in (Raventós et al., ) for in-distribution performance, while the OOD setting is not studied therein.

The theoretical evidence only states that the trained Transformer has a near-optimal in-distribution loss as the Bayes-optimal predictor. But it does not give any evidence that these two have a structural similarity that persists for OOD tasks. In particular, we note that the trained Transformer may take statistical shortcuts: When evaluated under in-distribution tasks or some simple task shifts (e.g. scaling the weights vectors or changing the signal-noise ratio), Zhang et al. (); Wu et al. () show that Transformer will construct shortcuts using the statistical property of the training distribution. More specifically, Transformers (can, and will) encode the information of the covariance matrix into their model parameters to reach near-optimal in-distribution performance. Such statistical shortcuts are beneficial to the in-distribution performance but can hurt its OOD ability. Increasing the training task diversity, such as a larger training pool size, may remove some of these statistical shortcuts to obtain near-optimal empirical loss, and thus better enable its in-context learning ability.

We defer more discussions and visualizations on this OOD experiment to Section C.2.

### 4.2 Covariates shift

For all the numerical experiments so far, the covariates are generated from $\mathcal{N}(0,I_{d}).$ This follows the standard setup of the existing literature (Akyürek et al., ; Von Oswald et al., ; Li et al., ; Raventós et al., ). It is also noted from the literature (Garg et al., ; Zhang et al., ) that the trained Transformer in this way lacks in-context learning ability under covariates shift. In this subsection, we propose a meta-training procedure that effectively improves the trained Transformer’s ability to handle covariates shifts. Specifically, we consider generating the covariates in the training data as follows:

- •
For each training sequence (say, the $i$-th), we first sample a vector $(\lambda_{1},...,\lambda_{d})$ where each $\lambda_{j}$ is i.i.d. Uniform$[0,2]$. Then all the $X_{t}^{(i)}$’s for $t=1,...,T$ are sampled from $\mathcal{N}(0,\mathrm{diag}((\lambda_{1},\ldots,\lambda_{d}))).$ In this sense, the covariance matrix of $X_{t}^{(i)}$’s is also a random variable, and the $X_{t}^{(i)}$’s can be viewed as being sampled in a hierarchical manner from a meta-distribution.

We examine the performance of such a training procedure under four OOD test settings. In other words, the $X_{t}^{(i)}$’s in the test data is generated from the following four distributions where $d=8.$

- •
Large covariance (L-cov): $X_{t}^{(i)}$’s are sampled from $\mathcal{N}(0,4I_{d})$.
- •
Decreasing diagonal (Dec.): $X_{t}^{(i)}$’s are sampled from $\mathcal{N}(0,\mathrm{diag}([d/i]_{i=1}^{d}))$.
- •
Shrinking diagonal (Shr.): $X_{t}^{(i)}$’s are sampled from $\mathcal{N}(0,\mathrm{diag}([d/i^{2}]_{i=1}^{d}))$.
- •
Rotation (Rot.):
$X_{t}^{(i)}$’s are sampled from $\mathcal{N}(0,U_{i}\mathrm{diag}([d/i]_{i=1}^{d})U_{i}^{\top})$ where $U_{i}$ is an orthogonal matrix independently generated for each sequence.

Figure 9 gives the evaluation result under the $4$ OOD settings. We note that the meta-distribution used is still significantly different from the four OOD test environments. Thus the results show the effectiveness of the meta-training approach.

### 4.3 Length shift and positional embedding

Existing work (Dai et al., ; Anil et al., ; Zhang et al., ) have pointed out the failure of Transformers to generalize to longer contexts than the ones they have seen during training. It is worth mentioning that the code implementations of some previous works (Zhang et al., ; Garg et al., ) are based on the “transformers” package of Hugging Face. Although these works have not included positional embedding explicitly, the GPT2 module imported from this package adds a built-in positional encoding implicitly. We suspect that some unexpected behaviors (like the “unexpected spikes of prediction error” mentioned in Zhang et al. ()) are due to that the built-in positional encoding is not disabled.
In this subsection, we investigate the length generalization ability of the trained Transformer on the uncertainty quantification task. Specifically, we control the prompt lengths that the model is trained on. Previous experiments train the model on prompts with lengths (number of in-context samples) ranging from $1$ to $100$. In this experiment, we control the training prompts such that the lengths are either shorter than $44$ or longer than $45$ (the choice of 45 as the cutoff point is not essential). We specify these two configurations below.

- •
Trained on $\leq 44$: the model is trained on prompts with length ranging from $1$ to $44$, and is evaluated with prompt length from $1$ to $100$
- •
Trained on $\geq 45$: the model is trained on prompts with length ranging from $45$ to $100$, and is evaluated with prompt length from $1$ to $100$

We regard this difference in prompt length between training and testing as length shift. We evaluate the effect of removing positional encoding under this prompt length generalization task. If positional encoding is added to the embedding, samples at unseen positions will be associated with an unseen positional encoding vector in the embedding space. This requires the model to handle not just an unseen number of in-context samples, but also a possibly unseen embedding distribution, and generalization ability will likely deteriorate. As mentioned previously, the built-in positional encoding of GPT2 model use a positional encoding which is set to be $(t,0,\cdots,0)^{\top}$ for the $t$-th token, and the encoding will then be concatenated to the embedding vector. We validate the above intuitions with the following 4 training configurations.

- •
No positional encoding (w/o Pos.): the model is trained without positional encoding.
- •
Add positional encoding (w/ Pos.): the model is trained with GPT2’s built-in positional encodings.
- •
Add segment encoding (w/ S-Pos.): the positional encoding is added with a random amount offset. For the $i$-th training sequence, a random offset $t_{i}$ is first uniformly sampled from $\{0,1,\ldots,22\}$. Next, for each token in this prompt at position $t$, the positional encoding is set to $(t+t_{i},0,\ldots,0)^{\top}$.
- •
Add full range encoding (w/ F-Pos.): similar to the S-Pos. configuration, the positional encoding is added with a random amount offset. But here the offset is uniformly sampled from $\{0,1,\ldots,100\}$.

For the model trained with the “w/o Pos.” configuration, it is also tested without positional encodings. For the models trained with the rest configurations, they are all tested with the “w/ Pos.” way of encoding.

Figure: (a) Generalization from long to short
Refer to caption: extracted/5616625/fig/exp_3_Dev.png

The results are shown in Figure 3. The models in the left figure are trained on prompts shorter than $44$, and the models in the right figure are trained on prompts longer than $45$. We make the following observations. The pre-trained transformer in general can generalize to prompts with unseen length, under the condition of using/removing the positional embedding properly. The “w/o Pos.” curve in the left figure shows that even at positions larger than $44$, the model can still produce predictions close to Bayes-optimal. Adding positional encoding hurts the generalization ability. From the “w/ Pos.” curve in the left figure, we find that the model’s performance drops significantly at positions larger than $44$. The main cause of the failure of length generalization is due to the distribution shift in the positional embedding space. As given in the “w/ S-Pos.” and “w/ F-Pos.” curves in the left figure, if the model has seen the positional encodings for a certain position during training, then its performance at this position is significantly improved, even if the corresponding prompt length is never seen. The length generalization ability is not unrestrictively strong, and such generalization ability for smaller lengths is generally weaker compared to that for larger lengths. The right figure shows that even for the “w/o Pos.” configuration, its performance still degrades when the prompt length is shorter than $20$.

Theoretically, Wu et al. ()’s Theorem 5.3 points out that under the case of the single-layer linear-attention-only Transformer model on a linear regression task with Gaussian priors, if we train the model to only predict one single label after observing $T$ context exemplars, the optimally trained model under $T=T_{1}$ also performs well at the case $T=T_{2}$ (compared to the Bayes-optimal predictor for $T=T_{2}$) if $T_{1}$ and $T_{2}$ are close. This result implies the possibility of context length generalization by a simplified Transformer model due to shared structures in the attention matrices.

## 5 Conclusion and Limitations

In this paper, we study the in-context learning ability of the trained Transformer through the lens of uncertainty quantification. In particular, we train the Transformer for a bi-objective task of mean prediction and uncertainty prediction. We develop new results both theoretically and numerically. The takeaway messages are: First, the Transformer can perform in-context uncertainty quantification. Second, the trained Transformer is only guaranteed to achieve a near-optimal in-distribution risk against the Bayes-optimal predictor. This does not imply that the Transformer behaves as the Bayes-optimal predictor either in-distribution or out-of-distribution. Third, the Transformer has the in-context ability for out-of-distribution tasks, but this in-context ability is contingent on a proper training method such as sufficient task diversity, meta-training for covariates shift, and effective removal of the positional encoding. Two important future directions are as follows. First, we believe our method for deriving the generalization bound has implications for a scope much larger than uncertainty quantification and can be used to improve the existing bounds for various tasks using Transformers. Second, all the numerical experiments in the paper are conducted for the linear functions $f_{i}$’s. We believe the same results still hold for nonlinear functions as well; and such results can further consolidate the in-context ability for uncertainty quantification of the Transformer.

## Appendix A Related Works

#### Theoretical Understanding of In-Context Learning.

There are two streams of research in the theoretical understanding of ICL: the first tries to give sharp approximation error bounds on different tasks, while the second focuses on how the trained Transformer approaches the potential optimum. For the approximation error, following the pioneering empirical investigations on simple function classes (Garg et al., ), Von Oswald et al. (); Akyürek et al. () conjecture that the Transformer is doing ICL via gradient descent, and verify it both empirically and theoretically. Based on the mechanism of layer-wise gradient descent construction, Bai et al. () show that Transformers are able to behave (approximately) as well as some well-known algorithms on some statistical problems. Some following works generalize the layer-wise gradient descent construction to other settings such as decision-making (Lin et al., ) and linear regression under representations (Guo et al., ). Apart from the layer-wise gradient descent, some other works consider the one-step gradient descent reached by a single-layer linear-activated Transformer (Zhang et al., ; Wu et al., ) and curve the excessive population risk of the optimal model compared to oracle or the Bayes-optimal predictor. Ahn et al. () give a set of global optima for some specific one-layer or two-layer attention-only models with linear or ReLU activation. Aside from characterizing where the Transformer can reach, another group of works is making efforts towards understanding where the Transformer will reach. One typical way is to study the simplified attention-only Transformers. Zhang et al. () start the analysis of the training dynamics of the gradient flow over the population risk on the linear regression task and show that a single-layer linear-attention-only model converges to some specific sets with suitable initialization. Wu et al. () keep the same spirit and give a sample complexity bound based on a certain gradient descent scheme. For general Transformer models, technical tools from the statistical learning theory are applied. As for the task of predicting the next token in natural language tasks, Xie et al. () provide a viewpoint from the Hidden Markov Model (HMM) and prove the asymptotic consistency under the regularity condition. Bai et al. (); Lin et al. () use chaining arguments with covering numbers for generalization, where Bai et al. () consider the training under fixed length and Lin et al. () consider the problem of sequential decision-making. Li et al. () adopt algorithm stability arguments obtaining a bound of $\tilde{O}(1/\sqrt{nT})$. As is discussed in the main text (see discussions after Theorem 3.2), their analysis will result in a suboptimal $\tilde{\mathcal{O}}(S/\sqrt{nT})$ for the case $S<T$. Zhang et al. () adopt a similar concentration inequality for Markov chains to get a bound of $\tilde{\mathcal{O}}(\sqrt{\tau_{\text{mix}}/(nT)})$. Since they do not consider the limit of context window $S$, their derivation ends up with $\tau_{\text{mix}}\geq T$, which is suboptimal compared to our case. In short, our paper is the first theoretical analysis on the limit of context window $S$ and gets a tighter generalization bound than previous works on the generalization bound when $S<T$.

#### Bayesian Behavior of In-context Learning.

Due to the complex structure of transformers, showing theoretical properties of ICL without proper assumptions are challenging. There has been growing interest in developing experiments to test various properties of ICL, leading to new observations and insights. Some of the earliest works that show transformers behave like Bayesian estimator can be found in Akyürek et al. (); Garg et al. (), and this argument is supported in follow-up works including Li et al. (); Wu et al. (); Bai et al. (). However, there is also increasing empirical evidence demonstrating transformers’ non-Bayesian behavior. Singh et al. () design flipped experiment and show transformers’ Bayesian behavior could be transient. Raventós et al. (); Panwar et al. () demonstrate that the Bayesian behavior of transformers is dependent on the task diversity in the pretraining dataset, and transformers could deviate from the Bayesian predictor if number of different training tasks is large. Falck et al. () design experiments based on the martingale property, a necessary condition of Bayesian behavior, and provide evidence that transformers exhibit non-Bayesian behavior from a statistical perspective.

#### Transformers for Uncertainty Quantification.

Uncertainty quantification has seen significant development within the general machine learning and deep learning domains (Abdar et al. (); Gawlikowski et al. ()), generating considerable interests within communities working on transformer-based large language models (LLMs). See Kuhn et al. (); Manakul et al. (); Lin et al. () for uncertanty quantification using black-box LLMs, and Slobodkin et al. (); Chen et al. (); Ahdritz et al. () for that of white-box LLMs. Most of these works focus on natural language processing tasks which have less statistical properties. Indeed, uncertainty quantification has traditionally been developed from a more statistical and probabilistic perspective (Smith (); Sullivan ()). By adopting transformer models to study more statistics-related problems, our work aims to bridge and contribute to both fields.

## Appendix B Transformer Model

Following Radford et al. (), we consider a decoder-only $L$-layer Transformer model that processes the input sequence $H_{t}$ by applying multi-head attention (MHA) of $M$ heads and multi-layer perceptron (MLP) layer-wise. Without loss of generality, we assume $x_{t}\in\mathbb{R}^{d}$ for some $d\geq 2$. We concatenate each $y_{t}$ with $d-1$ zeros so that it matches the format of each $x_{t}$, while we still denote the concatenated vector by $y_{t}$ with a slight abuse of notations. We denote $H_{t}$ by a matrix in $\mathbb{R}^{d\times(2t-1)}$ for $t=1,\dots,T$, where $H_{t}=[x_{1},y_{1},\cdots,x_{t}]$. We may also refer to $x_{t}$ by $h_{2t-1}$ and $y_{t}$ by $h_{2t}$. In practice, the attention mechanism has a maximum dependence length, and therefore the Transformer model can only produce an output based on the most recent tokens up to a context window size $S$. Hence we assume that at each time step $t$, the Transformer model has a maximum capacity of making predictions based on $x_{t}$ and previous $S$ pairs of $(x_{s},y_{s})$ observations for $s=t-S,\dots,t-1$. In other words, the Transformer has a maximum capacity of processing $2S+1$ tokens, and it is making predictions $\texttt{TF}_{\theta}(H_{t})=\texttt{TF}_{\theta}(H_{t}^{S})$ based on the truncated history $H_{t}^{S}$, where $H_{t}^{S}\coloneqq(x_{\max\{1,t-S\}},y_{\max\{1,t-S\}},\dots,x_{t})$. In the following, we formally describe the architecture of the Transformer used in this paper.

###### Definition B.1 (Multi-Head Attention) .

A multi-head attention layer with $M$ heads and activation function $\texttt{act}(\cdot)$ can be defined as a function $\texttt{MHA}_{W}(\cdot)$ for any sequence $Z_{t}\in\mathbb{R}^{d\times(2t-1)}$ and $t=1,\dots,S+1$,

$$ $\texttt{MHA}_{W}(Z_{t})=Z_{t}+\sum_{m=1}^{M}(W_{V}^{m}Z_{t})\texttt{act}\big{( }(W_{K}^{m}Z_{t})^{\top}(W_{Q}^{m}Z_{t})\big{)},$ $$

where $W=\{(W_{Q}^{m},W_{K}^{m},W_{V}^{m})\}_{m=1}^{M}$ denotes all the parameters, $W_{Q}^{m},W_{K}^{m}\in\mathbb{R}^{d_{m}\times d}$, $W_{V}^{m}\in\mathbb{R}^{d\times d}$ for each $m=1,\dots,M$, and $\texttt{act}\colon\mathbb{R}^{(2t-1)\times(2t-1)}\rightarrow\mathbb{R}^{(2t-1)
\times(2t-1)}$ is the activation function.

Here we merge the residual connection into the multi-head layer and skip the layer normalization to ease the notations and simplify the analysis.
The activation function is usually set to be columns-wise softmax in practice: for each vector $z\in\mathbb{R}^{2t-1}$,

$$ $\texttt{softmax}(z)\coloneqq\left(\frac{\exp(z_{1})}{\sum_{i=1}^{2t-1}\exp(z_{ i})},\dots,\frac{\exp(z_{2t-1})}{\sum_{i=1}^{2t-1}\exp(z_{i})}\right)^{\top}.$ $$

Some theoretical results also consider alternative choices for act. For example, Akyürek et al. (); Ahn et al. (); Zhang et al. () consider the linear activation (that is, to entry-wise divide by the sequence length $2t-1$). Bai et al. (); Guo et al. () also examine the ReLU activation (that is, to entry-wise apply a ReLU function $\texttt{ReLU}(z)=\max\{0,z\}$ and later divide by the sequence length $2t-1$).

###### Definition B.2 (Multi-Layer Perceptron) .

A multi-layer perceptron layer with hidden dimension $d_{h}$ can be defined as a (token-wise) function $\texttt{MLP}_{A}(\cdot)$ for any sequence $Z_{t}\in\mathbb{R}^{d\times(2t-1)}$ and $t=1,\dots,S+1$,

$$ $\texttt{MLP}_{A}(Z_{t})=Z_{t}+A_{2}\texttt{ReLU}(A_{1}Z_{t}),$ $$

where $A=(A_{1},A_{2})$ denotes all the parameters, $A_{1}\in\mathbb{R}^{d_{h}\times d}$, $A_{2}\in\mathbb{R}^{d\times d_{h}}$, and ReLU is the entry-wise ReLU function.

We merge the residual connection into the multi-layer perceptron layer and omit the layer normalization to simplify the theoretical development.

###### Definition B.3 (Transformer) .

A Transformer model with $L$ layers can be defined as a function $\texttt{TF}_{\theta}(\cdot)$ for any sequence $Z_{t}\in\mathbb{R}^{d\times(2t-1)}$ and $t=1,\dots,S+1$. For the $l$-th layer, the model receives $Z_{t}^{(l-1)}$ as the input and processes it by an MHA block and an MLP block, such that

$$ $Z_{t}^{(l)}=\texttt{MLP}_{A^{(l)}}(\texttt{MHA}_{W^{(l)}}(Z_{t}^{l-1})),\quad \forall l=1,\dots,L,$ $$

where $Z_{t}^{(0)}=Z_{t}$. After the $L$-th layer, the model linearly maps the $Z_{t}^{(L)}\in\mathbb{R}^{d\times(2t-1)}$ onto $\mathbb{R}^{2\times(2t-1)}$ via a matrix $P\in\mathbb{R}^{2\times d}$, and we process the second dimension by a softplus function to get the final prediction as

$$ $\hat{y}_{\theta}(Z_{t})=(PZ_{t}^{(L)})_{1,2t-1},$ $$

and

$$ $\hat{\sigma}_{\theta}(Z_{t})=\texttt{softplus}\big{(}(PZ_{t}^{(L)})_{2,2t-1} \big{)}.$ $$

Here $\theta=(\{(W^{(l)},A^{(l)})\}_{l=1}^{L},P)$ encapsulates all the parameters and the function $\texttt{softplus}(z)=\log(1+\exp(z))$ is introduced to avoid negative output. The output is summarized as $\texttt{TF}_{\theta}(Z_{t})\coloneqq(\hat{y}_{\theta}(Z_{t}),\hat{\sigma}_{
\theta}(Z_{t}))$.

###### Remark B.4 .

To enable parallel training, the decoder-only Transformer receives a full sequence in the training phase. The model has a masking component that prevents the model from seeing into the “future”. However, such masking is unnecessary in our setting as the Transformer model receives exactly what it should “see” at each time $t$, and the full dynamics are identical to those in the masked setting.

###### Definition B.1 (Multi-Head Attention) .

###### Definition B.2 (Multi-Layer Perceptron) .

###### Definition B.3 (Transformer) .

###### Remark B.4 .

#### Miscellaneous notations.

Denote the set $\{1,\dots,K\}$ by $[K]$. Denote the consecutive sequence $\{i,i+1,\dots,j\}$ by $i:j$. Denote the matrix $A$’s entry at the $i$-th row and the $j$-th column by $A_{i,j}$. Denote the vector $x$’s $i$-th element by $(x)_{i}$. Define the $d$-dimensional vector $x$’s $p$-norm as $(\sum_{i=1}^{d}(x)_{i}^{p})^{1/p}$ for $p\in[1,\infty]$, where $\|x\|_{\infty}=\max_{1\leq i\leq d}(x)_{i}$. Define the $m\times n$-sized matrix $A$’s $(p,q)$-norm as $(\sum_{j=1}^{n}\|A_{:,j}\|_{p}^{q})^{1/q}$. Denote the $d$-dimensional diagonal matrix by $\mathrm{diag}\{\lambda_{1},\dots,\lambda_{d}\}$. Denote the $d$-dimensional identity matrix by $I_{d}$. Denote the total variation distance between two probability distributions $P$ and $Q$ by $\mathrm{TV}(P,Q)$. Denote the Kullback-Leibler divergence between two probability distributions such that $P\ll Q$ by $D_{\mathrm{kl}}(P\|Q)$. Denote the product measure of $P$ and $Q$ by $P\times Q$ or $P\otimes Q$. Denote the Cartesian product of two spaces $\mathcal{X}$ and $\mathcal{Y}$ by $\mathcal{X}\times\mathcal{Y}$. Denote the tensor-product $\sigma$-algebra of two $\sigma$-algebras $\Sigma_{1}$ and $\Sigma_{2}$ by $\Sigma_{1}\otimes\Sigma_{2}$. Denote the limiting behavior of being upper (lower, both upper and lower, respectively) bounded by up to some constant(s) by $\mathcal{O}$ ($\Omega$, $\Theta$, respectively). Denote
$\tilde{\mathcal{O}}$ to be the $\mathcal{O}$ but omitting some poly-logarithmic terms.

### B.1 Assumptions

Based on this setup of the Transformer model, we introduce the following bounded assumptions used for Theorem 3.2. Such assumptions are common in the analyses of the Transformer model (Bai et al., ; Zhang et al., ) by either assuming an extra clipping operator or explicit upper bounds.

###### Assumption B.5 .

Assume $\Theta=\mathcal{B}(0,B_{\text{TF}})$, where the norm is defined as

$$ $\|\theta\|\coloneqq\max\{\|W^{(l)}\|,\|A^{(l)}\|,\|P\|\ \colon\ l=1,\dots,L\}.$ $$

The corresponding norms are defined as

$$ $\|W\|\coloneqq\max\{\|W_{V}^{m}\|_{2,2},\|W_{K}^{m}\|_{2,2},\|W_{Q}^{m}\|_{2,2 }\ \colon\ m=1,\dots,M\},$ $$

$$ $\|A\|\coloneqq\max\{\|A_{1}\|_{2,2},\|A_{2}\|_{2,2}\},\ \ \ \|P\|\coloneqq\|P^ {\top}\|_{2,\infty},$ $$

where we omit some superscripts/subscripts of the layer number $(l)$ for simplicity.

###### Assumption B.6 .

Assume $\|H_{t}\|_{2,\infty}$ is bounded by $B_{H}$ almost surely. Such a regularization is equivalent to assuming $\|x_{t}\|_{2}\leq B_{H}$ and $|y_{t}|\leq B_{H}$ almost surely.

###### Assumption B.5 .

###### Assumption B.6 .

### B.2 Approximation Error

In Section 3, we provide the generation bound in Theorem 3.2. Now we give an analysis for the approximation error. We define the Bayes-optimal risk obtained by the Bayes-optimal predictor in Proposition 3.1: for each $t=1,\dots,T$,

$$ $R_{t}^{*}\coloneqq\mathbb{E}\bigg{[}\ell\Big{(}\big{(}y_{t}^{*}(H_{t}),\sigma_ {t}^{*}(H_{t})\big{)},y_{t}\Big{)}\bigg{]}.$ (4) $$

However, Transformers only have access to the truncated history $H_{t}^{S}$, which prevents them from reaching $R_{t}^{*}$. By using Proposition 3.1 for the $H_{t}^{S}$, we denote the truncated Bayes optimum for each $t$:

$$ $y_{t}^{S*}(H_{t}^{S})\coloneqq\mathbb{E}[y_{t}|H_{t}^{S}],$ $$

and

$$ $\left(\sigma_{t}^{S*}(H_{t}^{S})\right)^{2}\coloneqq\mathbb{E}[(f(x_{t})-y_{t} ^{S*}(H_{t}^{S}))^{2}|H_{t}^{S}]+\mathbb{E}[\sigma^{2}|H_{t}^{S}].$ $$

We denote the truncated Bayes-optimal risk as

$$ $R_{t}^{S*}\coloneqq\mathbb{E}\bigg{[}\ell\Big{(}\big{(}y_{t}^{S*}(H_{t}^{S}), \sigma_{t}^{S*}(H_{t}^{S})\big{)},y_{t}\Big{)}\bigg{]}.$ (5) $$

It is straightforward to check that

$$ $R_{t}^{S*}=R_{t}^{*},\quad\text{for any }t\leq S.$ (6) $$

However, the equality is generally not true for $t>S$. We give an example to illustrate the gap.

###### Example B.7 .

Consider the case where one has oracle access to the noise level $\sigma$. Note that the oracle knowledge only reduces the risk $R_{t}^{S*}$, since we use information that is not a measurable function of $H_{t}^{S}$. The problem is reduced to a regression problem.

Suppose the function $f$ is linear and its weight vector has a prior distribution of $\mathcal{N}(0,\sigma^{2}I_{d})$, and the noise $\epsilon\sim\mathcal{N}(0,1)$. Suppose $x_{t}\sim\mathcal{N}(0,I_{d})$. Then the optimal estimator is an optimally tuned Ridge regression.

Tsigler and Bartlett () show that with high probability, the optimal Ridge regression estimator has an average risk of $\frac{1}{2}+\Theta(1/S)$,
where the term $\frac{1}{2}$ is due to $\mathbb{E}[(y-f(x))^{2}]/(2\sigma^{2})$. But as the length $t$ approaches infinity, the average risk of the optimal Ridge regression over the full sequence $H_{t}$ will converge to $\frac{1}{2}$ with high probability, meaning that the estimated $\hat{f}$ will converge to true $f$ for every sequence. Hence one can always construct an uncertainty estimation by averaging all the residuals, and such an estimation $\hat{\sigma}$ will converge to the true $\sigma$. Thus, we have $R_{t}^{*}$ approaching $\frac{1}{2}$ as $t$ grows to infinity,
leading to the conclusion that

$$ $R_{t}^{S*}-R_{t}^{*}\geq\Omega(1/S),$ $$

for sufficiently large $t$.

Example B.7, together with Theorem 3.2, shows the approximation-estimation tradeoff in selecting the context window $S$ of Transformer models. Previous works (Wu et al., ; Bai et al., ; Zhang et al., ; Guo et al., ) consider the case where $t\leq S$, and establish the upper bounds for the approximation error. In other words, these existing results are all made with respect to the gap between $R_{t}^{S*}$ and $R(\texttt{TF}_{\theta^{*}})$. To our knowledge, we are the first work to point out the extra term of approximation error due to truncation.

###### Example B.7 .

## Appendix C More Numerical Results and Discussions

In this section, we provide more numerical results with our discussions. The code for these experiments is available at [https://github.com/ZhongzeCai/ICL_UQ](https://github.com/ZhongzeCai/ICL_UQ).

### C.1 In-distribution performance

In Figure 1, we provide a comparison of the in-distribution performance of the trained Transformer v.s. the Bayes-optimal predictor. A subtle point is that for the uncertainty prediction, we only plot the average predicted uncertainty, this does not fully imply that the Transformer gives a similar prediction as the Bayes-optimal predictor. To this end, Figure 4 plots the difference between each of the models and the Bayes-optimal predictor in terms of uncertainty estimation.

Figure: Figure 4: In-distribution performance of the uncertainty prediction against the Bayes-optimal predictor. The $y$-axis gives an estimate of $\mathbb{E}\left[-\log|\hat{\sigma}(H_{t})-\sigma^{*}(H_{t})|\right]$ where the expectation is taken with respect to $H_{t}$. Here $\hat{\sigma}(H_{t})$ is the uncertainty estimate produced by an algorithm (ridge regression, linear regression, or transformer), and $\sigma^{*}(H_{t})$ is the Bayes-optimal predictor given in Proposition 3.1 and calculated by Section G.3. The figure shows that the Transformer and the Bayes-optimal predictor produce similar uncertainty predictions. In addition, the Transformer trained on a larger pool of tasks (larger $N$) produces a better approximation of the Bayes-optimal predictor.
Refer to caption: extracted/5616625/fig/exp_1_Uncertainty_Quality.png

### C.2 Out-of-distribution perfomance

In Figure 2, we plot the Bayes-optimal predictor under three OOD settings, and we note that though the Bayes-optimal predictor uses a wrong prior, it has the ability to work as an algorithm to correct the prediction with the in-context samples. Now in Figure 5 (a), we compare the Bayes-optimal predictor that uses the wrong prior with the Bayes-optimal predictor that uses the correct prior (which replaces the in-distribution prior with the correct OOD prior of $\sigma^{2}$). The figure is based on the large OOD setting. We observe that the Bayes-optimal predictors with the ID prior or the OOD prior both converge to the true uncertainty level. For Figure 5 (b), we plot the performances under the same large OOD setting. As a reference line, we copy and paste the Bayes-optimal predictor’s curve in Figure 1 (b) here. We note this reference line is computed based on the in-distribution (ID) data and is not comparable at all to the predicted uncertainty level on the OOD data. Yet, we note that when the number of tasks is small when training the Transformer (say $N=4096$), it tends to make predictions on the OOD data by treating the OOD data just as ID data, and this means the trained Transformer is doing in-weight learning and has no in-context learning ability. As the number of tasks increases, the Transformer gradually gains the in-context ability and moves towards the Bayes-optimal predictor on the OOD data.

Figure: (a) Bayes-optimal w/ ID or OOD prior
Refer to caption: extracted/5616625/fig/exp_2_OOD_Std.png

### C.3 Training dynamics and task shift OOD performance

Now we zoom into the training dynamics to further investigate the OOD performance under task shift. In the following example, we derive a theoretical result based on Theorem 4.1 in Zhang et al. (). Specifically, $R$ and $R^{\prime}$ (following the notations therein) denote the in-distribution and out-of-distribution expected risk. The result says that while the in-distribution risk continues decreasing over time, the out-of-distribution risk may keep increasing or may first decrease and then increase. Importantly, the out-of-distribution risk may depend on the initial point of the training procedure.

Reaching the Bayes optimum requires prior knowledge of the underlying distribution. We provide a simple example of where the Transformer stores its prior knowledge and how it hurts the OOD performance even under a mild distribution shift.

###### Example C.1 (A corollary that can be derived based on Theorem 4.1 in Zhang et al. ( 2023a ) ) .

Consider a one-layer attention-only Transformer model with linear activation and one attention head on the linear regression task. We now concatenate each of the inputs to be $[x_{t}^{\top},y_{t}]^{\top}\in\mathbb{R}^{d+1}$. Suppose we focus on the linear regression task on the $(T+1)$-th sample after observing $T$ context exemplars, where each $w^{(i)}\sim\mathcal{N}(0,I_{d})$, each $x_{t}^{(i)}\sim\mathcal{N}(0,I_{d})$, each $\epsilon_{t}^{(i)}\sim\mathcal{N}(0,1)$, and $y_{t}^{(i)}=w^{(i)\top}y_{t}^{(i)}+\sigma_{0}\cdot\epsilon_{t}^{(i)}$. If we adopt the same training setup as Zhang et al. () (with details referred to therein), then for any $|\sigma_{0}^{\prime}-\sigma_{0}|\geq\Delta$ for some $\Delta>0$, if we train on the distribution w.r.t. $\sigma_{0}$ but test on the distribution w.r.t. $\sigma_{0}^{\prime}$ (denoted by $R^{\prime}$), then for $C=d/(16(2+\sigma_{0}))$ and any sequence $0<\delta_{1}<\delta_{2}<\dots<C\Delta$, there exists a non-decreasing sequence $0\leq\tau(\delta_{1})\leq\tau(\delta_{2})\leq\dots$, such that

$$ $R^{\prime}(\tau(\delta_{i}))-R^{\prime}_{\theta^{*\prime}}\geq\delta_{i},\quad \text{for each }i=1,2,\dots,$ $$

while the parameter $(W_{K}^{\top}W_{Q})_{1:d,1:d}(W_{V})_{d+1,d+1}$ converges to $1/(1+(2+\sigma_{0})/T)\cdot I_{d}$ (which is the corresponding part of some $\theta^{*}$). Here $\theta^{*}$ and $\theta^{*\prime}$ minimize the population risk $R$ and $R^{\prime}$, accordingly.

We design an experiment to show that as training proceeds, the model’s OOD performance is improved abruptly in the starting phase, but then degrades steadily after too many steps of training. We introduce the experiment settings below. A visualization of the setup is given in Figure 6.

Figure: Figure 6: The settings of the OOD experiment. (Left) The in-distribution (ID) tasks are sampled from regions denoted by the green blocks, and the OOD tasks are sampled from the red blocks. (Right) In the starting phase, training improves both the ID and OOD performance. But if training for too many steps, the ID performance is only marginally improved, while the OOD performance steadily degrades.
Refer to caption: extracted/5616625/fig/ood_demopng.png

Each linear task in our uncertainty quantification setting is characterized by parameters $(w,\sigma)$. We define two regions for $w$, denoted by $\mathcal{W}_{1}$ and $\mathcal{W}_{2}$. And two regions for $\sigma$, denoted by $\mathcal{G}_{1}$ and $\mathcal{G}_{2}$. When $w$ is sampled from $\mathcal{W}_{1}$, $w$ follows the following distribution

$$ $w=|\beta|,\quad\beta\sim\mathcal{N}(0,I_{8}).$ $$

When $w$ is sampled from $\mathcal{W}_{2}$, $w$ follows the following distribution

$$ $w=-|\beta|,\quad\beta\sim\mathcal{N}(0,I_{8}).$ $$

For $\sigma$, define $\mathcal{G}_{1}=[0.1,0.3]\cup[0.5,0.7]$ and $\mathcal{G}_{2}=[0.3,0.5]\cup[0.7,0.9]$. Define $\mathcal{G}_{1}$ and $\mathcal{G}_{2}$ to be the “complementary” group of each other. We sample $\sigma$ independently from $w$, and we always sample $\sigma$ uniformly from either group $\mathcal{G}_{1}$ or $\mathcal{G}_{2}$. As marked in Figure 6, the “ID” tasks sample its parameters from $(w,\sigma)\in\mathcal{W}_{1}\otimes\mathcal{G}_{1}\bigcup\mathcal{W}_{2}
\otimes\mathcal{G}_{2}$ and the “OOD” tasks sample its parameters from $(w,\sigma)\in\mathcal{W}_{1}\otimes\mathcal{G}_{2}\bigcup\mathcal{W}_{2}
\otimes\mathcal{G}_{1}$. The training is on “ID” tasks, and the trained model is tested on both “ID” tasks and “OOD” tasks. The metric we evaluate in this experiment is the “prediction accuracy” of uncertainty. The accuracy denotes the probability that the model predicts the $\sigma$ into its “*right*” group. (for a prompt generated from $(w,\sigma)$ with $\sigma\in\mathcal{G}$, we say that the model makes a “right” prediction if the predicted $\sigma$ falls into $\mathcal{G}$).

Figure 7 presents the experiment result. The prediction accuracy on the ID dataset peaks after $20$k steps of training. At the same time, the prediction accuracy on the OOD dataset also increases to $80\%$. After that, the ID performance remains unchanged, but the OOD accuracy keeps dropping.

Figure: Figure 7: The accuracy denotes the probability that the model predicts the $\sigma$ into the “right” group. For example, if the sampled tasks take $\sigma$ from group $\mathcal{G}_{1}$, then accuracy denotes the probability that the model predicts $\sigma$ into $\mathcal{G}_{1}$. The data is collected for the $100$-th token in order to eliminate the epistemic uncertainty due to insufficient in-context samples. The x-axis denotes the training steps. This figure shows that when training too many steps ($>40k$ in this case), the generalization ability of the model steadily declines.
Refer to caption: extracted/5616625/fig/exp_5_Std_pred_accuracy.png

In order to verify that the degradation of OOD performance is due to the increasing confidence in the prior information of the training data, we check for the OOD distribution whether the model has predicted $\sigma$ into the complementary group. The result is presented in Figure 8, which verifies that after training too many steps, the model tends to predict $\sigma$ following the training prior. A more concrete way to explain it: consider an OOD sampled task $(w,\sigma)$ where $w>0$. According to the sampling rule of OOD tasks, it must have $\sigma\in\mathcal{G}_{2}$. If the model has the OOD ability, it should predict $\sigma\in\mathcal{G}_{2}$. But if it has too much confidence in its training prior, it will predict $\sigma$ into $\mathcal{G}_{1}$, the complementary group of $\mathcal{G}_{2}$. Figure 8 shows that for the misclassified OOD tasks, the model has predicted them into complementary groups.

Figure: Figure 8: This figure validates that the decline of OOD ability is due to increasing confidence in the training prior. The blue bars correspond to the OOD accuracy, and the red bars give the probability that the model predicts uncertainty $\sigma$ into the complementary group (i.e. the training distribution of $\sigma$). As the training proceeds, most of the misclassified $\sigma$ are predicted following the training prior.
Refer to caption: extracted/5616625/fig/exp_5_OOD_vs_Prior.png

###### Example C.1 (A corollary that can be derived based on Theorem 4.1 in Zhang et al. ( 2023a ) ) .

### C.4 Covariate shift experiment

The experiment result of Section 4.2 is given in Figure 9. We evaluate the prediction error of models ordinarily trained, and models trained by the meta-training process. For both mean and uncertainty, the models trained by the meta-training procedure have a smaller prediction error.

Figure: Figure 9: The errors of the mean and uncertainty prediction where the error is measured by the absolute difference against the Bayes-optimal predictor. The static_x_model corresponds to models trained with the standard way in generating $X_{t}$’s, while the meta_x_model corresponds to the new approach of drawing $X_{t}$’s from the meta-training procedure. In all 4 OOD settings, meta-trained models have better performance.
Refer to caption: x1.png

## Appendix D Proofs of the Results in the Main Paper

### D.1 Proof of Proposition 3.1

###### Proof.

Recall that the population risk is

$$ $L(\hat{y},\hat{\sigma})\coloneqq\mathbb{E}_{f,x_{[t]},\epsilon_{[t]},\sigma} \left[\log\hat{\sigma}(H_{t})+\frac{(y_{t}-\hat{y}(H_{t}))^{2}}{2\hat{\sigma}^ {2}(H_{t})}\right].$ $$

We first prove that for any $\hat{\sigma}(H_{t})$, the choice of $\hat{y}_{t}=y^{*}=\mathbb{E}[y_{t}|H_{t}]$ minimizes the population risk. With any fixed $\sigma_{0}>0$, when $\hat{\sigma}_{t}=\sigma_{0}$, then minimizing the population risk reduces to minimizing $\mathbb{E}[(y_{t}-\hat{y}(H_{t}))^{2}]$. Using Fubini’s Theorem and the fact that the conditional distribution exists, we have

$$ $\displaystyle\mathbb{E}_{f,x_{[t]},\epsilon_{[t]},\sigma}\big{[}(y_{t}-\hat{y} (H_{t}))^{2}\big{]}$ $\displaystyle=\mathbb{E}_{H_{t}}\Big{[}\mathbb{E}\big{[}(y_{t}-\hat{y}(H_{t})) ^{2}\big{|}H_{t}\big{]}\Big{]}$ $\displaystyle=\mathbb{E}_{H_{t}}\Big{[}\mathbb{E}\big{[}(f(x_{t})-\hat{y}(H_{t }))^{2}\big{|}H_{t}\big{]}\Big{]}+\mathbb{E}_{H_{t}}\big{[}\mathbb{E}[\sigma^{ 2}|H_{t}]\big{]},$ (7) $$

where the last equality follows from the fact that $\epsilon_{t}$ is independent of $H_{t}$ and $\sigma$ and is of zero mean and unit variance. Since the second term on the right-hand-side of (7) does not depend on $\hat{y}$, we only need to focus on the first term. For each realization of $H_{t}$, the prediction $\hat{y}(H_{t})$ is a single point; combining it with the fact that the squared loss is minimized with respect to one single point prediction if and only if that point is the expectation (in this case, the conditional expectation $\mathbb{E}[f(x_{t})|H_{t}]$), we prove that for any $\sigma_{0}$, the population risk’s minimizer

$$ $y_{t}^{*}(\sigma_{0})=\mathbb{E}[f(x_{t})|H_{t}]=\mathbb{E}[f(x_{t})+\sigma \cdot\epsilon_{t}|H_{t}]=\mathbb{E}[y_{t}|H_{t}],$ $$

where the second equality follows again from the fact that $\epsilon_{t}$ is independent of $H_{t}$ and $\sigma$, and $\epsilon_{t}$ is of zero mean. Since this equality holds for an arbitrary $\sigma_{0}$, we can conclude that

$$ $y_{t}^{*}=\mathbb{E}[y_{t}|H_{t}].$ $$

Now we have confirmed the optimal choice of $y_{t}^{*}$ regardless of whatever $\hat{\sigma}$ is. We can thus find the optimal choice of $\hat{\sigma}$ by fixing $\hat{y}=y_{t}^{*}$ and minimizing the population risk. Similarly, we can change the integration order so that we only need to minimize $\mathbb{E}[\log\hat{\sigma}(H_{t})+\frac{(y_{t}-\hat{y}(H_{t}))^{2}}{2\hat{
\sigma}^{2}(H_{t})}|H_{t}]$ for any realization of $H_{t}$.
Calculations show that

$$ $\displaystyle\phantom{=}\frac{\partial\mathbb{E}\left[\log\hat{\sigma}(H_{t})+ \frac{(y_{t}-\hat{y}(H_{t}))^{2}}{2\hat{\sigma}^{2}(H_{t})}\mid|H_{t}\right]}{ \partial\hat{\sigma}(H_{t})}$ $\displaystyle=\frac{\partial\left(\log\hat{\sigma}(H_{t})+\frac{\mathbb{E}[(y_ {t}-\hat{y}(H_{t}))^{2}|H_{t}]}{2\hat{\sigma}^{2}(H_{t})}\right)}{\partial\hat {\sigma}(H_{t})}$ $\displaystyle=\frac{\hat{\sigma}^{2}(H_{t})-\mathbb{E}[(y_{t}-\hat{y}(H_{t}))^ {2}|H_{t}]}{\hat{\sigma}^{3}(H_{t})},$ $$

where the first equality follows from the fact that on observing $H_{t}$, $\hat{\sigma}(H_{t})$ is a fixed value, and the second equality from the calculus. Thus, the risk is minimized if and only if
$\hat{\sigma}(H_{t})=\mathbb{E}[(y_{t}-\hat{y}(H_{t}))^{2}|H_{t}]$. Substituting $\hat{y}$ for $y_{t}^{*}$, we have

$$ $\sigma_{t}^{*2}(H_{t})=\mathbb{E}[(y_{t}-y_{t}^{*}(H_{t}))^{2}|H_{t}]=\mathbb{ E}[(f(x_{t})-y_{t}^{*}(H_{t}))^{2}|H_{t}]+\mathbb{E}[\sigma^{2}|H_{t}],$ $$

where the last equality follows again from the fact that $\epsilon_{t}$ is independent of $H_{t}$ and is of zero mean and unit variance.
∎

###### Proof.

### D.2 Proof of Theorem 3.2

###### Proof.

$\hat{\theta}^{\text{ERM}}$
Before we start the detailed proof, we define another flattened sequence $(\tilde{x}_{k},\tilde{y}_{k})$ for $k=1,\dots,nT$, where for $k=iT+t$ we have

$$ $\big{(}\tilde{x}_{iT+t},\tilde{y}_{iT+t}\big{)}\coloneqq\big{(}x_{t}^{(i)},y_{ t}^{(i)}\big{)}.$ (8) $$

Here, we merge all the sequences $\{(x_{t}^{(i)},y_{t}^{(i)})\}_{t=1}^{T}$ for $i=1,\dots,n$ into one sequence $(\tilde{x}_{k},\tilde{y}_{k})_{k=1}^{nT}$. Similarly, we can define a flattened truncated history $\tilde{H}_{k}^{S}$ as

$$ $\tilde{H}_{iT+t}^{S}\coloneqq(x_{\max\{t-S,1\}}^{(i)},y_{\max\{t-S,1\}}^{(i)}, \dots,x_{t}^{(i)},y_{t}^{(i)}.$ (9) $$

Note that $\tilde{H}_{k,k=iT+t}^{S}=(H_{t}^{S(i)},y_{t}^{(i)})$, since we have added the target label $y_{t}^{S(i)}$ into the flattened truncated history $\tilde{H}_{k}^{S}$ for notation simplicity. With a slight abuse of notations, we have

$$ $\ell_{\theta}(\tilde{H}_{k,k=iT+t}^{S})\coloneqq\ell(\texttt{TF}_{\theta}(H_{t }^{S}),y_{t})=\ell(\texttt{TF}_{\theta}(H_{t}),y_{t}),$ (10) $$

where the equality holds since we are making predictions based on at most $S$ pairs of $(x_{t},y_{t})$. We can similarly replace the $\ell$ function in the definition of empirical risk $r$ and population risk $R$, obtaining

$$ $\displaystyle r(\texttt{TF}(\theta))$ $\displaystyle=\frac{1}{nT}\sum_{i=1}^{n}\sum_{t=1}^{T}\ell(\texttt{TF}_{\theta }(H_{t}^{S}),y_{t})$ $\displaystyle=\frac{1}{nT}\sum_{k=1}^{nT}\ell_{\theta}(\tilde{H}_{k}^{S}),$ (11) $$

and

$$ $\displaystyle R(\texttt{TF}(\theta))$ $\displaystyle=\frac{1}{T}\mathbb{E}_{H_{t}}\left[\sum_{t=1}^{T}\ell\big{(} \texttt{TF}(\theta)(H_{t}),y_{t}\big{)}\right]$ $\displaystyle=\frac{1}{T}\mathbb{E}_{\tilde{H}_{k}^{S\prime}}\left[\sum_{t=1}^ {T}\ell_{\theta}(\tilde{H}_{k,k=iT+t}^{S\prime})\right]$ $\displaystyle=\mathbb{E}_{\tilde{H}_{k}^{S\prime}}\left[\frac{1}{nT}\sum_{k=1} ^{nT}\ell_{\theta}(\tilde{H}_{k}^{S\prime})\right],$ (12) $$

where $\tilde{H}_{t}^{S\prime}$ is another flattened truncated history that is i.i.d. to $\tilde{H}_{t}^{S}$. For notation simplicity, we define

$$ $\tilde{\mathcal{H}}^{S}\coloneqq(\tilde{H}_{1}^{S},\dots,\tilde{H}_{nT}^{S}).$ (13) $$

Then we simplify the notations as

$$ $r_{\theta}\big{(}\tilde{\mathcal{H}}^{S}\big{)}\coloneqq r(\texttt{TF}(\theta)),$ (14) $$

and

$$ $R_{\theta}\coloneqq\mathbb{E}_{\tilde{\mathcal{H}}^{S\prime}}\left[r_{\theta} \big{(}\tilde{\mathcal{H}}^{S\prime}\big{)}\right]=R(\texttt{TF}(\theta)).$ (15) $$

To control the difference between $R_{\theta}$ and $r_{\theta}(\tilde{\mathcal{H}}^{S})$ for any $\theta$ (which could potentially depend on training data $\mathcal{D}$), we use PAC-Bayes arguments for simplicity.

All the following arguments are made with the conditional distribution on knowing each ${f^{(i)}}$ and ${\sigma^{(i)}}$, for each ${i=1,\dots,n}$. We omit the conditional dependencies in our notations only for simplicity.

By our definition of data generation, the flattened truncated history $\tilde{H}_{k}^{S}$ naturally forms up a Markov chain on the space $\otimes_{k=1}^{nT}\Omega_{k}$ (verified in Lemma E.13), since the newly generated $(x_{t},y_{t})$ are conditionally independent of all previous observations. Here $\Omega_{k,k=iT+t}\coloneqq(\mathcal{X}\times\mathcal{Y})^{\otimes\min\{t,S\}}$.

Fix a $\theta$ that does not depend on the training data $\mathcal{D}$. We now bound the difference between $R_{\theta}$ and $r_{\theta}(\tilde{\mathcal{H}}^{S})$ via concentration inequality for Markov chains. From Lemma F.2, we know that if the Markov chain’s mixing time is small enough (which means it quickly converges to the stationary distribution), the concentration properties over the Markov chain would be good enough to enable the standard PAC-Bayes arguments. We also know from Lemma E.15 that the flattened truncated history has a mixing time no greater than $\min\{S,T\}$, since all the histories $S$ pairs before the current time would be truncated from the input, and the history $H_{t}^{S}$ restarts every time a sequence reaches length $T$. With these observations, we start our detailed derivation.

Since the function $\ell$ is almost surely bounded by $C_{2}$ as is shown in Lemma E.3, we have almost surely for any $\tilde{\mathcal{H}}^{S}$ and $\tilde{\mathcal{H}}^{S\prime}$,

$$ $r_{\theta}(\tilde{\mathcal{H}}^{S})-r_{\theta}(\tilde{\mathcal{H}}^{S\prime}) \leq\sum_{k=1}^{nT}\frac{2C_{2}}{nT}\cdot\mathbbm{1}\{\tilde{H}_{k}^{S}\neq \tilde{H}_{k}^{S\prime}\}.$ (16) $$

We can use McDiarmid type’s inequality for Markov chains (Lemma F.2, with the mixing time upper bound no greater than $\min\{S,T\}$ (specified in Lemma E.15), such that for any $\lambda\in\mathbb{R}$,

$$ $\mathbb{E}_{\mathcal{D}}\Big{[}\exp\big{(}\lambda(r_{\theta}(\tilde{\mathcal{H }}^{S})-R_{\theta}(\tilde{\mathcal{H}}^{S}))\big{)}\Big{]}\leq\exp\Big{(}\frac {2\lambda^{2}C_{2}^{2}\min\{S,T\}}{nT}\Big{)}.$ (17) $$

Set $\pi$ to be the distribution over $\Theta$ defined in Lemma E.11. Since $\pi$ is chosen independently from $\mathcal{D}$, we can integrate (17) with respect to $\theta\sim\pi$ such that

$$ $\mathbb{E}_{\theta\sim\pi}\bigg{[}\mathbb{E}_{\mathcal{D}}\Big{[}\exp\big{(} \lambda(r_{\theta}(\tilde{\mathcal{H}}^{S})-R_{\theta}(\tilde{\mathcal{H}}^{S} )\big{)}\Big{]}\bigg{]}\leq\exp\Big{(}\frac{2\lambda^{2}C_{2}^{2}\min\{S,T\}}{ nT}\Big{)}.$ (18) $$

Using Fubini’s Theorem, we can exchange the order of integration, such that

$$ $\mathbb{E}_{\mathcal{D}}\bigg{[}\mathbb{E}_{\theta\sim\pi}\Big{[}\exp\big{(} \lambda(r_{\theta}(\tilde{\mathcal{H}}^{S})-R_{\theta}(\tilde{\mathcal{H}}^{S} )\big{)}\Big{]}\bigg{]}\leq\exp\Big{(}\frac{2\lambda^{2}C_{2}^{2}\min\{S,T\}}{ nT}\Big{)}.$ (19) $$

By applying Donsker-Varadhan’s formula (Lemma F.3), we derive from (19) that

$$ $\mathbb{E}_{\mathcal{D}}\bigg{[}\exp\Big{(}\sup_{\rho\in\mathcal{P}(\Theta)} \big{\{}\mathbb{E}_{\theta\sim\rho}\big{[}\lambda(r_{\theta}(\tilde{\mathcal{H }}^{S})-R_{\theta}(\tilde{\mathcal{H}}^{S})\big{]}-D_{\mathrm{kl}}(\rho\|\pi) \big{\}}\Big{)}\bigg{]}\leq\exp\Big{(}\frac{2\lambda^{2}C_{2}^{2}\min\{S,T\}}{ nT}\Big{)}.$ $$

Rearranging terms, we have

$$ $\mathbb{E}_{\mathcal{D}}\bigg{[}\exp\Big{(}\sup_{\rho\in\mathcal{P}(\Theta)} \big{\{}\mathbb{E}_{\theta\sim\rho}\big{[}\lambda(r_{\theta}(\tilde{\mathcal{H }}^{S})-R_{\theta}(\tilde{\mathcal{H}}^{S})\big{]}-D_{\mathrm{kl}}(\rho\|\pi) \big{\}}-\frac{2\lambda^{2}C_{2}^{2}\min\{S,T\}}{nT}\Big{)}\bigg{]}\leq 1.$ (20) $$

Using Chernoff’s bound (Lemma F.4) with probability $\delta/4$, we have with probability at least $1-\frac{\delta}{4}$ w.r.t. $\mathcal{D}$,

$$ $\sup_{\rho\in\mathcal{P}(\Theta)}\big{\{}\mathbb{E}_{\theta\sim\rho}\big{[} \lambda(r_{\theta}(\tilde{\mathcal{H}}^{S})-R_{\theta}(\tilde{\mathcal{H}}^{S} )\big{]}-D_{\mathrm{kl}}(\rho\|\pi)\big{\}}-\frac{2\lambda^{2}C_{2}^{2}\min\{S ,S\}}{nT}\leq\log(4/\delta).$ (21) $$

Since this bound (21) holds for any distribution $\rho$ over $\Theta$, we can set $\rho$ to be $\rho_{\hat{\theta}^{\text{ERM}}}$ as defined in Lemma E.11, resulting in a high-probability bound

$$ $\displaystyle\phantom{=}\mathbb{E}_{\theta\sim\rho_{\hat{\theta}^{\text{ERM}}} }\big{[}r_{\theta}(\tilde{\mathcal{H}}^{S})-R_{\theta}(\tilde{\mathcal{H}}^{S} \big{]}$ $\displaystyle\leq\frac{D_{\mathrm{kl}}(\rho_{\hat{\theta}^{\text{ERM}}}\|\pi)} {\lambda}+\frac{2\lambda C_{2}^{2}\min\{S,T\}}{(nT)}+\log(4/\delta)\quad$ (rearranging terms) $\displaystyle\leq C_{2}\sqrt{\min\{S,T\}/(nT)}\cdot\big{(}D_{\mathrm{kl}}(\rho _{\hat{\theta}^{\text{ERM}}}\|\pi)+2\big{)}+\log(4/\delta)\quad$ (by setting $\lambda=\sqrt{nT/\min\{S,T\}}\cdot(1/C_{2})$ ) $\displaystyle\leq\tilde{\mathcal{O}}(\sqrt{\min\{S,T\}/(nT)}).\quad$ (by Lemma E.11 ) (22) $$

By Lemma E.12, the loss function is Lipschitz. Since for any $\theta\in\mathrm{supp}(\rho_{\hat{\theta}^{\text{ERM}}})$, $\theta$ is up to $\mathcal{O}(1/(nT))$ away from $\hat{\theta}^{\text{ERM}}$, we can control the difference between the risks of any $\theta\in\mathrm{supp}(\rho_{\hat{\theta}^{\text{ERM}}})$ and $\hat{\theta}^{\text{ERM}}$ as

$$ $\big{|}r_{\theta}(\tilde{\mathcal{H}}^{S})-r_{\hat{\theta}^{\text{ERM}}}( \tilde{\mathcal{H}}^{S})\big{|}\leq\tilde{\mathcal{O}}(1/(nT)),$ (23) $$

$$ $\big{|}R_{\theta}-R_{\hat{\theta}^{\text{ERM}}}\big{|}\leq\tilde{\mathcal{O}}( 1/(nT)).$ (24) $$

Thus, we have

$$ $r_{\hat{\theta}^{\text{ERM}}}(\tilde{\mathcal{H}}^{S})-R_{\hat{\theta}^{\text{ ERM}}}\leq\tilde{\mathcal{O}}(\sqrt{\min\{S,T\}/(nT)}).$ (25) $$

Applying the above arguments again for the negative of $r$, we have with probability at least $1-\delta/2$,

$$ $\big{|}r_{\hat{\theta}^{\text{ERM}}}(\tilde{\mathcal{H}}^{S})-R_{\hat{\theta}^ {\text{ERM}}}\big{|}\leq\tilde{\mathcal{O}}(\sqrt{\min\{S,T\}/(nT)}).$ (26) $$

For $\theta^{*}$, we can repeat the above steps and get

$$ $\big{|}r_{\theta^{*}}(\tilde{\mathcal{H}}^{S})-R_{\theta^{*}}\big{|}\leq\tilde {\mathcal{O}}(\sqrt{\min\{S,T\}/(nT)}).$ (27) $$

The probability that all these bounds hold simultaneously is at least $1-\delta$ w.r.t. $\mathcal{D}$.

Hence with probability at least $1-\delta$,

$$ $\displaystyle\phantom{=}R({\texttt{TF}_{\hat{\theta}^{\text{ERM}}}})-R({ \texttt{TF}_{\theta^{*}}})$ $\displaystyle=R_{\hat{\theta}^{\text{ERM}}}-R_{\theta^{*}}\quad$ (by definition in ( 15 )) $\displaystyle\leq r_{\hat{\theta}^{\text{ERM}}}(\tilde{\mathcal{H}}^{S})-r_{ \theta^{*}}(\tilde{\mathcal{H}}^{S})+\tilde{\mathcal{O}}(\sqrt{\min\{S,T\}/(nT )})\quad$ (by ( 26 ) and ( 27 ) $\displaystyle=r(\texttt{TF}_{\hat{\theta}^{\text{ERM}}})-r(\texttt{TF}_{\theta ^{*}})+\tilde{\mathcal{O}}(\sqrt{1/n}+\sqrt{S/T})\quad$ (by definition in ( 14 )) $\displaystyle\leq\tilde{\mathcal{O}}(\sqrt{\min\{S,T\}/(nT)})\quad$ (by definition of ERM ( 1 )) (28) $$

We now take the expectation over each ${f^{(i)}}$ and ${\sigma^{(i)}}$ to conclude the proof.
∎

###### Remark D.1 (Why truncation) .

Previous analysis (Zhang et al., ) to derive a similar Bayes-optimal argument does not truncate the history and treats the whole history as an inhomogeneous Markov chain. Then they apply the concentration inequalities on Markov chains (for example, Lemma F.2) to control the difference between $R$ and $r$. However, their arguments have two limitations: the first one is that their model is assumed to make decisions based on the full history, which clearly exceeds the Transformer’s model’s capacity. The second limitation is that such a concentration argument for Markov chains often relies on upper bounding the mixing time or lower bounding the spectral gap (for example, Fan et al. ()). But Zhang et al. () do not specify this the mixing time. Furthermore, in each sampled task sequence (assume we know the task $f^{(i)}$), the mixing time of the (untruncated) history $\tilde{H}_{t}$ is infinity: if two sequences start with different initial pairs of $(x_{1},y_{1})$, then they will never become identical no longer what comes consecutively. Thus, their mixing time will be $T$, leading to an $\tilde{O}(1/\sqrt{n})$ generalization, which is suboptimal if $S\ll T$ compared to our result.

###### Proof.

###### Remark D.1 (Why truncation) .

## Appendix E Proofs of Lemmas

In this section, we prove these lemmas based on the choice of the activation function $\texttt{act}=\texttt{softmax}$. Similar results for other options $\texttt{act}=\texttt{ReLU}$ can also be found in many existing literatures (for example, see Bai et al. ()).

### E.1 Boundedness of Transformers

###### Lemma E.1 (Layer-wise boundedness) .

Suppose at the $l$-th layer of the Transformer, we have $\|W_{V}^{m,(l)}\|_{2,2}\leq B_{V}$ for any $m=1,\dots,M$, $\|A_{1}^{(l)}\|_{2,2},\|A_{2}^{(l)}\|_{2,2}\leq B_{A}$. Then for any input $H^{(l-1)}$, we have

$$ $\|H^{(l)}\|_{2,\infty}\leq(1+B_{A}^{2})(1+MB_{V})\|H^{(l-1)}\|_{2,\infty}.$ $$

###### Proof of Lemma E.1 .

For notation simplicity, we denote $\texttt{softmax}((W_{K}^{(l)}H^{(l-1)})^{\top}W_{Q}^{(l)}H^{(l-1)})$ as $\texttt{S}^{m}$. Note that every column of $\texttt{S}^{m}$ is of unit 1-norm. Denote each column of $\texttt{S}^{m}$ by $s_{t}^{m}$.
For any input $H$, we have

$$ $\displaystyle\phantom{=}\|\texttt{MHA}_{W^{(l)}}(H)\|_{2,\infty}$ $\displaystyle\leq\|H\|_{2,\infty}+\sum_{m=1}^{M}\|W_{V}^{m,(l)}H\texttt{S}\|_{ 2,\infty}\quad$ (by triangle inequality) $\displaystyle=\|H\|_{2,\infty}+\sum_{m=1}^{M}\max_{t}\|W_{V}^{m,(l)}Hs_{t}^{m} \|_{2}\quad$ (by definition of $\|\cdot\|_{2,\infty}$ ) $\displaystyle\leq\|H\|_{2,\infty}+\sum_{m=1}^{M}\max_{t}\|W_{V}^{m,(l)}H\|_{2, \infty}\|s_{t}^{m}\|_{1}\quad$ (by Lemma F.5 ) $\displaystyle=\|H\|_{2,\infty}+\sum_{m=1}^{M}\|W_{V}^{m,(l)}H\|_{2,\infty}\quad$ (since $s_{t}^{m}$ is of unit 1-norm) $\displaystyle\leq\|H\|_{2,\infty}+\sum_{m=1}^{M}\|W_{V}^{m,(l)}\|_{2,2}\|H\|_{ 2,\infty}\quad$ (by Lemma F.6 ) $\displaystyle\leq(1+MB_{V})\|H\|_{2,\infty}.\quad$ (by assumption of bounded norm) (29) $$

For any input $H$, we have

$$ $\displaystyle\phantom{=}\|\texttt{MLP}_{A^{(l)}}(H)\|_{2,\infty}$ $\displaystyle\leq\|H\|_{2,\infty}+\|A_{2}^{(l)}\texttt{ReLU}(A_{1}^{(l)}H)\|_{ 2,\infty}\quad$ (by triangle inequality) $\displaystyle\leq\|H\|_{2,\infty}+\|A_{2}^{(l)}\|_{2,2}\|\texttt{ReLU}(A_{1}^{ (l)}H)\|_{2,\infty}\quad$ (by Lemma F.6 ) $\displaystyle=\|H\|_{2,\infty}+\|A_{2}^{(l)}\|_{2,2}\max_{t}\|\texttt{ReLU}(A_ {1}^{(l)}H)_{:,t}\|_{2}\quad$ (by definition of $\|\cdot\|_{2,\infty}$ ) $\displaystyle\leq\|H\|_{2,\infty}+\|A_{2}^{(l)}\|_{2,2}\max_{t}\|(A_{1}^{(l)}H )_{:,t}\|_{2}\quad$ (since $|\texttt{ReLU}(z)|\leq|z|$ for any $z\in\mathbb{R}$ ) $\displaystyle=\|H\|_{2,\infty}+\|A_{2}^{(l)}\|_{2,2}\|A_{1}^{(l)}H\|_{2,\infty}\quad$ (by definition of $\|\cdot\|_{2,\infty}$ ) $\displaystyle\leq\|H\|_{2,\infty}+\|A_{2}^{(l)}\|_{2,2}\|A_{1}^{(l)}\|_{2,2}\| H\|_{2,\infty}\quad$ (by Lemma F.6 ) $\displaystyle\leq(1+B_{A}^{2})\|H\|_{2,\infty}.\quad$ (by assumption of bounded norm) (30) $$

Combining (29) and (30) yields the conclusion.
∎

###### Lemma E.2 (Transformer’s boundedness) .

Suppose $\|W_{V}^{m,(l)}\|_{2,2}\leq B_{V}$ for any $m=1,\dots,M$, $\|A_{1}^{(l)}\|_{2,2},\|A_{2}^{(l)}\|_{2,2}\leq B_{A}$ for any $l\in[L]$. We further assume the projection matrix $P$ is of bounded norm $\|P^{\top}\|_{2,\infty}\leq B_{P}$. Then the Transformer’s outputs satisfy that

$$ $|\hat{y}(H)|\leq C_{1}\|H\|_{2,\infty},\quad\text{and}\quad\exp(-C_{1}\|H\|_{2 ,\infty})\leq\hat{\sigma}(H)\leq 1+C_{1}\|H\|_{2,\infty},$ $$

where $C_{1}\coloneqq B_{P}(1+B_{A}^{2})^{L}(1+MB_{V})^{L}$ is a specified constant.

###### Proof of Lemma E.2 .

By Lemma E.1 and a “peeling” argument, we can easily prove that

$$ $\|H^{(L)}\|_{2,\infty}\leq(1+B_{A}^{2})^{L}(1+MB_{V})^{L}\|H^{(0)}\|_{2,\infty}.$ $$

Thus,

$$ $\|H^{(L)}_{:,t}\|_{2}\leq\|H^{(L)}\|_{2,\infty}\leq(1+B_{A}^{2})^{L}(1+MB_{V}) ^{L}\|H^{(0)}\|_{2,\infty}.$ $$

Denote $P$ by $P=[p_{1},p_{2}]^{\top}$, where $p_{1}$ and $p_{2}$ are vectors of dimension $d$. Then the first output

$$ $\hat{y}=p_{1}^{\top}H^{(L)}_{:,t},$ $$

where we have (by Cauchy-Schwarz inequality),

$$ $|\hat{y}|\leq\|p_{1}\|_{2}\|H^{(L)}_{:,t}\|_{2}\leq B_{P}(1+B_{A}^{2})^{L}(1+ MB_{V})^{L}\|H^{(0)}\|_{2,\infty}.$ $$

The other output $\hat{\sigma}$ can be proved similarly as long as one notices

$$ $\log(1+\exp(-x))\geq\exp(-x),\quad\text{and}\quad\log(1+\exp(x))\leq 1+x,$ $$

for any $x\geq 0$.
∎

###### Lemma E.3 (Boundedness of loss) .

Under Assumption B.5 with $\|\theta\|\leq B_{\text{TF}}$ and Assumption B.6 with $\|H\|_{2,\infty}\leq B_{H}$ almost surely, we have

$$ $|\ell(\texttt{TF}_{\theta}(H_{t}),y_{t})|\leq C_{2}$ $$

almost surely, where $C_{2}\coloneqq(C_{1}+1)^{2}B_{H}^{2}\cdot\exp(2C_{1}B_{H})+\max\{C_{1}B_{H},1+
\log(C_{1}B_{H})\}$ is a specified constant, and $C_{1}$ is a constant defined in Lemma E.2.

###### Proof of Lemma E.3 .

By Lemma E.2, we have

$$ $\displaystyle\frac{(y_{t}-\hat{y}(H_{t}))^{2}}{2\hat{\sigma}^{2}(H_{t})}$ $\displaystyle\leq(y_{t}-\hat{y}_{t}(H_{t}))^{2}\cdot\frac{\exp(2C_{1}B_{H})}{2}$ $\displaystyle\leq(y_{t}^{2}+\hat{y}_{t}(H_{t})^{2})\cdot\exp(2C_{1}B_{H})$ $\displaystyle\leq(C_{1}+1)^{2}B_{H}^{2}\cdot\exp(2C_{1}B_{H}),$ $$

where the second inequality follows from Cauchy’s inequality. Combining with a triangle inequality, we have the desired result.
∎

###### Lemma E.1 (Layer-wise boundedness) .

###### Proof of Lemma E.1 .

###### Lemma E.2 (Transformer’s boundedness) .

###### Proof of Lemma E.2 .

###### Lemma E.3 (Boundedness of loss) .

###### Proof of Lemma E.3 .

### E.2 Lipschitzness of Transformers

###### Lemma E.4 (Lipschitzness of multi-head attention) .

Suppose we define the output’s norm as $\|\cdot\|_{2,\infty}$, the norm of $W$ as

$$ $\|W\|\coloneqq\max\{\|W_{V}^{m}\|_{2,2},\|W_{K}^{m}\|_{2,2},\|W_{Q}^{m}\|_{2,2 }:\ m=1,\dots,M\},$ $$

and the input $H$’s norm as $\|\cdot\|_{2,\infty}$. Suppose at the $l$-th layer of the Transformer, we have $\|W^{m,(l)}\|\leq B_{W}$ for any $m=1,\dots,M$, and $\|H^{(l-1)}\|_{2,\infty}\leq B_{H}^{(l-1)}$ almost surely.
Then $\texttt{MHA}_{W^{(l)}}(H^{(l-1)})$ is $C_{3}^{(l)}$-Lipschitz with respect to $W^{(l)}$ and $C_{4}$-Lipschitz with respect to $H^{(l-1)}$ almost surely. Here $C_{3}^{(l)}\coloneqq 2B_{W}^{2}(B_{H}^{(l-1)})^{3}+(B_{H}^{(l-1)})$ and $C_{4}\coloneqq 1+MB_{W}$ are specified constants.

###### Proof of Lemma E.4 .

We first prove the Lipschitzness result for $W$. To ease the notations, we omit the dependence on $l$ and sometimes abbreviate $W_{K}^{\top}W_{Q}$ as $W_{KQ}$. For any $W$ and $W^{\prime}$, using triangle inequality twice, we have

$$ $\displaystyle\phantom{=}\big{\|}\texttt{MHA}_{W}(H)-\texttt{MHA}_{W^{\prime}}( H)\big{\|}_{2,\infty}$ $\displaystyle\leq\sum_{m=1}^{M}\big{\|}W_{V}^{m}H\texttt{softmax}(H^{\top}W_{ KQ}^{m}H)-W_{V}^{m\prime}H\texttt{softmax}(H^{\top}W_{KQ}^{m\prime}H)\big{\|}_ {2,\infty}$ $\displaystyle\leq\sum_{m=1}^{M}\Big{\|}W_{V}^{m}H\big{(}\texttt{softmax}(H^{ \top}W_{KQ}^{m}H)-\texttt{softmax}(H^{\top}W_{KQ}^{m\prime}H)\big{)}\Big{\|}_{ 2,\infty}$ $\displaystyle\phantom{=}+\sum_{m=1}^{M}\Big{\|}\big{(}W_{V}^{m}-W_{V}^{m\prime }\big{)}H\texttt{softmax}(H^{\top}W_{KQ}^{m\prime}H)\Big{\|}_{2,\infty}.$ (31) $$

We now deal with two terms in (31) separately. Since our conclusion will be made for arbitrary $m\in[M]$, we omit the dependence on $m$ for notation simplicity from now on.

For the first term, we have

$$ $\displaystyle\phantom{=}\Big{\|}W_{V}H\big{(}\texttt{softmax}(H^{\top}W_{KQ}H) -\texttt{softmax}(H^{\top}W_{KQ}^{\prime}H)\big{)}\Big{\|}_{2,\infty}$ $\displaystyle=\max_{t}\big{\|}W_{V}H\big{(}\texttt{softmax}(H^{\top}W_{KQ}h_{t })-\texttt{softmax}(H^{\top}W_{KQ}^{\prime}h_{t})\big{)}\big{\|}_{2}\quad$ (by definition of $\|\cdot\|_{2,\infty}$ ) $\displaystyle\leq\|W_{V}H\|_{2,\infty}\cdot\max_{t}\big{\|}\big{(}\texttt{ softmax}(H^{\top}W_{KQ}h_{t})-\texttt{softmax}(H^{\top}W_{KQ}^{\prime}h_{t}) \big{)}\big{\|}_{1}\quad$ (by Lemma F.5 ) $\displaystyle\leq\|W_{V}\|_{2,2}\|H\|_{2,\infty}\cdot\max_{t}\big{\|}\big{(} \texttt{softmax}(H^{\top}W_{KQ}h_{t})-\texttt{softmax}(H^{\top}W_{KQ}^{\prime} h_{t})\big{)}\big{\|}_{1}\quad$ (by Lemma F.6 ) $\displaystyle\leq 2\|W_{V}\|_{2,2}\|H\|_{2,\infty}\cdot\max_{t}\big{\|}H^{\top }W_{KQ}h_{t}-H^{\top}W_{KQ}^{\prime}h_{t}\big{\|}_{\infty}\quad$ (by Lemma F.7 ) $\displaystyle\leq 2\|W_{V}\|_{2,2}\|H\|_{2,\infty}\cdot\max_{t}\|H\|_{2,\infty }\big{\|}W_{KQ}h_{t}-W_{KQ}^{\prime}h_{t}\big{\|}_{2}\quad$ (by Lemma F.5 ) $\displaystyle\leq 2\|W_{V}\|_{2,2}\|H\|_{2,\infty}^{2}\cdot\max_{t}\big{\|}W_{ KQ}-W_{KQ}^{\prime}\big{\|}_{2,2}\|h_{t}\|_{2}\quad$ (by Lemma F.5 ) $\displaystyle\leq 2\|W_{V}\|_{2,2}\|H\|_{2,\infty}^{2}\cdot\big{\|}W_{KQ}-W_{ KQ}^{\prime}\big{\|}_{2,2}\|H_{t}\|_{2,\infty}\quad$ ((by definition of $\|\cdot\|_{2,\infty}$ ) $\displaystyle\leq 2\|W_{V}\|_{2,2}\|H\|_{2,\infty}^{3}\cdot\Big{(}\big{\|}W_{K }W_{Q}-W_{K}W_{Q}^{\prime}\big{\|}_{2,2}+\big{\|}W_{K}W_{Q}^{\prime}-W_{K}^{ \prime}W_{Q}^{\prime}\big{\|}_{2,2}\Big{)}\quad$ ((by triangular inequality) $\displaystyle\leq 2\|W_{V}\|_{2,2}\|H\|_{2,\infty}^{3}\cdot\big{(}\|W_{K}\|_{2 ,2}\|W_{Q}-W_{Q}^{\prime}\|_{2,2}+\|W_{K}-W_{K}^{\prime}\|_{2,2}\|W_{Q}^{ \prime}\|_{2,2}\big{)}.\quad$ ((by sub-multiplicativity of matrix norm) $\displaystyle\leq 2B_{W}^{2}(B_{H}^{(l-1)})^{3}\cdot\big{(}\|W_{Q}-W_{Q}^{ \prime}\|_{2,2}+\|W_{K}-W_{K}^{\prime}\|_{2,2}\big{)}.\quad$ ((by bounded norm assumption) (32) $$

For notation simplicity, we denote $\texttt{softmax}(H^{\top}W_{KQ}^{m\prime}H)$ by S. Note that every column of S is of unit 1-norm. Denote each column of S by $s_{t}$. For the second term, we have

$$ $\displaystyle\phantom{=}\Big{\|}\big{(}W_{V}-W_{V}^{\prime}\big{)}H\texttt{ softmax}(H^{\top}W_{KQ}^{m\prime}H)\Big{\|}_{2,\infty}$ $\displaystyle=\|(W_{V}-W_{V}^{\prime})H\texttt{S}\|_{2,\infty}\quad$ (by notation substitution) $\displaystyle=\max_{t}\|(W_{V}-W_{V}^{\prime})Hs_{t}\|_{2}\quad$ (by definition of $\|\cdot\|_{2,\infty}$ ) $\displaystyle\leq\max_{t}\|(W_{V}-W_{V}^{\prime})H\|_{2,\infty}\|s_{t}\|_{1}\quad$ (by Lemma F.5 ) $\displaystyle=\|(W_{V}-W_{V}^{\prime})H\|_{2,\infty}\quad$ (since $s_{t}$ is of unit 1-norm) $\displaystyle\leq\|W_{V}-W_{V}^{\prime}\|_{2,2}\|H\|_{2,\infty}\quad$ (by Lemma F.6 ) $\displaystyle\leq B_{H}^{(l-1)}\|W_{V}-W_{V}^{\prime}\|_{2,2}.\quad$ (by assumption of bounded norm) (33) $$

Substituting (32) and (33) into (31), we can conclude that $\texttt{MHA}_{W^{(l)}}(H^{(l-1)})$ is $C_{3}^{(l)}$-Lipschitz with respect to $W^{(l)}$ for $C_{3}^{(l)}\coloneqq 2B_{W}^{2}(B_{H}^{(l-1)})^{3}+(B_{H}^{(l-1)})$.

As for the second Lipschitzness conclusion (the one w.r.t. $H$), it is straightforward if one replaces $H$ with $H-H^{\prime}$ in the proof of (29).
∎

###### Lemma E.5 (Lipschitzness of multi-layer perceptron) .

Suppose we define the output’s norm as $\|\cdot\|_{2,\infty}$, the norm of $W$ as

$$ $\|A\|\coloneqq\max\{\|A_{1}\|_{2,2},\|A_{2}\|_{2,2}\},$ $$

and the input $H$’s norm as $\|\cdot\|_{2,\infty}$. Suppose at the $l$-th layer of the Transformer, we have $\|A^{(l)}\|\leq B_{A}$ and $\|H\|_{2,\infty}\leq B_{H}^{\prime(l-1)}$ almost surely. Then $\texttt{MLP}_{A^{(l)}}(H)$ is $C_{5}^{(l)}$-Lipschitz with respect to $A^{(l)}$ and $C_{6}$-Lipschitz with respect to $H$ almost surely. Here $C_{5}^{(l)}\coloneqq B_{A}B_{H}^{\prime(l-1)}$ and $C_{6}\coloneqq 1+B_{A}^{2}$ are specified constants.

###### Proof of Lemma E.5 .

We first prove the Lipschitzness result for $A$. To ease the notations, we omit the dependence on $l$. For any $A$ and $A^{\prime}$, we have

$$ $\displaystyle\phantom{=}\big{\|}\texttt{MLP}_{A}(H)-\texttt{MLP}_{A^{\prime}}( H)\|_{2,\infty}$ $\displaystyle\leq\big{\|}(A_{2}-A_{2}^{\prime})\texttt{ReLU}(A_{1}H)\big{\|}_{ 2,\infty}+\big{\|}A_{2}^{\prime}(\texttt{ReLU}(A_{1}H)-\texttt{ReLU}(A_{1}^{ \prime}))\big{\|}_{2,\infty}\quad$ (by triangle inequality) $\displaystyle\leq\|A_{2}-A_{2}^{\prime}\|_{2,2}\big{\|}\texttt{ReLU}(A_{1}H) \big{\|}_{2,\infty}$ $\displaystyle\phantom{=}+\|A_{2}^{\prime}\|_{2,2}\big{\|}\texttt{ReLU}(A_{1}H) -\texttt{ReLU}(A_{1}^{\prime}H)\big{\|}_{2,\infty}\quad$ (by Lemma F.6 ) $\displaystyle=\|A_{2}-A_{2}^{\prime}\|_{2,2}\max_{t}\|\texttt{ReLU}(A_{1}H)_{: ,t}\|_{2}$ $\displaystyle\phantom{=}+\|A_{2}^{\prime}\|_{2,2}\max_{t}\|\texttt{ReLU}(A_{1} H)_{:,t}-\texttt{ReLU}(A_{1}^{\prime}H)_{:,t}\|_{2}\quad$ (by definition of $\|\cdot\|_{2,\infty}$ ) $\displaystyle\leq\|A_{2}-A_{2}^{\prime}\|_{2,2}\max_{t}\|(A_{1}H)_{:,t}\|_{2}+ \|A_{2}^{\prime}\|_{2,2}\max_{t}\|(A_{1}H)_{:,t}-(A_{1}^{\prime}H)_{:,t}\|_{2}$ (since $|\texttt{ReLU}(z_{1})-\texttt{ReLU}(z_{2})|\leq|z_{1}-z_{2}|$ for any $z_{1},z_{2}\in\mathbb{R}$ ) $\displaystyle=\|A_{2}-A_{2}^{\prime}\|_{2,2}\|A_{1}H\|_{2,\infty}+\|A_{2}^{ \prime}\|_{2,2}\|A_{1}H-A_{1}^{\prime}H\|_{2,\infty}\quad$ (by definition of $\|\cdot\|_{2,\infty}$ ) $\displaystyle\leq\|A_{2}-A_{2}^{\prime}\|_{2,2}\|A_{1}\|_{2,2}\|H\|_{2,\infty} +\|A_{2}^{\prime}\|_{2,2}\|A_{1}-A_{1}^{\prime}\|_{2,2}\|H\|_{2,\infty}\quad$ (by Lemma F.6 ) $\displaystyle\leq B_{A}B_{H}^{\prime(l-1)}\big{(}\|A_{1}-A_{1}^{\prime}\|_{2,2 }+\|A_{2}-A_{2}^{\prime}\|_{2,2}\big{)}\quad$ (by assumption of bounded norm) (34) $$

As for the second Lipschitzness conclusion (the one w.r.t. $H$), it is straightforward if one replaces $H$ with $H-H^{\prime}$ in the proof of (30).
∎

###### Lemma E.6 (Lipshitzness of Transformer) .

Suppose we define each output’s norm as $|\cdot|$ for $\hat{y}$ and $\hat{\sigma}$, the norm of $\theta$ as

$$ $\|\theta\|\coloneqq\max\{\|W\|,\|A\|,\|P\|\},$ $$

where $\|W\|$ is as defined in Lemma E.4, $\|A\|$ is as defined in Lemma E.5, and $\|P\|\coloneqq\|P^{\top}\|_{2,\infty}$, and the input $H$’s norm as $\|\cdot\|_{2,\infty}$. Suppose we have $\|\theta\|\leq B_{\text{TF}}$, and $\|H\|_{2,\infty}\leq B_{H}$ almost surely. Then $\hat{y}_{\theta}(H)$ is $C_{7}$-Lipschitz with respect to $\theta$, and $\hat{\sigma}_{\theta}(H)$ is $C_{8}$-Lipschitz with respect to $\theta$.

###### Proof of Lemma E.6 .

First we quantify the constants $B_{H}^{(l-1)}$ in Lemma E.4 and the constants $B_{H}^{{}^{\prime}(l-1)}$ in Lemma E.5 via Lemma E.1. As is shown in the proof of Lemma E.1, we can define

$$ $B_{H}^{(l-1)}\coloneqq(1+MB_{\text{TF}})^{l-1}(1+B_{\text{TF}}^{2})^{l-1}, \quad l=1,\dots,L,$ $$

and

$$ $B_{H}^{\prime(l-1)}\coloneqq(1+MB_{\text{TF}})^{l}(1+B_{\text{TF}}^{2})^{l-1}, \quad l=1,\dots,L,$ $$

such that all requirements in Lemma E.4 and Lemma E.5 are met almost surely. Thus, we bound the gap between $H^{(l)}$ (the output of $\texttt{TF}_{\theta}$ after $l$ layers) and $H^{\prime(l)}$ (the output of $\texttt{TF}_{\theta^{\prime}}$ after $l$ layers) by induction. We claim that if $H^{(0)}=H^{\prime(0)}$, then there exists a constant $C_{9}^{(l)}$ for any $l=1,\dots,L$ that do not depend on $\theta$ or $H$, such that

$$ $\|H^{(l)}-H^{\prime(l)}\|_{2,\infty}\leq C_{9}^{l}\|\theta-\theta^{\prime}\|.$ $$

We prove it by induction. For $l=1$, the case can be verified by calculation: by Lemma E.4,

$$ $\|\texttt{MHA}_{W^{(1)}}(H^{(0)})-\texttt{MHA}_{W^{\prime(1)}}(H^{(0)})\|_{2, \infty}\leq C_{3}^{(1)}\|\theta-\theta^{\prime}\|.$ $$

Similarly, by Lemma E.5,

$$ $\displaystyle\|H^{(1)}-H^{\prime(1)}\|_{2,\infty}$ $\displaystyle=\|\texttt{MLP}_{A^{(1)}}(\texttt{MHA}_{W^{(1)}}(H^{(0)}))- \texttt{MLP}_{A^{\prime(1)}}(\texttt{MHA}_{W^{\prime(1)}}(H^{(0)}))\|_{2,\infty}$ $\displaystyle\leq\|\texttt{MLP}_{A^{(1)}}(\texttt{MHA}_{W^{(1)}}(H^{(0)}))- \texttt{MLP}_{A^{(1)}}(\texttt{MHA}_{W^{\prime(1)}}(H^{(0)}))\|_{2,\infty}$ $\displaystyle\phantom{=}+\|\texttt{MLP}_{A^{(1)}}(\texttt{MHA}_{W^{\prime(1)}} (H^{(0)}))-\texttt{MLP}_{A^{\prime(1)}}(\texttt{MHA}_{W^{\prime(1)}}(H^{(0)})) \|_{2,\infty}$ $\displaystyle\leq C_{6}\|\texttt{MHA}_{W^{(1)}}(H^{(0)})-\texttt{MHA}_{W^{ \prime(1)}}(H^{(0)})\|_{2,\infty}+C_{5}^{(1)}\|\theta-\theta^{\prime}\|$ $\displaystyle\leq(C_{6}C_{3}^{(1)}+C_{5}^{(1)})\|\theta-\theta^{\prime}\|,$ (35) $$

where we define $C_{9}^{1}$ as $C_{9}^{1}\coloneqq C_{6}C_{3}^{(1)}+C_{5}^{(1)}$. Suppose our conclusion holds for any $l\leq l_{0}-1$. Then for $l=l_{0}$, we have

$$ $\displaystyle\phantom{=}\|\texttt{MHA}_{W^{(l_{0})}}(H^{(l_{0}-1)})-\texttt{ MHA}_{W^{\prime(l_{0})}}(H^{\prime(l_{0}-1)})\|_{2,\infty}$ $\displaystyle\leq\|\texttt{MHA}_{W^{(l_{0})}}(H^{(l_{0}-1)})-\texttt{MHA}_{W^{ (l_{0})}}(H^{\prime(l_{0}-1)})\|_{2,\infty}+\|\texttt{MHA}_{W^{(l_{0})}}(H^{ \prime(l_{0}-1)})-\texttt{MHA}_{W^{\prime(l_{0})}}(H^{\prime(l_{0}-1)})\|_{2,\infty}$ $\displaystyle\leq C_{4}\|H^{(l_{0}-1)}-H^{\prime(l_{0}-1)}\|_{2,\infty}+C_{3}^ {(l_{0})}\|\theta-\theta^{\prime}\|$ $\displaystyle\leq(C_{4}C_{9}^{(l_{0}-1)}+C_{3}^{(l_{0}-1)})\|\theta-\theta^{ \prime}\|,$ $$

by applying Lemma E.4. We can again compute the difference between $H^{(l_{0})}$ and $H^{\prime(l_{0})}$ similar to what we do in (35) as

$$ $\|H^{(l_{0})}-H^{\prime(l_{0})}\|_{2,\infty}\leq\big{(}C_{6}(C_{4}C_{9}^{(l_{0 }-1)}+C_{3}^{(l_{0}-1)})+C_{5}^{(l_{0})}\big{)}\|\theta-\theta^{\prime}\|.$ $$

Hence the induction holds if we define $C_{9}^{(l_{0})}\coloneqq C_{6}(C_{4}C_{9}^{(l_{0}-1)}+C_{3}^{(l_{0}-1)})+C_{5}
^{(l_{0})}$. Now we have proved

$$ $\|H^{(L)}-H^{\prime(L)}\|_{2,\infty}\leq C_{9}^{(L)}\|\theta-\theta^{\prime}\|.$ $$

We shall see from Cauchy-Schwarz inequality that

$$ $\displaystyle|\hat{y}-\hat{y}^{\prime}|$ $\displaystyle\leq\|p_{1}-p_{1}^{\prime}\|_{2}\|H^{(L)}\|_{2,\infty}-\|p_{1}^{ \prime}\|_{2}\|H^{(L)}-H^{\prime(L)}\|_{2,\infty}$ $\displaystyle\leq\|\theta-\theta^{\prime}\|(1+MB_{\text{TF}})^{L}(1+B_{\text{ TF}}^{2})^{L}+B_{\text{TF}}C_{9}^{(L)}\|\theta-\theta^{\prime}\|$ $\displaystyle=\big{(}(1+MB_{\text{TF}})^{L}(1+B_{\text{TF}}^{2})^{L}+B_{\text{ TF}}C_{9}^{(L)}\big{)}\|\theta-\theta^{\prime}\|,$ $$

where the second inequality follows from the proof of Lemma E.6. We can now define

$$ $C_{7}\coloneqq(1+MB_{\text{TF}})^{L}(1+B_{\text{TF}}^{2})^{L}+B_{\text{TF}}C_{ 9}^{(L)},$ $$

and conclude the proof for $\hat{y}$. As for $\hat{\theta}$, we can see from the fact $\log(1+\exp(\cdot))$ is 1-Lipschitz that the Lipschitzness also holds for $C_{8}\coloneqq C_{7}$.
∎

###### Lemma E.7 (Lipschitzness of loss) .

Suppose we have $\|\theta\|\leq B_{\text{TF}}$ and $\|H\|_{2,\infty}\leq B_{H}$ almost surely, where the norm of $\theta$ is the same as defined in Lemma E.6. Then $\ell(\texttt{TF}_{\theta}(H),y)$ is $C_{10}$-Lipschitz with respect to $\theta$ almost surely.

###### Proof of Lemma E.7 .

Based on the Lipschitzness of the Transformer w.r.t. $\theta$ (Lemma E.6), we only need to prove that both partial derivatives $\frac{\partial\ell}{\partial\hat{y}}$ and $\frac{\partial\ell}{\partial\hat{\sigma}}$ are bounded. For the first partial derivative, we have

$$ $\displaystyle\left|\frac{\partial\ell}{\partial\hat{y}}\right|$ $\displaystyle=\big{|}(y-\hat{y})\big{|}\cdot\frac{1}{\hat{\sigma}^{2}}$ $\displaystyle\leq(1+C_{1})B_{H}\exp(2C_{1}B_{H}).\quad\text{(by Lemma \ref{ lemma:transformer_boundedness})}$ (36) $$

For the second partial derivative, we have

$$ $\displaystyle\left|\frac{\partial\ell}{\partial\hat{\sigma}}\right|$ $\displaystyle=\frac{\big{|}-(y-\hat{y})^{2}+\sigma^{2}\big{|}}{\hat{\sigma}^{3}}$ $\displaystyle\leq\big{(}(1+C_{1})^{2}B_{H}^{2}+(1+C_{1}B_{H})^{2}\big{)}\cdot \exp(3C_{1}B_{H}).\quad\text{(by Lemma \ref{lemma:transformer_boundedness})}$ (37) $$

Combining inequalities (36) and (37) with the Lipschitzness of $\hat{y}$ and $\hat{\sigma}$ w.r.t. $\theta$, we conclude the result with

$$ $C_{10}\coloneqq(1+C_{1})B_{H}\exp(2C_{1}B_{H})C_{7}+\big{(}(1+C_{1})^{2}B_{H}^ {2}+(1+C_{1}B_{H})^{2}\big{)}\exp(3C_{1}B_{H})C_{8},$ $$

where $C_{7}$ and $C_{8}$ are constants that appear in Lemma E.6.
∎

###### Lemma E.4 (Lipschitzness of multi-head attention) .

###### Proof of Lemma E.4 .

###### Lemma E.5 (Lipschitzness of multi-layer perceptron) .

###### Proof of Lemma E.5 .

###### Lemma E.6 (Lipshitzness of Transformer) .

###### Proof of Lemma E.6 .

###### Lemma E.7 (Lipschitzness of loss) .

###### Proof of Lemma E.7 .

### E.3 Constructing Distributions over Parameter Space

In this section, we formally define two distributions over the parameter space $\Theta$. The first distribution $\rho_{\hat{\theta}}$ may depend on the empirical distribution, while the second distribution $\pi_{\theta}$ should be independent of the training dataset. We control the Kullback-Leibler divergence between $\rho_{\hat{\theta}}$ and $\pi_{\theta}$ in Lemma E.11. For notation simplicity, we may use some notations of different meanings from the main text.

For any dimension $d$, we denote the Lebesgue measure over $\mathbb{R}^{d}$ by $\lambda_{d}(\cdot)$. Then we have the following lemma.

###### Lemma E.8 (Upper bound for p.d.f.) .

Suppose $\rho$ is the uniform distribution over $\mathcal{B}(x_{0},3r)\cap\mathcal{B}(0,R)$ for some $x_{0}\in\mathcal{B}(0,R)\subset\mathbb{R}^{d}$, where the Lebesgue measure is defined as $\lambda_{d}(\cdot)$, and $R>3r$. Then the p.d.f. $p_{\rho}(\cdot)$ exists and

$$ $p_{\rho}(x)\leq\frac{1}{\lambda_{d}\big{(}\mathcal{B}(0,r)\big{)}}.$ $$

###### Proof of Lemma E.8 .

Denote the set to be $S\coloneqq\mathcal{B}(x_{0},3r)\cap\mathcal{B}(0,R)$.
Since $\rho$ is the uniform distribution, we just need to prove that

$$ $\lambda_{d}(S)\geq\lambda_{d}\big{(}\mathcal{B}(0,r)\big{)}.$ $$

This is true because there exists some $x^{\prime}\in\mathbb{R}^{d}$ s.t. $\mathcal{B}(x^{\prime},r)\subset S$. In fact, we can construct the small ball as

$$ $\mathcal{B}\Big{(}x_{0}-\frac{x_{0}}{\|x_{0}\|}\cdot 1.5r,r\Big{)}\subset S.$ $$

∎

###### Lemma E.9 (Upper bound for KL divergence) .

Suppose the probability space is defined on $\mathcal{B}(0,R)$. Suppose $\rho$ is the uniform distribution over $\mathcal{B}(x_{0},3r)\cap\mathcal{B}(0,R)$ for some $x_{0}\in\mathcal{B}(0,R)\subset\mathbb{R}^{d}$, where the Lebesgue measure is defined as $\lambda_{d}(\cdot)$, and $R>3r$. Suppose $\pi$ is the uniform distribution over $\mathcal{B}(0,R)$. Then

$$ $D_{\mathrm{kl}}(\rho\|\pi)\leq\mathcal{O}(C_{d}\cdot\log(R/r)),$ $$

where $C_{d}\coloneqq\log(\lambda_{d}(\mathcal{B}(0,1)))$ is some constant related to $d$.

###### Proof of Lemma E.9 .

Since $\rho\ll\pi$, we can define the Radon-Nikodym derivative as $\frac{\mathrm{d}\rho}{\mathrm{d}\pi}$. By Lemma E.8, we can upper bound the RN derivative by

$$ $\frac{\mathrm{d}\rho}{\mathrm{d}\pi}(x)=\frac{1/\lambda_{d}(\mathcal{B}(x_{0}, 3r)\cap\mathcal{B}(0,R))}{1/\lambda_{d}{\mathcal{B}(0,R)}}\leq\mathcal{O}(C_{d }\cdot\log(R/r)).$ $$

Hence,

$$ $\displaystyle D_{\mathrm{kl}}(\rho\|\pi)$ $\displaystyle=\int_{x\in\mathcal{B}(0,R)}\log\Big{(}\frac{\mathrm{d}\rho}{ \mathrm{d}\pi}(x)\Big{)}\mathrm{d}\rho(x)$ $\displaystyle\leq\int_{x\in\mathcal{B}(0,R)}\mathcal{O}(C_{d}\cdot\log(R/r)) \mathrm{d}\rho(x)$ $\displaystyle=\mathcal{O}(C_{d}\cdot\log(R/r)).$ $$

∎

###### Remark E.10 .

Note that $C_{d}=\frac{\pi^{\frac{n}{2}}}{\Gamma(\frac{n}{2}+1)}$ is uniformly upper bounded. Here $\pi$ denotes the ratio of a circle’s circumference to its diameter, and $\Gamma$ is the Gamma-function.

###### Lemma E.11 (Upper bound for $D_{\mathrm{kl}}(\rho_{\hat{\theta}}\|\pi_{\theta})$ ) .

Suppose we are considering probability measures over the space specified by Assumption B.6 (that is, $\Theta=\mathcal{B}(0,B_{\text{TF}})$). For each layer $l$ and each $m$, suppose we define the norm over each $W_{Q},W_{K}\in\mathbb{R}^{d_{m}\times d}$, $W_{V}\in\mathbb{R}^{d\times d}$, $A_{1}\in\mathbb{R}^{d_{h}\times d}$, $A_{2}\in\mathbb{R}^{d\times d_{h}}$ to be the Frobenius norm (that is, $\|\cdot\|_{2,2}$). Suppose $P=[p_{1},p_{2}]^{\top}$, and we define the norm over $p_{1},p_{2}\in\mathcal{R}^{d}$ to be the Euclidean norm. For each layer $l$ and each $m$, suppose we have the probability measures $\rho_{\hat{W}_{Q}}$, $\rho_{\hat{W}_{K}}$, $\rho_{\hat{W}_{V}}$, $\rho_{\hat{A}_{1}}$, $\rho_{\hat{A}_{2}}$ as the uniform distribution over $\mathcal{B}(0,1/(nT))\cap\mathcal{B}(0,B_{\text{TF}}))$, and the probability measures $\pi_{W_{Q}}$, $\pi_{W_{K}}$, $\pi_{W_{V}}$, $\pi_{A_{1}}$, $\pi_{A_{2}}$ as the uniform distribution over $\mathcal{B}(0,B_{\text{TF}}))$. Suppose we have the probability measures $\rho_{\hat{p}_{1}}$, $\rho_{\hat{p}_{2}}$ as the uniform distribution over $\mathcal{B}(0,1/(nT))\cap\mathcal{B}(0,B_{\text{TF}}))$, and the probability measures $\pi_{p_{1}}$, $\pi_{p_{2}}$ as the uniform distribution over $\mathcal{B}(0,B_{\text{TF}}))$. Suppose we define

$$ $\displaystyle\rho_{\hat{\theta}}$ $\displaystyle\coloneqq\Big{(}\bigotimes_{m,l}\rho_{\hat{W}_{Q}^{m,(l)}}\Big{)} \otimes\Big{(}\bigotimes_{m,l}\rho_{\hat{W}_{K}^{m,(l)}}\Big{)}\otimes\Big{(} \bigotimes_{m,l}\rho_{\hat{W}_{V}^{m,(l)}}\Big{)}$ $\displaystyle\phantom{=}\otimes\Big{(}\bigotimes_{l}\rho_{\hat{A}_{1}^{(l)}} \Big{)}\otimes\Big{(}\bigotimes_{l}\rho_{\hat{A}_{2}^{(l)}}\Big{)}$ $\displaystyle\phantom{=}\otimes\rho_{\hat{p}_{1}}\otimes\rho_{\hat{p}_{2}},$ (38) $$

and

$$ $\displaystyle\pi_{\theta}$ $\displaystyle\coloneqq\Big{(}\bigotimes_{m,l}\pi_{W_{Q}^{m,(l)}}\Big{)}\otimes \Big{(}\bigotimes_{m,l}\pi_{W_{K}^{m,(l)}}\Big{)}\otimes\Big{(}\bigotimes_{m,l }\pi_{W_{V}^{m,(l)}}\Big{)}$ $\displaystyle\phantom{=}\otimes\Big{(}\bigotimes_{l}\pi_{A_{1}^{(l)}}\Big{)} \otimes\Big{(}\bigotimes_{l}\pi_{A_{2}^{(l)}}\Big{)}$ $\displaystyle\phantom{=}\otimes\pi_{p_{1}}\otimes\pi_{p_{2}},$ (39) $$

where $\otimes$ represents the product of measures. Then we have

$$ $D_{\mathrm{kl}}(\rho_{\hat{\theta}}\|\pi_{\theta})\leq\mathcal{O}\big{(}C_{11} \log(nTB_{\text{TF}})\big{)},$ $$

where $C_{11}$ is some specified constant that depends polynomially on $L,M,d,d_{m},d_{h}$.

###### Proof of Lemma E.11 .

By setting $r=\frac{1}{3nT}$ for Lemma E.9 and $R=B_{\text{TF}}$, we have for each $m=1,\dots,M$ and $l=1,\dots,L$,

$$ $\displaystyle D_{\mathrm{kl}}(\rho_{\hat{W}_{Q}^{m,(l)}}\|\pi_{W_{Q}^{m,(l)}})$ $\displaystyle\leq\mathcal{O}\big{(}C_{dd_{m}}\log(nTB_{\text{TF}})\big{)},$ $\displaystyle D_{\mathrm{kl}}(\rho_{\hat{W}_{K}^{m,(l)}}\|\pi_{W_{K}^{m,(l)}})$ $\displaystyle\leq\mathcal{O}\big{(}C_{dd_{m}}\log(nTB_{\text{TF}})\big{)},$ $\displaystyle D_{\mathrm{kl}}(\rho_{\hat{W}_{V}^{m,(l)}}\|\pi_{W_{V}^{m,(l)}})$ $\displaystyle\leq\mathcal{O}\big{(}C_{d^{2}}\log(nTB_{\text{TF}})\big{)},$ $\displaystyle D_{\mathrm{kl}}(\rho_{\hat{A}_{1}^{(l)}}\|\pi_{A_{1}^{(l)}})$ $\displaystyle\leq\mathcal{O}\big{(}C_{dd_{h}}\log(nTB_{\text{TF}})\big{)},$ $\displaystyle D_{\mathrm{kl}}(\rho_{\hat{A}_{2}^{(l)}}\|\pi_{A_{2}^{(l)}})$ $\displaystyle\leq\mathcal{O}\big{(}C_{dd_{h}}\log(nTB_{\text{TF}})\big{)},$ $\displaystyle D_{\mathrm{kl}}(\rho_{\hat{p}_{1}}\|\pi_{p_{1}})$ $\displaystyle\leq\mathcal{O}\big{(}C_{d}\log(nTB_{\text{TF}})\big{)},$ $\displaystyle D_{\mathrm{kl}}(\rho_{\hat{p}_{2}}\|\pi_{p_{2}})$ $\displaystyle\leq\mathcal{O}\big{(}C_{d}\log(nTB_{\text{TF}})\big{)}.$ $$

By Lemma F.8, we can sum up the above inequalities and get the final result.
∎

###### Lemma E.12 (Bounded difference) .

For any $\hat{\theta}\in\Theta$, suppose we construct the distribution $\rho_{\hat{\theta}}$ as in (38). Then for any $\theta\in\mathrm{supp}(\rho_{\hat{\theta}})$, under Assumption B.6 and Assumption B.5, we have

$$ $\Big{|}\ell(\texttt{TF}_{\theta}(H),y)-\ell(\texttt{TF}_{\hat{\theta}}(H),y) \Big{|}\leq\mathcal{O}\big{(}C_{10}/(nT)\big{)},$ $$

almost surely.
Here $C_{10}$ is the same as defined in Lemma E.7.

###### Proof of Lemma E.12 .

By construction shown in (38), we can see that for any $\theta\in\mathrm{supp}(\rho_{\hat{\theta}})$,

$$ $\|\theta-\hat{\theta}\|\leq 1/(nT).$ $$

Then from the Lipschitzness of the loss function w.r.t. $\theta$ (Lemma E.7), we conclude the proof.
∎

###### Lemma E.8 (Upper bound for p.d.f.) .

###### Proof of Lemma E.8 .

###### Lemma E.9 (Upper bound for KL divergence) .

###### Proof of Lemma E.9 .

###### Remark E.10 .

###### Lemma E.11 (Upper bound for D kl ⁢ ( ρ θ ^ ∥ π θ ) subscript 𝐷 kl conditional subscript 𝜌 ^ 𝜃 subscript 𝜋 𝜃 D_{\mathrm{kl}}(\rho_{\hat{\theta}}\|\pi_{\theta}) italic_D start_POSTSUBSCRIPT roman_kl end_POSTSUBSCRIPT ( italic_ρ start_POSTSUBSCRIPT over^ start_ARG italic_θ end_ARG end_POSTSUBSCRIPT ∥ italic_π start_POSTSUBSCRIPT italic_θ end_POSTSUBSCRIPT ) ) .

###### Proof of Lemma E.11 .

###### Lemma E.12 (Bounded difference) .

###### Proof of Lemma E.12 .

### E.4 Markov Chain’s Property

###### Lemma E.13 ( $\tilde{\mathcal{H}}^{S}$ is a Markov chain (conditioned on knowing $f$ and $\sigma$ )) .

Suppose we have $\tilde{\mathcal{H}}^{S}$ defined as (13). Then $\tilde{\mathcal{H}}^{S}$ is a Markov chain conditioned on knowing each $f^{(i)}$ and $\sigma^{(i)}$ for each $i=1,\dots,n$.

###### Proof of Lemma E.13 .

By definition, the state of $\tilde{\mathcal{H}}^{S}$ will restart and does not depend on all previous histories once $\tilde{H}_{k}^{S}$’s index $k$ reaches the point of $k=iT+1$. Therefore, we only need to verify that inside each task’s sequence, the state $\tilde{H}_{k}^{S}$ is also Markovian.

Suppose $k=iT+t$ for some $i$, and we considering $k=iT+1,\dots,iT+T$ for each $t=1,\dots,T$.
We write $\tilde{H}_{k}^{S}$ and $(x_{\max\{1,t-S\}},y_{\max\{1,t-S\}},\dots,x_{t},y_{t})$ interchangeably for notation simplicity.

Each pair of $(x_{t},y_{t})$ is now independent conditioned on knowing the underlying $f^{(i)}$ and $\sigma^{(i)}$. We omit the conditional dependencies on $f^{(i)}$ and $\sigma^{(i)}$ for notation simplicity.
The p.d.f. of $\tilde{H}_{k}^{S}$ conditioned on observing $\{\tilde{H}_{\tau}^{S}\}_{\tau=iT+1}^{iT+t}$ and knowing $f^{(i)}$ and $\sigma^{(i)}$ is

$$ $\displaystyle\phantom{=}p(x_{\max\{1,t-S\}},y_{\max\{1,t-S\}},\dots,x_{t},y_{t }|\{\tilde{H}_{\tau}^{S}\}_{\tau=iT+1}^{iT+t}=\{\tilde{H}_{\tau}^{S\prime}\}_{ \tau=iT+1}^{iT+t},f=f^{(i)},\sigma=\sigma^{(i)})$ $\displaystyle=\mathbbm{1}\{x_{\max\{1,t-S\}}=x_{\max\{1,t-S\}}^{\prime},\dots, y_{t-1}=y_{t-1}^{\prime}\}\cdot p(x_{t},y_{t}|f=f^{(i)},\sigma=\sigma^{(i)})$ (by conditional independence of each pair of $(x_{\tau},y_{\tau})$ ) $\displaystyle=p(x_{\max\{1,t-S\}},y_{\max\{1,t-S\}},\dots,x_{t},y_{t}|\tilde{H }_{t-1}^{S}=\tilde{H}_{t-1}^{S\prime},f=f^{(i)},\sigma=\sigma^{(i)})$ $$

Thus the Markovian property holds.
∎

We present the definition of mixing time as used in Paulin ().

###### Definition E.14 (Mixing time for inhomogeneous Markov chains) .

Let $X_{1},\dots,X_{N}$ be a Markov chain with Polish state space $\Omega_{1}\times\cdots\times\Omega_{N}$ (that is, $X_{i}\in\Omega_{i}$). Let $\mathcal{L}(X_{i+t}|X_{i}=x)$ be the conditional distribution of $X_{i+t}$ given $X_{i}=x$. Let us denote the minimal $t$ such that $\mathcal{L}(X_{i+t}|X_{i}=x)$
and $\mathcal{L}(X_{i+t}|X_{i}=y)$ are less than $\epsilon$ away in total variational distance for every
$1\leq i\leq N-t$ and $x,y\in\Omega_{i}$ by $\tau(\epsilon)$, that is, for $0<\epsilon<1$, let

$$ $\displaystyle\bar{d}(t)$ $\displaystyle\coloneqq\max_{1\leq i\leq N-t}\sup_{x,y\in\Omega_{i}}\mathrm{TV} (\mathcal{L}(X_{i+t}|X_{i}=x),\mathcal{L}(X_{i+t}|X_{i}=y)),$ $\displaystyle\tau(\epsilon)$ $\displaystyle\coloneqq\min\{t\in\mathbb{N}\ :\ \bar{d}(t)\leq\epsilon\}.$ $$

We now upper bound the mixing time of $(H_{t}^{S},y_{t})$.

###### Lemma E.15 (Mixing time for truncated history) .

Suppose we are considering the conditional distribution on knowing each $f^{(i)}$ and $\sigma^{(i)}$. Then for the Markov chain $\tilde{\mathcal{H}}_{k}^{S}$, we have

$$ $\tau(\epsilon)\leq\min\{S,T\},$ $$

for any $\epsilon\in[0,1)$.

###### Proof of Lemma E.15 .

We first consider the case when $S\leq T$.
The mixing property inside each sequence $\tilde{H}_{k}^{S}$ for $k=iT+1,\dots,iT+T$. Since each $(x,y)$ is i.i.d. distributed conditioned on knowing $f^{(i)}$ and $\sigma^{(i)}$, the conditional distribution of the consecutive sequence $(x_{t+1},y_{t+1}),\dots,(x_{T},y_{T})$ is never affected by previous $t$ pairs $(x_{1},y_{1}),\dots,(x_{t},y_{t})$ for any $1\leq t\leq T$. We consider the conditional distribution on knowing $f^{(i)}$ and $\sigma^{(i)}$ from now on and omit the dependencies for notation simplicity.

For any $1\leq t\leq T-S$, for any two points $\tilde{H}_{iT+t}^{S\prime}\neq\tilde{H}_{iT+t}^{S\prime\prime}$, the distribution of $\tilde{H}_{iT+t+S}^{S}$ is independent of previous $t$ pairs of observed samples. In other words,

$$ $\mathcal{L}(\tilde{H}_{iT+t+t^{\prime}}^{S}|\tilde{H}_{iT+t}^{S}=\tilde{H}_{iT +t}^{S\prime})=\mathcal{L}(\tilde{H}_{iT+t+t^{\prime}}^{S}|\tilde{H}_{iT+t}^{S }=\tilde{H}_{iT+t}^{S\prime\prime}),$ $$

for any $t^{\prime}\geq S$.
Hence,

$$ $\bar{d}(t)=0,\quad\text{for any }t\geq S.$ $$

We have

$$ $\tau(\epsilon)\leq S,\quad\text{for any }\epsilon\in[0,1).$ $$

When $S>T$, note that the flattened (truncated) history $\tilde{H}_{k}^{S}$ restarts every time it meets the end of a sequence generated by some $f^{(i)}$ and $\sigma^{(i)}$. Since the length of those sequences is $T$, we have

$$ $\tau(\epsilon)\leq T,\quad\text{for any }\epsilon\in[0,1).$ $$

∎

###### Lemma E.13 ( ℋ ~ S superscript ~ ℋ 𝑆 \tilde{\mathcal{H}}^{S} over~ start_ARG caligraphic_H end_ARG start_POSTSUPERSCRIPT italic_S end_POSTSUPERSCRIPT is a Markov chain (conditioned on knowing f 𝑓 f italic_f and σ 𝜎 \sigma italic_σ )) .

###### Proof of Lemma E.13 .

###### Definition E.14 (Mixing time for inhomogeneous Markov chains) .

###### Lemma E.15 (Mixing time for truncated history) .

###### Proof of Lemma E.15 .

## Appendix F Technical Lemmas

In this section, we present some technical lemmas. Note that all the notations in this section are chosen for simplicity and may have different meanings than those in other sections.

###### Lemma F.1 (McDiarmid’s inequality (McDiarmid et al., 1989 ) ) .

Let $X=(X_{1},\dots,X_{N})$ be a vector of independent random variables taking values in a Polish space $\Lambda=\Lambda_{1}\times\cdots\times\Lambda_{N}$.
Suppose that $f\colon\Lambda\rightarrow\mathbb{R}$ satisfies

$$ $f(x)-f(y)\leq\sum_{i=1}^{N}c_{i}\mathbbm{1}\{x_{i}\neq y_{i}\},$ $$

for any $x,y\in\Lambda$. Then for any $\lambda\in\mathbb{R}$,

$$ $\mathbb{E}\Big{[}\exp\big{(}\lambda(f(X)-\mathbb{E}[f(X)])\big{)}\Big{]}\leq \frac{\lambda^{2}\|c\|_{2}^{2}}{2}.$ $$

###### Lemma F.2 (Corollary 2.11 in Paulin ( 2015 ) ) .

Let $X=(X_{1},\dots,X_{N})$ be a Markov chain taking values in a Polish space $\Lambda=\Lambda_{1}\times\cdots\times\Lambda_{N}$, with mixing time $\tau(\epsilon)$ for $0\leq\epsilon<1$. Define

$$ $\tau_{\min}\coloneqq\inf_{\epsilon\in[0,1)}\tau(\epsilon)\cdot\Big{(}\frac{2- \epsilon}{1-\epsilon}\Big{)}^{2}.$ $$

Suppose that $f\colon\Lambda\rightarrow\mathbb{R}$ satisfies

$$ $f(x)-f(y)\leq\sum_{i=1}^{N}c_{i}\mathbbm{1}\{x_{i}\neq y_{i}\},$ $$

for any $x,y\in\Lambda$. Then for any $\lambda\in\mathbb{R}$,

$$ $\mathbb{E}\Big{[}\exp\big{(}\lambda(f(X)-\mathbb{E}[f(X)])\big{)}\Big{]}\leq \frac{\lambda^{2}\tau_{\min}\|c\|_{2}^{2}}{8}.$ $$

###### Lemma F.3 (Donsker-Varadhan variational formula (Donsker and Varadhan, 1983 ) ) .

Let $P$ and $Q$ be two probability distributions over $(\Theta,\mathcal{F})$. If $Q\ll P$, then for any real-valued function $h$ integrable w.r.t. $P$,

$$ $\log\mathbb{E}_{P}[\exp h]=\sup_{Q\ll P}\{\mathbb{E}_{Q}[h]-D_{\mathrm{kl}}(Q \|P)\}.$ $$

###### Lemma F.4 (Chernoff’s bound (Chernoff, 1952 ) ) .

For any random variable $X$, if $\mathbb{E}[\exp(X)]\leq 1$, then for any $\delta\in(0,1)$,

$$ $\mathbb{P}(X\leq\log(1/\delta))\geq 1-\delta.$ $$

###### Lemma F.5 (Lemma M.7 in Zhang et al. ( 2022 ) ) .

Given any two conjugate numbers $p,q\in[1,\infty]$ s.t. $1/p+1/q=1$, for any $r\in[1,\infty]$, we have

$$ $\|Ax\|_{r}\leq\|A\|_{r,p}\|x\|_{q},\quad\text{and}\quad\|Ax\|_{r}\leq\|A^{\top }\|_{p,r}\|x\|_{q}$ $$

for any matrix $A\in\mathbb{R}^{m\times n}$ and vector $x\in\mathbb{R}^{n}$.

###### Lemma F.6 (Lemma M.8 in Zhang et al. ( 2022 ) ) .

Given any two conjugate numbers $p,q\in[1,\infty]$ s.t. $1/p+1/q=1$, we have

$$ $\|AB\|_{p,\infty}\leq\|A\|_{p,q}\|B\|_{p,\infty}$ $$

for any matrix $A\in\mathbb{R}^{m\times n}$ and matrix $B\in\mathbb{R}^{n\times r}$.

###### Lemma F.7 (Lemma M.9 in Zhang et al. ( 2022 ) ) .

Given any two vectors $x,y\in\mathcal{R}^{d}$, we have

$$ $\|\texttt{softmax}(x)-\texttt{softmax}(y)\|_{1}\leq 2\|x-y\|_{\infty}.$ $$

###### Lemma F.8 (Property of Kullback-Leibler divergence, Proposition 7.2 in Polyanskiy and Wu ( 2024 ) ) .

Given any two probability distributions $\mu_{1}$ and $\mu_{2}$ over $(\Omega,\mathcal{F})$ and any two distributions $\nu_{1}$ and $\nu_{1}$ over $(\Omega^{\prime},\mathcal{F}^{\prime})$, if $\mu_{1}\ll\mu_{2}$ and $\nu_{1}\ll\nu_{2}$, then we have

$$ $D_{\mathrm{kl}}(\mu_{1}\otimes\nu_{1}\|\mu_{2}\otimes\nu_{2})=D_{\mathrm{kl}}( \mu_{1}\|\mu_{2})+D_{\mathrm{kl}}(\nu_{1}\|\nu_{2}).$ $$

###### Lemma F.1 (McDiarmid’s inequality (McDiarmid et al., 1989 ) ) .

###### Lemma F.2 (Corollary 2.11 in Paulin ( 2015 ) ) .

###### Lemma F.3 (Donsker-Varadhan variational formula (Donsker and Varadhan, 1983 ) ) .

###### Lemma F.4 (Chernoff’s bound (Chernoff, 1952 ) ) .

###### Lemma F.5 (Lemma M.7 in Zhang et al. ( 2022 ) ) .

###### Lemma F.6 (Lemma M.8 in Zhang et al. ( 2022 ) ) .

###### Lemma F.7 (Lemma M.9 in Zhang et al. ( 2022 ) ) .

###### Lemma F.8 (Property of Kullback-Leibler divergence, Proposition 7.2 in Polyanskiy and Wu ( 2024 ) ) .

## Appendix G Experiment Details

### G.1 Training data generation

We first describe a basic setup of all our experiments. For some experiments, we change some part(s) in below to design the corresponding “flipped” experiment or to examine the OOD ability of the trained transformer. In particular, the $i$-th thread the training data

$$ $\left(x_{1}^{(i)},y_{1}^{(i)},x_{2}^{(i)},y_{2}^{(i)},...,x_{T}^{(i)},y_{T}^{( i)}\right)$ $$

is generated by the following distributions:

- •
$\mathcal{P}_{X}$: the feature vector $x_{t}^{(i)}\overset{\text{i.i.d.}}{\sim}\mathcal{N}(0,I_{d})$ where $I_{d}$ is $d$-dimensional identity matrix.
- •
$\mathcal{P}_{\epsilon}$: the noise $\epsilon_{t}^{(i)}\overset{\text{i.i.d.}}{\sim}\mathcal{N}(0,1)$.
- •
$\mathcal{P}_{\sigma}$: the noise intensity $\sigma_{i}$ is sampled i.i.d. from
$\tau_{i}\sim\mathrm{Gamma}(\underline{\tau},\bar{\tau}),\quad\sigma_{i}=\dfrac
{1}{\sqrt{\tau_{i}}}$
where the parameters $\underline{\tau}=\bar{\tau}=20$ for the basic setup of the experiment. We change these two parameters for some OOD experiments.
- •
$\mathcal{P}_{\mathcal{F}}$: The function $f_{i}(x)\coloneqq w_{i}^{\top}x$ where $w_{i}$ is generated from
$w_{i}|\sigma_{i}\sim\mathcal{N}(\bar{w},\sigma_{i}^{2}\cdot I_{d})$
where $I_{d}$ is the $d$-dimension identity matrix and $\bar{w}$ is set to be an all-one vector of dimension $d$. The covariance matrix of $w_{i}$ is related with the noise intensity $\sigma_{i}$ to control the signal-to-noise ratio.

Finally, the target variable is calculated by

$$ $y_{t}^{(i)}=w_{i}^{\top}x_{t}^{(i)}+\sigma_{i}\epsilon_{t}^{(i)}.$ $$

Throughout the paper, we consider the dimension $d=8.$

### G.2 Number of Tasks N 𝑁 N italic_N and Training Procedure

In the previous Section G.1, we define how we generate the training data. As in the previous work, we introduce the notion of task where each realization of $(w_{i},\sigma_{i})$ is referred to as one task. The rationale is that each configuration of $(w_{i},\sigma_{i})$ corresponds to one pattern of the sequence $(x_{t},y_{t})$’s. While the distribution of $(w_{i},\sigma_{i})$ corresponds to infinitely many possible task configurations, we use a finite pool of tasks for training the Transformer. Specifically, we generate

$$ $\mathcal{T}\coloneqq\{(w_{i},\sigma_{i})\}_{i=1}^{N}$ $$

from the distributions discussed above. Throughout the paper, we use $N$ to refer to the total number of tasks or the pool size.

Training the Transformer for our setting is slightly different from the classic ML model’s training. We do not use a fixed set of training data. Rather, we generate a new batch of training data freshly for each batch.

- •
The batch size $b=64.$ For each batch, we first sample with replacement $b$ tasks from the task pool $\mathcal{T}.$ And based on each sampled $(w_{i},\sigma_{i}),$ we generate a training sequence $\left(x_{1}^{(i)},y_{1}^{(i)},x_{2}^{(i)},y_{2}^{(i)},...,x_{T}^{(i)},y_{T}^{(
i)}\right)$ following the setting in the previous Section G.1.
- •
All the numerical experiments in our paper run for 200,000 batches.

The validation and testing sets are also randomly generated instead of fixed beforehand. But unlike the training phase which draws the task configuration from the task pool $\mathcal{T}$, the validation and test phase samples $(w_{i},\sigma_{i})$ directly from the original distribution described in the previous Section G.1. This is aimed to validate or test whether the trained model has learned the ability to solve *a family of* problems, or it only just memorizes a fixed pool of tasks $\mathcal{T}$.

### G.3 Derivation of Bayes-optimal Predictor

In Proposition 3.1, we state the Bayes-optimal predictor in the form of a posterior expectation. Now we calculate the Bayes-optimal predictor explicitly under the generation mechanism specified in Section G.1. Conditional on history $H_{t}=(x_{1},y_{1},\ldots,x_{t})$, the posterior distribution of $(w,\sigma)$ that governs the generation of $H_{t}$ can be calculated based on the Bayesian posterior as

$$ $\mathbb{P}(\tau|H_{t})=\mathrm{Gamma}(\tau;\underline{\tau}_{t},\bar{\tau}_{t} ),\quad\sigma=\dfrac{1}{\sqrt{\tau}},$ $$

$$ $\mathbb{P}(w|\sigma,H_{t})=\mathcal{N}(w_{t},\sigma^{2}\cdot\Sigma_{t}),$ $$

where

$$ $\displaystyle\Sigma_{t}=\left(I_{d}+\sum_{s=1}^{t-1}x_{s}x_{s}^{\top}\right)^{ -1},\quad$ $\displaystyle w_{t}=\Sigma_{t}\left(\bar{w}+\sum_{s=1}^{t-1}x_{s}y_{s}\right)$ $\displaystyle\underline{\tau}_{t}=\underline{\tau}+\dfrac{t}{2},\quad$ $\displaystyle\bar{{\tau}}_{t}=\bar{\tau}+\dfrac{1}{2}\sum_{s=1}^{t-1}\left(y_{ s}^{2}+\bar{w}^{\top}\bar{w}-w_{s}^{\top}\Sigma_{t}^{-1}w_{s}\right).$ $$

Accordingly, the Bayes-optimal predictor becomes

$$ $y_{t}^{*}(H_{t})=\mathbb{E}[y_{t}|H_{t}]=w_{t}^{\top}x_{t},$ $$

and

$$ $\displaystyle\sigma_{t}^{*2}(H_{t})$ $\displaystyle=\mathbb{E}[(y_{t}-y_{t}^{*}(H_{t}))^{2}|H_{t}]=\mathbb{E}[(f(x_{ t})-y_{t}^{*}(H_{t}))^{2}|H_{t}]+\mathbb{E}[\sigma^{2}|H_{t}]$ $\displaystyle=\dfrac{\bar{\tau}_{t}}{\underline{\tau}_{t}-1}\cdot(\mathrm{tr} \left(x_{t}x_{t}^{\top}\Sigma_{t}\right)+1).$ $$

These formulas are used to generate the Bayes-optimal curves in the figures.

## Peer Reviews

confidence: 3
contribution: 3
initial_rating: 5
presentation: 3
questions: What is the purpose of analyzing the regime when S << T? In this regime, the generalization error is better, but the approximation error is worse.
soundness: 3
strengths: 1. The paper considered the in-context learning and in-context uncertainty prediction problem, a new problem not considered in the literature. 
2. The theoretical result of Theorem 3.2 is stronger than prior results in the regime when S << T. 
3. The paper analyzed the OOD generalization performance of trained transformers.
summary: This paper studies the in-context learning abilities of the trained transformer through the lens of uncertainty quantification. The authors train the transformer for the task of mean and uncertainty prediction and demonstrate that transformers achieve Bayes near-optimal estimation and uncertainty prediction. However, the transformer is only able to achieve near-optimal in-distribution risk, which doesn't imply it behaves the same as the Bayes-optimal predictor. When testing on out-of-distribution tasks, the performance of transformers heavily relies on the training method.
weaknesses: 1. The paper just provided a generalization error bound and didn't upper bound the approximation error, and hence, it does not fully theoretically justify that transformer can be trained to perform in-context uncertainty prediction. 
2. The limited OOD generalization performance has already been explored in the literature, e.g., [Garg et al., 2022], albeit with the in-context learning task.

---

confidence: 3
contribution: 2
initial_rating: 5
presentation: 2
questions: 1. Is the loss function devised by the authors and is there any previous works that study the same loss function? Could the authors provide a brief discussion for this loss function whether a similar loss function has been utilized in the literature?

2. What is pattern and sequence? Could the authors define these terms rigorously? Is the pattern a distribution? The authors use $\mathcal{P}$ for distribution first, then use the same notation for a pattern.

3. How is the loss function defined based on a pattern? It is used in the definition of IWL and ICL, but I could not find the definition.

4. Could you provide a more rigorous argument about the definitions of IWL and ICL?
soundness: 2
strengths: 1. This paper is fairly written even though I do not understand some parts. Their problem setup is easy to follow and their organization is good (except the section 2.1). However, I could not understand why they write transformers with capital T.

2. The authors develop a good framework for studying the uncertainty quantification. Their theoretical analysis of the generalization properties of the loss function with the uncertainty quantification is interesting. They empirically observe the performance of transformers under task shift, distribution shift, and length shift.

3. The observation that the positional embedding worsens the performance of length generalization is valuable.

4. The way they define the loss function with respect to $\hat{\sigma}$ and $\hat{y}$ is valuable and they provide the Bayesian optimal of these parameters.
summary: This paper addresses the in-context uncertainty quantification for pre-trained transformers, focusing on predicting both the mean and variance of the conditional distribution of a target variable given input features.  They derive a Bayes-optimal solution for the task and demonstrate that transformers are capable of performing in-context uncertainty quantification.

1. The authors rigorously analyze the uncertainty quantification problem when Transformers are constrained to a context window of size \( S \). They derive a generalization bound of \( \tilde{O}(\sqrt{\min\{S, T\}/(nT)}) \) for pre-training over \( n \) tasks with sequence length \( T \), extending to other cases under the assumption of bounded and Lipschitz loss functions. They laim that this bound is tighter than existing analyses, particularly when \( S < T \), and is the first of its kind to utilize the context-window structure to model a Markov chain over the prompt sequence and construct an upper bound on mixing time. They also discuss the additional approximation error due to a finite context window and quantify the trained Transformer's risk convergence to the Bayes-optimal risk.

2. The paper provides an empirical evaluation of the in-context learning abilities of transformers for uncertainty quantification across three distribution shift scenarios: task shift, covariate shift, and prompt length shift. The results suggest that transformers can learn both mean and uncertainty predictions in-context, even under moderate task distribution shifts, when trained on data with substantial task diversity. Moreover, the authors show that increasing task diversity through meta-learning enhances robustness to covariate shifts. Finally, they observe that removing positional encoding from the embedding vector significantly improves generalization performance.
weaknesses: 1. I do not understand some parts of section 2.1 ( I think this section is written poorly). I think there is a gap between their definitions of IWL and ICL and the loss function the authors use. It would be better if the authors explain the relationship between their loss function and their definitions of ICL and IWL.

1.1. I am not able to follow the discussion of the pattern and sequence in section 2.1. I will ask some questions in the questions section.

1.2. The authors claim that the understanding of uncertainty quantification objective is important for understanding the contribution of in-context learning compared to the contribution of in-weight learning. This is fair. However, towards end of the subsection, they define IWL(M) and ICL(W) based on the worst and best case test distribution, which are not directly related to variance of the model. Therefore, the way of defining the loss function with the variance does not perfectly align with the definition of IWL(M) and ICL(M). Furthermore, they do not utilize (at least I did not notice) the definitions of IWL(M) and ICL(M) in their paper. When two functions are defined at the beginning of the paper (specifically in problem setup), I expect that the functions are frequently used, which is not the case in the paper.  

2. Theorem 3.2 is suboptimal, and their comparison to previous work, specifically Li et al. (2023b), is not fair.

2.1.  I think that Theorem 3.2 is suboptimal because I do not understand why the generalization performance of the transformers decreases with the context length. Consider the case when $S = T/4$ and $S = T/2$. From Theorem 3.2, I observe that the generalization performance is worse when $S = T/2$, but I think the generalization performance should improve when the context length increases. How can increasing the context length decreases the generalization performance. Please correct me if my reasoning fails.

2.2. The authors compare their bound with Li et al. (2023b) and Zhang et al. (2023b) in an unfair way. The bounds obtained in Li et al. (2023b) is better than the current paper when $S \geq T$. Li et al. (2023b) does not cover the case where $S << T$. In order to compare the results in the case of $S<T$, the authors change the algorithmic stability assumption (in line 297-298) and compare their results. However, when the algorithmic stability term is $\mathcal{O}(1/T)$, then I think the bounds in Li et al. (2023b) still better than the current paper.

---

confidence: 3
contribution: 2
initial_rating: 5
presentation: 3
questions: * Can the ICL and IWL ability measure defined in Section 2.1 be analyzed theoretically or empirically?
* In many ICL setups, the prediction loss is not summed up over all $t=1,\cdots,T$ but only evaluated for the final query token $(t=T)$. How would this affect the derived generalization error bound?
* Appendix B.2 seems to study the approximation error in terms of the truncated Bayes-optimal error, but this does not tell us the behavior of the population minimizer. Can $R(TF_{\theta^*})$ be bounded, e.g. in terms of depth?
soundness: 3
strengths: The uncertainty quantification setup is quite interesting, allowing us to examine out-of-distribution prediction capability in a more controlled manner. The paper is overall well written and easy to follow. The experiments on distribution shifts are well-designed and extensive, and the results demonstrating deviation from the Bayes optimal prediction are quite surprising, contradicting previous works which equate ICL with Bayesian averaging. While the linear regression setup is quite stylized, the insights indicate new directions for future work to study ICL/OOD ability at larger scales and practical datasets.
summary: The paper explores the in-context learning (ICL) ability of transformers to learn both the weight vector and noise variance (uncertainty quantification) of linear regression tasks. For a class of multilayer transformers with bounded context window $S$, it is shown that the empirical risk minimizer converges to the Bayes optimum with excess risk bounded as $\widetilde{O}(\sqrt{(S\wedge T)/nT})$. Experiments show that transformers can perform uncertainty quantification under task and covariate shifts if task diversity during pretraining is sufficiently large. Nonetheless, it is also shown that transformers may deviate from the Bayes optimal predictor on out-of-distribution tasks, possibly due to taking statistical shortcuts, in contrast to previous works which postulate an equivalence with Bayesian averaging.
weaknesses: My main issues lie with the theoretical parts of the paper.

* Theorem 3.2 seems to be similar to existing analyses such as the cited work Zhang et al. (2023b), which derives a bound in terms of the Markov chain mixing time $\sqrt{\tau_{\mathrm{mix}}/nT}$ and under more general assumptions on data generation and loss function. While the authors compare to these works and claim their result is tighter, it is clear that considering a finite context window gives $\tau_{\mathrm{mix}} = S\wedge T$ and retrieves their rate. (In Zhang et al., there is also a TV-to-sqrt(KL) "conversion factor" incurred in the approximation error when considering the excess risk, so the given bound could be seen an improvement. However, Zhang et al. also show that the KL term decays exponentially with increasing depth, so it has little effect on the overall risk bound.)
* The theory and experimental parts of the paper are disjointed and do not support each other. The focus of the theoretical contribution is to quantify the effect of the finite context window and the analysis does not really depend on the specific bi-objective task as the authors mention. In contrast, the experiments focus on the uncertainty quantification ability and do not study the effect of $S$.
* The discussion on ICL versus IWL ability (Section 2.1) is interesting and the constraint (3) seems reasonable, however it is not rigorously shown whether it is actually satisfied for the proposed ICL setup. Also, the proposed measurements of ICL and IWL ability are not evaluated theoretically or empirically. I believe a more through justification of the setup is important since the experiments are limited to linear regression tasks (this point should also be made clearer in the main text).
* Minor: the definition of the Transformer model class needs to be given in the main text.