Cottention: Linear Transformers With Cosine Attention

Gabriel Mongaras, Trevor Dohm, Eric C. Larson
9/27/2024
cs.LG

No official code found. Best implementation: gmongaras/Cottention_Transformer (20 stars, last commit 2025-11-15).

Abstract

Attention mechanisms, particularly softmax attention, have been instrumental in the success of transformer-based models such as GPT. However, the quadratic memory complexity of softmax attention with respect to sequence length poses significant challenges for processing longer sequences. We introduce Cottention, a novel attention mechanism that replaces the softmax operation with cosine similarity. By leveraging the properties of cosine similarity and rearranging the attention equation, Cottention achieves native linear memory complexity with respect to sequence length, making it inherently more memory-efficient than softmax attention. We demonstrate that Cottention can be reformulated as a recurrent neural network (RNN) with a finite hidden state, allowing for constant memory usage during inference. We evaluate Cottention on both the bidirectional BERT and causal GPT tasks, demonstrating comparable performance to softmax attention while significantly reducing memory requirements. To ensure efficient computation, we develop a custom CUDA kernel for Cottention. Our results show that Cottention is a promising alternative to softmax attention, enabling the processing of longer sequences without sacrificing performance, due to its native linear memory complexity and ability to maintain a constant memory footprint during inference.

View on arXivView PDF

Code Implementations(1)

Code for the paper "Cottention: Linear Transformers With Cosine Attention"

200C++, CudaSep 20, 202311 months ago
attentionlinear-attentiontransformers

Similar Papers

Cite this paper

@article{mongaras2024cottention,
  title  = {Cottention: Linear Transformers With Cosine Attention},
  author = {Gabriel Mongaras and Trevor Dohm and Eric C. Larson},
  year   = {2024},
  eprint = {2409.18747},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2409.18747v1}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2409.18747.svg)](https://codeofpaper.com/paper/2409.18747)

Discussion