L1 Beginner · Module 1: The LLM forward pass, for inference engineers
1.1Tokens, embeddings and logits
A model never sees text. It sees integer token ids going in and a score for every possible next token coming out. This chapter covers both ends of the model, and the costs that sit there.
By the end you will be able to
- ✓Explain what a token is and why BPE tokenizers split text the way they do
- ✓Describe an embedding lookup and the residual stream it starts
- ✓Turn a final hidden state into logits and probabilities
- ✓Estimate the size and per-token cost of the embedding table and LM head
An LLM is a function from a list of integers to a list of scores. Text only exists at the edges: a tokenizer turns your prompt into integers on the way in, and a detokenizer turns integers back into text on the way out. Everything in between is matrix math.
Tokens
A token is a chunk of text from a fixed vocabulary of size . Most modern models use byte-pair encoding (BPE):
- Start with the 256 possible bytes as the vocabulary.
- Count every adjacent pair of tokens in a large training corpus.
- Merge the most frequent pair into a new token. Repeat until the vocabulary reaches the target size.
Frequent words end up as a single token (" the"), and rare words get split into pieces ("Paged" + "Attention"). In English a
token averages about 4 characters, or about ¾ of a word. Code, other languages and numbers usually need more tokens per character.
Tokenization runs on the CPU, before the GPU is involved. It is cheap, but it is not free: on a busy server, a slow Python tokenizer can become the bottleneck for short requests. Production engines use fast Rust tokenizers and run them off the main scheduling loop.
Embeddings
Each token id selects one row of the embedding table , where is the model width (d_model):
That is a gather, not a matmul: no multiplications happen, the GPU just copies rows. The result is the residual stream, one -dimensional vector per token. Every layer reads it and adds its contribution back onto it (1.2).
Logits
After the last layer, the model takes the final hidden vector of a position and scores every token in the vocabulary against it, using the LM head (also called the unembedding) :
The raw scores are the logits. Softmax turns them into a probability distribution over the next token. Some models reuse the embedding table as the LM head (tied embeddings, ), which saves a matrix.
What the two ends cost
The vocabulary used to be 32K tokens. Newer models use 128K–152K, which makes the edges of the model expensive:
| Model | One table in BF16 | LM head FLOPs per token | ||
|---|---|---|---|---|
| Llama-2-7B | 32,000 | 4,096 | 262 MB | 0.26 GFLOP |
| Llama-3-8B | 128,256 | 4,096 | 1,051 MB | 1.05 GFLOP |
| Qwen2.5-7B | 152,064 | 3,584 | 1,090 MB | 1.09 GFLOP |
For Llama-3-8B, the LM head alone is about 7% of the roughly 15 GFLOP it takes to produce one token, and it has to read a full gigabyte of weights every decode step. On a small model the share is much larger: in Qwen2.5-0.5B, the embedding table is over a quarter of all parameters.
Lab: train a tokenizer and score the vocabulary 🟢
pairs = Counter(zip(ids, ids[1:]))(a, b), count = pairs.most_common(1)[0] # most frequent adjacent pairvocab[new_id] = vocab[a] + vocab[b] # ...becomes one new tokenpython labs/m01/1.1/tokens.pyfirst merges (most frequent pair becomes a new token): b'h' + b'e' -> id 256 b'he' (seen 12x) b't' + b'he' -> id 257 b'the' (seen 9x) b' ' + b'the' -> id 258 b' the' (seen 8x)after 20 merges: 71 tokens for 144 bytes (2.03 bytes/token)
Real models: embedding table = vocab x d_model x 2 bytes (BF16) Llama-3-8B 128256 x 4096 -> 1051 MB (and the same again for the LM head if untied)Sources
Check your understanding
0 / 4 answered1.In a BPE tokenizer, how does a new token get added to the vocabulary?
2.What operation does an embedding lookup perform on the GPU?
3.Llama-3-8B has V = 128,256 and d = 4,096. Roughly how many FLOPs does its LM head cost per generated token?
4.Why do engines slice the hidden states to the last position before the LM head during prefill?