Every token talks to every other token
Every token attends to every other token — perfect information, quadratic cost.
Row 0 (Query 0) attends to all N tokens. Each lit cell = one attention score computed between this query and a key.
Imagine a town hall meeting where every person simultaneously whispers to every other person in the room. Every pair of people exchanges a message. Nobody is left out. You get perfect information flow, but the number of whispered conversations scales as N² — double the crowd, quadruple the noise.
In attention, each token asks: 'which other tokens should I pay attention to right now?' Dense attention answers that question exhaustively — every token scores every other token. If your sequence has N tokens, you compute N × N scores.
The mechanism works by projecting each token into three vectors: a Query (Q, 'what am I looking for?'), a Key (K, 'what do I offer?'), and a Value (V, 'what information do I carry?'). Each query is then compared to every key to produce a relevance score, which is normalised with softmax to form attention weights. The output is a weighted sum of the values.
Autoregressive models like GPT apply a causal mask — the upper triangle of the score matrix is set to −∞ before softmax. This means each token can only attend to tokens that appeared before it, preventing it from 'cheating' by looking at future tokens during training.
The full N×N score matrix must be computed and stored in GPU memory. At N = 1000 tokens that is 1 million scores. At N = 16 000 tokens (a short book chapter) that is 256 million scores. This quadratic memory footprint is the fundamental limitation of dense attention and the reason every other mechanism on this page exists.
Each input token embedding x is linearly projected into three vectors using learned weight matrices W_Q, W_K, W_V. These projections are what the model learns to optimise.
Take the dot product of every query with every key. This produces an N×N score matrix. Dividing by √d_k stabilises gradients — without it, dot products grow large in magnitude as d_k increases, pushing softmax into regions with near-zero gradients.
Softmax is applied row-wise, converting raw scores into a probability distribution. Each row sums to 1. The causal mask (for autoregressive models) zeroes out entries where a query attends to a future key by replacing them with −∞ before softmax.
Multiply the attention weight matrix by V. Each output token is a weighted sum of all value vectors, where the weights reflect relevance. Tokens that received high attention scores contribute more to the output.
The bottleneck is the N×N score matrix — it requires O(N²) memory to store, and O(N²·d_k) compute to produce. Every row requires N dot products. There is no shortcut: every query must compare itself against every key.
The dominant cost is the N×N attention score matrix. At N = 4096 tokens (a long prompt) with float16, the score matrix alone occupies 32 MB per attention head — and modern transformers have 32–128 heads across many layers.
Short-to-medium sequences (up to ~4k tokens), research baselines, tasks where exact attention is critical, and any situation where you need the simplest correct implementation.
Long documents, audio, video, or any modality where sequences routinely exceed 4k–8k tokens. At N = 32k tokens the score matrix requires ~2 GB per head — entirely impractical.