Files
myWiki/concepts/reference-sliding-window-attention.md

51 lines
1.7 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: "Reference Sliding Window Attention (R-SWA)"
created: 2026-06-24
updated: 2026-06-24
type: concept
tags: ["attention-mechanism", "kv-cache", "long-horizon", "efficient-inference"]
sources:
- "[[unlimited-ocr-works-2026]]"
---
# Reference Sliding Window Attention (R-SWA)
R-SWA 是 Unlimited OCR 提出的注意力机制,模仿人类解析工作记忆:每个生成 token 关注全部参考 token + 前 n 个输出 token。核心创新在于**将参考 token 排除在状态转移之外**。
## 注意力计算
给定前缀段 P长度 Lm含视觉 token + prompt和因果滑动窗口 Dn(t)(宽度 n
- P = {1, ..., Lm},全局可见
- Dn(t) = {j | max(Lm+1, Lm+t-n) ≤ j ≤ Lm+t-1},因果滑动
- N(t) = P Dn(t)
注意力权重:
$$\alpha_{tj} = \frac{\exp(q_t^T k_j / \sqrt{d_k})}{\sum_{i \in N(t)} \exp(q_t^T k_i / \sqrt{d_k})}, \quad j \in N(t)$$
## KV Cache 管理
标准 MHA$C_{MHA}(T) = L_m + T$(线性增长)
R-SWA$C_{R\text{-}SWA}(T) = L_m + \min(n, T) \leq L_m + n$(有界常数)
Cache 压缩比:$\rho(T) = \frac{L_m + n}{L_m + T} \to 0$(当 T 足够大时)
## 与标准 SWA 的关键区别
| 维度 | 标准 SWA | R-SWA |
|------|---------|-------|
| 参考 token 状态 | 参与状态转移,逐渐滑出窗口 | 不参与状态转移,永久保留 |
| 视觉特征退化 | 是(逐渐模糊) | 否(静态编码) |
| KV cache | 线性增长 | 有界常数 |
## 认知启发
人类抄写时不回溯全部已写内容仅关注附近上下文维持空间定向。R-SWA 的 soft forgetting 机制与此一致——历史输出信息通过滑动窗口传递,而非全量保留。
## 参考
- [[unlimited-ocr-works-2026]]
- [[constant-kv-cache]]
- [[kv-cache]]
- [[rolling-kv-cache]]