All models

Cottention

BERT with softmax attention replaced by cosine attention, which uses memory linear in sequence length.

Language · 571 operations Checked

100%

Showing the top corner; full diagram not loaded yetShowing a small copy; full diagram not loaded yetLarge diagram, not loaded yet

Open SVG

Loading diagram (31 KB)

Open in a new tab

Click or tap a spot to read it up close. Scroll or drag inside the diagram to move through it. Zoom with + and − or Ctrl and scroll. Press 0 for the whole graph. Swipe inside the diagram to move through it. Use + and − to zoom.

At a glance

Parameters
108.9M
Operations
571
Framework
PyTorch
Recurrent
No
Published
Lecture notes in networks and systems (arXiv 2024)
Cited by
2 (as of 28 October 2026)
Authors
Gabriel Mongaras, Trevor Dohm, Eric C. Larson
Checked
Passed: the trace was replayed operation by operation and matched the model's own output for this input.

Files

The diagram is a drawing of a TorchLens trace of the source code. It contains no code or weights.

Details
Stable ID
m15460
Graph hash
2e26382a08930019b2dfb7fb6eeb678d537a57d21d3c0ec826db456c1e3ab34d
Skeleton hash
7fff01bc34bbf37d8e8a14d0f31b70479706628ad04842538eabae7aeb38f431
Architecture hash
9204b339666ea2f3be4a3bcbd61707e61bfd4878bfe56d2f77eb35f928f20696
Diagram SHA-256
5a36faff6758033b1986ff5dab281d447f8265210afd6a9042362ad33b0170d5
TorchLens version
2.36.0
Renderer
graphviz 2.43.0+pyvips 3.2.0+libvips 8.18.7+render-opts@06c1f0d3
Check
Forward pass replayed in PyTorch

What it is

Cottention is an attention mechanism that replaces the softmax operation in a transformer with cosine similarity. The motivation is memory: softmax attention needs an amount that grows quadratically with sequence length, which makes longer sequences hard to process. Rearranging the cosine attention equation brings this down to memory that grows linearly with sequence length.

The paper tests the idea in a bidirectional setting with BERT and in a causal setting with a variant of GPT-J. In each, the attention mechanism is replaced with cosine attention and all other architectural components are kept unchanged. The query and key matrices are L2 normalized along their row vectors before being multiplied. Because cosine attention can be unstable in training, the value is divided by the sequence length raised to the power of a learned scalar that is passed through a sigmoid. A bidirectional model such as BERT can also compute the outer product of keys and values first, an ordering that is better when the sequence is longer than the key dimension.

The paper further shows that Cottention can be rewritten as a recurrent neural network with a finite hidden state, which allows constant memory use during inference. On the GLUE benchmark, the paper reports that the BERT model with cosine attention performs comparably to its softmax counterpart. The repository's results table lists a GLUE average of 81.8 for the cosine BERT.

Key idea. Cottention replaces softmax attention with cosine similarity and rearranges the attention equation to reach memory linear in sequence length, making it more memory-efficient than softmax attention.

  • Cottention
  • cosine attention
  • Cottention Transformer
  • linear transformer
  • linear attention
  • BERT-Cos
  • efficient attention
  • cosine similarity attention
  • linear memory attention

AI-written summary, based on the paper and checked automatically against it; not reviewed by a person. Corrections welcome.

Cite the paper

@misc{mongaras2024cottentionlineartransformerscosine,
      title={Cottention: Linear Transformers With Cosine Attention}, 
      author={Gabriel Mongaras and Trevor Dohm and Eric C. Larson},
      year={2024},
      eprint={2409.18747},
      archivePrefix={arXiv},
      primaryClass={cs.LG},
      doi={https://doi.org/10.1007/978-3-031-92602-0_32},
      url={https://arxiv.org/abs/2409.18747}, 
}