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.
The 38 words, in learning order
Start at 1. Each word builds on the ones before it.
- 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.
- Self-AttentionSelf-attention lets every word in a sentence look at every other word and decide which ones matter most for understanding it.
- 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.
- 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.
- Query (Q)In attention, the query is a list of numbers that represents what a word is looking for in other words.
- Key (K)In attention, a key is a list of numbers that describes what a word offers, so queries can find it.
- Value (V)In attention, a value is the actual information a word passes along when another word pays attention to it.
- Query MatrixThe query matrix is a grid of learned weights that turns each word's numbers into its query.
- Key MatrixThe key matrix is a grid of learned weights that turns each word's numbers into its key.
- Value MatrixThe value matrix is a grid of learned weights that turns each word's numbers into the value it shares.
- Attention ScoresAttention scores are numbers that show how much each word should focus on every other word.
- 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.
- Causal AttentionCausal attention only lets each word look at the words before it, never the words after.
- Masked AttentionMasked attention is attention where some positions are hidden, so certain words cannot look at certain other words.
- Cross-AttentionCross-attention lets one part of a model pay attention to a different input, like a decoder looking at an encoder's output.
- 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.
- Grouped-Query Attention (GQA)Grouped-query attention is a middle ground where groups of heads share keys and values, balancing speed and quality.
- FlashAttentionFlashAttention is a clever way to compute attention on GPUs that is much faster and uses much less memory, while giving the same results.
- Attention MaskAn attention mask is a pattern that tells the model which positions it may or may not attend to.
- Positional EncodingPositional encoding adds information about word order, so the model knows which word came first, second, and so on.
- 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.
- ALiBiALiBi is a simple way of handling word position by adding a penalty to attention scores based on how far apart two words are.
- Feed-Forward Network (FFN)A feed-forward network is the part of each Transformer block that processes each token on its own after attention.
- 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.
- Activation FunctionAn activation function decides how strongly a neuron "fires" by transforming its total input into its output.
- ReLUReLU is a simple activation function that outputs 0 for any negative number and leaves positive numbers unchanged.
- GELUGELU is a smooth activation function that works like a softer ReLU, letting small negative values through a little.
- SwiGLUSwiGLU is an activation design used in many modern LLMs that adds a "gate" so the network can control how much information passes.
- Layer NormalizationLayer normalization rescales the numbers in each layer so they stay in a steady, healthy range during training.
- RMSNormRMSNorm is a simpler, faster version of layer normalization that only rescales numbers by their overall size.
- Residual ConnectionA residual connection lets a layer's input skip ahead and be added to its output, creating a shortcut through the network.
- Transformer BlockA Transformer block is one repeated layer of a Transformer, usually made of attention, a feed-forward network, normalization and residual connections.
- Hidden DimensionThe hidden dimension is the number of values used to represent each token inside the model.
- Hidden StateA hidden state is the list of numbers representing a token at some point inside the model.
- Intermediate DimensionThe intermediate dimension is the larger size that the feed-forward network expands each token to before shrinking it back.
- Model DepthModel depth is how many layers, or Transformer blocks, a model has stacked on top of each other.
- Model WidthModel width is how big each layer is, mostly set by the hidden dimension and number of attention heads.
- 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.