Cure the headache of Transformers via Collinear Constrained Attention

Shiyi Zhu, Jing Ye, Wei Jiang, Qi Zhang, Yifan Wu, Jianguo Li · DOI

TL;DR

This paper introduces Collinear Constrained Attention (COCA), a novel attention mechanism that fuses position embeddings (specifically RoPE) with self-attention. COCA addresses an identified anomaly in how RoPE interacts with attention, significantly improving Transformers' ability to handle much longer context windows than they were trained on, without requiring extensive fine-tuning.

What Problem Does This Paper Address?

Transformers, the backbone of modern Large Language Models (LLMs), are theoretically capable of extrapolating to sequence lengths longer than their training data. However, in practice, this "long context extrapolation" is a significant challenge. Existing methods often focus on either attention mechanisms or position embeddings in isolation, neglecting their intrinsic relationship. This paper identifies a specific issue: an "anomalous behavior" arising from the interaction between Rotary Position Embedding (RoPE) and the standard self-attention mechanism, particularly at the boundaries of the context window. This anomaly hinders the model's ability to generalize to longer sequences.

Methods

The core innovation is the Collinear Constrained Attention (COCA) mechanism. COCA enforces a "collinear constraint" between the query (Q) and key (K) vectors after RoPE has been applied. This is achieved by initializing the angle between corresponding 2D slices of Q and K to zero, effectively making them collinear. This constraint is integrated into the attention calculation, ensuring a seamless fusion of RoPE's positional information with the self-attention mechanism. The authors also propose an optimized implementation that acts as a drop-in replacement for existing transformer architectures, minimizing computational and spatial overhead. They also explore a "slack" version of the constraint to potentially improve performance.

Datasets

The paper uses the following datasets:

  • Pile: A large, diverse dataset for training.
  • BookCorpus: Used for training.
  • Wikipedia Corpus: Used for training.
  • Manually collected open-source code: Integrated for training.
  • PG-19: Used for evaluating long sequence language modeling performance.

Key Insights and Ideas

  • RoPE and Attention Have a Hidden Relationship: The paper highlights that the interaction between position embeddings (specifically RoPE) and the self-attention mechanism is not well-understood, and this interaction can lead to undesirable behaviors, especially at context window boundaries.
  • Collinearity is Key: By enforcing a collinear constraint between query and key vectors after RoPE, COCA effectively resolves these boundary issues and significantly enhances long-context extrapolation.
  • Minimal Overhead, Maximum Gain: COCA is designed to be a drop-in replacement with minimal computational and spatial complexity, making it practical to integrate into existing LLMs.
  • Dynamic NTK Synergy: Combining COCA with other RoPE extension methods like Dynamic-NTK further boosts performance, suggesting a synergistic effect.
  • Slack Constraint Advantage: Surprisingly, a "slack" version of the collinear constraint (COCA-Slack) showed comparable or even better performance in some tasks, particularly passkey retrieval, suggesting that strict adherence might not always be optimal.

Key Results

  • Dramatic Context Extension: A COCA-based GPT model trained with a 512-token context length could seamlessly extend to 32K tokens (a 60x increase) without fine-tuning, showing significantly lower perplexity than RoFormer.
  • LLaMA-7B Improvement: Fine-tuning LLaMA-7B with COCA allowed it to extrapolate to 32K tokens with only 2K training length, achieving much lower perplexity than LLaMA-7B with other methods.
  • Passkey Retrieval Accuracy: COCA consistently achieved superior passkey retrieval accuracy compared to baselines, demonstrating its effectiveness in capturing long-range dependencies. For instance, RoFormer+NTK & COCA maintained 50% accuracy even when the test sequence length was 16x its training length.
  • Comparable Inference Speed: COCA's inference speed is comparable to vanilla self-attention, with only a slight increase (5-10%).

Limitations and Future Work

  • RoPE Specificity: The current work primarily focuses on RoPE. The authors acknowledge that further investigation is needed to understand COCA's applicability to other positional encoding methods.
  • Slack Constraint Understanding: The superior performance of the slack constraint in certain tasks warrants further theoretical investigation to understand the underlying mechanisms.
  • Generalizability: While COCA shows promise, its applicability to more general methods beyond RoPE needs to be explored.

Summary updated

BibTeX
@inproceedings{zhu2024cure,
  author = {Shiyi Zhu and Jing Ye and Wei Jiang and Qi Zhang and Yifan Wu and Jianguo Li},
  title = {{Cure the headache of Transformers via Collinear Constrained Attention}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.acl-long.233},
}
Figure