Attention·Mechanisms
Explained
Mechanisms
⇄ Compare
Attention pattern
Every token talks to every other token
O(N²)
↑ 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
Intuition

“Like a town hall meeting where everyone hears everyone else simultaneously.”

How it works

In attention, each word (token) asks: 'which other words should I pay attention to?' Dense attention says: look at ALL of them. Every token computes a relevance score against every other token. Note: autoregressive models (GPT, LLaMA) apply a causal mask — the upper triangle is zeroed out so each token can only attend to earlier tokens.

Key terms — click to expand
Trade-offs

Perfect information — every word can influence every other. But memory and compute scale as O(N²). A 10× longer document costs 100× more. Fine for short sequences, prohibitive for long ones.

Used in: Original Transformer, BERT, GPT-2
Interactive Tool

Complexity Calculator

Drag the slider to see real memory and compute numbers — not just O(N²). Watch Dense Attention become physically impossible while Flash and Linear stay cheap.

Parameters
Sequence Length (tokens)4k
1281k4k16k64k100k
d_model
Heads
Batch Size
GPU
Precision
Real model presets
Dense (Full) Attention
O(N²)
Fits
MEMORY2.15 GB
Compute
68.7G
Est. Time
886 µs
Flash Attention
O(N²) compute, O(N) memory
Fits
MEMORY33.6 MB
Compute
68.7G
Est. Time
886 µs
Local (Sliding Window) Attention
O(N·w)
Fits
MEMORY268.4 MB
Compute
8.6G
Est. Time
111 µs
Sparse Attention
O(N√N)
Fits
MEMORY33.6 MB
Compute
1.1G
Est. Time
14 µs
Linear Attention
O(N)
Fits
MEMORY33.6 MB
Compute
17.2G
Est. Time
221 µs
Paged Attention
O(N²) compute, O(N) memory
Fits
MEMORY35.2 MB
Compute
68.7G
Est. Time
886 µs