How AI Works

Attention

Every token looks at every other token. That's the trick.

The Hook: Context Without Memory

Earlier language models processed tokens in isolation. "Bank" in sentence 5 had no connection to "river" in sentence 1. The model couldn't maintain context across long sequences.

Attention changed everything. Now every token in a sequence can attend to (look at, weigh the importance of) every other token simultaneously. This is the core innovation of the transformer architecture, introduced in "Attention Is All You Need" (2017). It won. Completely.

Key Insight

Attention is a weighted lookup. For each token, the model computes how much it should "pay attention" to every other token, based on learned relevance. The result is a context-aware representation for each position that depends on the full input.

The Three Key Matrices

Attention uses three learned projections: Query (Q), Key (K), and Value (V). These are learned during training. For each token, we compute how much it queries other tokens, how much other tokens are "keys" that match that query, and what values should be retrieved.

// Scaled dot-product attention (the math) def attention(Q, K, V): // Q, K, V are shape (seq_len, d_k) scores = Q @ K.T // (seq_len, seq_len) scores = scores / sqrt(d_k) // scale by sqrt(dimension) weights = softmax(scores, axis=-1) return weights @ V // (seq_len, d_k) // What this does: // - scores[i][j] = how much position i asks about position j // - weights[i] = how position i distributes its attention // - output[i] = weighted sum of values, weighted by attention

The Step-by-Step Process

Step 1
Linear Projections
Step 2
Compute Scores
Step 3
Scale
Step 4
Softmax
Step 5
Weighted Sum

Step 1: Each token embedding gets projected into Q, K, V vectors via learned weight matrices.

Step 2: For position i, compute scores[i][j] = how much token i queries token j. Dot product of Q_i with K_j.

Step 3: Scale by sqrt(d_k). This prevents softmax saturation (gradients vanishing) for large embedding dimensions.

Step 4: Softmax along each row: converts scores to probabilities that sum to 1. Now "how much attention does i pay to j?" is a probability.

Step 5: Multiply the attention weights by V matrices. Each token output is a weighted blend of all value vectors.

Multi-Head Attention

One attention operation is called a head. A transformer runs multiple heads in parallel (typically 12-96). Each head has its own Q, K, V projections. The outputs are concatenated and projected again. This lets the model attend to different aspects of the relationship simultaneously.

MULTI-HEAD ATTENTION (h heads) Input embeddings (seq_len, d_model) ┌────┴────┐ │ split h │ └────┬────┘ ┌───────┼───────┐ │ │ │ HEAD_1 HEAD_2 HEAD_h (Q₁K₁V₁)(Q₂K₂V₂)(QₕKₕVₕ) │ │ │ └───────┼───────┘ │ concat │ ▼ linear ▼ Output (seq_len, d_model) Each head learns different attention patterns: Head 1: syntax / grammar Head 2: coreference (pronouns → antecedents) Head 3: semantic relationships ...

Research on analyzing attention heads has shown specific heads specialize: some track subject-verb agreement, some resolve pronouns, some detect synonyms. Different heads learn different linguistic features from the same input.

Why Softmax?

The softmax at the end of attention computation is critical. Softmax converts arbitrary scores into a probability distribution that sums to 1. This means:

Sparse attention: One token attends strongly to a few relevant tokens, weakly to most others. No binary hard cuts.

Differentiable: Softmax is smooth and differentiable everywhere, so gradients flow through it during backpropagation.

Competitive: Because probabilities sum to 1, paying more attention to one token means paying less to others. This creates meaningful tradeoffs.

The Quadratic Problem

Attention computes pairwise scores for every position against every other position. For a sequence of length N, this is O(N²) in memory and compute. For N=10,000 tokens (roughly a short paper), that's 100 million operations. For N=1,000,000 (long document), it's 1 trillion.

This is why context windows are expensive. Extending context requires either more memory, more compute, or approximations. Flash attention, sparse attention, and linear attention variants all attack this problem from different angles.

The Key Takeaway

Attention lets every token dynamically weigh every other token's importance. Q/K/V projections make this learned rather than hardcoded. Multi-head attention runs several attention operations in parallel, capturing diverse relationships. The result: context-aware representations without recurrence, without sequential processing, and with direct access to any token from any position.