Shahzad Ali · Learning notes
Inside a transformer.
From text, to numbers, to the next token.
My notes on what happens inside a GPT-style language model. A sequence of calculations turns the available context into a prediction of what comes next.

Follow the flow
A response grows
one token at a time.
A simplified decoder-only language model. The example is scripted to show the loop; token boundaries vary between models.
The cat sat on the
- 01Tokens + positionRepresent the input as numbers in order.
- 02Attention + feed-forwardPass through the trained layers.
- 03Next-token scoresForm a distribution over the vocabulary.
- 04Choose + appendThe selected token joins the context.
Use the expanded context for the next prediction.
The trained weights stay fixed.Ready to predict the first continuation.
Example continuation: “ mat.” Two illustrated steps: mat, then . Generation can then finish with an end-of-sequence token.
Tokenisation
Text becomes token IDs
A tokeniser splits text into pieces and assigns each piece an ID. A piece can be a word, part of a word, punctuation or a space-related sequence.
The word boxes in the picture make the idea visible. A real tokeniser may split the same sentence differently, depending on its vocabulary.
TokenisationEmbeddings and position
IDs become useful numbers
An embedding maps each token ID to a vector: a list of numbers. Position information lets the network account for order.
“The dog chased the cat” and “The cat chased the dog” contain the same words in different roles. Order changes the task the model has to solve.
EmbeddingsSelf-attention
Each position gathers context
Attention lets each position draw on useful information from the available tokens. It calculates how strongly to combine information from each permitted position.
Queries and keys calculate the matching scores; values carry the information being combined. A causal decoder uses the current and earlier positions. These attention scores change with the input; the trained parameters stay fixed during ordinary inference.
AttentionFeed-forward layers
The representations change again
After attention combines information across tokens, a feed-forward network transforms the numbers at each position. Repeating these blocks develops the representation used for prediction.
Residual connections carry information around a block, and normalisation helps control numerical scale. The original transformer combines these ingredients; later architectures alter the details.
Transformer blocksThe output
Scores become a next-token choice
The final representation produces a score for each token in the vocabulary. Softmax turns the scores into a probability distribution. A decoding rule chooses a token from it.
A system might select the highest-probability token or sample from several plausible ones. This distribution describes a continuation of the text; factual accuracy requires separate checking.
ProbabilityRepeat the loop
The chosen token joins the context
After a token is selected, it becomes part of the sequence used to predict the next one. The response grows until a stopping condition is reached.
Implementations often reuse stored key and value vectors from earlier tokens, reducing repeated calculation. Context capacity is limited, and generation speed depends on the model and the hardware.
ContextTraining versus inference
Training sets the knobs
Training measures prediction error and uses it to adjust parameters over many examples. Ordinary inference uses those learned parameters to process the current context.
During training, a known sequence supplies targets for many positions at once while causal masking preserves the order of prediction. During generation, each selected token feeds the next step.
How training worksThe wider family
One design, several uses
The original transformer used an encoder and a decoder for translation. BERT uses a bidirectional encoder. GPT-style generators use a causal decoder.
Architecture and training objectives shape what the model can do. These calculations alone leave questions about consciousness and subjective experience open.
The transformer family
Sources behind the technical details
Sources for these notes.
Reviewed . This is a simplified account of an autoregressive decoder-only language model. Exact architecture, tokeniser and decoding choices vary.
- Primary research · First released
Attention Is All You Need ↗
Vaswani and colleagues. Introduces the transformer. Sections 3.1–3.5 describe attention, masking, feed-forward networks, residual connections, normalisation, embeddings and position.
- Primary research · First released
Language Models are Few-Shot Learners ↗
Brown and colleagues. Describes autoregressive language modelling, whole-model parameter counts and learning from context with fixed trained weights. Its results also document limitations.
- Primary research · First released
BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding ↗
Devlin and colleagues. Masked-language training supplies targets from the text itself. BERT uses bidirectional context, showing how transformer designs can serve different tasks.
- Primary research · First released
Efficient Memory Management for Large Language Model Serving with PagedAttention ↗
Kwon and colleagues. Explains key–value caching during autoregressive generation and how the vLLM system manages that memory.