Attention in one sentence
Attention computes a weighted average of value vectors, where the weights come from comparing a query vector to a set of key vectors. Each output position 'attends to' the inputs that matter most for it, with the weights learned end-to-end via backprop.
Self-attention is the special case where queries, keys, and values all come from the same sequence. Now every token can directly look at every other token in one step — no recurrence, no fixed-size hidden state. Long-range dependencies become trivially expressible.
The math of one attention head
Given Q, K, V (each shape [B, T, d_k]): attention(Q, K, V) = softmax(Q K^T / sqrt(d_k)) V. The sqrt(d_k) prevents the dot products from growing too large with dimension. The softmax turns the dot products into a probability distribution over input tokens. The result is [B, T, d_k] — same shape as the input.
Multi-head attention
One attention head learns one type of relationship. Multi-head attention runs many heads in parallel (each with its own Q, K, V projections) and concatenates the results. The intuition: different heads can learn different relationship types (syntax, coreference, position, semantic similarity) without competing for one set of weights.
Why attention is parallelizable
Unlike RNNs, attention can compute the output for every position simultaneously — all the dot products are independent. This is what made transformers train so much faster than RNNs on GPUs.