Alternatives to dense attention

Recurrence and segment memory

An early approach to scaling long sequences was to break them into segments and propagate information across segments, effectively giving an "infinite" context length through recurrence.

Transformer-XL

Transformer-XL (2019) introduced segment-level recurrence. Instead of discarding the hidden states of the previous segment after processing, the model caches them in memory and attends to the next segment along with the cached tokens from the one before. The Query is derived from the current segment, while the Key and Value vectors are derived from the concatenation of the previous segment's output (with gradients stopped) and the current segment.

Gradients are not back-propagated to the cached segment, but the forward pass still benefits from the history, creating a sliding window of context that moves forward with every step.

Transformer-XL was also built on relative positional encodings. This was needed to prevent the cached tokens from the previous segment (segment A) and the tokens of the current segment (segment B) from clashing under absolute positional encodings.

Compressive Transformer

Compressive Transformer (2019) extended Transformer-XL by distinguishing between episodic memory (detailed recollection of recent events, working like Transformer-XL's memory) and semantic memory (storing compressed, more abstract representations when the primary memory fills up). Compression is performed using a 1D convolution that was empirically shown to work better than max or mean pooling.

During the forward pass, the Query vector attends to a concatenation of the current segment, the primary memory, and the compressed memory. Because the compression function is differentiable, the model learns during training how to compress memories while preserving the underlying information.

Possible successors to dense attention

  • Mega (2022): Moving-average equipped gated attention with exponential decay.
  • RetNet (2023 — Retentive Network: A Successor to Transformer for Large Language Models).
  • State-space models (SSMs):
    • S4
    • Hydra
    • Mamba:
      • Replaces attention with a selective scan mechanism.
      • Throughput scales linearly O(N)O(N).
      • The "KV cache" does not grow — it is a fixed-size state.
      • Struggles with "copying" tasks: because it compresses the entire history into a fixed-size state, it cannot perfectly recall a specific token from 100K steps ago if that token was not deemed "important" at the time. The transformer, by keeping the full KV cache, has "perfect memory".
    • Jamba:
      • Interleaves Mamba layers (for throughput and memory savings) with occasional transformer attention layers (for retrieval capability).
  • Interleaved local + global attention models:
    • Alternating cheap local (sliding-window or chunked) attention layers with periodic global attention layers — seen early in GPT-3 (alternating dense and locally banded sparse layers) and widely adopted in modern models like Mistral, Gemma 2/3, and Llama 4.
  • Perceiver:
    • A multi-modal model that uses a latent bottleneck to handle very large inputs by projecting them to a smaller set of latent vectors.

Tags: AI