Understanding Positional Embeddings in Modern LLMs
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
This article explains how LLMs encode and process token position information by covering classic and contemporary methods like RoPE. It’s essentially a comprehensive journey where we will work through the mechanisms with visual examples, a little math, and a small PyTorch implementation. And, of course, we will then also look at how modern architectures adapt or omit explicit positional encodings.
本文解释了大型语言模型(LLM)如何通过涵盖 RoPE 等经典和现代方法来编码和处理标记位置信息。这本质上是一次全面的旅程,我们将通过视觉示例、少量数学运算和一个小型 PyTorch 实现来研究这些机制。当然,我们还将探讨现代架构如何调整或省略显式位置编码。
Below, you can find a preview of which positional embedding choices are used in 2026 open-weight models. We will discuss these choices later after covering the underlying mechanisms.
在下方,您可以预览 2026 年开源权重模型中使用的位置嵌入选择。在介绍完底层机制后,我们将稍后讨论这些选择。
But why spending so much time writing/reading about this rather minor building block? LLMs keep evolving, and as I was preparing a new article on the latest LLM architecture trends this year, I realized that I never covered the positional information methods used in LLMs in any article so far. So, instead of letting the other article get out of hand, I thought it might make sense to publish this as a standalone building block for the upcoming LLM architecture and trends analysis.
但是,为什么要花这么多时间来撰写/阅读这个相对较小的构建模块呢?LLM 不断发展,当我在准备今年关于最新 LLM 架构趋势的新文章时,我意识到我之前从未在任何文章中涵盖过 LLM 中使用的位置信息方法。因此,与其让那篇文章变得过于冗长,我认为将其作为即将到来的 LLM 架构和趋势分析的独立构建模块发布是有意义的。
Also, over the years, many readers have asked me for a tutorial on relative positional embeddings (aka RoPE) to follow up on the learned absolute embeddings used in GPT. It’s been on my TODO list for a long time, so I finally revisited my old draft and put this walkthrough together, which will cover: different positional methods in LLMs; (finally) how RoPE works; what methods are used by recent open LLMs.
此外,多年来,许多读者向我询问有关相对位置嵌入(即 RoPE)的教程,以跟进 GPT 中使用的学习绝对嵌入。这已经在我的待办事项列表中很久了,所以我终于重新审视了我的旧草稿并整理了这份指南,其中将涵盖:LLM 中的不同位置方法;(最终)RoPE 的工作原理;近期开源 LLM 使用的方法。
Note that this article assumes some familiarity with basic LLM architectures and works well as a follow-up to Build a Large Language Model (From Scratch). If the concepts and computation of the attention internals, particularly query, key, and value vectors, are a little fuzzy, I recommend checking my self-attention walkthrough as a refresher:
请注意,本文假设读者对基本的 LLM 架构有一定的了解,并且非常适合作为《从零构建大型语言模型》(Build a Large Language Model (From Scratch)) 的后续阅读。如果对注意力机制内部的概念和计算(特别是查询、键和值向量)感到模糊,我建议查看我的自注意力机制指南作为复习。
The article is also very technical and likely too detailed for some. However, I really wanted to have this foundation out there for future articles to reference. And no worries, the next one will return to a more comfortable style.
这篇文章也非常专业,对某些人来说可能过于详细。然而,我确实希望为未来的文章提供这个基础参考。不用担心,下一篇文章将回归到更舒适的风格。
PS: I started experimenting with Substack’s new “perks” system. As a thank-you to supporters, I’ve added image exports and metadata from the architecture gallery for you to use in your own analyses.
附注:我开始尝试 Substack 新的“福利”系统。作为对支持者的感谢,我添加了架构图库中的图像导出和元数据,供您在自己的分析中使用。
PPS: If you are already familiar with how position embeddings work, feel free to fast-forward to Section 6, Positional choices in modern architectures.
再附注:如果您已经熟悉位置嵌入的工作原理,请随意跳转到第 6 节“现代架构中的位置选择”。
Note that this article may be best read in a web browser than an email client. I recommend checking out the web view here, which also has a navigation bar on the left side.
请注意,本文在网页浏览器中阅读的效果可能优于电子邮件客户端。我建议点击此处查看网页视图,该视图左侧还配有导航栏。
Let’s start with the overall architecture. A tokenizer encodes the input text into token IDs. Next, the input token embedding layer of an LLM maps each of these IDs to a vector of numbers. We usually refer to these entries in that vector as “features” (similar to classic ML). The transformer blocks inside the LLM then modify these vectors using attention and feedforward layers. Lastly, the model’s output layer produces logit scores (converted into probabilities) for the next token.
让我们从整体架构开始。分词器(Tokenizer)将输入文本编码为标记 ID。接下来,LLM 的输入标记嵌入层将这些 ID 中的每一个映射为一个数字向量。我们通常将该向量中的这些条目称为“特征”(类似于经典机器学习)。LLM 内部的 Transformer 块随后使用注意力和前馈层修改这些向量。最后,模型的输出层为下一个标记生成 Logit 分数(转换为概率)。
The following shows this path in GPT-2 (a relatively classic LLM) and MiniMax-M3 (a more recent LLM released this year; a placeholder for most other recent LLMs). As we can see, the architectures are not too different from each other, but before we get to the positional information parts, let’s focus on the orange blocks for now (the token embedding layers) and the transformer blocks above them; the other architecture differences aren’t needed for this explanation.
下图展示了 GPT-2(一种相对经典的 LLM)和 MiniMax-M3(今年发布的较新的 LLM;代表大多数其他近期 LLM)中的这一路径。正如我们所见,这些架构彼此之间差异不大,但在进入位置信息部分之前,我们先关注橙色块(标记嵌入层)以及它们上方的 Transformer 块;本解释不需要其他架构差异。
A token embedding layer, shown in the figure above, maps each token ID to a vector. Position information is then added afterward. Concretely, and as we will discuss in more detail later, GPT-2 adds a position vector to each token embedding before the modified embedding enters the first transformer block. On the other hand, MiniMax-M3 adds the positional information in each transformer block using a method called RoPE inside attention.
上图所示的标记嵌入层将每个标记 ID 映射到一个向量。随后会添加位置信息。具体来说,正如我们稍后将详细讨论的那样,GPT-2 在修改后的嵌入进入第一个 Transformer 块之前,会向每个标记嵌入添加一个位置向量。另一方面,MiniMax-M3 使用一种称为 RoPE 的方法在注意力机制内部的每个 Transformer 块中添加位置信息。
We will talk about these methods in more detail later, but first, let’s start with the big picture information explaining why explicit positional information is added or needed.
我们稍后将更详细地讨论这些方法,但首先,让我们从宏观角度解释为什么需要或添加显式位置信息。
Consider the following two input sentences as a simple example: “The red fox jumps over the gray fox.” “The gray fox jumps over the red fox.” Changing the word order changes which fox does the jumping, as illustrated in the following figure.
以以下两个输入句子作为简单示例:“红狐狸跳过了灰狐狸。”“灰狐狸跳过了红狐狸。”改变词序会改变是哪只狐狸在跳跃,如下图所示。
For the examples in this article, we will treat each word as one token for simplicity. (As you know, real tokenizers can split words into smaller subword tokens.) The first sentence then has eight token positions, from 0 through 7.
为了本文的示例,为简单起见,我们将每个单词视为一个标记。(如您所知,实际的分词器可以将单词拆分为更小的子词标记。)那么第一个句子有八个标记位置,从 0 到 7。
(If you are interested in how vocabularies are built, check out my tokenizer materials on GitHub.)
(如果您对词汇表是如何构建的感兴趣,请查看我在 GitHub 上的分词器资料。)
Note that “fox”, which appears twice in the input sentence, gets the same token ID 2 in both cases. And since they both have the same token ID, irrespective of the position in the input sentence, they would get the same embedding vector from the embedding layer.
请注意,在输入句子中出现两次的“fox”在两种情况下都获得了相同的标记 ID 2。由于它们具有相同的标记 ID,无论在输入句子中的位置如何,它们都会从嵌入层获得相同的嵌入向量。
Now, let’s consider a regular self-attention mechanism with no positional encoding (and no causal mask), where every input token can attend to every other token in the input. In this scenario, swapping any two words, like “red” and “gray”, would still result in the same embeddings and attention scores between each token pair.
现在,让我们考虑一个没有位置编码(也没有因果掩码)的常规自注意力机制,其中每个输入标记都可以关注输入中的所有其他标记。在这种情况下,交换任何两个词(如“red”和“gray”)仍然会导致每个标记对之间产生相同的嵌入和注意力分数。
GPT-style models do use a causal (autoregressive) mask, which restricts each token to itself and earlier tokens. Interestingly, this causal mask gives the computation some implicit order information. In our example, “jumps” can attend to “red” in the first sentence and “gray” in the second. So, a causal LLM can theoretically already distinguish these sentences even without explicit positional embeddings. (We will return to this idea later when discussing the “NoPE” concept.)
GPT 风格的模型确实使用了因果(自回归)掩码,它将每个标记限制为仅能关注自身和之前的标记。有趣的是,这种因果掩码为计算提供了一些隐式的顺序信息。在我们的示例中,“jumps”在第一个句子中可以关注“red”,在第二个句子中可以关注“gray”。因此,从理论上讲,即使没有显式的位置嵌入,因果 LLM 也已经可以区分这些句子。(稍后在讨论“NoPE”概念时,我们将回到这个想法。)
Nonetheless, GPT-2 and most newer LLMs include explicit positional information because it gives attention a direct signal about positions. That’s because even though the causal m
尽管如此,GPT-2 和大多数较新的 LLM 还是包含了显式的位置信息,因为它为注意力机制提供了关于位置的直接信号。这是因为即使因果 m