Mega: Moving Average Equipped Gated Attention

Xuezhe Ma, Chunting Zhou, Xiang Kong, Junxian He, Liangke Gui, Graham Neubig, Jonathan May, Luke Zettlemoyer

TL;DR

Mega (Moving Average Equipped Gated Attention) is a new sequence modeling architecture that combines the local inductive bias of Exponential Moving Averages (EMA) with the global modeling power of gated attention. It outperforms both Transformers and State Space Models (SSMs) across diverse tasks while offering a "Mega-chunk" variant that achieves linear time and space complexity without significant quality loss.

What Problem Does This Paper Address?

While Transformers have revolutionized sequence modeling, they suffer from two major flaws:

  1. Weak Inductive Bias: The standard attention mechanism is position-agnostic. It treats every token pair equally, meaning it has to "learn" that nearby tokens are often more relevant than distant ones.
  2. Quadratic Complexity: The computational cost grows quadratically ($O(n^2)$) with sequence length, making it prohibitively expensive for long-form data like high-resolution images, long documents, or raw audio.

Recent alternatives like State Space Models (e.g., S4) handle long sequences well but can be mathematically complex and difficult to initialize. Mega aims to bridge this gap by creating a simpler, theoretically grounded architecture that captures both local and global dependencies efficiently.

Methods

Mega introduces several key innovations to the standard attention block:

  • Multi-dimensional Damped EMA: Before the attention step, Mega applies a damped EMA to the input. This places a strong inductive bias on local dependencies—weights decay exponentially as tokens get further apart. Illustration of EMA weights decaying over time
  • Single-Head Gated Attention: Instead of the standard multi-head attention, Mega uses a single-head gated mechanism. The authors provide a theoretical proof (Theorem 1) showing that adding a gate makes a single-head unit just as expressive as traditional multi-head attention, but with significantly less overhead.
  • Laplace Attention Function: To improve training stability compared to Softmax or squared ReLU, the authors propose a new attention function based on the Laplace distribution.
  • Mega-chunk: To achieve linear complexity, this variant splits sequences into fixed-length chunks. Crucially, because the EMA sub-layer captures context across the entire sequence, the chunked attention doesn't "lose" as much information as standard local window approaches. Mega-chunk architecture showing context flow across chunks
Figure 1: Illustration of the exponential moving average (EMA) approach, which averages the input values X with weights decaying exponentially over timesteps.
Mega-chunk architecture showing context flow across chunks

Datasets

The authors evaluated Mega on a massive battery of benchmarks:

  • Long Range Arena (LRA): A suite of six tasks (ListOps, Text, Retrieval, Image, Pathfinder, Path-X) specifically designed to test long-context modeling (up to 16K tokens).
  • Language Modeling: WikiText-103 (word-level) and enwik8 (character-level).
  • Neural Machine Translation: WMT16 English-German.
  • Image Classification: ImageNet-1k (treating images as sequences of pixels).
  • Speech Classification: SC10 (raw speech signals).

Key Insights and Ideas

  • Local + Global = Success: The "aha moment" is that attention doesn't need to do everything. By letting the EMA handle local, position-aware dependencies, the gated attention unit is free to focus on complex, global interactions.
  • Gating as an Expressivity Multiplier: The mathematical proof that a gated single-head attention is equivalent to multi-head attention is a significant simplification for future architecture design.
  • Stability Matters: The transition to Laplace attention highlights that for very long sequences, the unbounded gradients of functions like squared ReLU can lead to training instability. Laplace attention provides a bounded alternative that maintains performance. Stability comparison between Laplace and squared ReLU
Stability comparison between Laplace and squared ReLU

Key Results

Mega sets new state-of-the-art results across multiple domains:

  • LRA Benchmark: Mega achieved an average accuracy of 88.21%, significantly higher than S4 (85.86%) and vanilla Transformers (59.24%). It successfully solved the difficult Path-X task (97.98% accuracy), where standard Transformers fail completely.
  • Efficiency: On sequences of 4K tokens, Mega-chunk is 5.5x faster and uses only 13% of the memory compared to a vanilla Transformer.
  • Translation & Language Modeling: Mega-base outperformed Transformer-base by over 1.1 BLEU on WMT16 and achieved a lower perplexity on WikiText-103 while being significantly faster at inference.

Limitations and Future Work

  • Hyperparameter Tuning: The authors noted that standard data augmentation and regularization techniques (like those used in DeiT for vision) were tuned for Transformers and might be sub-optimal for Mega.
  • Multi-modality: While Mega performs well on text, images, and speech individually, the authors suggest that exploring its potential in unified multi-modal models is a promising next step.
  • Initialization: Unlike SSMs, Mega does not rely on complex initialization frameworks (like HiPPO), but further research into optimal parameter initialization for the damped EMA could yield even better results.

Summary updated

BibTeX
@inproceedings{ma2023mega,
  author = {Xuezhe Ma and Chunting Zhou and Xiang Kong and Junxian He and Liangke Gui and Graham Neubig and Jonathan May and Luke Zettlemoyer},
  title = {{Mega: Moving Average Equipped Gated Attention}},
  booktitle = {International Conference on Learning Representations},
  year = {2023},
  url = {https://openreview.net/forum?id=qNLe3iq2El},
}
Figure