Learn AI Math

Topic 3 of 20 · 38 words

Transformer Architecture

Inside the Transformer block: self-attention, queries, keys, values, positional encodings, feed-forward layers, and residual connections.

✎ Napkin Diagram · Topic 03Transformers & Attention: The Smart Highlighter
PNG ↗
Transformers & Attention: The Smart Highlighter Simple cartoon diagram showing how a Transformer reads a whole sentence at once and shines a bright highlighter beam from the word "IT" back to "trophy" so it knows what "IT" means. Read the whole sentence at once & connect related words:"The trophy""didn’t""fit""in the bag""because""IT""was big"Strong connection! (92%)Weak (5%)"was big" tells the AI that "IT" means the trophy, not the bag!That spotlight is called "Self-Attention" — the secret inside every Transformer. getaimath.com
Older AI read word-by-word and often lost track of earlier words; a Transformer highlights which words connect to which.

The 38 words, in learning order

Start at 1. Each word builds on the ones before it.

  1. TransformerA Transformer is a type of neural network design that reads all the words in a passage at once and works out how they relate, using attention.
  2. Self-AttentionSelf-attention lets every word in a sentence look at every other word and decide which ones matter most for understanding it.
  3. Attention MechanismAn attention mechanism is a way for a model to focus on the most useful parts of its input when producing each piece of output.
  4. Multi-Head Attention (MHA)Multi-head attention runs several attention processes side by side, so the model can notice different kinds of relationships at once.
  5. Query (Q)In attention, the query is a list of numbers that represents what a word is looking for in other words.
  6. Key (K)In attention, a key is a list of numbers that describes what a word offers, so queries can find it.
  7. Value (V)In attention, a value is the actual information a word passes along when another word pays attention to it.
  8. Query MatrixThe query matrix is a grid of learned weights that turns each word's numbers into its query.
  9. Key MatrixThe key matrix is a grid of learned weights that turns each word's numbers into its key.
  10. Value MatrixThe value matrix is a grid of learned weights that turns each word's numbers into the value it shares.
  11. Attention ScoresAttention scores are numbers that show how much each word should focus on every other word.
  12. Scaled Dot-Product AttentionScaled dot-product attention is the standard math recipe for attention: compare queries and keys, scale the results, softmax them, and mix the values.
  13. Causal AttentionCausal attention only lets each word look at the words before it, never the words after.
  14. Masked AttentionMasked attention is attention where some positions are hidden, so certain words cannot look at certain other words.
  15. Cross-AttentionCross-attention lets one part of a model pay attention to a different input, like a decoder looking at an encoder's output.
  16. Multi-Query Attention (MQA)Multi-query attention is a faster type of attention where all heads share one set of keys and values but keep separate queries.
  17. Grouped-Query Attention (GQA)Grouped-query attention is a middle ground where groups of heads share keys and values, balancing speed and quality.
  18. FlashAttentionFlashAttention is a clever way to compute attention on GPUs that is much faster and uses much less memory, while giving the same results.
  19. Attention MaskAn attention mask is a pattern that tells the model which positions it may or may not attend to.
  20. Positional EncodingPositional encoding adds information about word order, so the model knows which word came first, second, and so on.
  21. Rotary Positional Embedding (RoPE)RoPE is a popular way to tell a model about word positions by rotating its query and key numbers by an angle based on position.
  22. ALiBiALiBi is a simple way of handling word position by adding a penalty to attention scores based on how far apart two words are.
  23. Feed-Forward Network (FFN)A feed-forward network is the part of each Transformer block that processes each token on its own after attention.
  24. Multi-Layer Perceptron (MLP)A multi-layer perceptron is a basic neural network made of layers of neurons where every neuron connects to every neuron in the next layer.
  25. Activation FunctionAn activation function decides how strongly a neuron "fires" by transforming its total input into its output.
  26. ReLUReLU is a simple activation function that outputs 0 for any negative number and leaves positive numbers unchanged.
  27. GELUGELU is a smooth activation function that works like a softer ReLU, letting small negative values through a little.
  28. SwiGLUSwiGLU is an activation design used in many modern LLMs that adds a "gate" so the network can control how much information passes.
  29. Layer NormalizationLayer normalization rescales the numbers in each layer so they stay in a steady, healthy range during training.
  30. RMSNormRMSNorm is a simpler, faster version of layer normalization that only rescales numbers by their overall size.
  31. Residual ConnectionA residual connection lets a layer's input skip ahead and be added to its output, creating a shortcut through the network.
  32. Transformer BlockA Transformer block is one repeated layer of a Transformer, usually made of attention, a feed-forward network, normalization and residual connections.
  33. Hidden DimensionThe hidden dimension is the number of values used to represent each token inside the model.
  34. Hidden StateA hidden state is the list of numbers representing a token at some point inside the model.
  35. Intermediate DimensionThe intermediate dimension is the larger size that the feed-forward network expands each token to before shrinking it back.
  36. Model DepthModel depth is how many layers, or Transformer blocks, a model has stacked on top of each other.
  37. Model WidthModel width is how big each layer is, mostly set by the hidden dimension and number of attention heads.
  38. Architecture ConfigurationAn architecture configuration is the list of design settings that define a model's structure, like its number of layers, hidden size and attention heads.

How Transformer Architecture fits together

Before the Transformer arrived in 2017, language models read sentences word by word like a conveyor belt and quickly forgot earlier words. Self-attention changed that by letting every token look directly at every other token at the same time using Queries (what I am looking for), Keys (what I contain), and Values (what information I pass along).

A full Transformer stacks dozens of identical blocks: an attention layer mixes context across tokens, a feed-forward network (MLP/SwiGLU) processes each token's representation, normalization keeps numbers stable, and residual skip connections give gradients a clean highway during training.