The Evolution of Statistical Induction Heads: In-Context Learning Markov Chains

Large language models have the ability to generate text that mimics patterns in their inputs. We introduce a simple Markov Chain sequence modeling task in order to study how this in-context learning (ICL) capability emerges. In our setting, each example is sampled from a Markov chain drawn from a prior distribution over Markov chains. Transformers trained on this task form \emph{statistical induction heads} which compute accurate next-token probabilities given the bigram statistics of the context. During the course of training, models pass through multiple phases: after an initial stage in which predictions are uniform, they learn to sub-optimally predict using in-context single-token statistics (unigrams); then, there is a rapid phase transition to the correct in-context bigram solution. We conduct an empirical and theoretical investigation of this multi-phase process, showing how successful learning results from the interaction between the transformer's layers, and uncovering evidence that the presence of the simpler unigram solution may delay formation of the final bigram solution. We examine how learning is affected by varying the prior distribution over Markov chains, and consider the generalization of our in-context learning of Markov chains (ICL-MC) task to $n$-grams for $n > 2$.

Paper

References (47)

Scroll for more · 35 remaining

Similar papers

Peer review

Reviewer 1EuX6/10 · confidence 3/52024-07-11

Summary

This work introduces a simple “in-context learning” Markov chain modelling task and studies how transformer models tackle it. The task consists of inferring the transition probability matrix of the Markov chain, which is sampled from a prior Dirichlet distribution. The authors consider the task to pertain to “in-context learning” because each data point (that is, each sequence in the dataset) is sampled from a different transition probability matrix. The prior over the matrices is however global and thus shared by both training and test datasets. More in detail, the authors focus on Markov chains with only two states, and sample each sequence from the stationary distribution of the process. Given this dataset, the authors investigate the training dynamics of a two-layer attention-only transformer model both empirically and theoretically. The theoretical investigation involves the introduction of a simplified model, which is defined to mimic the main features of the transformer model. The authors report three different training stages, during which the model very quickly changes from predicting random transitions, to predicting samples similar to the stationary distribution of the chain, and lastly, predicting transition from the inferred transition probability matrix. The authors further connect their findings with previous work.

Strengths

Deep learning architectures are remarkably complex systems and designing both tractable models and modelling tasks that mimic the behaviour of these systems, prior, during or after the training process, is an important approach to unveil their inner workings. This paper attempts to carry one such analysis, by introducing a simple “in-context learning” Markov chain modelling task and studying how a two-layer attention-only transformer model and a simplification thereof solve it, thereby adding another interesting contribution to our understanding of transformer models. One strength of the paper is that the authors first demonstrate that both their models can indeed find the optimal solution of their Markov chain task, and then empirically verify this, which gives soundness to their claims. In particular the results in Figure 3, which demonstrate the similarity in behaviour of both models, are very compelling (see however the questions below). A second strength is that the authors identify different phenomena within their setting which have also been observed before, like multiple distinct stages of training, the latter of which is also connected to induction head formation, or the order in which the layers in the network are learned. Finally, the authors demonstrate that some of their findings are also present in a second-order Markov chain modelling task. Putting aside some minor details with some notation and content organisation (see below), the paper is overall well written.

Weaknesses

My first issue with the paper is: - how much does the proposed learning task really concern “in-context learning”? especially in the context of large language models, which the authors use as motivation. As framed by e.g. Xie et al. (2022) who focus on LLMs, "in-context learning" deals with sequences which have low probability under the training distribution. The authors do not really elaborate in their understanding or interpretation of "in-context learning", nor do they explain how it relates to "in-context learning" in the setting of language models. In short, I believe the paper contributes more to our understanding of transformers than to our understanding of in-context learning. A second major issue is that the authors do not explain their reasoning behind nor the limitations of their dataset/task definition. - First, the authors consider Markov chains with only two states and do not comment on why they restricted their study to that case nor how their findings extend to Markov chains with more states, if at all. - Second, the authors choose the initial distribution of the chain to be its stationary distribution but, again, did not explain why. Note that a stationary Markov chain corresponds to a very special case, and it's not very clear how the observations made by the authors extend beyond it. Indeed, choosing a Markov chain with more states, initialised from a distribution far from stationarity, can generate long sequences which are “out-of-equilibrium”. One can't but wonder whether one would still find multiple training stages in this case. See e.g. question 8 below. Despite its merits, I think the issues above require some revision, or more detailed explanation, before the paper can be published. *Other comments*: The comment in line 205 “We observe that training a 1-layer transformer fails to undergo a phase transition or converge to the right solution”, could be better highlighted as evidence for the emergence of induction heads. It feels somewhat buried in between other comments and statements. Also there’s a typo in line 239 and another in line 280. *References:* - An explanation of in-context learning as implicit Bayesian inference. Xie et al. (2022)

Questions

1. Why did you consider Markov chains with two states only? 2. Can you please elaborate on your comment in line 145, page 4: “the stationary distribution of this random Markov chain does not admit a simple analytical characterization when there is a finite number of state”? 3. Why did you choose the stationary distribution as the initial distribution? 4. Is the ground-truth unigram strategy then computed by estimating the stationary distribution with the count histogram? Is this the one used to compute the KL distance in Figure 3? 5. Similarly, do you use the bigram strategy of section 2.1 to compute the corresponding KL distance in Figure 3? Or do you use the instance-dependent ground-truth transition probability matrix instead? 6. Is the difference in KL between the unigram and bigram strategy due to finite sampling? 7. Why does the bigram strategy have a smaller loss? is it because of the inductive bias of the model? 8. In the setting with an initial condition far from the stationary distribution, would the model first fit the step-dependent marginal distribution of the chain (a unigram strategy) and later fit the bigram strategy of section 2.1? Isn't it simpler to fit the latter first (as opposite to the stationary case)? 9. Is the 2-layer transformer model initialised to the matrices in Appendix C.1? 10. The sequence length is first labelled by $t$ (e.g. after eq. 1) and then by $T$ (e.g. before eq. 4). Is this change of notation intentional? Or am I misunderstanding something?

Rating

6

Confidence

3

Soundness

3

Presentation

3

Contribution

2

Limitations

Yes, they authors did address (some of) the limitations of their method.

Authorsrebuttal2024-08-07

New Lemma for $k>2$

We can prove a version of Lemma 3.1 for any number of states (k) as long as we consider a different distribution for transition matrices: specifically, a mixture of the distribution where the unigram strategy is optimal, and the distribution where the unigram strategy is as bad as guessing randomly. The proof only requires different versions of lemmas C.1 and C.2, and actually adds more intuition to our stage-wise learning story. Specifically, in the first phase of learning, the contribution to the gradient from the unigram-optimal distribution dominates, but in the second phase, the other component (from the distribution where unigrams are useless) is dominant. We would like to add this variation of Lemma 3.1 to our camera-ready version.

Reviewer aVXq5/10 · confidence 4/52024-07-12

Summary

The paper studies in-context learning with transformer models in a simple Markov Chain sequence modelling task. The authors empirically show the formation of statistical induction heads which correctly compute the posterior probabilities given bigram statistics. Moreover, they observe that during training the model undergoes phase transitions where the complexity of the n-gram model increases. They also propose a simplified theoretical model of a two-layer transformer to analyse these phenomena.

Strengths

1. The paper addresses a relevant topic within a simplified setting, facilitating the interpretability of transformer model solutions and an understanding of their training dynamics. 1. Utilising Markov chains is an effective approach to studying sequence-to-sequence modelling. 1. The study seeks to balance empirical investigation and theoretical analysis. 1. The observation that models may progress from simple solutions, like unigrams, to more complex structures during training is interesting. 1. It is relevant that transformers can learn an algorithm to estimate the transition matrix in context by gradient descent.

Weaknesses

The major weakness of this work is that the abstract and introduction suggest a primary focus on analysing and uncovering the mechanisms behind the simplicity bias and the phase transition from simple unigram solutions to more complex ones. However, upon reading the main body of the paper, it seems that the experiments mainly reveal the existence of such biases and transitions, while the theoretical section addresses a different phenomenon: how the two layers are learned in different training phases. The plateau behaviour is particularly intriguing, such as the model learning unigrams, bigrams, trigrams, and so on, but their theory does not seem to describe or explain this observation. 1. **Unigram strategy:** Can the authors clarify this statement "*the stationary distribution of this random Markov chain does not admit a simple analytical characterization when there is a finite number of states*". In this context, what does "*this Markov chain*" refer to? 1. **Simplified transformer:** the proposed simplified transformer seems to be able to capture all the phenomenology of a real transformer. Nevertheless, it is not clear to me if the proposed model is a good proxy for an attention-only transformer or not. In particular: 1. The first attention layer does not use the interaction between tokens but represents the attention through a learnable matrix that could in principle learn the same structure. Could the authors elaborate on this choice? 1. The second attention instead captures the interaction between tokens but between the input and the output of the first layer which is unusual. I understand that this is based on the construction of the real transformer where the output of the first attention is copied in the second block of the embedding and therefore can interact with the input but it still means that the simplified version doesn't need to learn this mechanism. Could the author elaborate on this choice? 1. The model seems to be composed of a non-linear attention for the first layer (softmax is present) and linear attention for the second. Could the authors elaborate on this particular choice? Is it a way to simplify the analysis while maintaining the properties of the softmax where needed? 1. **Data generation:** in section 2, it is explained how the transition matrices are generated according to a Dirichlet distribution, nevertheless in the theory and some of the experiments there is the additional requirements for the matrix to be doubly stochastic which is not mentioned when the setup is described. Could the authors clarify this point and highlight the importance of doubly stochastic transition matrices? 1. **Proof of Proposition 2.2:** the proof of this proposition appears disorganised and not easy to follow. In particular: 1. Setting the internal dimension d=3k seems fundamental to ensure that the model has the correct number of dimensions to copy the tokens from one layer to the next. I do not necessarily have a problem with this choice but it would be useful to see it discussed in the main text. 2. Is the definition of $v^{(1)}$ correct? if I am not mistaken it appears that the matrix is of dimensions $3k \times 3k$ whereas by the main text, it should be of dimensions $t \times 3k$ could the author clarify this together with the definition of $\delta_2$ and $1_k$. 3. There are multiple quantities used in the text such as $e_{x_i}$ or $e_i$ or $e_{{i-1},j}$ could the authors clarify their meaning. 4. In the expression $\text{softmax}(\text{mask}(A))_{i,j} \approx \mathbb{1}[j=i-1]$ shouldn't it be $[i=j-1]$ ? Why is the indicator function comparing the indexes instead of the values $x_i$ ? Moreover, could the authors clarify why it is only the indicator function and not $\frac{1}{\text{count}([x_i=x_j-1])}$ given the softmax? 5. In the expression of $\text{Attn}_2(e)$ why is the summation from $h=1$ to $h=3k$ if h is the index of the element in the sequence? Furthermore a new index $g$ appears which is not used anywhere. 1. **Experiments in Figure 8:** are the experiments in Figure 8 using d=3k ? do the authors observe that the parameters converge to the construction given in Proposition 2.2 ? 1. **Proof of Proposition 2.3:** The proof for the unigram construction seems to be missing. 1. **Unigram and bigram constructions:** For both the two-layer transformer and the simplified model it is possible to show constructions for the unigram and bigram models. Nevertheless, besides giving the weights for such constructions the authors do not discuss the relationship between them and the objective functions. Are they stationary points of the dynamics? Does this help explain the plateau observed in the experiments? is the unigram a saddle point? 1. **Minimal model:** In line 241 the authors state that "*the minimal model converges to the bigram solution spending however significantly less time at the unigram solution*". By looking at Figure 3 I understand how the minimal model reaches the same Kl value in almost half the time but it also has fewer parameters and some of the structure is already enforced by construction (for example the fact that the second attention is between input and output of the first layer). Could the authors explain why the comparison still makes sense? 1. **Varying the data distribution:** I find this part unclear. In line 231 the authors state that they define distributions "*we define distributions over Markov chains that are in between the distribution where unigrams is Bayes optimal, and the distribution where unigrams is as good as uniform.*" can the authors provide a mathematical definition of such distribution? Are doubly stochastic transition matrices used to create Markov chains with uniform stationary distribution such that the latter would be the only unigram possible? 1. **Two phases of learning in Lemma 3.1:** The model is capable of reproducing the effect of learning the second layer first, nevertheless I am not convinced that this is a property of the model of the simplified transformer but rather a consequence of the initialization and step size. Could the authors clarify this point?

Questions

See above.

Rating

5

Confidence

4

Soundness

2

Presentation

2

Contribution

2

Limitations

- Simplified transformer architecture - Synthetic task only, not clear if similar phenomena appear in larger models trained on natural language data.

Authorsrebuttal2024-08-07

Minimal Model Design

To create our minimal model, we started from a two layer attention-only disentangled (see [Elhage et al](https://transformer-circuits.pub/2021/framework/index.html)) transformer (using relative positional embeddings) and iteratively simplified parts that empirically did not affect the training dynamics. In our construction, the first layer only attends to positional embeddings, and the second layer ignores positional embeddings, so we set the first layer key matrix and the positional embeddings from the second layer to zero. Then we set the value matrices and query matrices to the identity in both layers. In the experiments, all optimizations such as layer norms and weight decay were removed, and the optimizer used was SGD. With all of these changes, the overall training dynamics were not changed much at all, depending on hyper parameters they could speed up training, but the same loss curve and phases were observed. To make analysis of gradient descent on the minimal model feasible, the softmax on the second layer had to be removed, which did make the phase transition less sharp.

Authorsrebuttal2024-08-07

Doubly stochastic and 'unigram' distributions

Doubly stochastic transition matrices are those for which the stationary distribution is the uniform vector, or those for which the unigram strategy and the uniform strategy get the same loss. If we want to observe the inductive biases of the model when there is no signal encouraging the unigram strategy, then sampling from doubly stochastic transition matrices is natural. Finally, we also considered the distribution where each row in the transition matrix is the same, resulting in the unigram and bigram strategies having the same loss. The mathematical definition of the mixed distribution in the graph on the left of figure 4 is as follows: with 75% chance, choose a uniformly random doubly stochastic transition matrix, otherwise make each row of the transition matrix the same vector, chosen from a flat Dirichlet distribution.

Authorsrebuttal2024-08-07

Question 4

4.1. We use a dimension of $3k$ to have a simple and intuitive construction. In practice, models can learn with a far smaller internal dimension. We can add discussion of this to the main text. 4.2. We apologize for the confusing notation with regard to the matrix definitions, it will be improved. $v^{(1)}$ is of dimension $t\times d$, where $d=3k$. We will make sure that the dimensions of each submatrix used to define $v^{(1)}$ and all other matrices are all specified in the notation. 4.3. $x_i$ refers to the token at position $i$. $e$ is used to represent the $1$-hot embeddings, $e_i$ is a vector that is $0$s besides at position $i$ where it is $1$. Similarly, $e_{x_i}$ is all zeros except at position $x_i$ where it has a $1$. $e_{i-1,j}$ is the $j$th index of the vector $e_{i-1}$, hence $e_{i-1,j}=\mathbb{1}[i-1=j]$. We will do an extra pass over all of this notation to clarify and improve it. 4.4. and 4.5. We thank you for pointing out these typos. It should say: $$\text{softmax}(\text{mask}(A))\_{i,j}\approx \frac{\mathbb{1}[x_{j-1}=i]}{\sum_{h=1}^i \mathbb{1}[x_{h-1}=i]}$$ Fixing these typos, we get the result $$Attn_2(e)\_{i,j+2k} =\frac{\sum_{h=1}^{k}\mathbb{1}[x_{h-1}=x_i]\mathbb{1}[x_h=j]}{\sum_{g=1}^i\mathbb{1}[x_{g-1}=x_i]}$$ Which is more correct, since this is the bigram probabilities, instead of just the bigram statistics. That is, this is the empirical approximation of $P_{x_i, j}$.

Reviewer Kgpp5/10 · confidence 2/52024-07-17

Summary

This paper introduces a task to investigate how in-context learning capabilities are learnt by transformer models. They show that models trained on this task go through a phase transition from which they start by modeling unigram to then acting as a bigram model. The authors further extend their work to the case of n=3 and show similar behavior.

Strengths

1. Nice presentation, with clear figures exhibiting the key behavior in question (sudden "emergence" of the correct behavior. 2. Good attempt to form a theoretical/mathematical foundation, which extends to the appendix. 3. Attempt to understand ICL LLMs using a good toy task.

Weaknesses

1. Questionable impact since this topic has been explored heavily in the past years, with other similar tasks and toy models existing, showing similar results. 2. Questionable how well the toy setting can actually transfer to real-world ICL, albeit interesting.

Questions

What is the key differentiating contribution of the work in comparison with the myriad of other works in this space? What is the conclusion of the work that you believe is transferable to real-world ICL setting. What n-gram statistics would that follow?

Rating

5

Confidence

2

Soundness

3

Presentation

3

Contribution

2

Limitations

It would potentially be useful to also discuss the limitations of the toy task to explain the full-fledged LLM ICL setting. A lot has been said in the paper about conclusions made, and I understand that this the same with any work employing a toy task, but an attempt for making more direct comparisons between real vs. toy setting may enhance the value of the work.

Reviewer Kgpp2024-08-09

Thank you for answering my questions. I admit that some, but definitely not all of the papers in mind may be concurrent works. I have read the other reviewer's comments, and in general it seems like while there is an agreement that this is a well-done piece of scientific literature, the impact is questionable, as the toy-setting field particularly when it pertains to ICL is definitely crowded. Finding new angles to attack the problem is a worthwhile pursuit, but at times may distract from perhaps other more important and less-studied issues. As I see there are better reviewers than me to assess this work, so I will raise my score from a 4 to a 5 while keeping my confidence low, and leave it up to them to reach a consensus.

Reviewer oy836/10 · confidence 3/52024-07-19

Summary

In the paper, the authors investigate the phenomenon of in-context learning exhibited by Transformers with the help of a simplified architecture and Markovian synthetic data. The authors show that experimentally the attention layers form statistical induction heads that help the model to implement an add-constant estimator based on the empirical counts of the input. They also show that a simplified transformer architecture can effectively represent such an estimator, and they provide a SGD convergence analysis for a minimal model.

Strengths

The paper joins a line of works that aim at studying transformers with the help of synthetic data generated according to Markov distributions. Even if the model considered is significantly simplified, I believe that the theoretical and experimental insights provided by the paper are intriguing. The paper is also well written and the results seem correct to me, even if the notation can be improved, especially for the proofs in the appendix, that are not easy to follow.

Weaknesses

The main limitations of the paper are: (1) the transformer architecture is heavily simplified (even if I believe that the results obtained in the paper should extend to more complex architectures); (2) the model used for the SGD analysis is, indeed, minimal. While limitation (1) is not so important to me, I am a bit concerned about limitation (2). I am not sure what is the actual utility of a SGD convergence analysis on such a minimal model. While I think that the representation result of Proposition 2.2 can be extended to a more complex model, I don't think that the SGD analysis of the minimal model would work for a more complex architecture, which would then need completely different and more sophisticated techniques.

Questions

1. Do you think that an SGD analysis similar to the one carried out for the minimal model would carry over to a more complex architecture, closer to the actual transformer? If not, what are the main issues with working out such a analysis for, for example, the simplified transformer model of Equation (1)? 2. Your model architecture uses relative positional embeddings, as opposed to most state-of-the-art transformer models. Do you think that your analysis would be easily extended to a model with absolute positional embeddings?

Rating

6

Confidence

3

Soundness

3

Presentation

3

Contribution

2

Limitations

N/A

Area Chair 9Bro2024-08-09

Reviewers, please respond to the author rebuttals.

Dear reviewers, this is your AC! The authors have responded to your review. Could you please take a look at the rebuttal (and respond/edit your scores if need be)? (Thank you if you have already done so) Thanks!

Reviewer 1EuX2024-08-11

I appreciate the authors' detailed response. Before I update my score, I have a couple more questions/comments, if possible. I apologize in advance for these additional inquiries; my intention is simply to gain a better understanding of some of the claims made in the paper. I hope these questions also help the authors present their results more clearly. @3: When you write: *"The marginal distributions of the tokens generated from a markov chain approach the stationary distribution exponentially fast no matter the initial distribution"* do you mean for $k=2$ states? Because one can construct transition probability matrices which yield Markov chain that exhibit slow convergence to stationarity. A random walk with periodic boundary conditions is an example, where the mixing times are of the order of $k^2$, for $k$ the number of states. Or am I missing sth? @6: Could you please remind me how you compute the KL divergence in your experiments? Meaning, you compute the KL wrt. what? @8: Let's assume that we are dealing with a Markov chain that exhibits slow convergence to stationarity. In such a case, the marginal unigram distribution changes at every step. Let's also assume one train your model on the *out-of-equilibrium* sequences sampled from said Markov chain. Do you still think the model will first find such a unigram strategy? I hope this rephrasing makes my question clearer.

Authorsrebuttal2024-08-12

Response to Reviewer Comment

@3: To clarify, in our setting $T$ is taken to be large, while $k$ is effectively a constant. This is similar to natural language settings, where the number of letters or tokens is some constant, but the context can be arbitrarily long. When we wrote that the "marginal distributions... approach the stationary distribution exponentially fast", we were referring to Lemma C.4, which states that the distribution of the token n tokens after the current has distance to the stationary distributed bounded by $\alpha^n$, for some $\alpha<1$ depending on the specific chain. @6: In our experiments, we used KL divergence to measure the difference between the probabilities predicted by the model and other probability distributions. For test loss, this other distribution was the appropriate rows of the transition matrices used to generate the test examples. Formally, let $f(x_{1:T-1})$ be the softmax distribution of the transformer's output, given the input sequence $x_{1:T-1}$. In our standard setting, we measured $$d_{KL}(\mathcal{P}\_{x_{T-1}} || f(x_{1:T-1}))$$ where $\mathcal{P}\_{x_{T-1}}$ is the true distribution of the next state $x_T$ given the previous state, under the true Markov chain $\mathcal{P}$. Note that $\mathcal{P}$ varies from sequence to sequence (it is drawn from a prior over transition matrices) and is not directly observable by the learner—this is what needs to be learned in-context. For measuring how close the model was to various strategies, we computed the predicted probabilities given by said strategies, and used those as the base distribution. Note that the output of the bigrams strategy (which is Bayes-optimal for our base setting) is different from the aforenentioned ground-truth $\mathcal{P}\_{x_{T-1}})$. Instead, as described in Section 2.1, it is a Bayesian posterior distribution of the next state given the observed sequence, with the prior determined by the prior distribution of transition matrices. Formally: $$ \mathbb{E}[\mathcal{P}\_{x_{T-1}} | x_{1:T-1}] $$ where the expectation is taken over the draw of Markov chain transition matrix. @8: Setting aside the mixing time issue discussed in @3, your question seems to suppose that all the training sequences are drawn from a single Markov chain, whereas in our work there is a new chain for each sequence. Do you have a more precise formulation of your question in mind? Regardless, we agree it could be interesting to explore what happens when the number of states is comparable to the sequence length, and the prior distribution over chains is crafted to favor chains that mix slowly, though this is beyond the scope of this work.

Reviewer 1EuX2024-08-12

I thank the authors again for their responses. I will update my score.

Authorsrebuttal2024-08-12

Link correction

A minor fix to our rebuttal: the reference to Akyürek et al., 2024 should point to [this paper](https://arxiv.org/abs/2401.12973).

Reviewer aVXq2024-08-13

After considering your clarifications, I acknowledge that despite the multiple typos and minor concerns that affect the readability of the paper and the communication of the results, your work contains some insightful observations. These contributions are valuable and, therefore, I have decided to increase my score.

Program Chairsdecision2024-09-25

Decision

Accept (poster)

© 2026 NYSGPT2525 LLC