← Back to explorer
O(N²)Beginner

Dense (Full) Attention

Every token talks to every other token

Every token attends to every other token — perfect information, quadratic cost.

Attention Pattern
↑ Queries ↑
← tokens (Keys) →
Query 0 scans all tokens

Row 0 (Query 0) attends to all N tokens. Each lit cell = one attention score computed between this query and a key.

← → Space to navigate
Intuitive Explanation
Analogy
A town hall where everyone hears everyone else

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.

The Math
01
Project tokens into Q, K, V

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.

Q = X·W_Q, K = X·W_K, V = X·W_V
02
Compute raw attention scores

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.

Scores = Q · Kᵀ / √d_k
03
Apply softmax to get attention weights

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.

A = softmax(Scores) [shape: N × N]
04
Compute weighted output

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.

Output = A · V [shape: N × d_v]
05
Why O(N²)?

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.

Memory: O(N²) Compute: O(N² · d_k)
Memory & Compute

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.

costly
Score matrix (N×N)
Must be fully materialised in HBM to compute softmax row-wise. This is the O(N²) memory wall.
moderate
Q, K, V projections (3×N×d_k)
Linear in N — not the bottleneck. These are typically much smaller than the score matrix for long sequences.
cheap
Output matrix (N×d_v)
Also linear in N. Cheap compared to the score matrix.
costly
Causal mask
A boolean N×N matrix masking future tokens. Same shape as the score matrix — adds memory but not compute.
costly
Compute (FLOPs)
2·N²·d_k for the QKᵀ multiply, plus 2·N²·d_v for the AV multiply. Both O(N²).
Tradeoffs
Advantages
+Perfect recall — every token can directly attend to every other token in one layer
+Mathematically exact — no approximation, no information loss
+Simple to implement and reason about
+Works well for short sequences (N < 2048)
Disadvantages
−O(N²) memory: 10× longer sequence = 100× more memory
−O(N²) compute: training on long documents becomes prohibitively slow
−Full N×N matrix must fit in GPU VRAM — limits maximum context length
−Slow HBM read/write bandwidth is the practical bottleneck (see Flash Attention)
When to use

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.

When to avoid

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.

Used in
Original TransformerBERTGPT-2