The Transformer replaced recurrence with attention. Instead of processing tokens one at a time, it allows every position to directly examine other positions and decide which ones matter for its current representation.

Retro futuristic android
FIG. 01 — Multiple attention heads, multiple glamorous perspectives.

The Core Operation

Each token representation is projected into a query, key and value. The query asks what the token is looking for, the keys describe what other tokens offer, and the values contain the information that can be combined.

scores = (Q @ K.transpose(-2, -1)) / sqrt(d_k) weights = softmax(scores, dim=-1) output = weights @ V

Why Divide by the Square Root?

As the dimensionality of the keys grows, dot products can become large. Large scores push softmax toward extremely sharp distributions, which can make optimization unstable. Scaling the scores helps keep them in a more manageable range.

Multi-Head Attention

Instead of performing one attention operation, the model performs several in parallel. Different heads may learn different relationships: local syntax, long-range dependencies, entity references or other useful patterns.

One sequence, several heads, many interpretations. Very gal behavior.

Positional Encoding

Because attention alone does not preserve sequence order, the model needs positional information. In the original paper, sinusoidal encodings were added to the token embeddings. Modern systems often use learned positions or rotary position embeddings instead.

ComponentPurpose
Token embeddingRepresents token identity
Position signalRepresents order
AttentionCombines contextual information
Feed-forward blockTransforms each position independently

Residual Connections and Normalization

Residual paths allow information and gradients to travel through deep networks more easily. Normalization stabilizes activations. The exact placement of normalization has changed in many later architectures, but the basic role remains critical.

My Implementation Checklist

  1. Token and position embeddings
  2. Causal attention mask
  3. Multi-head attention module
  4. Feed-forward network
  5. Residual connections
  6. Layer normalization
  7. Final language-model head