Abstract: Pretrained transformer models have demonstrated remarkable performance across various natural language processing tasks. These models leverage the attention mechanism to capture long- and short-range dependencies in the sequence. However, the (full) attention mechanism incurs high computational cost -- quadratic in the sequence length, which is not affordable in tasks with long sequences, e.g., inputs with 8k tokens. Although sparse attention can be used to improve computational efficiency, as suggested in existing work, it has limited modeling capacity and often fails to capture complicated dependencies in long sequences. To tackle this challenge, we propose MASFormer, an easy-to-implement transformer variant with mixed attention spans. Specifically, MASFormer is equipped with full attention to capture long-range dependencies, but only at a small number of layers. For the remaining layers, MASformer only employs sparse attention to capture short-range dependencies. Our experiments on natural language modeling and generation tasks show that a decoder-only MASFormer model of 1.3B parameters can achieve competitive performance to vanilla transformers with full attention while significantly reducing computational cost (up to 75%). Additionally, we investigate the effectiveness of continual training with long sequence data and how sequence length impacts downstream generation performance, which may be of independent interest.
The Scaling Problem in Standard Transformers
The standard Transformer architecture faces a significant bottleneck when processing long sequences: the self-attention mechanism's complexity is quadratic ($O(n^2)$) relative to the sequence length. Doubling the input length quadruples the computational and memory requirements, making the processing of 8k+ token documents—such as academic papers or legal contracts—prohibitively expensive for most hardware.
Existing solutions typically fall into two categories:
- Sparse Attention: Tokens attend only to local neighbors or fixed grids. While efficient, this often fails to capture critical long-range dependencies.
- State Space Models (SSMs) / Linear Transformers: These compress context into a fixed-size state. While efficient, they can be difficult to implement and unstable during training compared to standard attention.
MASFormer addresses this by questioning a fundamental assumption: Do we really need every layer to attend to every token?
Methodology: MASFormer Architecture
MASFormer (Mixed Attention Span transFormer) introduces a heterogeneous attention strategy across the model's depth. Instead of applying a uniform attention mechanism, it strategically mixes two types of layers:
- Full Attention Layers: A small subset of layers ($l$) retains standard quadratic attention to capture global dependencies.
- Block Sparse Attention Layers: The remaining layers ($L-l$) utilize linear-complexity sparse attention to handle local context efficiently.
Strategic Placement and Continual Training
Empirical testing revealed that placing full attention at the bottom layers (closest to the input) is most effective. This allows the model to capture global dependencies early, which are then propagated through subsequent sparse layers.
Rather than training from scratch, the authors utilized a pre-trained 1.3B parameter GPT-style model. They extended its context window from 2k to 8k tokens through "continual training" on the PILE dataset, duplicating absolute position embeddings to accommodate the longer sequences.
Key Results and Insights
MASFormer was evaluated on long-context benchmarks including ArXiv, PubMed, and GovReport. The results demonstrate that global attention is not required at every layer to maintain high performance.
- Efficiency vs. Performance: A MASFormer configuration with only 4 full-attention layers (out of 24) matched vanilla transformer performance while reducing attention computation costs by 72%.
- Superiority over Uniform Sparsity: MASFormer consistently outperformed models using uniform block-sparse attention. For instance, on PubMed, MASFormer ($l=2$) achieved a perplexity of 8.75, significantly better than the block-sparse baseline of 10.13.
- Summarization Gains: On the QMSUM dataset, MASFormer achieved an R2 score of 8.70, actually surpassing the full-attention baseline (8.00) while using significantly less memory.
The "Perplexity Paradox"
The study found that while increasing sequence length marginally improves perplexity, it drastically improves summarization performance. This suggests that standard language modeling metrics may undervalue a model's ability to reason over long distances, whereas generative tasks like summarization highlight the true utility of long-context windows.
Conclusion and Future Work
MASFormer offers a "drop-in" efficiency boost compatible with optimized libraries like FlashAttention. While demonstrated at the 1.3B parameter scale with absolute position embeddings, future work could explore scaling to 70B+ parameter models and integrating Rotary Position Embeddings (RoPE) to reach context lengths of 100k tokens or more. Additionally, automating the allocation of full-attention layers through learnable sparsity remains a promising research direction.
Summary updated