Choosing fewer connections
Most possible comparisons may be unnecessary for a task. Sparse patterns remove them deliberately. Explain the paths they retain, the information they can lose, and whether the kernel actually skips their work.
#Section 4Sparse attention
A code file refers to a variable declared far earlier. If the declaration lies outside a local-only decoder’s window, the final layer cannot inspect it directly. The connection pattern is part of what the model can learn.
Dense vs sparse attention and reducing attended positions
Sparse attention restricts each query to a subset of positions. A binary attention mask lists the allowed query-key pairs. Dense causal attention allows every nonfuture position; a sparse causal pattern allows fewer and still forbids the future.
Complexity reduction and information trade-offs
If each of queries reads at most keys, arithmetic scales as O(Tw) instead of O(T²), but only if the kernel actually skips disallowed pairs. Computing dense scores and then zeroing entries is still dense work, and a dense Boolean mask still costs quadratic memory.
Absent edges cannot carry a direct comparison at that layer. Deeper layers or global paths may recover indirect communication, which is not the same as full attention. Child and colleagues' Sparse Transformer explored structured sparsity for long sequences. Sparse Transformer paper.
For eight tokens, dense causal attention has 36 pairs. A width-4 causal window has 1 + 2 + 3 + 4 + 4 + 4 + 4 + 4 = 26, a structural saving of 10 pairs, not evidence that any GPU kernel is faster.
#Section 5Local attention
In prose, a pronoun usually depends on a nearby noun. Restricting attention to a neighborhood keeps that structure while skipping distant comparisons.
Fixed local neighborhoods and nearby-token attention
Local attention restricts each position to a neighborhood: a symmetric radius in a bidirectional encoder, a past-only span in a causal decoder. Fixed blocks do not overlap; sliding neighborhoods move with each query.
A causal window holding the current token and three predecessors lets position 7 read positions 4 to 7. Position 1 sees only 0 and 1 because nothing comes earlier. These boundaries matter when counting cost or writing masks.
Reduced cost
For , a causal window of width including the current position has
For a fixed window, each query compares with only a bounded neighborhood. Near the sequence start it sees fewer positions; a window covering the sequence reduces to full causal attention.
Fixed blocks can strand two adjacent tokens on either side of a boundary. A sliding window avoids that but still drops distant edges. Pick the pattern from the paths the task needs and the kernels you have.
#Section 6Sliding-window attention
The sequence keeps growing, but each query reads only the most recent span. Layers that need only that span can keep a rolling cache instead of all past keys and values.
Window size and restricted context
Sliding-window attention uses a neighborhood defined relative to each query. Our window size includes the current position, so width 4 means three prior positions plus the current one. Some implementations count only past tokens, so translate their values before comparing.
Receptive-field growth across layers
A receptive field is the set of input positions that can influence a representation. With layers of causal width- attention, the maximum span is 1 + L(w-1), clipped at the sequence start. That is a set of possible paths, not lossless storage along them.
Memory savings and long-range limitations
A rolling cache saves memory only if the runtime evicts old states and no full-attention layer still needs them.
Mistral 7B (2023) used sliding-window attention with a rolling cache. Mistral 7B paper.
#Section 7Global and strided attention
A distant header names the document's subject. Local edges alone need many layers to carry it to every paragraph; a few long-range links shorten the path.
Global tokens and periodic long-range links
Global tokens take part in connections beyond the local neighborhood. In a causal decoder they still obey the past-only rule: later queries can read a prefix token, but that token cannot summarize what comes after it in the same pass.
Longformer combined local attention with task-selected global positions in a bidirectional encoder. Copying its global behavior into causal generation unchanged would leak the future. Longformer paper.
Strided patterns and combining local/global information
Strided attention connects positions at periodic intervals. One rule reads keys in the same residue class modulo stride ; another lets every query read indices that are multiples of . They connect different pairs, so document which one a mask implements.
Count the union of local and global positions, avoiding duplicates. Global positions must remain hidden from earlier queries in a causal model.
A fixed number of globals gives O(T(w+g)) pairs. A stride exposing about T/s distant keys per query gives O(Tw + T²/s), still quadratic at fixed stride. "Strided" does not mean linear.
#Section 8Hybrid attention
Most layers can use local windows while a few read the full prefix. Recent-token work stays bounded, and long-range access survives at chosen depths.
Local layers, full-attention layers, and periodic global attention
Hybrid attention mixes patterns within one model, for example three local layers then one full layer, repeated. Periodic global attention here means a full causal layer at a set depth, not a bidirectional global token.
Efficiency vs context access
A hybrid retains full history in its full layers and rolling history in local layers. Its memory depends on the whole schedule, not the cheapest layer.
These counts predict arithmetic and payload, not wall time or quality. A dense fallback for local masks erases the saving, and the full layers only help if training teaches the model to use them. Compare against an all-full baseline at the same budget and test dependencies at several distances.