What this lesson covers
When you ask an AI assistant such as ChatGPT or Claude a question, a GPU in a server computes the reply. This lesson explains what happens between sending a prompt and reading the last word of the reply.
If you’d rather listen, the player at the bottom of the page reads the lesson aloud and scrolls along with it.
What is inference?
A language model is first trained on a large amount of data, most of it text, which takes weeks or months. Using the trained model to produce a reply is called inference, and it runs every time someone sends a prompt.
- 1Your prompt travels to a server that has a GPU.
- 2The model runs on the GPU and computes a reply.
- 3The reply comes back a piece at a time.
Tokenization
A model works with numbers, so text is first split into small pieces called tokens, a step called tokenization. A short word is usually one token and a longer word is often several, and each token is then replaced by a number.
The diagram below splits text into tokens and can show each one as the number the model receives. You can type any text into it.
Tokens are also the unit for how much text a model can process at once, how fast it generates, and what a reply costs.
Generating a reply
A model generates its reply one token at a time. It processes all the text so far, predicts the next token, appends it and repeats, which is why answers appear on your screen word by word.
The bar at the bottom shows the delay while the model processes your prompt, before anything appears, and then the steady rate of new tokens, which are measured as time to first token and tokens per second.
GPU vs CPU
Predicting one token takes billions of multiplications. Each one is simple and most are independent of each other, so they can be computed in parallel.
A CPU, the main processor in a laptop, has a few fast cores, while a GPU has thousands of simpler ones. For a large number of independent calculations, the GPU’s many cores finish first.
Memory constraints
A model is a set of numbers called parameters, so an “8 billion parameter” model has 8 billion of them, and they are normally all loaded into the GPU’s own memory before it runs.
How much memory they take depends on the precision each number is stored at, and lower precision makes the model smaller at some cost in quality.
In practice you choose the model size, the precision and the GPU together.
Recap
Inference is a trained model producing a reply to a prompt. The prompt is split into tokens, and after a short delay to process them, the model generates the reply one token at a time. Each token takes billions of calculations, which a GPU computes in parallel on thousands of cores, and the model’s parameters normally have to fit in the GPU’s memory, which depends on their precision.
This is why replies arrive word by word and why limits and prices are counted in tokens, and a label like “8B” or “4-bit” gives a model’s size and the memory it needs.
Athrael.net