Files
myWiki/concepts/ngram-embedding.md

55 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "N-gram Embedding (in LLMs)"
created: 2026-06-25
updated: 2026-06-25
type: concept
tags: ["embedding", "ngram", "memory", "language-modeling"]
sources:
- "[[engram-conditional-memory-2026]]"
---
# N-gram Embedding (in LLMs)
N-gram Embedding 是经典统计语言建模技术在现代 LLM 架构中的复兴——将局部 token 序列映射为稠密嵌入向量,通过 O(1) 查找替代昂贵的计算检索。
## 经典 N-gram 模型
N-gram 模型基于马尔可夫假设P(w_t | w_{1:t-1}) ≈ P(w_t | w_{t-n+1:t-1})。传统实现受限于:
- 数据稀疏性(组合爆炸)
- 平滑技术的局限
- 缺乏语义泛化
## 在 Transformer 中的现代化
### OverEncoding (Huang et al., 2025)
将 N-gram 嵌入直接平均到词表嵌入中——最简单的集成方式,但扩展潜力有限。
### Engram (Cheng et al., 2026)
将 N-gram 嵌入提升为**一等建模原语**
1. **Tokenizer Compression**NFKC 归一化 + 小写化,将语义等价 token 映射到同一规范 ID23% 词表缩减)
2. **Multi-Head Hashing**K 个独立哈希函数 × N-gram 阶数,乘性 XOR 哈希避免组合爆炸
3. **Context-aware Gating**:当前隐藏状态动态调制检索到的静态嵌入
4. **Depthwise Causal Convolution**:扩展感受野
## 为什么有效
语言中存在大量**局部静态规律**
- 命名实体("Alexander the Great")→ 多 token 但语义单一
- 公式化表达("on the other hand")→ 固定搭配
- 领域术语("stochastic gradient descent")→ 高频共现
这些模式天然适合廉价查找而非深度计算。经典 N-gram 能捕获它们的事实说明Transformer 用多个早期层重建这些模式是对计算深度的浪费。
## 与现代架构的关系
- **MoE**N-gram 嵌入是条件记忆的实例,与条件计算互补
- **Attention**N-gram 嵌入释放了注意力容量,使其聚焦全局上下文而非局部依赖
- **KV Cache**:与 N-gram 嵌入的关系尚未被充分探索——局部依赖被嵌入后,注意力所需的 KV 缓存可能缩小
## 参考
- [[engram-conditional-memory-2026]]
- [[engram]]
- [[conditional-memory]]
- [[mixture-of-experts]]