Why does the decoder-only LLM need positional encoding?

As is well known, mainstream LLMs today are all decoder-only models built on causal attention (we've discussed this in Why Are Current LLMs All Decoder-only Architectures?), and for causal attention, quite a few works have already shown that it can achieve non-trivial results without any extra positional encoding (referred to as NoPE for short). However, the fact is that mainstream decoder-only LLMs still add extra positional encoding, such as RoPE, ALIBI, and so on.

So here's the question: if it's been shown that positional encoding isn't strictly necessary, why do mainstream LLMs add it anyway? Doesn't "the fewer complications, the better" apply here? In this article, we'll offer our own views from three angles:

1. What is the role of positional encoding for attention?
2. How does causal attention with NoPE implement positional encoding?
3. What are the shortcomings of the positional encoding implemented via NoPE?

Positional encoding

Let's start with the first question: what does positional encoding mean for the attention mechanism?

Back in the era when BERT dominated, quite a few positional encoding schemes were proposed, and I summarized some of them in The Transformer Positional Encoding That Has Racked Researchers' Brains. Later, in The Path to the Transformer Upgrade: 1. Tracing the Origins of Sinusoidal Positional Encoding, I tried to understand positional encoding from a more principled perspective, and arrived at a theoretical explanation for the earliest Sinusoidal positional encoding, which directly inspired the later RoPE.

Put simply, the most fundamental role of positional encoding is to break the permutation invariance of attention. What is permutation invariance? In the BERT era, we mainly used bidirectional attention, whose basic form is:

\begin{equation}\boldsymbol{y}_n = \boldsymbol{f}(\boldsymbol{q}_n;\boldsymbol{x}_1,\boldsymbol{x}_2,\cdots,\boldsymbol{x}_L) = \frac{\sum_{m=1}^L e^{\boldsymbol{q}_n\cdot \boldsymbol{k}_m}\boldsymbol{v}_m}{\sum_{m=1}^L e^{\boldsymbol{q}_n\cdot \boldsymbol{k}_m}},\quad \boldsymbol{k}_n / \boldsymbol{v}_n= \boldsymbol{x}_n\boldsymbol{W}_{k/v} + \boldsymbol{b}_{k/v}\label{eq:bi-att}\end{equation}

Suppose $\sigma_1,\sigma_2,\cdots,\sigma_L$ is an arbitrary permutation of $\{1,2,\cdots,L\}$; then permutation invariance means that

\begin{equation}\boldsymbol{y}_n = \boldsymbol{f}(\boldsymbol{q}_n;\boldsymbol{x}_1,\boldsymbol{x}_2,\cdots,\boldsymbol{x}_L) = \boldsymbol{f}(\boldsymbol{q}_n;\boldsymbol{x}_{\sigma_1},\boldsymbol{x}_{\sigma_2},\cdots,\boldsymbol{x}_{\sigma_L})\end{equation}

In plain terms, this means that $\boldsymbol{y}_n$ is independent of the ordering of the key-value pairs, which does not match the nature of natural language, so we need some way to break this invariance. By analogy with a database, attention without positional encoding is like a database without timestamps — the retrieval result only depends on the query — while positional encoding is like tagging the items in the database with sequential timestamps, so that the retrieval result can also depend on the order of the items.

Prior knowledge

Another role of positional encoding is to inject prior knowledge into attention, or to give attention the ability to learn such prior properties.

For example, the Sinusoidal positional encoding mentioned above is an absolute positional encoding generated directly from trigonometric functions, where adjacent position vectors have higher similarity — this implicitly encodes the prior that nearby tokens should have similar embeddings. The positional encoding used by BERT is also an absolute positional encoding, but it is randomly initialized and then learned as a parameter, meaning it makes no such "nearby" assumption, but allows the model to learn this property if it deems it necessary.

More popular is relative positional encoding, whose prior assumption is that "relative position matters more than absolute position." Early relative positional encoding schemes typically also applied a truncation (relative positions beyond a certain value are directly mapped to the same value), which embodies the assumption that "long-distance relative positions don't need to be that precise." T5's positional encoding goes a step further, bucketing relative positions logarithmically, achieving the effect that "the farther the relative position, the blurrier it becomes." In addition, some relative positional encoding schemes directly impose a prior on token importance — for instance, ALIBI implicitly assumes that, on average, farther tokens are less important (long-range decay).

Models such as RNNs and CNNs essentially bake the prior that "closer tokens matter more" directly into the architecture, allowing them to dispense with positional encoding and reduce complexity to linear. However, priors are man-made and biased — to put it bluntly, they're not accurate enough — and it currently seems that the goal of LLMs is to surpass humans rather than imitate them. This also explains why the mainstream architecture is attention-based: it carries fewer architectural priors, i.e., fewer man-made biases and blind spots, and therefore has a higher ceiling.

Unidirectional attention

Having understood the role of positional encoding, let's now think about how NoPE works, or to what extent it can achieve the roles of positional encoding discussed above.

As noted in the previous two sections, bidirectional attention has permutation invariance, so it requires positional encoding to break it — which means NoPE is not applicable to bidirectional attention. Its precondition is unidirectional attention, i.e., causal attention:

\begin{equation}\boldsymbol{y}_n = \boldsymbol{f}(\boldsymbol{q}_n;\boldsymbol{x}_1,\boldsymbol{x}_2,\cdots,\boldsymbol{x}_L) = \frac{\sum_{m=1}^n e^{\boldsymbol{q}_n\cdot \boldsymbol{k}_m}\boldsymbol{v}_m}{\sum_{m=1}^n e^{\boldsymbol{q}_n\cdot \boldsymbol{k}_m}},\quad \boldsymbol{k}_n / \boldsymbol{v}_n= \boldsymbol{x}_n\boldsymbol{W}_{k/v} + \boldsymbol{b}_{k/v}\label{eq:uni-att}\end{equation}

The only difference from the bidirectional attention in equation $\eqref{eq:bi-att}$ is that the upper limit of the summation changes from $L$ to $n$. This makes it resemble $\text{cumsum}$, whose result depends on the order of $\boldsymbol{x}_1,\boldsymbol{x}_2,\cdots,\boldsymbol{x}_L$. In other words, it inherently lacks permutation invariance. Consequently, the combination of "causal + NoPE" in principle doesn't need positional encoding to achieve non-trivial results (non-trivial meaning results on the same level as those with positional encoding).

The paper that first pointed out this conclusion should be Transformer Language Models without Positional Encodings Still Learn Positional Information. Of course, this mainly refers to the fact that its authors were the first to announce this conclusion in a relatively rigorous "experiment + paper" manner; in fact, as far as I know, this conclusion had already been taken for granted by many people even before this paper. In addition, the later works The Impact of Positional Encoding on Length Generalization in Transformers and Length Generalization of Causal Transformers without Position Encoding further explored the length generalization ability of NoPE.

Distinguishing position via variance

Going further, through what mechanism does "causal + NoPE" identify positional information? We can get a feel for this through an extremely simplified example.

Intuitively, $\boldsymbol{y}_n$ as defined by equation $\eqref{eq:uni-att}$ is the (weighted) average of $n$ copies of $\boldsymbol{v}$, $\boldsymbol{y}_{n+1}$ is the (weighted) average of $n+1$ copies of $\boldsymbol{v}$, and so on. So let's first try the simplest case — a uniform distribution — that is, consider the following attention matrix:

\begin{equation}A = \begin{pmatrix}1 & \\ \frac{1}{2} & \frac{1}{2} & \\ \frac{1}{3} & \frac{1}{3} & \frac{1}{3} & \\ \vdots & \vdots & \vdots & \ddots \\ \frac{1}{n} & \frac{1}{n} & \cdots & \cdots & \frac{1}{n}\\ \vdots & \vdots & \vdots & \vdots & \vdots & \ddots \\ \end{pmatrix}\end{equation}

Under this assumption, we have

\begin{equation}\boldsymbol{y}_n = \frac{1}{n}\sum_{m=1}^n \boldsymbol{v}_m\end{equation}

Then, let's assume that each component of every $\boldsymbol{v}$ is independently sampled from the same distribution with "mean 0, variance $\sigma^2$". Under this assumption, we can work out the mean and variance of $\boldsymbol{y}_n$:

\begin{align}\frac{1}{d}\sum_{i=1}^d \boldsymbol{y}_{n,i} \approx&\, \mathbb{E}[\boldsymbol{y}_{n,i}] = \mathbb{E}\left[\frac{1}{n}\sum_{m=1}^n \boldsymbol{v}_{n,i}\right] = \frac{1}{n}\sum_{m=1}^n \mathbb{E}\left[\boldsymbol{v}_{n,i}\right] = 0 \\[5pt] \frac{1}{d}\sum_{i=1}^d \boldsymbol{y}_{n,i}^2 \approx&\, \mathbb{E}[\boldsymbol{y}_{n,i}^2] = \mathbb{E}\left[\left(\frac{1}{n}\sum_{m=1}^n \boldsymbol{v}_{n,i}\right)^2\right] = \frac{1}{n^2}\sum_{m=1}^n \mathbb{E}\left[\boldsymbol{v}_{n,i}^2\right] = \frac{\sigma^2}{n} \\ \end{align}

The second equality is exactly the "MS (mean square)" from RMS Norm, and we can see it depends on the position $n$. Since the mean is zero, the MS is also equivalent to the variance. From this we conclude that "causal + NoPE" essentially hides positional information in the component-wise variance of $\boldsymbol{y}$, or equivalently, in the $\mathcal{l}_2$ norm of $\boldsymbol{y}$. Of course, readers may question the assumptions behind this conclusion. Indeed, these two assumptions are at best applicable to a model at initialization, but they are sufficient for getting an intuitive feel for how NoPE identifies position: the intuitive difference between the various $\boldsymbol{y}_n$ lies in the number of $\boldsymbol{v}_m$ being averaged, and the most direct change resulting from averaging over different numbers of terms is the variance.

The same conclusion also appears in the paper Latent Positional Information is in the Self-Attention Variance of Transformer Language Models Without Positional Embeddings, where the authors further verified it on pretrained NoPE models, confirming the generality of this conclusion.

Shortcomings

Let's summarize the results so far: in the first two sections, we identified two roles of positional encoding — its main role is to break the permutation invariance of attention, and its secondary role is to inject certain priors into attention; we then showed that causal attention itself lacks permutation invariance, so in principle it doesn't need positional encoding (NoPE); finally, we found that NoPE mainly expresses positional information through the variance of the hidden state vectors.

Now let's return to the question in the title: why do decoder-only models based on causal attention usually still add positional encoding? We've actually already answered this — causal attention "in principle" doesn't need positional encoding, and "in principle" here usually means "it can get by, but it's not good enough." In plain terms, NoPE works, but adding positional encoding works better.

Why is that? This goes back to "NoPE expresses positional information through the variance of vectors," which is equivalent to saying that $\boldsymbol{y}_n$ is obtained by multiplying some position-free vector $\boldsymbol{z}_n$ by some scalar function $p(n)$ related to position $n$. This in turn implies:

1. NoPE effectively implements something like a multiplicative absolute positional encoding, and it only compresses positional information into a single scalar, so this is an extremely weak form of positional encoding;
2. A single scalar can only carry limited information — as the input length increases, the positional encoding becomes increasingly compact and thus harder to distinguish. For instance, in our simplified example we have $p(n)\sim \frac{1}{\sqrt{n}}$; when $n$ is large enough, $\frac{1}{\sqrt{n}}$ and $\frac{1}{\sqrt{n+1}}$ become nearly indistinguishable, meaning positions $n$ and $n+1$ can no longer be told apart;
3. The mainstream view holds that relative positional encoding is better suited to natural language; since NoPE implements a form of absolute positional encoding, it is naturally less efficient than supplementing the model with additional relative positional encoding;
4. NoPE neither adds priors such as long-range decay to the model, nor does it appear to grant the model the ability to learn such priors, so when the input length is large enough, issues with unfocused attention may arise.

In summary, NoPE may suffer from issues such as insufficient positional resolution, lower efficiency, and diffuse attention on long text. So even for decoder-only models, we still need to supplement them with additional positional encoding (especially relative positional encoding) to remedy the shortcomings described above.

Of course, this analysis mainly targets single-head attention. In fact, even if the positional information carried by each head is just a single scalar, under the combined effect of multiple heads and multiple layers, the total positional information amounts to a fairly substantial vector. So in practice, NoPE isn't really that bad — it's just that adding positional encoding makes things a bit better, because this lets the LLM itself focus more on overall reasoning ability, rather than having to spend effort reproducing capabilities that positional encoding could provide for free.

Summary

Although some works have shown that decoder-only models seem to achieve decent results even without positional encoding, mainstream LLMs still add extra positional encoding. This article has attempted to offer my own understanding of this phenomenon.

English translation of a post from 科学空间 | Scientific Spaces by 苏剑林. Original: https://kexue.fm/archives/10347
Translated automatically with claude-sonnet-5; all equations are reproduced verbatim from the source. Copyright remains with the original author.