How transformers learn structured data: insights from hierarchical filtering

## Abstract

Abstract We introduce a hierarchical filtering procedure for generative models of sequences on trees,
enabling control over the range of positional correlations in the data. Leveraging this controlled setting, we provide evidence that vanilla encoder-only transformer architectures can implement
the optimal Belief Propagation algorithm
on both root classification and masked language modeling tasks.
Correlations at larger distances corresponding to increasing layers of the hierarchy are sequentially included as the network is trained.
We analyze how the transformer layers succeed by focusing on attention maps from models trained with varying degrees of filtering. These attention maps show clear evidence for iterative hierarchical reconstruction of correlations, and we can relate these observations to a
plausible implementation of the exact inference algorithm for the network sizes considered.

## 1 Introduction

Transformer-based large language models have revolutionized natural language processing, and have notably demonstrated their capacity to perfectly assimilate the grammatical rules of the languages they are trained on. While this evidence shows that transformers can handle and exploit the subtle long-range correlations that emerge in natural language, their inner workings remain largely unclear.

Due to the complexity of the standard multi-layer transformer architecture (Vaswani et al., ), understanding what strategy is precisely implemented via the attention mechanism to solve a given problem has been limited so far to very simple tasks (Weiss et al., ; Zhong et al., ; Behrens et al., ). Nonetheless, significant results have been obtained by studying transformers on simplified models of language known as Context-Free Grammars (CFGs). Through probing of the so-called parsing tree of CFGs, which is not explicitly given to the learning model during training, evidence has notably pointed towards transformers implementing the optimal dynamic programming algorithm to reconstruct the hidden structure of the grammar, but alas without finding a fully plausible implementation within the architecture (Zhao et al., ; Allen-Zhu & Li, ).

Beyond language models, the significance of data structure in machine learning applications is well recognized, yet incorporating this structure into theoretical analyses often remains challenging. This challenge highlights a critical avenue for development in statistical-physics-based approaches, particularly when addressing more realistic machine learning problems (Mézard, ).
Context-free-grammars belong to a category of structured data characterized by hierarchical correlations (Mossel, ). In general, understanding how standard deep network structures can take advantage of this hierarchical structure in their training, to improve their generalization abilities, is an important research question. Towards this objective, simplified hierarchical models of structured data on fixed trees have proved very useful in understanding the effectiveness of Convolutional Neural Networks (CNNs) (Cagnetta et al., ), for which there are now formal results supporting the idea that the optimal Belief Propagation (BP) algorithm can be approximately implemented (Mei, ). Unfortunately, while the implementation of the hierarchy in CNNs is made quite transparent by the hierarchical structure of their convolutional filters, this is not true for transformers, and one can therefore not straightforwardly transpose this interpretation to other deep networks (Cagnetta & Wyart, ).

In this work, we present a complementary approach to those described above, which can aid in the mechanistic interpretation of the transformer computation.
In particular, we analyze the performance of an encoder-only vanilla transformer (Devlin et al., ) on a filtered hierarchical model of data, allowing us to truncate the hierarchical correlations at a desired level $k<\ell$, where the sequence length is $2^{\ell}$. The proposed model for structured sequences is illustrated in Fig. 1(a). Being defined as a tree-broadcast process, our data model is exactly solvable for any level of filtered hierarchy and any inference task. The exact solution can be efficiently computed through the BP algorithm, running in linear time in the number of variables. This controlled setup allows us to predict how a multi-layer transformer should perform if its training results in a close-to-optimal performance.
Moreover, by introducing a mismatch between the inference graph of BP and the filtered hierarchy of the data, we can also predict the test performance of the transformer on data from out-of-sample filtered hierarchy levels. In this study, we exploit the comparison with the BP reference performance, the inspection of the attention maps, and multiple probing experiments at different levels of the hierarchy, to provide evidence that trained transformer encoders implement the Belief Propagation algorithm in an interpretable way.

The paper is organized as follows. First, we provide a detailed description of our tunable hierarchical model in Sec. 2, where we notably discuss the exact BP algorithm in Sec. 2.5. We then perform numerical experiments on standard transformer architectures in Sec. 3, focusing on the supervised and unsupervised tasks in Sec. 3.2 and Sec. 3.3 respectively. We finally discuss our results, their implication, and their relation to existing work in Sec. 4.

Figure: Figure 1: Synthesis of our main results. (a) Proposed filtered hierarchical model, illustrated here with $\ell=3$ layers and with a filtering parameter $0\leq k\leq\ell$, allowing one to truncate the hierarchy and generate data with more or less structure but identical symbol occurrence probability given a root. (b) Validation accuracy of a vanilla encoder-only transformer with $n_{L}=4$ attention layers, each with a single head, on the supervised task of inferring the root $x_{0}^{\mu}$ among $q=4$ possible choices, given a sequence of leaves $\{x_{i}^{\mu}\}$ generated with $\ell=4$. The model is trained on $\mu=1,\dots,P$ labeled samples, generated with variable filtering parameters, and tested on the full hierarchy. From top to bottom, the four curves correspond to training with $k=0,1,2,3,4$. Dashed lines indicate, for each $k$, the accuracy computed with the belief propagation algorithm of the $k$-filtered trees (Bayes optimal for $k=0$ only). (c) Learning dynamics of a transformer with identical parameters on a masked language modeling task with a single hidden symbol, trained on $P=2^{18}$ sequences generated with the full hierarchy ($k=0$) and tested on samples generated by a model with $k\geq 0$ filtered layers (same color code as in (b)). (d) Validation accuracy, as in (b), now compared with the performance of a model where the attention layers have been pre-trained in the task of (c) and fine-tuned on a labeled training set of size $P$.
Refer to caption: x1.png

### 1.1 Our contributions

Our contributions can be summarized as follows:

- •
We introduce the *filtered hierarchical model*, a tree-based generative model of sequences with a tunable level of hierarchical correlations. An illustration is provided in Fig. 1(a). The filtering procedure, based on the factorization of the highest levels of the tree, keeps the correlations within the sub-trees while destroying the global correlations. It is thus analogous to a high-pass filter — using a signal processing terminology — in this hierarchical context.
- •
We train vanilla transformers on two tasks: the supervised classification task of inferring the root of the tree given a sequence of leaves, and the self-supervised Masked Language Modeling (MLM) task of inferring a masked symbol among the leaves. In both cases, we find that a well-trained transformer network replicates the BP predictions both on the training data distribution and on out-of-sample filtered hierarchical data, i.e. when trained on truncated hierarchies and tested on the full hierarchy or vice versa as shown in Figs. 1(b)-(c). This provides new evidence that transformers implement
optimal inference in these two tasks,
- •
In the case of Masked Language Modelling, we observe a clear pattern of hierarchical learning in time shown in Fig. 1(c): the top curve, which gives the dynamics of the generalization when training and testing our model on the full hierarchy ($k=0$) shows a couple of plateaux that correspond to the accuracy obtained when training on the full hierarchy and testing on partial hierarchies with $k=1$ and $k=2$,
showing that the small-scale correlations are learned first, then the next scale, etc.,
- •
We provide direct evidence that transformers build up a hidden representation of the tree by two approaches: 1) We show that the structure of attention matrices obtained after unsupervised MLM training reflects the hierarchical correlations of the data Fig. 4; 2) We verify that this is indeed the case with probing experiments on the successive attention layers.
- •
We confirm this evidence by showcasing the effectiveness of pre-training on the unsupervised task before training for root classification on labeled data, as illustrated in Fig. 1(d).
- •
We prove that a single-head encoder-only transformer architecture, with a number of transformer blocks equal to the depth of the hierarchical model, is sufficient to embed the Belief Propagation message-passing algorithm performing exact inference in the full hierarchical model.

## 2 A model with filtered hierarchical correlations

### 2.1 The full hierarchical model

Inspired by recent work (De Giuli (); Tomasini & Wyart ()), we consider a tree-like generative process capable of producing structured discrete-valued sequences. Choosing a tree topology for the generative process is key to allowing direct control over the effective range of the hierarchical correlations induced in the generated sequences, and at the same time enable
exact and efficient inference through message-passing.

The “full” hierarchical generative process shown in the first row of Fig 1(a) can be described as follows. The chain starts from an initial symbol $x_{0}$, which we will refer to as the root of the tree, sampled with probability ${\bm{p}}_{0}$ from a vocabulary $\mathcal{X}=\{1,\dots,q\}$. Then, the first layer of the tree is drawn randomly using a transition tensor ${\bm{\mathsfit{M}}}$, which assigns the probability of generating some children — from the same vocabulary $\mathcal{X}$ — given a parent (here $x_{0}$). In this work, we will restrict ourselves to binary trees for simplicity, but the generative process can be immediately generalized to arbitrary numbers of children. We therefore have ${\bm{\mathsfit{M}}}\in\mathbb{R}_{+}^{q\times q\times q}$, with ${\mathsfit{M}}_{abc}$ the probability of generating the pair $(b,c)$ given a parent $a$. Since its elements are transition probabilities, this tensor should of course satisfy ${\mathsfit{M}}_{abc}\in[0,1]\;\forall\,a,b,c$ and $\sum_{bc}{\mathsfit{M}}_{abc}=1\;\forall\,a$. The process, with the same tensor ${\bm{\mathsfit{M}}}$, is then repeated independently for each of the newly created children nodes for a total of $\ell$ generations, eventually yielding a sequence of $2^{\ell}$ integers $\{x_{i}\}_{i=1,\dots,2^{\ell}}$. We will naturally refer to the symbols in the sequence as the leaves of the generative tree.

The transition tensor ${\bm{\mathsfit{M}}}$ — the “grammar” of our generative model in CFG terminology — fully controls the properties of the above-defined generative process. To explicitly control some of these properties, it is useful to define a parametrized ensemble of random grammars, from which multiple transition tensors can be sampled independently. Two grammars generated with the same parameters are expected to share some high-level features and produce data of comparable complexity, at least in the large vocabulary size limit. Elaborating on recent work on context-free grammars (see Sec. 2.4.2 below), we generate the transition tensors ${\bm{\mathsfit{M}}}$ following a softmax procedure

$$ ${\mathsfit{M}}_{abc}=\frac{\mathrm{e}^{h_{abc}}}{\sum_{b^{\prime}c^{\prime}} \mathrm{e}^{h_{ab^{\prime}c^{\prime}}}}$ (1) $$

where the logits $h_{abc}$ are generated as

$$ $h_{abc}=\begin{cases}\sigma\xi_{abc}\quad&\text{if}\quad(b,c)\in\mathcal{O}_{a },\\ -\infty\quad&\text{otherwise},\end{cases}$ (2) $$

with $\xi_{abc}$ independent Gaussian random variables of zero mean and unit variance, and $\sigma$ controlling the probability fluctuations between likely and unlikely transitions. Here, the $q$ sets $\mathcal{O}_{a}$ build a equal-sized partition of the $q^{2}$ possible children pairs $(b,c)$, i.e. $\mathcal{O}_{a}\cap\mathcal{O}_{a^{\prime}}=\emptyset$ if $a\neq a^{\prime}$ and $|\cup_{a}O_{a}|=q^{2}$. This non-overlapping prescription implies that the broadcast from the root to the leaves has no ambiguity. Therefore, if the transition tensor ${\bm{\mathsfit{M}}}$ is known, given a set of leaves one can deterministically go up the hierarchy of the tree and infer the root. An interesting generalization would be to allow for some non-zero transition probabilities on the entries outside of $\mathcal{O}_{a}$; we leave the exploration of this setting for future work.

### 2.2 Filtering hierarchical correlations

To probe the correspondence between what can be learned by a transformer network and the optimal inference performance in our setting, we develop a filtering tool that enables control over the correlation structure in the generated sequences. In particular, we consider a family of generative models, indexed by an integer $k\leq\ell$, with hierarchical correlations truncated at a given depth $k$ of the tree.

In the “full hierarchical” case described in the previous paragraph, corresponding to $k=0$, all children generated at any level of the tree are sampled in pairs from their respective parents and are strongly correlated. When $k>0$, we instead generate the tree by drawing the children at level $k$ conditionally independently given the root, with the same marginals as the full model. Then, for layers below layer $k$, the generative process is the standard one described above. This filtering procedure is illustrated in Fig. 1(a), in which dashed segments indicate conditional independence.

In order to match the correct marginal probabilities in the truncated models, the conditional independent sampling at level $k$ is done as follows. For each of the $2^{k}$ variables at level $k$, say $x_{j}$,(^1^11Here we take $j>2^{\ell}$ to refer to the internal nodes of the tree, while $x_{0}$ remains the root and $x_{i}$ with $i=1,\dots,2^{\ell}$ are the leaves.) one considers the unique path that relates the root to this intermediate child in the original fully hierachical tree, yielding a probability

$$ $P\left(x_{j}=b\mid x_{0}=a\right)=\left({\bm{p}}_{0}{\bm{M}}^{\sigma_{0}(j)}{ \bm{M}}^{\sigma_{1}(j)}\dots\bm{M}^{\sigma_{k-1}(j)}\right)_{a,b},$ (3) $$

with $\sigma_{m}(j)\in\{L,R\}$ indicating whether the path leading to the tree element $j$ considered at layer $k$ takes a left or right branching at the previous layer $m$. The $q\times q$ transition matrices ${\bm{M}}^{L}$ and ${\bm{M}}^{R}$ are computed by tracing the original tensor

$$ ${M}^{L}_{ab}=\sum_{c}{\mathsfit{M}}_{abc},\qquad{M}^{R}_{ac}=\sum_{b}{ \mathsfit{M}}_{abc},$ (4) $$

while ${\bm{p}}_{0}\in\mathbb{R}_{+}^{q}$ is the vector of probabilities for the root symbol. By constructing our filtered trees in such a way, we ensure that the conditional correlations measured on the leaves capture up to the $k$^th level of the hierarchy.

### 2.3 Summary of the filtered-hierarchical-correlations model

The meta parameters defining the generative model are the vocabulary size $q$, the depth $\ell$, and the variance $\sigma$ of the normal variables $h_{abc}$. We set $q=4$, $\sigma=1$ and $\ell=4$ in the experiments below.
One instance of the filtered-hierarchical-correlations model is then defined by:

- •
The distribution $P(x_{0}=a)=p_{0}^{a}$ of the value of the root. For simplicity, it will be assumed uniform in the following.
- •
The children pair partition $\{\mathcal{O}_{a}\}$.
- •
The $q^{2}$ normal variables $\xi_{abc}$, from which the transition probabilities are produced through a softmax operation. (1).

The model can then be filtered up to level $k\in\{0,...,\ell\}$.
When $k=0$, we have no filtering: the pure model is kept with all its correlations; when $k\geq 1$, conditionally to the root value, our filtering procedure keeps only the correlations within the blocks of variables found by cutting the tree at level $k$.

### 2.4 Related models

The generative model presented above is next of kin to other prescriptions proposed in related work. Let us summarize the similarities and differences between our hierarchical construction and existing ones.

#### 2.4.1 Broadcast on trees

Considering the fully hierarchical version of our model ($k=0$), taking a “unit rank” structure for our transition tensor ${\mathsfit{M}}_{abc}={T}_{ab}{T}_{ac}$ amounts to generating the two children from a parent independently and with a unique transition matrix ${\bm{T}}$. In this special limit, our generative process corresponds to the well-known problem of broadcast on a tree. For instance, transition matrices of the form $T_{ab}=(1-\epsilon)\delta_{a,b}+\frac{\epsilon}{q-1}(1-\delta_{a,b})$ have received much attention, see Mossel & Peres () and Mézard & Montanari ().

#### 2.4.2 Context-free grammars

Being a tree-based generative process, our hierarchical model can be considered an instance of a simple (probabilistic) context-free grammar (CFG). Indeed, standard CFGs, which have been introduced in linguistics as the simplest generative processes supporting a hierarchical structure, typically include two distinct sets of symbols that are non-terminal and terminal, representing parts of speech — i.e. nouns, verbs etc. — and actual words respectively, plus a root symbol, instead of the single vocabulary $\mathcal{X}$ that we resort to. As a result of the notion of terminal symbols, the “parsing trees” underlying CFGs are also not fixed, and branches may be interrupted at different levels, leading to a more complex correlation structure. Our model is thus a simplification of random context-free grammars (De Giuli, ; ; Lalegani & De Giuli, ). Note that the special case where every entry of the transition tensor is non-zero and log-normally distributed was previously considered in De Giuli ().

#### 2.4.3 The Random Hierarchy Model

Being a simple probabilistic CFG on a fixed binary tree, our model is closely related to the recently introduced Random Hierarchy Model
of Cagnetta et al. (). This model, which has successfully been used to improve the understanding of the effect of hierarchy on generative diffusion (Sclocchi et al., ) or last token prediction (Cagnetta & Wyart, ), essentially relies on uniformly picking one of up to $q$ possible and non-overlapping pairs of children rather than having a possibly heterogeneous transition tensor as in our case. If the Random Hierarchy Model is parameterized to have $q$ possible pairs of children at every layer, it becomes identical to our model with $\sigma=0$ and $k=0$ factorized layers. This “full” case, in which all sequences are equiprobable, is quite limiting as it prevents self-supervised token predictions on the sequences and the MLM task becomes infeasible. Correlations between the leaves can arise in the Random Hierarchy Model when some children pairs cannot be produced, leading to a reduced entropy of viable sequences. Instead, our model requires $\sigma>0$, leading to significant differences in the correlation structure. One should also notice that the staircase decrease of the correlations as a function of the distance between leaves presented in Cagnetta & Wyart () is not visible in our case.

Finally, an important novelty in our approach lies in the proposed filtering procedure. As we shall see, the filtered-hierarchical-correlations model offers a valuable probe for analyzing and interpreting the strategy implemented by trained transformer architectures.

### 2.5 Exact inference

Figure: Figure 2: Illustration of the two types of BP updates: (a) above the filtration level $k$ and (b) below it.
Refer to caption: x2.png

A key advantage of generating our sequences using a tree-like process is that we can perform exact inference efficiently with message-passing algorithms. Using Belief Propagation (Mézard & Montanari, ), and assuming that the transition tensor ${\bm{\mathsfit{M}}}$ and root probabilities ${\bm{p}}_{0}$ are known, one can calculate the exact marginal probabilities for all the symbols at any position in the tree, with a linear computational cost in the size of the tree. This procedure can be used to infer the root given the leaves and to find the most likely value of a masked leaf (or set of leaves) given the rest of the sequence. Without going into detail on the derivation, let us describe the standard scheme to perform exact inference on the tree using BP.

We start by randomly initializing an upgoing and downgoing message — that are vectors in $\mathbb{R}^{q}$ representing a probability distribution over the $q$ possible symbols — for each edge in the generative tree. In the following, we denote with $\nu_{j\to\alpha}$ a message going from a so-called variable node $j$ (represented by a circle in the sketches) to a factor node $\alpha$ (represented by a full or empty square in the sketches), and with $\hat{\nu}_{\alpha\to j}$ the message in the opposite direction. Wherever there is a known variable one should then fix $\nu_{j\to\alpha}[x_{j}]=\delta_{x_{j},a}$, where $a$ is the known value e.g. of the leaf.

When the hierarchy is truncated, two distinct types of updates are possible, depending on whether one lies in the filtered or unfiltered regions of the tree. In the former, the root is directly connected to $2^{k}$ “empty” factor nodes, as shown in Fig. 2(a), each connected to a single and distinct variable node below. In this case the BP fixed point equations for messages from the root to the empty factor are given by

$$ $\nu_{0\to\alpha_{j}}[x_{0}]\propto\prod_{\ell\neq j}\hat{\nu}_{\alpha_{\ell} \to 0}[x_{0}],$ (5) $$

i.e. outgoing messages are simply a product of the incoming messages from all the other edges. At each of the $2^{k}$ factor nodes, both upgoing and downgoing messages satisfy

$$ $\hat{\nu}_{\alpha_{j}\to 0}[x_{0}]\propto\sum_{x_{j}}P(x_{j}\mid x_{0})\nu_{j \to\alpha_{j}}[x_{j}],\qquad\hat{\nu}_{\alpha_{j}\to j}[x_{j}]\propto\sum_{x_{ 0}}P(x_{j}\mid x_{0})\nu_{0\to\alpha_{j}}[x_{0}],$ (6) $$

where $P(x_{j}\mid x_{0})$ is given by equation 3, and is, in general, unique to the factor node considered. The notation $\propto$ means that the messages — which represent probabilities — should be normalized (e.g. $\sum_{x_{0}}\hat{\nu}_{\alpha_{j}\to 0}[x_{0}]=1$).

We now consider the lower, unfiltered part of the tree. As illustrated in Fig. 2(b), each of the “full” factor nodes is connected to three variable nodes, representing the parent and two children in the standard branching process. Each of the outgoing messages from the factor node should satisfy

$$ $\hat{\nu}_{\alpha\to u}[x_{u}]\propto\sum_{x_{l},x_{r}}{\mathsfit{M}}_{x_{u}x_ {\ell}x_{r}}\nu_{l\to\alpha}[x_{l}]\nu_{r\to\alpha}[x_{r}].$ (7) $$

For all variable nodes except for the root detailed above, the single outgoing messages are equal to the single incoming messages in these variable nodes at the previous/next layer of the tree. For example, the upgoing messages $\nu_{1\to\alpha_{1}}$ in Fig. 2(a) is simply $\hat{\nu}_{\alpha\to 1}$, where $\alpha$ is the “full” factor node lying below variable $1$ (assuming $k<\ell$).

Efficient convergence to the fixed point is guaranteed if one starts from the leaves and updates the messages in an upgoing pass, and then performs a downgoing pass from the root, for a total of $2(\ell-k+1)$ steps. Once the messages have converged, any unknown variable can be optimally reconstructed by computing the marginals as

$$ $\mu[x_{i}]\propto\prod_{\alpha\in\partial i}\hat{\nu}_{\alpha\to i}[x_{i}],$ (8) $$

where $\partial i$ is the set of factor nodes connected to variable node $i$. In our problem, this product will therefore typically be over a single factor node when inferring masked leaves, or $2^{k}$ factor nodes when inferring the root.

## 3 Going up the hierarchy with transformers

### 3.1 Experimental setup

To be as close as possible to real-world applications, we will focus on the encoder-only variant (Devlin et al., ) of the celebrated “vanilla” transformer architecture, introduced in Vaswani et al. (). Below we provide a short recap of this parametrization.

Given a sequence of leaves $\{x_{i}\}$ generated by the hierarchical model, represented by $2^{\ell}$ integers, we convert it to a sequence of one-hot vectors $\{{\bm{x}}_{i}\}$, with ${\bm{x}}_{i}\in\mathbb{B}^{q}$. (^2^22For simplicity, the procedure described here does not consider the inclusion of special tokens. In practice, we will take a vocabulary of size $q+1$ to account for masked symbols when doing the Masked Language Modeling, see Devlin et al. ().) Then, we perform the first encoding step producing a sequence of tokens $\{{\bm{x}}_{i}^{(0)}\}\in\mathbb{R}^{d}$, with arbitrary dimension $d\geq q$, obtained through a learnable projection to the embedding space and the inclusion of positional encoding ${\bm{p}}_{i}$,

$$ ${\bm{x}}_{i}^{(0)}={\bm{W}}_{E}{\bm{x}}_{i}+{\bm{p}}_{i},$ (9) $$

with
${\bm{W}}_{E}\in\mathbb{R}^{d\times q}$
and ${\bm{p}}_{i}\in\mathbb{R}^{d}$. For our experiments, we consider $d=128$ and the standard sinusoidal positional encoding of Vaswani et al. ().

Each transformer block in the network then maps the previous encoded sequence onto a new sequence of tokens with the same length and embedding dimension, through a concatenation of a self-attention layer and a fully connected layer, with residual connections and layer normalization operations

$$ $\tilde{{\bm{x}}}_{i}^{(l)}=\mathrm{layernorm}\left({\bm{x}}_{i}^{(l-1)}+ \mathrm{selfattention}({\bm{x}}^{(l-1)};{\bm{W}}_{Q}^{(l)},{\bm{W}}_{K}^{(l)}, {\bm{W}}_{V}^{(l)})\right),$ (10) $$

$$ ${\bm{x}}_{i}^{(l)}=\mathrm{layernorm}\left(\tilde{{\bm{x}}}_{i}^{(l)}+\mathrm{ FC}(\tilde{{\bm{x}}}_{i}^{(l)};{\bm{W}}_{1}^{(l)},{\bm{W}}_{2}^{(l)})\right).$ (11) $$

The single-head self-attention layer considered in this work entails the computation of three different quantities from each token: the query ${\bm{q}}_{i}={\bm{W}}_{Q}{\bm{x}}_{i}$, the key ${\bm{k}}_{i}={\bm{W}}_{K}{\bm{x}}_{i}$ and the value ${\bm{v}}_{i}={\bm{W}}_{V}{\bm{x}}_{i}$.
For simplicity, we take ${\bm{W}}_{Q}$, ${\bm{W}}_{K}$ and ${\bm{W}}_{V}$ in $\mathbb{R}^{d\times d}$. The queries and keys are combined in what will be referred to as the attention matrix $A\in\mathbb{R}^{2^{\ell}\times 2^{\ell}}$,

$$ ${A}_{ij}=\mathrm{softmax}\left(\frac{{\bm{q}}_{i}\cdot{\bm{k}}_{j}}{\sqrt{d}} \right),$ (12) $$

then used to build a linear combination of the values,

$$ $\mathrm{selfattention}({\bm{x}};{\bm{W}}_{Q},{\bm{W}}_{K},{\bm{W}}_{V})=\sum_{ j=1}^{2^{\ell}}A_{ij}{\bm{v}}_{j}.$ (13) $$

The fully-connected layer, instead, is a standard $2$-layer network with $\mathrm{relu}$ activations:

$$ $\mathrm{FC}({\bm{x}}_{i};{\bm{W}}_{1},{\bm{W}}_{2})={\bm{W}}_{2}\,\mathrm{relu }\left({\bm{W}}_{1}{\bm{x}}_{i}\right),$ (14) $$

where
${\bm{W}}_{1}\in\mathbb{R}^{d\times d^{\prime}}$, ${\bm{W}}_{2}\in\mathbb{R}^{d^{\prime}\times d}$, and
$d^{\prime}=2048$ in our experiments.
We refer the reader to the original paper by Vaswani et al. () for additional details on the transformer encoder operations.

Following these operations, repeated $n_{L}$ times to obtain the full encoder, we obtain a position-dependent high-dimensional representation of each of the original symbols in the sequence. What is finally done with this sequence of tokens depends on the task at hand.

### 3.2 Supervised classification

An important inference task in the context of CFGs is the so-called parsing problem, where one seeks to reconstruct the topology and the hidden symbols of the generative tree given the sequence. In the Full Hierarchical Model, parsing is greatly simplified by the fixed topology (a full tree) and a natural idea is to focus on the inference of the root $x_{0}$. The root can be seen as a label for the generated sequence $\{x_{i}\}$ used to train a neural network on the associated classification task. A similar setting has been studied in the Random Hierarchy Model (Cagnetta et al., ), to show that CNNs naturally implement the hierarchy of the data model, and allow for a drastic reduction in the sample complexity when the number of layers in these deep neural networks is greater or equal to $\ell$.

We here consider supervised training of an encoder-only transformer, given a dataset of $P$ labeled sequences. To perform the root prediction on top of the transformer encoding, the tokens in the final layer are concatenated position-wise (forming a large $d\times 2^{\ell}$ vector) and fed to a linear readout, which outputs the logits associated with the possible root symbols. The network is trained by minimizing the cross-entropy loss between these logits and the correct one-hot encoding of the root.

Optimal test accuracy. We find that, given sufficient labeled data $P\geq P^{*}$ and including enough transformer blocks in the architecture, multi-layer transformers achieve optimal root classification accuracy in the Full hierarchical Model. For a given level of factorization $k$ on the training set, the networks approach the optimal accuracy predicted by Belief Propagation on a test set with the same level of factorization. Notice that, while in the case $k=0$ the optimal algorithm finds the value of the root with certainty, this is no longer the case for $k\geq 1$, see Fig. 6 in Appendix B.1.

Required amount of data. The required amount of data $P^{*}$ for reaching optimal accuracy not only depends on the specific transition tensor ${\bm{\mathsfit{M}}}$, but also on the level of factorization. For intermediate values of $k$, $P^{*}$ is notably larger than with the $k=0$ full hierarchy. We interpret this as a consequence of the weaker correlations between distant tokens — and therefore the lower signal-to-noise ratio during learning — that must be resolved to match the BP prediction. Different from the Random Hierarchy Model of Cagnetta et al. (), characterizing analytically the scaling of $P^{*}$ with the parameters of the grammar, at finite $\sigma$, is a challenging goal, and is left for future work.

Required number of layers. Establishing a relation between the number of encoder layers $n_{L}$ in the transformer and the ability to achieve this optimal classification on data generated from the Full Hierarchical Model is also not straightforward. Indeed, given the concatenation of operations involved in a single transformer block and the presence of residual and normalization layers, the effective number of computational layers in a transformer is not as explicit as in a multilayer perceptron or a CNN architecture. As we will see below, setting $n_{L}=\ell$ — or $n_{L}\geq\ell-k$ for filtered data — enables the transformer to converge towards a more interpretable parameter configuration. However, this natural choice seems unnecessary for the neural networks to reach perfect accuracy with a comparable number of samples in this classification task (see Appendix B.2). For instance, in the case of fully filtered correlations $k=\ell$, the BP marginals for the root are just products of conditional probabilities on the leaves,

$$ $P(x_{0}=a\mid\{x_{i}\})\propto\prod_{i=1}^{2^{\ell}}P(x_{i}\mid x_{0}=a),$ (15) $$

i.e. a “Naive Bayes classifier” is optimal. Any layer of attention is thus superfluous since a standard feed-forward network with a single hidden layer is sufficient for this task. In the following, in the interest of allowing an intuitive interpretation of the attention maps, we focus on the case where the number of encoder layers equals the depth of the generative tree, $n_{L}=\ell$ (regardless of $k$), and refer to Appendix B.2 for experiments with $n_{L}<\ell$.

Out-of-sample testing. In the Full Hierarchical Model, one can also test all the trained models on out-of-sample values of the filtering parameter $k$. For example, one can test models trained on intermediate filtered data, with $k>0$, on the full hierarchical test dataset, with $k=0$, as illustrated in Fig. 1(c). Also in this experiment, the transformers achieve a performance that exactly matches that of the BP algorithm in the presence of the same mismatch between the assumed inference graph and the data generative graph. We stress that, in this mismatched task, the BP prediction is no longer optimal a priori, yet the trained networks appear to systematically reach the same accuracy. This observation provides the first piece of evidence that the transformers are implementing an approximation of the Belief Propagation algorithm.

Figure: Figure 3: Evolution of the root prediction accuracy computed on filtered validation datasets, with $k=0,1,2,3,4$ (from top to bottom), for a model trained on $k=0$ data and $P=2^{17}$, $\ell=4$, $q=4$. The dashed lines represent the out-of-sample BP prediction.
Refer to caption: x3.png

Supervised learning dynamics. Looking more specifically at the learning dynamics of a network trained on the full hierarchy sheds some light on the learning mechanism implemented by the transformer encoder. Fig. 3 shows the evolution of the validation accuracy not only on data generated in the same way as the training set with $k=0$, but also on filtered datasets with $k>0$. One can notice multiple stages in the learning procedure: in the first epochs, the prediction accuracy increases for all values of $k$, illustrating that the network first naturally resolves the leaf-to-root correlations — aided by the label —, as well as the short-range correlations between the leaves. As time progresses and longer-range correlations are implemented, the accuracy on the most filtered datasets eventually drops towards the sub-optimal mismatched-BP prediction, while the accuracy for the smallest values of $k$ keeps increasing. Eventually, and in a limited number of epochs, all levels of factorization reach the BP accuracy as the network has perfectly learned to infer the root and therefore uses the full correlation structure, which is however correct only for $k=0$.
This picture is consistent with the idea of the network implementing BP, and doing so by starting at the bottom of the tree and progressively reconstructing the generative layers going up to the root.

In the root inference task, the readout performing the root prediction is fed with the entire sequence of tokens. As a result, there are likely many ways for the transformer encoder to distribute the computation across its layers, and no necessity for single tokens to carry information on all the layers of the tree. This rather unconstrained context is notably visible in the attention maps of the trained networks shown in Appendix B.3. While the patterns that appear in the attention matrices of the different layers are somewhat related to the hierarchical nature of the data model, there is no clear one-to-one correspondence between the levels of factorization and the successive layers of attention. As we will see below, going to a task requiring more expressivity to the single tokens allows for a clearer interpretation of the role of transformer encoder layers.

### 3.3 Masked Language Modeling

We now turn to self-supervised training, where the model learns from a dataset of $P$ unlabeled sequences. In simple terms, the Masked Language Modeling (MLM) training procedure consists of randomly masking parts of the sequences and asking the model to recover them from the context. This is closer to what is done in practice to train large language models (see for example Devlin et al. () and Liu et al. ()).
While in principle one could mask several symbols simultaneously in training, we focus on single-symbol masking in the following, given the limited length of our sequences (a single symbol representing already 6.25% of the sequence for $\ell=4$).
Contrary to the root inference task, in MLM perfect accuracy cannot be achieved even in the fully hierarchical case, because of the stochastic nature of the branching process in the generative tree.
The optimal performance is still yielded by BP, which propagates the leaf information throughout the tree in the form of messages.

To reconstruct the masked symbol, we now feed a single token, selected from the final transformer encoding at the positions associated with the masked element, to a linear layer producing a vector of logits. The network is then trained by minimizing the cross-entropy loss between these logits and the correct one-hot encoding of the masked element in the sequence.

Optimal reconstruction performance. Given sufficient layers and data, we find that transformers can again achieve optimal validation accuracy on all the levels of filtering for this task. Consistent with intuition, the amount of data $P^{*}$ required to match the optimal prediction computed with BP is increased relative to the supervised task, as the network must learn to resolve the weak long-range correlations in the sequence without any supervised signal from the top of the hierarchy. Notably, compared to root classification, the networks trained for masked language modeling require much longer training (typically $\sim 10^{3}$ epochs in place of a mere $\sim 10$ epochs for classification, see Fig. 1(c) vs Fig. 3) to approach optimal performance.

Figure: Figure 4: Visualization of the $n_{L}=4$ attention matrices (averaged over $10^{4}$ input sequences) for transformers trained on the Masked Language Modeling task on different filtered datasets, with $k=0,1,2,3,4$ (top to bottom rows), and $P=2^{18}$, $\ell=4$, $q=4$. For the fully factorized model with $k=4$, the one with the smallest correlations (the leaves are independent conditional to the root) the attention matrix appears structureless. When $k$ decreases one sees the emergence of markedly non-zero attention blocks of size $\leq\sim 2^{\ell-k}$. For $k=0$ and $k=1$, the trained attention matrices reflect all the hierarchies of the correlations.
Refer to caption: x4.png

Self-supervised learning dynamics. The modularity of the filtering, allowing one to probe the model on related but out-of-sample data, can be exploited to unveil the sequential nature of the MLM learning process. Computing the validation accuracy on all $k$ levels throughout the training dynamics, we observe a clean “staircase” behavior, as shown in Fig. 1(c). This picture confirms and clarifies the experiments in Fig. 3, showing that the network sequentially resolves the nested levels of the hierarchy, in a bottom-up order.
Note that the observation of the shorter-range correlations being learned first is consistent with the signal-to-noise picture exposed in Cagnetta & Wyart ().
Moreover, the presence of a sequential mechanism of discovery and resolution of different moments of the data distribution has been studied in Refinetti et al. (); Rende et al. ().
Overall, the convergence of the transformer to both the in-sample and the out-of-sample token prediction accuracy of BP supports the claim that the transformer learns to implement a close approximation of the exact message-passing algorithm.

Attention map analysis. In the MLM task, single token encodings are read for predicting the masked symbols. This seems to guide the model towards more interpretable attention maps, shedding some light on how the model may implement the BP algorithm. They are shown in Fig. 4, for networks trained on all filtering levels.

In the fully filtered case (bottom-most row) there is no need to combine the different elements of the sequence before the readout, therefore the attention matrices are nearly uniform, their only purpose being to yield tokens containing information on all other leaves. In such a simple case, a single layer of attention in the transformer would likely be sufficient. Now, as we reduce the level of filtering in the generative process, clear patterns emerge in the attention map. First, the model focuses on short-ranged correlations between nearest neighbors when $k=3$ and, as we decrease $k$, towards patterns of size $\sim 2^{\ell-k}$. Given the recombination allowed by the readout layer, it would appear that having $n_{L}=\ell-1$ layers is sufficient to allow for a rather transparent implementation. Note that the similarity between the $k=1$ and $k=0$ cases (top two rows) is rather natural: as visible in Fig. 1(a), the tree topology in these two cases is identical and only the effective transition tensor for this first layer is different.

Interestingly, the network naturally organizes the attention layers hierarchically. This is particularly visible when there are fewer redundant layers i.e. in
the cases $k=0,1$ (two top rows in Fig. 4). Such a layout is consistent with the BP algorithm on the full tree, where one combines elements pairwise while going up the tree. While a typical BP implementation includes a downward pass,
it is possible to avoid this step if the token embedding dimension, $d$, is sufficiently large.
To illustrate this point, we propose an existence proof of a plausible implementation of the BP algorithm in an architecture like the one we are considering in section 3.5 and in Appendix C.

Figure: Figure 5: Validation accuracy in the ancestor prediction task (in our convention layer 0 is the root) with $\ell=4$, $q=4$. (Left) Predictions obtained by reading the intermediate transformer encoding levels of a model pre-trained on the full hierarchy ($k=0$), using the level 4 encoding (circles), 3 (squares), 2 (diamonds) and 1 (triangles). (Right) Predictions obtained by reading the last layer transformer encoding of models pre-trained on filtered data with $k=1,2,3,4$ (from light green to dark purple).
Refer to caption: x5.png

Probing the encoder representations. To confirm that the computation going up the tree is distributed sequentially in the transformer blocks, we undertake a probing experiment similar to those performed in natural language processing Zhao et al. ().
First, we analyze the encoder trained on the full hierarchy, i.e. the $k=0$ case from the experiment in Fig. 4.
While keeping the encoder weights fixed, we attach two-layer readouts (with 64 hidden units) after each of the $4$ blocks of the encoder, trained to extract information about the ancestors of the leaves from the tokens in the corresponding positions.
As shown in the left panel of Fig. 5, the prediction accuracy is high on the ancestors up to one level above
the probed representation, deteriorating on higher levels of ancestry. Note that the additional level is resolved because the two-layer read-out can learn to perform the computation to climb up one layer of the hierarchy.

As a means of comparison, in the right panel of Fig. 5 we conduct a similar probing experiment attaching read-outs to the last encoder layer of the models trained with $k>0$ data, from the experiment in Fig. 4. In this case, the token representations should not provide accurate information about ancestors past the last hierarchy level passed by the hierarchical filtering. We find qualitative agreement between the corresponding curves in the two panels, as expected from a direct comparison of the attention matrices. For example, the ancestor prediction performance from the second layer encoding of the $k=0$ case (first-row attention maps in Fig. 4) is compatible with that on the final encoding of the $k=2$ case (third row in Fig. 4). This finding supports the claim that the increasing levels in the hierarchy are learned sequentially in the transformer blocks, consistent with a natural implementation of the BP algorithm.

### 3.4 Synergy between tasks and MLM pre-training

The probing experiment in Fig. 5 shows that the MLM training produces transformer representations that carry information about the leaf ancestors, including the very root of the tree. Importantly, this procedure does not require supervised information. A natural question is thus whether MLM can be used effectively as a pre-training step to reduce the labeled data required to achieve optimal root inference, i.e. reduce $P^{*}$ in section 3.2. Note that, compared to the previously presented probing experiment, we now attach a linear read-out to the concatenated representation of all the tokens (as in Sec. 3.2), helping toward a more robust root prediction. In Fig. 1(d), we compare the accuracy as a function of the dataset size for supervised training from scratch and starting from an MLM pre-trained configuration.
As expected, and in line with many empirical observations on real-world data, the pre-training allows a large speed-up in the supervised training process.

### 3.5 Exact transformer embedding of BP

In a natural implementation of BP, inference in the MLM task requires the messages from the visible leaves to reach the top of the hierarchy, and descend from the root down to the masked symbol, effectively propagating through $2\ell$ layers. Moreover, a proposal in Zhao et al. () for a transformer embedding of the inside-outside parsing algorithm —which can be seen as a generalization of the above-described BP to the unknown topology setting— requires as many transformer blocks as double the sequence length (here $2^{\ell}$), and an attention head per hidden symbol in the hierarchy. Thus, it might seem surprising that a single-head transformer encoder with $\ell$ blocks could be sufficient to mimic the BP algorithm. To prove the feasibility of its implementation within these architectural constraints, we propose an exact implementation of the BP algorithm through the transformer computation. For brevity’s sake, the full existence argument is deferred to Appendix C, while here we provide a high-level description of some key ideas.

We consider a fully disentangled embedding of positional and semantic information in the vectorized tokens, contained in $d=q(q+2)+\ell$ dimensions. The isolation of the semantic information allows the implementation of a simple position-based attention mechanism, inspired by the factor graph structure, and compatible with the attention matrices in Fig. 4. Then, going up the hierarchy requires the computation of a trace of products (see equation 7), which can be well approximated by the fully connected layers in the second part of the transformer blocks, provided the attention selects the right terms in the product.

The less intuitive component of the implementation is the computation of the messages directed towards the leaves, used in the MLM task. Given the limit on the number of transformer blocks, this computation must be done in parallel with the upward climb of the hierarchy, despite the missing downward messages. It turns out that, by exploiting $\mathcal{O}(q^{2})$ memory slots in the token embedding — thus with an increased memory cost compared to BP —, a different recursion with the same result as the standard message-passing can be implemented, using only $n_{L}=\ell$ layers of transformers. The details are reported in Appendix C.

## 4 Conclusions

By using a simple model of structured data, namely a tuneable high-pass filtering procedure on a tree-based generative model,
we were able to shed some light on the inner workings of transformer encoders. We found evidence that transformers trained on both supervised and self-supervised tasks implement some version of Belief Propagation, which is exact for inference on trees. Interestingly,
the implementation is constructed sequentially during the training dynamics, as the network progressively resolves the shorter-range correlations and progresses in the ancestry of the generative process. In the masked language modeling task,
the implementation also turns out to be remarkably interpretable, as the attention maps reflect the correlation structure of the chosen data model. We have also shown the feasibility of an exact implementation of the BP equations within the constraints of the encoder-only transformer architecture and, notably, with a number of transformer layers equal to the depth of the generative tree and a single attention head.

Relative to existing work, our results can be seen as complementary to those of Zhao et al. (); Allen-Zhu & Li (), given the close relationship between Belief Propagation on trees with fixed topology and the celebrated inside-outside algorithm for more general context-free grammars.
However, the simplicity and modularity of our data model allowed us to uncover the hierarchical nature of both the learning dynamics and the inner organization of the attention layers, and understand how a more parsimonious implementation of the exact inference algorithm can be achieved.
Understanding how to generalize to the full parsing task, where the tree topology is not fixed, and how to embed this computation within a transformer is a challenging avenue of future research.

Beyond transformers, we also believe that our filtering approach for hierarchically structured data could help address other deep-learning questions, like those on the efficiency of generative diffusion.

#### Acknowledgments

The authors are grateful to Carlo Baldassi, Luca Biggio, Dirk Hovy and Gianmarco Perrupatto for fruitful discussions.

## Appendix A Further details on numerical experiments

All numerical experiments presented in this paper were performed using PyTorch (Paszke et al., ) version 2.3.0. We use the Adam (Kingma & Ba, ) optimizer with batches of size $32$ and a fixed learning rate of $10^{-4}$, other parameters left as default. We did not find learning rate scheduling to provide significant benefits in our experiments. All models were initialized randomly using the default settings (Xavier uniform distribution).

## Appendix B Additional figures

### B.1 In-sample classification performance on filtered datasets

Figure: Figure 6: Reproduction of Fig. 1(b) with the validation accuracy computed on (in-sample) factorized data, rather than the out-of-sample testing presented in the main text.
Refer to caption: x6.png

Fig. 6 shows the validation accuracy computed in in-sample factorized datasets. The optimal inference accuracy predicted by the Belief Propagation, which is not 1 when $k>0$, is reached by the transformers.

### B.2 Influence of the number of attention layers on classification performance

Figure: Figure 7: Reproduction of Fig. 1(b) with now $n_{L}\leq 4$ attention layers in the transformer encoder and restricted to the “worst case” $k=0$ unfiltered dataset.
Refer to caption: x7.png

Fig. 7 shows the validation accuracy on the root classification task on $k=0$ unfiltered data for $1\leq n_{L}\leq 4$ attention layers in the transformer encoder. While $n_{L}=\ell=4$ is the most sample efficient, it is clear that $n_{L}=3$ provides comparable performance, and only $n_{L}=1$ appears to lead to poor sample efficiency. In all the performed experiments, a bigger value for $n_{L}$ corresponded to better sample efficiency, which seems to indicate that more flexible models require less data to reach the same performance level despite the increased number of parameters to train. As mentioned in the main text, we resort to $n_{L}=\ell$ to ease the mechanistic interpretation of a possible transformer implementation of BP.

### B.3 Classifier attention maps

Figure: Figure 8: Reproduction of Fig. 4 for the supervised task on filtered datasets of size $P=2^{17}$ for $k=0$ and $P=2^{20}$ for $k>0$.
Refer to caption: x8.png

Fig. 8 shows the attention maps resulting from the supervised training for transformers achieving the optimal performance on datasets with different filtration levels. As in the masked language modeling task, one immediately notices the emergence of blocks of size $\sim 2^{\ell-k}$. In this prescription, where tokens are not required to be fully descriptive, it is however difficult to identify a clear pattern relating to the distribution of the computation across the different layers.

## Appendix C A possible transformer implementation of Belief Propagation

Figure: Figure 9: Illustration of the upgoing messages embedded in the tokens of the transformer implementation of BP for a tree with $\ell=3$. Complementary ancestors are shown with dashed lines.
Refer to caption: x9.png

We show here how the BP algorithm for leaf inference can be implemented using $\ell$ layers of transformers with token sizes which are compatible with what is used in our experiments.
We consider the “worst case” scenario of a complete, unfiltered tree generative process of depth $\ell$.

##### Token embedding.

We propose an implementation that relies on vectorized tokens with a structure of the form

$$ ${\bm{x}}_{i}^{(m)}=\begin{bmatrix}{\bm{r}}^{(0,m)}_{i}\\ \vdots\\ {\bm{r}}^{(q,m)}_{i}\\ {\bm{m}}^{(m)}_{i}\\ \overline{{\bm{m}}}^{(m)}_{i}\\ \tilde{{\bm{p}}}_{i}\end{bmatrix},$ (16) $$

where:

- •
$i\in\{1,...,2^{\ell}\}$ is the index of a leaf
- •
$m\in\{1,...,\ell\}$ is the index of a transformer layer
- •
${\bm{r}}^{(1,m)}_{i},\dots,{\bm{r}}^{(q,m)}_{i}$ are $q$ vectors of dimension $q$ ($q^{2}$ elements in total) storing the quantities needed to compute the final leaf marginals,
- •
${\bm{m}}^{(m)}_{i}$ is a vector of size $q$ storing the up-going message for the ancestor of leaf $i$ at level $m$,
- •
$\overline{{\bm{m}}}^{(m)}_{i}$ is a vector of size $q$ storing the up-going message for the $l$^th complementary ancestor of leaf $i$, see Fig. 9,
- •
$\tilde{{\bm{p}}}_{i}$ is a $\ell$-dimensional binary vector containing positional information on the full path from root to leaf $i$ (see below).

In this prescription, the total dimension of each token is therefore $d=q^{2}+2q+\ell$.

##### Initialization.

We are going to consider the following initialization,

$$ ${\left({\bm{r}}^{(a,0)}_{i}\right)}_{b}=\frac{1}{q},\quad\forall a,b=1,\dots,q,$ (17) $$

$$ $\overline{{\bm{m}}}^{(0)}_{i}=\bm{0},$ (18) $$

while the messages ${\bm{m}}^{(0)}_{i}$ should be initialized as in the standard BP given a sequence, i.e. with a Kronecker $\delta$ for known symbols and a uniform vector for masked leaves. The positional vector $\tilde{{\bm{p}}}_{i}$ should finally be a binary $\pm 1$ vector representing the sequence of left/right turns from the root to leaf $i$ (as $\sigma$ in equation 3).

##### Attention layer.

In our implementation, the dot product

$$ $\left({\bm{W}}_{Q}^{(m)}{\bm{x}}_{i}^{(m)}\right)^{\top}\left({\bm{W}}_{K}^{(m )}{\bm{x}}_{j}^{(m)}\right)$ $$

entering the softmax and at the heart of the attention mechanism only encodes positional information; more precisely, it combines the common ancestors of tokens $i$ and $j$ down to layer $\ell-m$ of the generative tree. This can be achieved with query and key matrices such that $\left({\bm{W}}_{Q}^{(m)}\right)^{\top}{\bm{W}}_{K}^{(m)}$ has elements equal to zero except in its lower right corner of size $\ell\times\ell$ which has the following structure:

$$ $\begin{bmatrix}\beta\bm{1}_{(\ell-m-1)\times(\ell-m-1)}&0&\bm{0}\\ 0&-\beta&\bm{0}\\ \bm{0}&\bm{0}&\begin{bmatrix}\bm{0}\end{bmatrix}_{m\times m}\end{bmatrix},$ (19) $$

with $\beta\gg 1$.
Let us detail the role of this $\ell\times\ell$ sub-matrix. Its upper left terms proportional to $\beta$ will be relevant in the softmax, when $\beta\gg 1$, if they are positive, meaning these are common ancestors to tokens $i$ and $j$, and negligible if they are negative. The diagonal term proportional to $-\beta$ requires the two considered tokens to be in different positions in the sequence to contribute to the softmax, ensuring there is no influence of the messages on themselves in the following steps. Its lower right corner, which is populated by a $m\times m$ matrix of zeros, ensures that layers below $\ell-m$ in the generative tree are no longer considered.

On the other hand, the value matrix may be used to select the correct messages in the token vector, with zeros elsewhere.

As a result, the total operation amounts to averaging the message incoming from the complementary sub-tree over all the trajectories within the complementary sub-tree

$$ $\mathrm{selfattention}({\bm{x}}^{(m)};{\bm{W}}_{Q}^{(m)},{\bm{W}}_{K}^{(m)},{ \bm{W}}_{V}^{(m)})_{i}\approx\begin{bmatrix}0\\ \vdots\\ \mathbb{E}_{j\in\overline{\mathcal{S}}_{i}^{(m)}}\left[{\bm{m}}_{j}^{(m)} \right]\\ \vdots\\ 0\end{bmatrix}=\begin{bmatrix}0\\ \vdots\\ \overline{{\bm{m}}}_{i}^{(m)}\\ \vdots\\ 0\end{bmatrix},$ (20) $$

where $\overline{\mathcal{S}}_{i}^{(m)}$ is the set of tokens belonging to the complementary tree of token $i$ at layer $\ell-m$ of the generative tree. Note that in principle it is not necessary to average since all of the paths should lead to the same message from the complementary tree, however keep in mind that in practice some tokens will be masked. The averaging procedure therefore allows recovering the information (unless all of the tokens in $\overline{\mathcal{S}}_{i}^{(l)}$ happen to be masked). Thanks to the skip connections, this contribution is added to the initial token, populating the initially empty entries of these complementary messages while leaving the rest of the tokens unaffected.

##### Fully connected feedforward layer.

Following the initialization and after the attention layer, the encoded token has the correct structure of equation 16. One must now update the relevant information in order to go to the next attention layer and therefore the next layer in the generative tree. More precisely, we need to:

- •
Compute the messages of the $m+1$^th ancestor,
- •
Update the quantities needed to compute the marginal for the leaf associated with the token considered,
- •
Remove temporary or unwanted quantities stemming from the previous steps.

All of these must be done with an identical operation for all tokens as the feedforward layer is applied independently for all positions in the sequence.

The first part is to update the messages following the equivalent of equation 7,

$$ $\left({\bm{m}}_{i}^{(m+1)}\right)_{a}\propto\sum_{bc}{\mathsfit{M}}_{a\mathcal {P}_{i}(b,c)}\left({\bm{m}}_{i}^{(m)}\right)_{b}\left(\overline{{\bm{m}}}_{i}^ {(m)}\right)_{c},$ (21) $$

where $\mathcal{P}_{i}(b,c)$ is either $bc$ or $cb$ depending on the topology of the factor node at which the update takes place — a piece of information fully contained in $\tilde{{\bm{p}}}_{i}$. This type of operation should be implementable, at least approximately, by a two-layer network since it is known to be a universal approximator.

Now, we are to compute the actual leaf marginals. As mentioned in the presentation of the standard BP implementation (Sec. 2.5), the standard approach is to perform both an upwards and downwards pass, which would require $2\ell$ attention layers.

Here, we instead wish to perform the computation in $\ell$ step, as we have seen from experiments that the transformer can achieve perfect accuracy with $\ell$ attention layers and that it does not appear to use all layers when $k<\ell$. To do so, we have included the $q^{2}$ elements of ${\bm{r}}^{(l)}_{1},\dots,{\bm{r}}^{(l)}_{q}$ in the token and now show how to update these. Note that if we had $2\ell$ layers, we could instead only store $q$ quantities.

We can in particular exploit the recursion

$$ $\left({\bm{r}}_{i}^{(a,l+1)}\right)_{b}\propto\sum_{h,k}{\mathsfit{M}}_{b \mathcal{P}_{i}(h,k)}\left({\bm{r}}_{i}^{(a,l)}\right)_{h}\left(\overline{{\bm {m}}}_{i}^{(l)}\right)_{k},$ (22) $$

where we trace only over the index of the complement ancestor, and keep in memory all the separate contributions with respect to the parent index. At the last transformer layer, one can trace also over the root index, completing the recursion. Doing so in the final feedforward layer notably yields, at the end of the transformer encoder,

$$ $\sum_{b}\left({\bm{r}}_{i}^{(a,\ell)}\right)_{b}\propto\sum_{h,k}\left(\sum_{b }{\mathsfit{M}}_{b\mathcal{P}_{i}(h,k)}\right)\left({\bm{r}}_{i}^{(a,\ell-1)} \right)_{h}\left(\overline{{\bm{m}}}_{i}^{(\ell-1)}\right)_{k},$ (23) $$

which is proportional to the incoming message on the leaf and therefore to its marginal if it is to be inferred. The final linear readout may then select this relevant part of the outputted tokens to perform the masked language modelling.

##### Including intermediate layers.

In principle, one could add $q\times(\ell-1)$ new vectors entries in the token in order to store the marginals at intermediate layers. These would simply be used to store the intermediate values of the $\sum_{b}\left({\bm{r}}_{i}^{(a,l)}\right)_{b}$.

##### Accommodating for filtration.

The implementation described above considered the case of $k=0$, unfiltered generative trees, i.e. the most complex case from the BP standpoint. In the case of a dataset with filtering parameter $k$, one can adapt the implementation by taking $\ell-k$ layers. The central difference then lies in the $\ell-k$^th block, which must then combine the $2^{k}$ messages going up to the root in its feedforward layer (instead of two messages like at all other layers in the $k=0$ case).

## Peer Reviews

confidence: 3
contribution: 2
initial_rating: 5
presentation: 3
questions: W2.1: In both the supervised root prediction task and the MLM task, the authors argue that the transformer performs similar in accuracy to BP is evidence that the transformer is implementing an approximation to BP. While it is very intriguing that the accuracies are so systematically similar, there are plausible alternative explanations (that additional experiments or analysis could rule out):

W2.1.1: Similar accuracy doesn’t imply similar behavior on individual inputs. Have authors considered measuring the match between BP and transformer predictions on individual inputs? If the match is high, this would strengthen the authors’ claim that the model actually behaves like BP. 

W2.1.2: Similar behavior on individual inputs doesn’t imply similar implementation. For example, in figure 1(c), the authors show accuracy of a model trained on k=0 (and for root classification) data and evaluated on k>=0 data, whose accuracy is similar to running BP on the k=0 graph. The authors claim this is evidence in support of model having learned to implement BP. Why couldn’t the model have model the k=0 data well without implementing BP? BP on k=0 graph is optimal for k=0 data, so the transformer that was trained on lots of k=0 data would necessarily behave like BP on individual inputs, which (without necessarily implementing BP) would make similar predictions to BP on out-of-sample data as well.

W2.2 In the supervised root prediction task, the authors write that (line 314-316) “We interpret this as a consequence of the weaker correlations between distant tokens—and therefore the lower signal-to-noise ratio during learning—that must be resolved to match the BP prediction.” Since the task is to predict the root, could the authors elaborate more on why they believe the weaker correlations among **tokens within a sequence** is causing difficulty for learning? An alternative interpretation is that this is due to the weaker correlation between the **root** and the **entire sequence** of tokens.	

W3. Following up on the BP construction, could the authors elaborate more on the leaf-to-root passing? In particular, what do the $r$’s represent? It looks like all $r$’s are initialized to uniform distribution, and they each get updated with the same formula (eqn 22), so would $r^{(a,m)}_i$ ever be different from $r^{(a’,m)}_i$ for $a \neq a’$? A walkthrough of the formulas on a minimal example with small $l$, and $q$ may be helpful here.

Other: Did the authors mean to refer to figure 3 instead of 1c on lines 337? In the caption of figure 1c it says it’s for MLM instead of root classification, and the scale of the x-axis suggests it’s MLM too.
soundness: 2
strengths: 1. The work is well-placed in the context of other mechanistic interpretability work for transformers, as well as other work looking into the effect of structured data on machine learning models.
2. Understanding what strategy is learned by transformer models trained on structured distributions is an important problem.
3. The family of synthetic distributions is interesting and has a hyperparameter that allows one to control the locality of correlations between tokens in the sequence.
4. The authors present a novel construction for how to implement BP on a depth $l$ tree using only $l$ layers of a transformer, whereas previous constructions required $2l$ layers.
summary: The paper investigates how transformer models make predictions on samples coming from a structured data distribution, focusing on the hypothesis that transformers implement belief propagation to make predictions. 

Contributions:
1. The authors propose a novel family of synthetic data distributions based on PCFGs to test the hypothesis empirically. 
2. The authors present experimental results on two tasks, root prediction and masked language modeling, which the authors claim to support their hypothesis.
3. The authors present a construction for how to implement belief propagation for a tree-structured factor graph of depth $l$, using only $l$ layers.
weaknesses: 1. It’s not clear how much the observations made in this work generalizes to larger models, and more complicated data distributions.
2. The empirical evidence presented has alternative interpretations that haven't been ruled out. (Please see Questions.)
3. The construction for implementing BP on depth-$l$ tree using a transformer of only $l$-layers could benefit from a bit more details, especially on the root-to-leaf message passing part. (Please see Questions.)

---

confidence: 3
contribution: 2
initial_rating: 5
presentation: 3
questions: 1. Intuitively, what "knob" does the filtering parameter $k$ represent? Is it the case that lower $k$ result in more long-range correlations in the data?
1. 035: Another relevant paper: https://arxiv.org/abs/2305.02386
1. Is there a particular reason why you chose to frame the paper mostly in terms of factor graphs and belief propagation, rather than CFGs and standard parsing algorithms (e.g., the inside algorithm)? Is there an advantage to presenting it this way? Is there an advantage in time complexity vs. using a CFG parsing algorithm?
1. 119: What would the equivalent PCFG be, incorporating the depth constraint and $k$?
1. 133: What is $\mathcal{O}_a$? What is $q$? This part is very unclear to me.
1. 144: It's not clear to me what this means. Can you express this in equations?
1. Can the root always be uniquely determined by the input symbols? According to my understanding, the underlying CFG can be ambiguous. How is it possible to get 100% accuracy?
1. How does the BP algorithm described in the main text relate to the experiments? In what way is it used? I don't think this is stated explicitly.
1. Fig 3: Why do you report validation accuracy but not test accuracy? Why does the accuracy go up and then down? Did you not use the best checkpoint when evaluating on OOD data?
1. Why use accuracy instead of perplexity for MLM? Since the CFG can be ambiguous, there isn't only one correct answer, right?
soundness: 2
strengths: Some of the experimental results are quite interesting. For example, Fig 4 shows the expected attention patterns, and Sec 3.4 is interesting in that it shows that MLM pretraining improves sample efficiency.
summary: This paper evaluates encoder-only transformers on a synthetic task, where data is sampled from a complete binary tree-structured generative model of fixed depth $\ell$. There is a knob $k$ that determines at which layer that subtrees are forced to be conditionally independent. This generative process is equivalent to a PCFG, although it is framed mostly in terms of factor graphs and belief propagation. The authors find that transformers can predict the root node type given the leaves with high accuracy. They find that they can do MLM prediction with high accuracy, and that using MLM as a pretraining step improves sample efficiency for root prediction. They analyze attention patterns and see that they attend in a hierarchical fashion as expected.
weaknesses: Although the paper includes some interesting visualizations, I am not quite convinced that the contributions of this paper are particularly novel or rise to the level of a full ICLR paper. At times I also found the paper difficult to read, and its claims unclear.

1. In terms of novelty, there seems to be significant overlap with Allen-Zhu & Li (2023), and the experiments seem to be a simpler case of that paper. Like this paper, Allen-Zhu & Li (2023) trained transformers on data generated from CFGs of fixed depth and showed that the the transformer layers learned to attend to constituents as expected.
1. In terms of the significance of the contribution, independently of the question of whether it is novel, this is a very simple synthetic task that seems a bit contrived so that transformers are successful on it (an issue that is also in Allen-Zhu & Li (2023)), and it is not clear that we learn very much about transformers from these experiments. The strings in the training data are all of the same length, and the depth of the underlying parse tree is also fixed and does not exceed the number of transformer layers. It is not surprising that a transformer encoder with the same number of layers as the underlying parse tree can learn to mimic the structure of the underlying complete binary tree. It would be more interesting to test the transformer on a CFG with parse trees of varying depths. Do we see similar behavior, and does the fact that the number of layers is finite matter then?
1. In terms of clarity, it is not clear at the beginning of the paper what its primary goal is. Is the paper primarily about proposing a new data model, and if so, what is the purpose and significance of $k$? Are you primarily interested in analyzing the transformer architecture, and if so, how will the analysis on this synthetic task help us understand the behavior of transformers on real tasks such as natural language?
1. One of the main claims of the paper is that the transformer learns to implement a belief propagation algorithm, but I don't see significant evidence that shows that it is learning to implement BP vs. another algorithm, e.g., some version of the inside algorithm. I don't think the analysis of accuracy on OOD examples and attention patterns rules this case out.
1. Major figures supporting the paper's claims are only in the appendix (Fig 7, App C.3 and C.4).

---

confidence: 2
contribution: 2
initial_rating: 5
presentation: 2
questions: Line355 I did not quite understand why the accuracy would have a drop in the middle of training.

Also I hope the writing for section3.3 can be improved by giving more easy-to-understand intuition.
soundness: 3
strengths: It's a novel viewpoint to study transformer learning from belief propagation.

The CFG construction with filtering is interesting.

The study is from multiple perspective: prediction accuracy, probing, and manual construction.
summary: The authors build a binary-tree CFG with a filtering mechanism that the nodes on layer k is sampled only conditioned on the root node. 

A transformer encoder is then trained to predict the root node. With k=0, the prediction is perfect. And the performance decreases as k increase. It is also shown that the model's performance on out-sample k exactly matches the performance from BP. The authors then conduct experiments on masked prediction (like BERT). Finally, the authors propose an exact implementation of the BP algorithm through the transformer computation.
weaknesses: I'm not sure whether the transformer accuracy matches that of belief propagation is surprising, could it be a natural consequence that the transformer is simply learning the "optimal" prediction function (which is the prediction made by belief propagation)?

I think the writing of this paper can be improved.

While the construction sec3.5 is interesting, it does not mean that the learned transformer is actually doing that (if I understand correct).

I may consider raise my score if other two reviewers show strong interest.

Meta-review: This paper trains Transformer models on a synthetic PCFG in order to compare their mechanisms with an exact solution through belief propagation. Their analysis demonstrates that the attention patterns behave hierarchically in an interpretable way when the layers match the actual tree depth, which matches the behavior of the exact solution. They also show that this hierarchical structure emerges gradually during training.

Although most reviewers found some merit in the various results and appreciated many of the visualizations, overall, several criticisms remained. In particular, all reviewers remain skeptical of the claim that the BP algorithm is the *only* implementation compatible with their results; further confirmation is needed. Broadly, the authors map several of their visualizations to specific claims about the algorithm implemented by the model and they show that the algorithm can be computed by the Transformer model, but while their findings are compatible with these proposed algorithms, they are not strong evidence. Some reviewers are also skeptical of the generality of their findings, given the small models trained on a synthetic setting, although this complaint can be applied to most empirically rigorous work on the science of deep learning.

Decision: Reject