What an LLM computes, end to end
Text is split into tokens: subword pieces from a fixed vocabulary of tens of thousands. Each token is looked up as a vector of numbers (its embedding), and the vectors flow through a stack of layers. At the end, each position's vector becomes a score (logit) for every vocabulary token, and the scores are turned into probabilities for the next token. The context window is the maximum number of tokens the model can look at. One pass predicts a next token at every position.
What the animation says
What goes in?
You type a prompt, and a reply comes back. What is computed in between? Let's follow one single pass through the model, from text in to numbers out. First question: what does the model actually receive? Letters one at a time, whole words, or something else? Pick one.
Ids pick rows
Subword pieces. The word unbelievable becomes three tokens: un, believ, and able. Each piece comes from a fixed vocabulary of tens of thousands, and has an id number. In a toy model with ten tokens, ids seven, two and five each pick one row of a table: the embedding table. Each row is a vector of d numbers. Stack the rows for T tokens and you get a T by d grid. Your turn: tap the row that token id five selects.
Layers keep the shape
The grid now passes through a stack of layers. The shape never changes: T by d in, T by d out. Inside, each position blends in information from itself and earlier positions, never later ones. So the last position has seen the whole prompt. The first has seen only itself. At the end, the model scores possible next tokens. Which positions get a next-token guess?
Scores for every token
Every position. Each final vector is multiplied by an unembedding matrix, with d rows and V columns. T by d, times d by V, gives T by V. The d's meet in the middle and drop out. Row t holds one score, called a logit, for every token in the vocabulary. In our toy, three tokens and a vocabulary of ten give three rows of ten scores. A real model does this over about fifty thousand tokens, at every position at once.
Scores to probabilities
Scores aren't probabilities yet. Take one row, with four tokens: cat, dog, the, sat. The scores are two, one, zero, zero. Raise e to each: about seven point four, two point seven, one, and one. Those add up to about twelve point one. Divide each one by that sum. Cat gets sixty-one percent, dog about twenty-two, the and sat about eight each. Now raise cat's score from two to three. What happens to sat's probability?
The whole pass
Sat drops to about four percent. Cat's bigger share squeezes everyone else, but nobody hits zero. That's the softmax, and it runs on every row of scores. So, end to end: text, token ids, embedding rows, layers that keep the shape, scores, and probabilities. One next-token distribution at every position, from a single pass. And the model only ever sees the tokens that fit in its context window.
Tokens in, scores for every token out
The pipeline with shapes
Scores to probabilities, by hand
A toy end-to-end pass in numpy
import numpy as np
rng = np.random.default_rng(0)
V, d, T = 10, 4, 3
W_E = rng.normal(size=(V, d)) # one row per token
W_U = rng.normal(size=(d, V))
ids = np.array([7, 2, 5]) # token ids x_1..x_T
X = W_E[ids] # [T, d] lookup
H = np.tanh(X @ rng.normal(size=(d, d))) # stand-in for the layers
Z = H @ W_U # [T, V] logits
P = np.exp(Z) / np.exp(Z).sum(axis=1, keepdims=True)
print(Z.shape, P.sum(axis=1)) # (3, 10) [1. 1. 1.]
Remember this
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses