CSCE 471 Natural Language Processing · Spring 2027

Longformer

Kuan-Hao Huang · Course Website

Note: This page was created with the help of AI as a supplementary reading. It may contain errors or simplifications. The lecture slides are the primary reference for this course; please refer to them first.

Self-attention lets every token look at every other token. That is what makes Transformers powerful, and also what makes them expensive: for a sequence of $n$ tokens, each layer computes $n^2$ attention scores. For a sentence that is nothing. For a 20-page legal contract or a long scientific article with thousands of tokens, it quickly becomes too slow and too memory-hungry.

Longformer (Beltagy, Peters & Cohan, 2020) is a Transformer designed for long documents. Its idea is simple: most of what a token needs comes from nearby tokens, so let each token attend only to a local window, plus a few special tokens that see everything. This reading explains the cost problem, the sliding-window idea and why stacking layers recovers long-range information, and the dilated and global attention patterns.

After reading this, you should be able to explain why full self-attention costs $O(n^2)$ · describe sliding window attention and its $O(n\cdot w)$ cost · explain why the savings require computing only the window, not masking a full matrix · compute the receptive field after stacking layers · describe dilated and global attention and when global tokens are useful · read character-level language modeling results in bits per character

1The cost of full attention

In full self-attention, the score matrix $QK^\top$ has one entry for every pair of tokens: $n\times n$ entries per head, per layer. Both the computation and the memory grow with $n^2$:

This is why many Transformer models, such as BERT, are limited to 512 tokens. A longer document must be cut into pieces, and information that spans the pieces is lost.

Sequence length $n$:Window $w$ = 512
Figure 1 (interactive). Number of attention scores computed per head and per layer. Full attention grows with $n^2$; sliding window attention grows with $n\cdot w$, linearly in $n$.

2Sliding window attention

In sliding window attention, each token attends only to the tokens in a fixed-size window around it: itself and $\tfrac{w-1}{2}$ neighbors on each side (for an odd window size $w$). The attention pattern becomes a band along the diagonal of the attention matrix.

Masking alone does not save anything. One could compute the full $n\times n$ score matrix and then set everything outside the band to $-\infty$. The result would be correct, but all $n^2$ scores would still be computed and stored. Longformer's savings come from computing only the scores inside the window, using a specially written implementation. The mask picture tells us which scores to compute.
Pattern:
Window size $w$:Click a row to pick a query
rows: queries · columns: keys
the query itselflocal attentionglobal attention
Figure 2 (interactive). Attention patterns for a 16-token sequence. Each filled cell is one score that is actually computed. Compare the number of computed scores with the $16^2=256$ of full attention, and see how dilation and global tokens change the pattern.

3Stacking layers: a growing receptive field

If each token only sees its neighbors, how can information travel across a whole document? The answer is the same as in convolutional networks: stack layers. After one layer, token $i$ contains information from its window. In the next layer, its neighbors' vectors already contain information from their windows, so token $i$ indirectly sees farther.

With window size $w$ (that is, $\tfrac{w-1}{2}$ neighbors per side), the receptive field after $L$ layers covers about

$$L\cdot(w-1)+1 \;\approx\; L\times w \text{ tokens}.$$

For example, Longformer uses windows of 512 tokens; with 12 layers, the top layer's receptive field covers several thousand tokens.

Layers $L$:Window $w$:
Figure 3 (interactive). Which input tokens can influence the highlighted token at the top layer. Each layer widens the cone by $\tfrac{w-1}{2}$ tokens on each side, just like stacked convolutions. Click a token in the top row to move it.

4Dilated and global attention

Longformer adds two variations to the basic sliding window.

Dilated sliding window

Instead of attending to the $w$ consecutive neighbors, a token attends to every second (or every $d$-th) token in a wider range, leaving gaps. The number of scores stays the same, but the window reaches $d$ times farther. Longformer uses dilation only in some attention heads of the upper layers, so other heads still focus on the immediate neighborhood.

Global attention

Some tasks need a few tokens to see the whole document. Longformer lets the user mark a small number of tokens as global:

Which tokens are global depends on the task. For classification, the <cls> token is global, so it can summarize the whole document. For question answering, the question tokens are global, so every part of the document can be compared with the question. Since there are only a few global tokens, the total cost stays linear in $n$.

5Longformer in practice

Language modeling

For language modeling, the window only extends to the left (each token sees itself and earlier tokens in its window), combining the sliding window with the causal mask. The paper evaluates on two character-level language modeling benchmarks, text8 and enwik8 (both made of Wikipedia text), using bits per character (BPC): the average number of bits the model needs to encode each next character. Lower is better.

Model (small models, test BPC)text8enwik8
T121.181.11
Transformer-XL–1.06
Reformer–1.05
Adaptive span1.111.02
BP-Transformer1.111.02
Longformer1.101.00

Numbers from Beltagy et al. (2020), Table 2. All models have roughly 40M parameters.

Long documents

The authors also took a pretrained RoBERTa model (limited to 512 tokens), replaced its attention with Longformer attention, and continued pretraining. The result handles inputs of up to 4,096 tokens and improved results on tasks with long documents, such as question answering over long Wikipedia articles, because the model no longer has to cut the document into short pieces.

Other approaches to long sequences

6Summary

7Practice

Exercise 1: counting scores

For $n=8{,}192$ tokens, how many attention scores does full attention compute per head and layer? How many does sliding window attention with $w=512$ compute? What is the ratio?

Full: $8192^2 \approx 67.1$ million. Window: $8192\times 512\approx 4.19$ million. Ratio: $n/w = 16$, so sliding window attention computes 16× fewer scores.

Exercise 2: receptive field

With window size $w=3$ (one neighbor on each side), how many layers are needed before token 1 can be influenced by token 9?

Each layer extends the reach by 1 token per side. The distance is $9-1=8$, so 8 layers are needed.

Exercise 3: global tokens

A document has $n=4{,}096$ tokens, the window is $w=512$, and a 20-token question is marked as global. Roughly how many scores are computed? Is this closer to $n\cdot w$ or to $n^2$?

Local: $4096\times 512\approx 2.10$M. Global: each of the 20 global tokens attends to all 4,096 tokens, and every token attends to the 20 global tokens: about $2\times 20\times 4096\approx 0.16$M. Total about $2.26$M, close to $n\cdot w$ and far below $n^2\approx 16.8$M.

Quick quiz

8Further reading