


Transformers power modern language models, but explanations often jump from a colorful attention diagram to dense matrix notation. This series takes the middle route: clear intuition, enough mathematics to verify the mechanism, and practical implications for model architecture and GPU inference. It assumes familiarity with programming, arrays and basic algebra, not previous experience building Transformers.
Reading path. Parts 1–4 build one attention calculation. Parts 5–8 assemble a Transformer block, including positional information and feed-forward networks (FFNs). Parts 9–13 connect the math to memory-efficient GPU execution and text generation. Parts 14–15 close with training, implementation and evidence-based performance analysis.
Editorial conventions. denotes token positions, the number of hidden features per token, Query/Key width, batch size and attention-head count. Dimensions are usually written as rows × columns. Approximate costs are analytic estimates, not measurements of a particular GPU.
Modern language models generate text one token at a time, yet they learn from long sequences of tokens that are already known. This sounds contradictory until we distinguish two things: the information a prediction is allowed to use, and whether several training positions can be evaluated together.
Causal self-attention enforces the first rule. Parallel GPU computation makes the second possible. Together they explain how a model trained on complete sequences can later generate unknown tokens sequentially.
An autoregressive model predicts the next token from the prefix available so far. If a sequence is , the probability chain rule expresses its joint probability as
Read this as a product of conditional probabilities: the probability assigned to token depends on the preceding tokens. The right-hand terms are next-token probabilities, not the attention weights that describe how one Query mixes Value vectors within a layer.
A decoder cannot meaningfully condition on an unknown future generated token. During generation, it must use the prefix available now, choose the next token, extend the prefix, and repeat.

During training, the example sequence is already provided by the dataset. Suppose the tokens are “The”, “cat”, “sleeps”, “on”, “the”. The model can process the sequence with causal masking, producing one set of logits at each input position. Each position is trained to predict the following token, so targets are shifted by one position.
The position reading “The” should learn to predict “cat”; the position reading “The cat” should learn to predict “sleeps”; and so on. The future target is available to the loss calculation after the model has made its prediction, but the predicting position cannot look ahead through attention to obtain the answer prematurely.
Because the full training sequence exists up front, many masked Query rows and their losses can be computed in parallel. This is not the same as parallelizing the actual choice of previously unknown next tokens during free generation.
A logit is an unnormalized score for a possible vocabulary token. A vocabulary softmax converts logits into a probability distribution over possible next tokens. The training target identifies which vocabulary token should have been predicted.
A widely used objective is cross-entropy. For a correct target token assigned probability , its contribution to the loss is . This makes intuitive sense: a confident correct prediction has close to one, so the loss is small; assigning the correct target an extremely small probability gives a large penalty.
For example, , whereas . Across scored positions, an illustrative average objective is
where is the correct next-token target at position and is the model probability assigned to that target. This is not the attention distribution over Keys.
The matrices , and are trained parameters. There is no human-written rule that assigns one head the job of recognising verbs or another the job of resolving pronouns. Instead, a prediction error generates gradients that travel backward through the model.
For a parameter , simple gradient descent has the form
where is the learning rate. A change to a Query projection may alter attention scores, which may alter softmax weights, Value mixtures, later hidden states, vocabulary logits and finally the loss. Training adjusts parameters through these linked sensitivities. Real optimisers may be more sophisticated than basic gradient descent, but the principle is the same.
An attention head produces weights across Key positions: “How much of each Value vector is mixed into this Query’s contextual result?” A language-model output layer produces probabilities over vocabulary tokens: “Which token might be generated next?” These are both called probabilities or distributions, but they live in different spaces and serve different purposes.
An attention weight of 0.8 for a source position does not mean that the model has an 80% chance of generating the word at that position. It means that, within the particular head and Query row, 80% of that head’s attention weight was assigned to the Key position, before all later layers and output processing.
Takeaway. Autoregression defines the permitted information dependency for text generation. Shifted targets and causal masks let an entire known training sequence supply many supervised next-token examples in parallel. Gradients, not manually assigned linguistic roles, shape the attention projections and FFNs.
Next: Part 12 follows a real inference request through prompt prefill, the first generated token and incremental decoding.