Megalodon: Efficient LLM Pretraining and Inference with Unlimited Context Length

Xuezhe Ma, Xiaomeng Yang, Wenhan Xiong, Beidi Chen, Lili Yu, Hao Zhang, Jonathan May, Luke Zettlemoyer, Omer Levy, Chunting Zhou

Abstract

Large Language Models (LLMs) based on the Transformer architecture face a significant computational bottleneck due to the quadratic complexity of self-attention. While sub-quadratic alternatives like State Space Models (SSMs) and linear attention have been proposed, they often struggle to match the accuracy and training stability of Transformers at scale. This paper introduces MEGALODON, a neural architecture that achieves linear computational complexity while outperforming the Llama 2 Transformer-based model in both efficiency and accuracy at the 7-billion parameter scale. By introducing technical innovations such as Complex Exponential Moving Average (CEMA) and Timestep Normalization, MEGALODON enables the processing of sequences of virtually unlimited length.

1. Introduction

The dominant Transformer architecture is constrained by a "quadratic bottleneck": doubling the input sequence length quadruples the computational cost ($O(n^2)$). This scaling behavior makes processing massive documents, long-form conversations, or high-resolution video prohibitively expensive. While researchers have explored sub-quadratic models to mitigate this, these alternatives historically fail to maintain raw accuracy and training stability when scaled to billions of parameters.

MEGALODON addresses this efficiency-accuracy trade-off. Building upon the Moving Average Equipped Gated Attention (MEGA) framework, MEGALODON introduces several architectural advancements that allow it to scale effectively to 7B parameters and beyond, maintaining stability across trillions of training tokens and generalizing to context lengths of up to 2 million tokens.

2. Architectural Innovations

MEGALODON evolves the MEGA framework through five critical technical innovations designed for stability and expressiveness at scale:

  1. Complex Exponential Moving Average (CEMA): To improve upon the limited expressiveness of standard EMAs, CEMA extends the moving average into the complex domain. This allows the model to capture more intricate temporal patterns within the data.
  2. Timestep Normalization: Traditional Layer Normalization does not account for internal covariate shift along the time dimension in auto-regressive modeling. MEGALODON introduces a method to compute cumulative mean and variance across the sequence, enabling normalization without leaking future information.
  3. Normalized Attention: To prevent training instability, the authors utilize a QK-normalization approach (similar to cosine attention), which keeps attention scores bounded and prevents gradient explosions.
  4. Pre-norm with Two-hop Residual: In deep architectures, hidden state variance can explode. MEGALODON rearranges residual connections into a "two-hop" configuration to ensure signal stability through dozens of layers.
  5. 4D Parallelism: To facilitate training on 2 trillion tokens, the authors implemented "chunk parallelism" alongside standard data, tensor, and pipeline parallelism. This allows long sequences to be distributed efficiently across multiple GPU nodes.

3. Experimental Results

3.1 Pretraining and Parameter Efficiency

MEGALODON-7B was pretrained on 2 trillion tokens using the same data mix as Llama 2. In head-to-head comparisons, MEGALODON achieved a training loss of 1.70, significantly lower than Llama 2-7B (1.75). Notably, this performance lands between the Llama 2-7B and the much larger Llama 2-13B (1.67), suggesting that the MEGALODON architecture is more parameter-efficient than standard Transformers.

3.2 Efficiency and Context Length

At a context length of 32K tokens, MEGALODON is 32% faster than Llama 2 (utilizing Flash-Attention V2). Due to its linear complexity, this efficiency gap widens as sequence lengths increase. Furthermore, MEGALODON demonstrates true length generalization; its perplexity continues to improve monotonically as context length increases, tested up to 2 million tokens.

3.3 Benchmarks

MEGALODON-7B outperformed Llama 2-7B across various standard LLM benchmarks:

  • MMLU (Knowledge): 49.8 vs 45.3
  • WinoGrande (Reasoning): 71.4 vs 69.2
  • Long Range Arena (LRA): Achieved a state-of-the-art average score of 88.63, validating its ability to handle long-distance dependencies.
  • NarrativeQA: Achieved an F1 score of 23.9, outperforming other long-context models like Xgen-7B and MPT-7B.

4. Discussion and Limitations

While MEGALODON offers superior scaling for long sequences, it does introduce a slight overhead for short-context tasks. At a 4K token context length, MEGALODON is approximately 6% slower than Llama 2 due to the complexity of CEMA and Timestep Normalization. Additionally, while the 7B results are promising, the stability and efficiency gains at the 70B+ parameter scale remain to be demonstrated.

Future work will explore the application of MEGALODON to multimodal domains, such as high-resolution video and long-form audio generation, where its ability to handle massive sequences provides a distinct advantage over quadratic architectures.

Summary updated

BibTeX
@inproceedings{ma2024megalodon,
  author = {Xuezhe Ma and Xiaomeng Yang and Wenhan Xiong and Beidi Chen and LILI YU and Hao Zhang and Jonathan May and Luke Zettlemoyer and Omer Levy and Chunting Zhou},
  title = {{Megalodon: Efficient LLM Pretraining and Inference with Unlimited Context Length}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=XlAbMZu4Bo},
}
Figure