Models
Which token comes next
One forward pass through a transformer, stop by stop
Every lesson about the factory ends at a GPU that reads a prompt and writes an answer. This one goes inside that GPU's work. A language model does one thing: given everything so far, it scores every token it knows for how likely it is to come next. One is picked, added to the end, and the question is asked again. That is all generation is. The model that answers in the factory is Qwen3-32B, and the numbers here are its real shape. The ideas are the same ones a model with 520 weights uses to write bad Shakespeare. Follow five words through it and watch a sixth come out.
- The question
- given everything so far, which token comes next?
- The model
- 64 layers, 32.8 billion weights
- Each token is
- a list of 5,120 numbers
- The answer is
- one score for each of 151,936 tokens
Read the whole lesson as text
1. Text becomes numbers
A model can't read letters. The first stop is a tokenizer, which cuts the text into pieces it has an entry for and replaces each piece with that entry's number. A teaching model can use one token per character, which gives about 65 tokens for the works of Shakespeare. Real tokenizers merge pieces that often appear together, so a common word is one token and a rare one is several: unbelievably becomes un, believ, able, ly. This model's vocabulary has 151,936 entries. The tokenizer is built before training and learns nothing afterwards. Everything from here on is arithmetic on numbers.
- The prompt arrives as text and is cut into five tokens.
2. A number becomes a meaning
A token number says nothing about the token. So the model keeps a table with one row per entry of the vocabulary, and each token number picks its row. A row is a list of 5,120 numbers, a point in a space with 5,120 directions. Nobody chose what the directions mean. Training moved the rows until tokens that are used alike ended up close together, which is how something learned about happy helps with glad. This table alone holds 778 million weights. The teaching model's table is 65 rows of 8 numbers, 520 weights in all, and it works the same way.
- Each token number picks one row of the table, a list of 5,120 numbers.
3. Order has to be added
The next stop compares tokens with each other, and that comparison has no sense of order built in. Left alone, dog bites man and man bites dog would look the same to it. So position has to be worked into the numbers. The classic way adds a second list of numbers to each row, one that depends only on where the token sits. This model does it differently and rotates each token's query and key by an angle that grows with its position, so that two tokens compare according to how far apart they are. Either way the result is the same kind of thing: five lists of numbers that now carry both what the token is and where it is.
- Position is worked in, so the same words in another order would give different numbers.
4. Every token asks a question
Now the tokens look at each other, and this is the only place in the model where they do. From its own numbers each token makes three shorter lists. A query: what am I looking for. A key: what do I have. A value: what I will pass on if I am asked. To build the new meaning of is, the model multiplies its query with the key of every token up to and including itself. A high product means a good match. The products are turned into shares that add up to one, and the token takes in the values of the others in those shares. A token is never allowed to look at what comes after it, because during training that would be reading the answer.
- The last token compares its query with the key of every token so far and blends their values.
5. Keys and values are kept
Look at what that step left behind. Every token made a key and a value in this layer, and a later token will need exactly those to ask its own question. They do not change when more text is added, because a token never looks forward. So the model keeps them. That store is the KV cache from the other lessons, and now its size can be worked out. A query is used once and thrown away. A key and a value are kept for each token, in each of 64 layers, in each of 8 key and value heads, as 128 numbers of 2 bytes. That is 262,144 bytes for every token of every conversation, held in GPU memory for as long as the conversation is served.
- The keys and values of all five tokens are written to GPU memory for later tokens to use.
6. Each token is worked on alone
After attention has mixed in what the other tokens had to say, each token goes through a second stage by itself. Its 5,120 numbers are stretched to 25,600, pushed through a simple curve, and brought back to 5,120. This is where the model keeps most of what it knows: about three quarters of its 32.8 billion weights are in these layers. Two things hold the whole arrangement together. The result of each stage is added to what went in, so nothing learned earlier is overwritten. And the numbers are rescaled before each stage so they stay in a workable range. Attention plus feed-forward is one block. This model runs 64 of them in a row, each with weights of its own.
- Each token's numbers pass through the feed-forward layer by themselves, and the block repeats 64 times.
7. A score for every token
After 64 blocks the model holds five finished lists of numbers, one per position. To predict what follows the prompt it needs only the last one. That list goes through one more table, which turns its 5,120 numbers into 151,936 scores, one for every token in the vocabulary. The scores can be any size and can be negative. A formula called softmax turns them into shares that add up to one: each score becomes e to its power, divided by the sum over all of them. A score that is one higher than another gets about 2.7 times its share.
- The last position's numbers are turned into one score for every token the model knows.
8. One token is drawn
The model has not chosen anything yet. It has produced a table of shares, and the last stop draws from it like a weighted dice roll. Paris will come up most of the time and London some of the time. That is why the same prompt can give different answers. Temperature is the one dial on this stop. Every score is divided by it before softmax. A temperature below one stretches the scores apart, so the favourite wins almost always. Above one squeezes them together and the long shots get a real chance. Nothing in the model changes. Only the draw does.
- The sampler turns the scores into shares and draws one token. This time it is Paris.
9. Add it and go again
Paris is added to the end of the text, and the question is asked again for the token after it. This is the whole of generation: one token per pass, each pass a full trip through 64 blocks. Here the cache pays for itself. The five old tokens are not read again, because their keys and values are already stored. Only the new token goes through the model. It makes its query, reads the six keys and values that now exist, and adds its own. That is the difference between the two phases of serving. Reading the prompt is one wide pass over many tokens. Writing the answer is many narrow passes over one token each, and every one of them reads the whole cache.
- The token that was drawn is added to the end, and the model is asked for the one after it.
- Only the new token passes through the model, and its attention reads the stored keys and values.
Summary
A language model answers one question: given everything so far, which token comes next. Text is cut into tokens, each token becomes a list of numbers, and position is worked in. Then 64 blocks repeat the same two moves. Attention lets each token take in what earlier tokens offer, by matching its query against their keys and blending their values. Feed-forward reworks each token by itself. The last position is turned into a score for every token, one is drawn, and the loop runs again. The keys and values are kept between passes, which is why the first token of an answer is slow, the rest are quick, and GPU memory fills up with conversations.
Next lesson: From random weights to a model