Longformer › Visualizing the Attention Pattern
Language AI HandbookAll chapters Longformer › Visualizing the Attention PatternOne section · Sections in this chapter4 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The Longformer attention pattern creates a distinctive sparse structure when visualized as a matrix. The sparsity pattern is not random. It has a specific geometry: a diagonal band of width (the local window) with full rows and columns at the global token positions. When you see this pattern, you can immediately read off the design intent of the model. The density of the diagonal band tells you how much local context each token accesses. The number of full rows tells you how many global aggregation points the task requires. Building intuition for this visual representation will help you debug attention configurations and understand why certain configurations work better than others.
Let's build a visualization to understand this pattern in detail.
import numpy as np
def create_longformer_attention_mask(seq_len, window_size, global_positions):
"""
Create a Longformer-style attention mask.
Args:
seq_len: Total sequence length
window_size: Size of sliding window (must be even)
global_positions: List of positions that have global attention
Returns:
Attention mask of shape (seq_len, seq_len)
"""
mask = np.zeros((seq_len, seq_len))
half_window = window_size // 2
# Add sliding window attention for all positions
for i in range(seq_len):
start = max(0, i - half_window)
end = min(seq_len, i + half_window + 1)
mask[i, start:end] = 1
# Add global attention: global tokens attend to all, all attend to global
for g in global_positions:
mask[g, :] = 1 # Global token attends to all
mask[:, g] = 1 # All tokens attend to global token
return mask
The visualization reveals the structure of Longformer attention. The diagonal band represents sliding window attention: each position attends to its local neighborhood. The vertical and horizontal stripes at positions 0 and 32 show global attention: these tokens have bidirectional access to the entire sequence.
Notice how sparse this pattern is compared to full attention. Full attention would fill the entire square, requiring storage for attention weights (where is our sequence length in this example). The Longformer pattern only needs storage for the non-zero entries, which scales linearly with sequence length.