生产级大模型编码 Token 位置的 6 种策略解析
6 ways production LLMs encode token positions:
系统梳理了 LLM 位置编码的工程实现细节与取舍,适合算法工程师深入理解模型底层机制并指导长上下文优化实践。
6 ways production LLMs encode token positions:
生产级 LLM 编码 token 位置的 6 种方式:
(bookmark this)
(收藏此内容)
A plain Transformer has no built-in sense of token order.
普通的 Transformer 没有内置的 token 顺序感知能力。
Without positional information or a causal mask, the same three tokens in a different order produce the same output vectors, just rearranged.
如果没有位置信息或因果掩码,不同顺序的三个相同 token 会产生相同的输出向量,只是排列顺序不同。
This is because the feed-forward network processes each token independently with the same weights.
这是因为前馈网络使用相同的权重独立处理每个 token。
And attention compares token content through query-key dot products. A different ordering rearranges those comparisons, but each token still sees the same keys and values.
而注意力机制通过查询-键的点积来比较 token 内容。不同的排序会重新排列这些比较,但每个 token 看到的键和值仍然相同。
The computation has no native signal for before or after.
计算过程本身没有关于先后顺序的原生信号。
Positional encoding embeds that info, and there are 6 common strategies:
位置编码将这一信息嵌入其中,常见的策略有 6 种:
1. Sinusoidal
1. 正弦编码 (Sinusoidal)
Each token embedding gets a fixed vector of sine and cosine values. Different frequencies describe position at different scales.
每个 token 嵌入都会获得一个固定的正弦和余弦值向量。不同的频率在不同尺度上描述位置。
The formula works beyond the training length, although the model may struggle with unfamiliar positions.
该公式在训练长度之外也有效,尽管模型可能在面对不熟悉的位置时表现不佳。
2. Learned
2. 可学习编码 (Learned)
Each position gets its own embedding, trained alongside the model and added to the token embedding. Positions outside the allocated table have no entries, so longer sequences require an adaptation.
每个位置都有自己独立的嵌入,与模型一起训练并添加到 token 嵌入中。超出分配表格范围的位置没有对应条目,因此更长的序列需要进行适配。
3. RoPE
3. RoPE (旋转位置编码)
Query and key vectors rotate according to their positions inside attention.
查询和键向量会根据其在注意力机制中的位置进行旋转。
Their dot product incorporates relative offset. The useful property is that attention scores depend on both content and the distance between tokens.
它们的点积包含了相对偏移量。其有用特性在于,注意力分数取决于内容和 token 之间的距离。
4. ALiBi
4. ALiBi (线性偏置)
Attention scores receive a distance penalty before softmax. Each head uses a different fixed slope so nearby tokens get a preference, while distant tokens need stronger content matches to receive comparable attention.
在 softmax 之前,注意力分数会接受距离惩罚。每个头使用不同的固定斜率,使得邻近 token 获得偏好,而远距离 token 需要更强的内容匹配才能获得相当的注意力。
5. YaRN
5. YaRN
An existing RoPE model gets frequency-dependent rescaling and an attention-temperature adjustment.
对现有的 RoPE 模型进行频率相关的重缩放以及注意力温度调整。
Additional training helps it handle longer sequences while preserving useful distinctions between nearby positions.
额外的训练有助于它处理更长的序列,同时保留邻近位置之间有用的区分度。
6. NoPE
6. NoPE (无位置编码)
The model has no explicit positional encoding, but its causal mask remains. Each token sees only its prefix. That asymmetry gives the network structure from which it can learn position information.
模型没有显式的位置编码,但保留了因果掩码。每个 token 只能看到其前缀。这种不对称性为网络提供了结构,使其能够从中学习位置信息。
One common mistake to avoid is that a valid position calculation doesn't guarantee useful behavior.
需要避免的一个常见错误是:有效的 position 计算并不保证能产生有用的行为。
RoPE can compute a rotation for position 100,000 even when the model fails to retrieve information there. Longer contexts also cost memory and computation.
即使模型无法检索到位置 100,000 处的信息,RoPE 仍能为该位置计算旋转。更长的上下文还会增加内存和计算开销。
I have written an article on How LLM Inference Works, which covers that side through prefill and decode, with separate deep dives into KV caching and speculative decoding.
我写了一篇关于大语言模型推理工作原理的文章,涵盖了预填充(prefill)和解码(decode)阶段,并分别深入探讨了 KV 缓存和推测解码。
Read it below.
请在下方阅读。
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力