Guide

The transformer in one pass

Most models you will serve are transformers. Here is one pass through one, from token ids to the next token.

Embeddings

Each token id is looked up in a big table. Out comes a vector: a long list of numbers, like 4,096 of them. This is the token's embedding. Tokens used in similar ways get similar vectors. The table is part of the weights.

A stack of layers

A model is a stack of identical layers, often 32 in a 7B model. Each layer reads every token's vector and writes a refined one of the same size. Every token goes through every layer, so more layers means more work per token.

Attention

Attention lets a token look at other tokens, so "it" can find out it means "the cat". Each token makes three vectors:

  • Query (Q): what it is looking for.
  • Key (K): what it holds.
  • Value (V): what it passes on.

A token's query is checked against the keys, and good matches pass on more of their values.

Looking back only

In a causal model, a token can look only at itself and earlier tokens. That is why text is made left to right. Adding a new token never changes the work done for earlier tokens.

The MLP and the head

Each layer has attention, then a feed-forward block (the MLP), which works on each token alone. The MLPs hold most of the weights. After the last layer, the head turns the last token's vector into a logit for every token in the vocabulary. Softmax and sampling pick the next token.

Remember

  • Id, then embedding, then layers, then head, then logits.
  • Every token passes through every layer.
  • Attention uses query, key and value.
  • Causal: look back, never ahead.
  • The MLPs hold most of the weights.