20260514:增加新内容

This commit is contained in:
2026-05-14 13:54:52 +08:00
parent 56c4d3ef7c
commit b116710e4c
294 changed files with 10682 additions and 255 deletions

View File

@@ -0,0 +1,55 @@
---
title: "窗口注意力 (Window Attention)"
created: 2026-05-14
updated: 2026-05-14
type: concept
tags: [attention, llm, streaming, kv-cache]
sources: ["https://arxiv.org/abs/2309.17453", "https://arxiv.org/abs/2004.05150"]
---
# 窗口注意力 (Window Attention)
## 定义
窗口注意力Window Attention / Sliding Window Attention是一种注意力机制的变体模型**只缓存和关注最近的 $L$ 个 Token 的 KV 状态**,而非所有历史 Token。最早由 Beltagy et al. (2020) 在 Longformer 中提出,后被广泛应用于 LLM 的高效推理。
## 动机
- **O(T²) 计算不可行**Dense Attention 对长序列的计算和内存开销过大
- **恒定内存**:窗口注意力将 KV 缓存大小固定为 $L$,无论序列多长
- **直观且自然**:最近的上下文通常最有信息量
## 致命缺陷Attention Sink 崩溃
Xiao et al. (2024) 在 [[streaming-llm|StreamingLLM]] 论文中揭示了窗口注意力的关键失败模式:
**当序列长度超过缓存大小 $L$,初始 Token 的 KV 被逐出时,模型困惑度急剧飙升(从 ~5 跳至 5000+)。**
### 原因
初始 Token 在所有层和注意力头中扮演 [[attention-sinks|注意力汇]] 的角色——它们承载了大量本应分散的注意力分数。一旦被逐出SoftMax 的分母结构被破坏,注意力分布发生剧烈偏移,模型崩溃。
### 实验数据
| Cache Config | Llama-2-13B PPL |
|-------------|-----------------|
| 0 + 1024 (Window) | 5158.07 |
| 4 + 1020 (StreamingLLM) | 5.40 |
窗口注意力在序列长度超出缓存后彻底失效。
## 与其他方法的对比
| 方法 | 复杂度 | 长文本性能 | 问题 |
|------|--------|-----------|------|
| Dense Attention | O(T²) | 超出预训练窗口后崩溃 | 内存无限增长 |
| **Window Attention** | O(TL) | 初始 Token 被逐出后崩溃 | Attention Sink 效应 |
| Sliding Window + Recompute | O(TL²) | 优秀 | 太慢(每个 token 重建 KV |
| StreamingLLM | O(TL) | 优秀 | 需保留 4 个初始 Token |
## 相关概念
- [[attention-sinks|注意力汇]] — 解释窗口注意力失败的根本原因
- [[streaming-llm|StreamingLLM]] — 修复窗口注意力的方案
- [[length-extrapolation|长度外推]] — 更广泛的问题背景
- [[kv-cache-bottleneck|KV 缓存瓶颈]] — 相关效率问题