One-sentence summary: Sparse Attention computes fewer connections, Linear Attention changes the similarity rule, and Infini-attention compresses old cross-segment K/V into bounded state—the three mechanisms solve different problems, and none preserves an unlimited history for free.


24.1 Full Attention has three separate bills

24.1.1 Connections

Full Self-Attention over length NN compares every Query with every Key. Bidirectional attention has N2N^2 connections. Causal attention permits the lower triangle:

1+2++N=N(N+1)21+2+\cdots+N=\frac{N(N+1)}{2}

“Only the lower triangle is logically valid” does not mean an ordinary dense matrix multiplication automatically skips the other half. Whether masked regions avoid real work depends on the kernel.

NNBidirectional N2N^2Valid causal N(N+1)/2N(N+1)/2
1,0241,048,576524,800
4,09616,777,2168,390,656
32,7681,073,741,824536,887,296
1,000,0001,000,000,000,000500,000,500,000

Make the sequence ten times longer and the number of connections becomes roughly one hundred times larger. That is the first bill: quadratic arithmetic.

24.1.2 Intermediate storage

If one Head's full N×NN\times N score matrix were materialized in FP16 or BF16, it would occupy:

NNDense score matrix for one Head
4,09632 MiB
8,192128 MiB
32,7682 GiB
131,07232 GiB

Those figures describe one Head and one matrix, not an entire layer. Materializing every Head multiplies them by the Head count; training also needs state for backward.

Chapter 21 showed why this table is no longer a requirement. FlashAttention computes exact Softmax Attention without writing the full score matrix to HBM. It reduces I/O and working storage toward linear growth in sequence length, but it still processes the quadratic set of Query–Key pairs.

Full Attention connections, materialized score storage, and KV Cache as three different costs

24.1.3 KV Cache is a different axis

Chapter 23's KV Cache grows linearly with cached tokens; Full Attention's training or Prefill pairs grow quadratically. Keep the axes separate:

  • FlashAttention avoids materializing the full score matrix while preserving Full Attention math;
  • GQA/MQA reduce KV Heads and cache bytes;
  • Sparse Attention removes allowed Query–Key connections;
  • Linear Attention replaces the Softmax similarity computation;
  • Infini-attention compresses history across segments while retaining exact local Attention inside the current segment.

Once these are separated, the complexity claims become much easier to audit.


24.2 Sparse Attention: draw the graph before writing O(N)

Imagine reading an annotated logbook from a remote Orkney lighthouse. The current entry is most likely to depend on nearby weather observations. The first entry of a storm can serve as a global anchor. A few links across months can shorten the path between distant events. Sparse Attention makes this “who may see whom” graph explicit.

Sliding Window, Global, and Random sparse-attention connection patterns

Three common edge types are:

  1. Window / Local: every Query sees roughly ww nearby Keys.
  2. Global: a small set of gg positions connects to the entire sequence in both directions, as with task-selected Longformer tokens.
  3. Random: each position adds rr randomly chosen links; BigBird combines these with Window and Global links.

If each row has a fixed number of these edges, the main edge count is:

O ⁣(N(w+g+r))O\!\left(N(w+g+r)\right)

This is abbreviated to O(N)O(N) only when w,g,rw,g,r remain fixed as NN grows. A window that grows with the sequence changes the complexity.

24.2.1 What Longformer and BigBird actually establish

Longformer combines a sliding window with task-selected global Attention. Its original work centered on long-document encoding, masked language modeling, and the LED encoder–decoder. BigBird adds random connections and likewise focused largely on BERT-style long-sequence encoding tasks.

The BigBird paper proves, under its graph construction and assumptions about global tokens, depth, and precision, that a sparse Transformer can retain universal-approximation and Turing-completeness properties. It does not prove that:

  • an arbitrary random pattern connects any two tokens in a fixed O(1)O(1) number of layers;
  • a finite, trained BigBird always matches Full Attention;
  • accuracy loss is slight on every task.

Theoretical expressivity, graph path length, and trained-model quality are three different claims.

24.2.2 A mask is not a speedup

Filling a normal N×NN\times N score matrix with -inf in disallowed cells gives the right Sparse Attention result, but the dense matrix has already been computed. Real savings require a structured block-sparse or FlexAttention-style kernel that skips empty blocks. Block size, sparsity, shape, and hardware utilization determine real speed; with too little useful sparsity, scheduling overhead can erase the theoretical gain.

Applying a sparse mask to a dense score matrix versus using a structured sparse kernel

24.3 Sliding Window: simple idea, easy off-by-one

Consider a decoder's causal window. Here window=3 means that a position may see itself and its two immediate predecessors:

q0 -> k0
q1 -> k0 k1
q2 -> k0 k1 k2
q3 ->    k1 k2 k3
...

At length 8 there are not 8×3=248\times3=24 valid connections, but 1+2+6×3=211+2+6\times3=21. Three links are missing at the left boundary.

import math
import torch


def causal_window_mask(length, window, device=None):
    if length <= 0 or window <= 0:
        raise ValueError("length and window must be positive")
    positions = torch.arange(length, device=device)
    distance = positions[:, None] - positions[None, :]
    return (distance >= 0) & (distance < window)


def masked_attention(q, k, v, mask):
    """Dense teaching reference; use a structured sparse kernel for real savings."""
    scores = q @ k.transpose(-2, -1) / math.sqrt(q.size(-1))
    scores = scores.masked_fill(~mask, float("-inf"))
    return torch.softmax(scores, dim=-1) @ v

This is a dense reference for checking mask semantics, not a fast Sparse implementation. PyTorch's boolean masks also do not all share one meaning: SDPA/FlexAttention and MultiheadAttention APIs must be checked rather than guessing whether True means “keep” or “block.”

24.3.1 Receptive field across layers

Under the convention above—window includes the current token, with no dilation or global positions—the maximum causal span after LL layers is:

1+L(w1)1+L(w-1)

Some papers define WW as backward reach and write approximately LWLW. The apparent discrepancy can be only a definition. Do not mix window diameter, radius, and inclusion of the current position.

24.3.2 The historical Mistral 7B example

The Mistral 7B v0.1 technical report gives 32 layers and a Sliding Window of W=4096W=4096, then derives an approximately 131K-token theoretical information-propagation span. The same configuration table lists context_len=8192; 131K is not evidence that the v0.1 checkpoint was validated for exact token recall at every position out to 131K.

The report also describes a Rolling Buffer Cache: position ii writes K/V at imodWi\bmod W, overwriting entries that have left the window. The Cache stops growing after the window fills. Exact K/V outside the window is gone, though older content may still influence newer hidden states indirectly through stacked layers.

A causal window expanding its receptive field across layers and overwriting a Rolling Buffer KV Cache

24.4 Linear Attention is not Softmax with different parentheses

Without Softmax, associativity gives:

(QKT)V=Q(KTV)(QK^T)V=Q(K^TV)

The left order creates an N×NN\times N intermediate; the right creates a dk×dvd_k\times d_v intermediate. Standard Attention, however, is:

softmax(QKT/dk)V\operatorname{softmax}(QK^T/\sqrt{d_k})V

Softmax normalizes each Query's complete row and cannot simply pass through the parentheses. Kernelized Linear Attention instead chooses a nonnegative feature map ϕ\phi and defines a different similarity:

sim(Qi,Kj)=ϕ(Qi)Tϕ(Kj)\operatorname{sim}(Q_i,K_j)=\phi(Q_i)^T\phi(K_j)

For causal Attention, maintain two prefix states:

Si=jiϕ(Kj)VjT,Zi=jiϕ(Kj)S_i=\sum_{j\le i}\phi(K_j)V_j^T,\qquad Z_i=\sum_{j\le i}\phi(K_j)

Then:

Oi=ϕ(Qi)TSiϕ(Qi)TZiO_i=\frac{\phi(Q_i)^TS_i}{\phi(Q_i)^TZ_i}
Causal Linear Attention replacing an N by N Softmax matrix with prefix states S and Z

One concrete feature map from the paper is ϕ(x)=ELU(x)+1\phi(x)=\operatorname{ELU}(x)+1:

import torch
import torch.nn.functional as F


def phi(x):
    return F.elu(x) + 1.0


def causal_kernel_attention(q, k, v):
    """One head: q/k [N,C], v [N,M]."""
    state = q.new_zeros(q.size(1), v.size(1))
    normalizer = q.new_zeros(q.size(1))
    outputs = []
    for q_t, k_t, v_t in zip(phi(q), phi(k), v):
        state = state + torch.outer(k_t, v_t)
        normalizer = normalizer + k_t
        outputs.append((q_t @ state) / (q_t @ normalizer).clamp_min(1e-12))
    return torch.stack(outputs)

With feature dimension CC and Value dimension MM, the main cost is O(NCM)O(NCM). Streaming Decode holds CM+CCM+C state, fixed with respect to history length. Whether this beats Full Attention depends on N,C,MN,C,M, parallelism, and kernels. Most importantly, the code is exact for this Kernel Attention; it is not an error-free rearrangement of standard Softmax Attention.

The chapter's executable tests compare the streaming prefix-state implementation with an explicitly constructed lower-triangular Kernel Attention. Outputs and gradients match in float32 and float64.


24.5 Infini-attention: cross-segment state is what stays fixed

Infini-attention divides input into fixed-length segments. Every layer and Head does two jobs:

  1. exact causal scaled dot-product Attention inside the current segment for fine local detail;
  2. retrieval from the previous segment's compressed state, followed by a state update after the current segment is processed.
Infini-attention using local Attention within a segment and M plus z state across segments

For one Head, the state is not merely a d_model × d_model matrix. It is:

M: [d_key, d_value]
z: [d_key]

Retrieve from old state:

Amem=ϕ(Q)Ms1ϕ(Q)zs1A_{mem}=\frac{\phi(Q)M_{s-1}}{\phi(Q)z_{s-1}}

After producing the segment output, update with its K and V:

Ms=Ms1+ϕ(K)TV,zs=zs1+tϕ(Kt)M_s=M_{s-1}+\phi(K)^TV,\qquad z_s=z_{s-1}+\sum_t\phi(K_t)

The paper also evaluates a Delta update; we use its direct Linear version here. Local output AdotA_{dot} and retrieved memory output are mixed with one learned scalar per Head:

A=σ(β)Amem+(1σ(β))AdotA=\sigma(\beta)A_{mem}+(1-\sigma(\beta))A_{dot}
import torch
import torch.nn.functional as F


def phi(x):
    return F.elu(x) + 1.0


def retrieve_memory(q, memory, normalizer):
    q_features = phi(q)
    return (q_features @ memory) / (
        q_features @ normalizer
    ).unsqueeze(-1).clamp_min(1e-12)


def infini_segment(q, k, v, memory, normalizer, gate_logit):
    """One head and one segment; q/k [N,D_k], v [N,D_v]."""
    memory_output = retrieve_memory(q, memory, normalizer)
    local_output = F.scaled_dot_product_attention(
        q[None, None], k[None, None], v[None, None],
        is_causal=True, dropout_p=0.0,
    )[0, 0]
    gate = torch.sigmoid(gate_logit)
    output = gate * memory_output + (1.0 - gate) * local_output

    k_features = phi(k)
    next_memory = memory + k_features.T @ v
    next_normalizer = normalizer + k_features.sum(dim=0)
    return output, next_memory, next_normalizer

Order matters: the current segment first reads Ms1,zs1M_{s-1},z_{s-1} and writes Ms,zsM_s,z_s only after producing its output. The memory branch therefore cannot reveal a future position from the current segment; local causal Attention handles that segment.

24.5.1 What “infinite” means

Per layer and per Head, cross-segment state contains dkdv+dkd_kd_v+d_k numbers regardless of the number of processed segments. For fixed segment length SS and total length TT:

  • local Attention costs roughly O(TSd)O(TSd) in total, not O(1)O(1) total computation;
  • compressed state is O(1)O(1) with respect to TT;
  • temporary local-Attention memory still depends on SS and the kernel.

“Infinite” means that a stream need not have a preset total number of segments while the recurrent state remains bounded. It does not mean lossless, random access to every historical token. Many K/V bindings accumulate in one matrix, so interference and information loss are part of the compression.

24.5.2 Paper results are not Gemini 1.5 architecture disclosure

After the corresponding training, the Infini-attention paper reports a 1B model on a 1M-length passkey-retrieval task and an 8B model on 500K-length book summarization. Those are paper-specific models, tasks, and training conditions.

The Gemini 1.5 technical report appeared in March 2024; the first Infini-attention arXiv version appeared in April 2024. Neither public report states that Gemini 1.5 uses Infini-attention. Equating them because both address long context is speculation, not an architecture fact.


24.6 Put each method back on the right axis

MethodWhat changesMain term for total length TTExact historical retention?Important condition
Full + FlashAttentionKernel / I/OO(T2d)O(T^2d) arithmeticYes, inside the windowFull score matrix is not written to HBM
Sliding WindowConnection graphO(Twd)O(Twd)Outside-window influence is indirectFixed ww and a kernel that exploits structure
Longformer / BigBirdLocal, global, random graphO(T(w+g+r)d)O(T(w+g+r)d)Only allowed edges are exactOriginal claims have task and graph boundaries
Kernel Linear AttentionSimilarity and orderO(TCM)O(TCM)Compressed into S,ZS,ZNot a simple Softmax equivalence
Infini-attentionRecurrent cross-segment stateLinear in TT for fixed segmentCross-segment history is compressedRequires adapted training; local Attention remains

Some combinations are mathematically compatible, but that does not mean every framework provides an efficient fused kernel. FlashAttention, GQA, block-sparse masks, Rolling Cache, and compressed memory operate at different layers. Every added mechanism needs fresh validation of correctness, quality, peak memory, and measured throughput.


24.7 Chapter summary

  1. Full Attention has quadratic pairwise work. Materialized N2N^2 score storage and linear KV Cache are separate bills.
  2. Sparse Attention saves work only with a kernel that skips empty blocks; a dense matrix plus mask merely produces a sparse result.
  3. Longformer and BigBird are linear only when window, global, and random counts stay fixed. Theoretical expressivity is not a quality guarantee on arbitrary tasks.
  4. Linear Attention defines a new Kernel similarity through a feature map. It is not standard Softmax Attention with different parentheses.
  5. Infini-attention keeps M,zM,z bounded with respect to total history, but total computation grows with processed segments and cross-segment history is lossy.
  6. Public evidence does not establish that Gemini 1.5 uses Infini-attention.

References

  1. Longformer: The Long-Document Transformer (Beltagy et al., 2020)
  2. Big Bird: Transformers for Longer Sequences (Zaheer et al., 2020)
  3. Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention (Katharopoulos et al., 2020)
  4. Leave No Context Behind: Efficient Infinite Context Transformers with Infini-attention (Munkhdalai et al., 2024)
  5. Mistral 7B (Jiang et al., 2023)
  6. Gemini 1.5: Unlocking multimodal understanding across millions of tokens of context (Gemini Team, 2024)
  7. PyTorch FlexAttention documentation

Next chapter

This chapter quietly assumed that the model knows relative positions: a window must slide in a direction, causality needs an order, and segments need a position scheme. Chapter 25 returns to that foundation and follows positional encoding from absolute positions to relative distance, rotation, and linear bias.

Cite this page
Zhang, Wayland (2026). Chapter 24: Sparse Attention and Infini-attention. In Transformer Architecture: From Intuition to Implementation. https://waylandz.com/llm-transformer-book-en/chapter-24-sparse-infinite-attention/
@incollection{zhang2026transformer_en_chapter-24-sparse-infinite-attention,
  author = {Zhang, Wayland},
  title = {Chapter 24: Sparse Attention and Infini-attention},
  booktitle = {Transformer Architecture: From Intuition to Implementation},
  year = {2026},
  url = {https://waylandz.com/llm-transformer-book-en/chapter-24-sparse-infinite-attention/}
}