Why it matters

Attention is what makes transformers work. Understanding its math is understanding the core computation of every LLM. Getting comfortable with the QKV pattern unlocks everything else.

Advertisement

The architecture

Given queries Q (n × d), keys K (m × d), values V (m × d_v). Similarity = Q K^T (n × m matrix of dot products). Convert to weights with softmax over each row. Output = weights × V.

Interpretation: each query attends to all keys, weighted by similarity, and reads a weighted combination of values.

Attention computation stepsQ × K^Tsimilarity matrixsoftmaxrow-wise weightsweights × VoutputSoftmax makes each row a probability distribution over keys; weighted sum reads values
Attention math pipeline.
Advertisement

How it works end to end

Self-attention: Q, K, V come from the same input. Each position attends to all positions (including itself).

Cross-attention: Q from one sequence, K and V from another. Enables encoder-decoder connection.

Masking: causal mask sets upper triangle of similarity to -infinity. Softmax makes those weights zero. Ensures autoregressive generation.