Embeddings

Inspired by: hugging face article

Embeddings convert complex, high-dimensional data into low-dimensional vectors. It is a form of lossy compression

1. Taxonomy

1.1 Overview

Note

Static Embedding 和 Contextualized Embedding 的关系:

  • Static embedding:输入阶段的 token vector 查表映射,也就是 embedding layer 直接产出的向量。

  • Contextualized embedding:token 的表示经过 Transformer 层后,结合了上下文信息而动态变化得到的 hidden states。

  • 同一个 token 的 static embedding 是固定的;但它的 contextualized representation 会因为上下文不同而不同。

  • 在 Transformer 里,两者通常是先后关系:先得到 static token embedding,再经过多层网络变成 contextualized representations。

  • 很多资料会把这两者都叫 “embedding”,但更严格地说,后者常叫 hidden statescontextual representations (在图里,分别是 token embedding layer 和 output embedding layer)


1.2 Word 2 Vec

Static Embedding, but ensure closure between similar semantics

King - man + woman ≈ queen

1.2.1 Method 1: CBOW (Continuous Bag of Words)

The word2vec network is trained to predict a missing word when its given the neighbors of that word as input. The intuition is that you can infer the embeddings of a word given the words around it.

给两边,猜中间

  • 表面上:训练一个 Neural Net, 一层hidden layer,最小化 Loss (交叉熵,one-hot target v.s. predicted probability)
  • 实际上:训练出来的 hidden layer 的 weight 就是我们的 embedding layer! 它是词的稠密表示 (one-hot is too sparse)

1.2.2 Method 2: Skip-gram

逻辑类似,但就是和 CBOW 反过来,给中间,猜两边

1.3 BERT

Dynamic Embedding


2. How Embeddings Work in LLMs

  • Static Embeddings generated in the first layer and combine token embeddings (vectors representing tokens) with positional embeddings (vectors encoding a token’s position in the sequence).
    • Why add?
  • Dynamic Contextual Representations. As input tokens pass through the self-attention and feed-forward layers, their embeddings are updated to become contextual. These dynamic representations capture the meaning of tokens based on their surrounding context. For example, the word “bank” appears both as “river bank” and “bank robbery”, and while the token embedding of the word bank is the same in both cases, the transformations it goes through in the layers of the network take into account the context of which the word “bank” appears in.

3. Position Embeddings

Note

核心思路只有一条:给每个 token 额外加一个表示”你在第几个位置”的向量,然后和 token embedding 加在一起(或以其他方式融合)。 但具体怎么做,经历了好几代演进。

3.1 绝对位置编码 (Absolute Position Embedding)

思路: 直接给每个位置 学一个向量 ,加到 token embedding 上。

3.1.1 Sinusoidal Position Encoding (原始 Transformer)

Attention Is All You Need (Vaswani et al., 2017)

不用学,用三角函数固定生成

其中 是位置索引, 是维度索引, 是 embedding 维度。

直觉理解:

  • 不同维度用不同频率的正弦/余弦波来编码位置
  • 低维度变化快(高频),能区分相邻位置;高维度变化慢(低频),能编码全局位置
  • 这样任何两个位置的相对距离都能用线性函数表示(正弦的性质),模型理论上可以学到相对位置关系

优点: 不需要训练;可以泛化到比训练序列更长的长度 缺点: 实际效果不如 learned position embedding;外推能力理论上有但实际有限

3.1.2 Learned Position Embedding (BERT / GPT-2)

就是把 当成可训练参数,让模型自己学。本质上就是一个 shape 为 (max_seq_len, d_model) 的 embedding 查表。

优点: 简单有效,大多数情况下效果优于 sinusoidal 缺点: 有一个最大长度限制 max_seq_len(BERT 是 512,GPT-2 是 1024),超出就没办法了——这也是为什么需要更好的方案


3.2 相对位置编码 (Relative Position Encoding)

关键洞察: 语言里重要的不是”你第几个出现”,而是”你和我隔了几个词”。例如 “The cat near the dog” 中 near 关联的对象取决于相对距离,而非绝对位置。

3.2.1 Transformer-XL (Dai et al., 2019)

不再把位置加在 input embedding 上,而是直接修改 attention score 的计算

其中 是一个关于相对距离 的可学习向量。这样 attention 的权重天然包含了相对位置信息。

3.2.2 T 5 Bias (Raffel et al., 2020)

T 5 更简单粗暴:直接给每个相对距离 学一个 scalar bias,加到 attention score 上:

参数量极小(只需要 个标量),但效果意外地好。


3.3 RoPE (Rotary Position Embedding) ⭐

RoFormer (Su et al., 2021)目前主流大模型的事实标准(LLaMA、Qwen、Mistral 等全在用)

核心思想: 不把位置信息加到 embedding 上,而是通过旋转矩阵把位置信息编码进 的交互中。

数学: 把 embedding 的维度两两一组( 组),每组看做一个二维向量,然后根据位置 旋转一个角度

也做同样操作(位置 ),然后计算 dot product 时:

神奇之处: dot product 的结果只取决于相对位置 ,绝对位置被消掉了!

RoPE 的优势总结:

特性说明
相对位置attention score 天然只依赖相对距离
远距离衰减随着相对距离增大,不同维度的贡献逐渐解耦
外推性比 learned embedding 好,搭配 NTK-aware scaling 可以扩展上下文窗口
即插即用不改变模型架构,只修改 的计算方式

Tip

NTK-aware Scaling 是 RoPE 的一个常用扩展技巧:通过修改 base ( 更大的值) 让旋转频率变低,使得模型能处理更长的序列而不需要重新训练。这就是为什么 LLaMA 等模型可以从 4 K 上下文”扩展”到 32 K/128 K。


3.4 ALiBi (Attention with Linear Biases)

Press et al., 2021

最简洁的方案:根本不在 embedding 层加位置信息,直接在 attention score 上减一个和距离成正比的 bias:

其中 是一个固定的斜率(每个 head 不同,按 head index 线性递增)。

特点:

  • 极简——不需要额外的 embedding 参数,不改变 input
  • 外推能力极强——论文显示可以直接外推到训练长度 2-4 倍
  • 但纯靠 linear bias 表达力有限,目前在主流大模型中不如 RoPE 流行

Important

核心 trade-off:

  • 绝对位置编码简单直观,但外推差
  • 相对位置编码表达力强,但实现复杂度不同
  • RoPE 目前是最佳平衡点——兼顾了相对位置、实现简洁、和外推能力