What is the attention mechanism?

The attention mechanism is a neural network component that, while processing one item, works out how relevant the other items in a sequence are and combines their representations by those weights; items can be subword tokens, image patches or representations from other modalities.

It differs from self-attention in scope: self-attention computes weights within one sequence, while attention in general also links two sequences, as in cross-attention. Attention weights alone do not explain a model's decision.

Topic
Machine learning, models and training
Also known as
Attention, scaled dot-product attention
Origin
2014, Bahdanau, Cho and Bengio

How the attention mechanism works

  1. Each token representation is projected by learned matrices into query (Q), key (K) and value (V) vectors.
  2. The query's dot product with every key is divided by the square root of the key dimension and turned into weights with softmax.
  3. Value vectors are multiplied by these weights and summed to give an output that blends context.
  4. Multi-head attention repeats this in parallel with different projections; cost grows with the square of sequence length.

In more detail

Scaled dot-product attention and the causal mask

In scaled dot-product attention each query vector takes a dot product with every key vector; the scores are divided by the square root of the key dimension, an optional causal mask sets future positions to minus infinity, softmax turns the scores into weights and the value vectors are summed with those weights. Dividing by the square root keeps dot-product variance near 1 in high dimensions so softmax does not saturate into tiny gradients.

Positional information and attention

Because raw attention is permutation-equivariant and has no built-in sense of token order, language models apply positional information by rotating queries and keys with RoPE or by adding distance-dependent biases to attention scores with ALiBi. Context-window extension techniques (like YaRN) largely modify RoPE positional rotations rather than the attention formula itself.

Efficient attention: MLA and hybrid designs

Beyond grouped-query attention (GQA), recent frontier and open-weight models widely adopt Multi-Head Latent Attention (MLA) to compress keys and values into a low-rank latent vector, as well as hybrid architectures combining full attention layers with sliding-window or linear or state-space attention (such as DeltaNet) to cut KV-cache memory substantially.

Prefill and decode bottlenecks

On-premise serving exposes two distinct bottlenecks: prefill (processing the input prompt in parallel, which is compute-bound and scales quadratically) and decode (generating tokens one by one, which is memory-bandwidth-bound because every step reads the entire KV cache). FlashAttention reduces memory traffic via SRAM tiling without approximating the math.

Commonly confused terms

TermHow it differs
Feed-forward layerThe attention mechanism computes token-to-token relevance weights across the sequence, while the Transformer's position-wise feed-forward network (MLP) processes each token independently and stores a large share of factual associations.

Why the attention mechanism matters for on-premises AI

Because compute grows with the square of sequence length, long documents raise GPU use and latency sharply. During generation, keys and values of earlier tokens are held in the KV cache, the main item in capacity planning. Techniques such as grouped-query attention and FlashAttention allow longer contexts and more concurrent users on the same hardware. In long contexts attention can cluster at the start and end (lost in the middle), so RAG chunk size is tuned accordingly.

ON-PREMISES EXAMPLE

A legal department plans context length and concurrent users for its in-house contract summarisation model around the memory and latency cost of attention.

Common questions

Do attention weights fully explain a model's decision?

No. They show which tokens information flows from, but transformations across layers mean they cannot explain a decision alone; interpretability needs other methods.

How is attention sped up for long contexts?

FlashAttention uses the memory hierarchy efficiently and grouped-query attention shrinks the KV cache; sliding-window and sparse attention also help.

Sources

In the glossary: Glossary, A to D

What does this term mean for your infrastructure?

We can work out together in a technical assessment how it maps onto your data, hardware and security requirements.

Request a technical assessment →

This site sets no cookies. Visitor numbers are counted in aggregate using a cookieless method that does not identify anyone. See the Privacy Policy for details.