> For the complete documentation index, see [llms.txt](https://yeasy.gitbook.io/llm_internals/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yeasy.gitbook.io/llm_internals/di-si-bu-fen-mo-xing-yu-qian-yan-pian/12_encoder_models/12.3_longformer_bigbird.md).

# 12.3 长文本编码器：Longformer 与 BigBird

标准 Transformer 编码器的自注意力复杂度为 $$O(n^2)$$（详见 [2.5 节](/llm_internals/di-yi-bu-fen-ji-chu-pian/02_attention/2.5_complexity_limits.md)），使其难以处理超过 512-1024 词元的长文本。Longformer 和 BigBird 通过**稀疏注意力模式**突破了这一限制。

## 12.3.1 Longformer

**Longformer**（Beltagy 等人，2020 年）将注意力分解为两种模式的组合：

**局部滑动窗口注意力**：每个词元只关注其前后固定大小窗口（如 256 个位置）内的词元。这提供了局部上下文信息，复杂度为 $$O(n \cdot w)$$（$$w$$ 为窗口大小），对序列长度线性。

**全局注意力**：少数特殊位置（如 `[CLS]` 标记或任务特定的关键位置）关注所有位置，同时被所有位置关注。这确保了全局信息的汇聚。

这种混合模式在保持处理长文本能力的同时，将总复杂度降至 $$O(n)$$。Longformer 能够处理长达 4096 个词元的文档（是 BERT 的 8 倍），在长文档分类、长文本问答等任务上表现出色。

## 12.3.2 BigBird

**BigBird**（Zaheer 等人，2020 年）与 Longformer 类似使用局部与全局注意力，并额外加入第三种注意力模式：

* **局部窗口注意力**：与 Longformer 相同
* **全局注意力**：与 Longformer 相同
* **随机注意力**：每个词元**随机**关注若干个远距离位置

随机注意力的加入有一个深刻的理论意义——BigBird 的作者证明了这种稀疏注意力模式保持了标准注意力的**图灵完备性**。也就是说，理论上不存在标准注意力能完成而 BigBird 不能完成的计算任务。

## 12.3.3 超越 Longformer 和 BigBird

随着大语言模型上下文窗口的不断扩大（从 4K 到 128K 甚至 1M），长文本处理已从编码器模型的专属领地扩展到了解码器模型。现代方法（如 FlashAttention、RoPE 长度外推、Ring Attention 等）使得标准注意力也能高效处理数十万词元的序列。

编码器路线本身也在“现代化”：2024 年 12 月的 [ModernBERT](https://arxiv.org/abs/2412.13663) 把 RoPE、GeGLU、交替局部/全局注意力与 FlashAttention 等解码器时代的组件引回编码器，在 8K 上下文下同时刷新了 BERT 系模型的速度与精度，掀起了一轮“编码器复兴”。

而 Longformer 和 BigBird 的设计思想——**用结构化的稀疏模式近似全连接注意力**——仍然是一个活跃的研究方向，在资源受限的高效推理场景下有重要价值。关于百万级长上下文的工程实现和有效利用的系统性讨论，参见 [14.7 节](/llm_internals/di-si-bu-fen-mo-xing-yu-qian-yan-pian/14_future_trends/14.7_long_context.md)。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://yeasy.gitbook.io/llm_internals/di-si-bu-fen-mo-xing-yu-qian-yan-pian/12_encoder_models/12.3_longformer_bigbird.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
