Hugging Face Reads, Feb. 2021 - Long-range Transformers
Long-range attention methods attack the quadratic memory and time cost of self-attention in sequence length, and four papers illustrate the main families. Longformer combines dilated windowed and global attention to reach linear cost and serves as a drop-in replacement, so pretrained checkpoints can be adapted to long inputs. Compressive Transformer extends Transformer-XL with a compressed memory of past activations, using pooling or convolution, to model longer dependencies. Linformer exploits the low rank of attention matrices to project the sequence dimension and get linear complexity. Each paper comes with complexity analysis, main findings and open questions, such as TPU indexing costs, hyperparameter brittleness and long-target seq2seq tasks.
Related posts
- Mastering Long Contexts in LLMs with KVPress
Hugging Face mediumAdeptKV cache memory grows linearly with context length: Llama 3-70B at 1M tokens in bfloat16 needs about 330GB of KV cache, roughly 70% of total memory. NVIDIA's KVPress is a Python toolkit of "presses" that prune low-importance KV pairs during prefill via forward hooks on attention layers. Examples include KnormPress, SnapKVPress and ExpectedAttentionPress, each tuned with a compression_ratio and usable through a custom transformers pipeline. On Llama 3.1 8B at 128k context, 50% compression cuts peak memory from 45GB to 37GB and raises decoding from 11 to 17 tokens/s on an A100, while RULER benchmarks show accuracy dropping as compression rises.
- A failed experiment: Infini-Attention, and why we should keep trying?
Hugging Face hardMasterA Hugging Face reproduction of Google's Infini-attention found that retrieval quality degrades as memory is compressed more times. Ring attention, YaRN and RoPE scaling remain the better ways to extend a pretrained model's context length. Infini-attention splits the sequence into segments, runs local causal attention within each segment, and folds earlier segments into a fixed-size linear-attention memory, mixing the two outputs through a learned sigmoid gate. Small 200M-parameter models trained with Nanotron on FineWeb showed some use of the memory, but continual pretraining of Llama 3 8B failed passkey retrieval when the needle sat in an earlier segment. Most of the effort went into debugging a convergence issue, and the post lays out reproduction principles: start small, train a solid baseline, and run controlled ablations.
- Supercharging transformer residual connections
Google Research hardMasterDeepCrossAttention (DCA) replaces the fixed point-wise summation of transformer residual connections with learned, per-token routing over all preceding layer outputs. It does this through dynamic layer-mixing weights and depth-wise cross-attention with split Q/K/V, which prevents early-layer features from being diluted. On LM1B and C4, DCA matched standard transformers in up to 5x fewer training steps and hit sub-14 LM1B perplexity with 30 layers instead of 42, while adding under 0.2% parameters. It can also be retrofitted onto pre-trained models, and the post places it alongside related techniques such as attention residuals, hyper-connections and manifold-constrained hyper-connections.
- Nyströmformer: Approximating self-attention in linear time and memory via the Nyström method
Hugging Face hardMasterNyströmformer replaces quadratic softmax self-attention with an O(n) approximation built on the Nyström method, which reconstructs a matrix from a sampled subset of its rows and columns via a Moore-Penrose pseudoinverse. Sampling columns of the softmax matrix directly is impossible because each entry needs its full row for normalization. The model therefore builds landmarks from segment means of queries and keys and composes three small softmax kernels (n×m, m×m pseudoinverse, m×n), so QKᵀ is never computed. The paper reports 32–64 landmarks staying competitive at sequence lengths of 4096–8192. A walkthrough of the Hugging Face PyTorch implementation follows, along with masked-language-modeling usage of the released checkpoints.
- Sequential Attention: Making AI models leaner and faster without sacrificing accuracy
Google Research hardMasterSequential Attention is a greedy subset-selection algorithm that runs inside a single training run. It uses softmax attention scores over unselected candidates (features, embedding chunks, weight blocks) to add the most informative one at each step, and recomputes scores so the ranking reflects marginal gain and avoids redundant picks. For feature selection it matches or beats baselines on proteomics, image and activity-recognition benchmarks, and on linear regression it is provably equivalent to Orthogonal Matching Pursuit. SequentialAttention++ combines differentiable pruning with combinatorial search to produce block-sparse weights that run efficiently on GPUs and TPUs, with compression gains on ImageNet. Planned uses include recommender embedding optimization and structured LLM pruning.