Topic Inference
Foundations · How models work · 7 min read
Why do some AI answers start instantly while others crawl?
Inference · Part 1 of 3 · 2 min
The loop behind every reply
Every time you send a message to an AI , chips in a data center do a burst of work to write the reply. Running a finished model to answer you like this is . Training is when a model learns, once, slowly and at enormous cost, and inference is every use of the finished model after that, which is why it sets how fast and how expensive an AI product feels.
OpenAI describes inference in three steps. Your text is split into small chunks, , the model processes them, and it writes new tokens back. Through all of it one thing never changes, the billions of numbers the model learned in , its weights, which stay frozen while it answers you. That has a consequence most people find surprising. When a model seems to learn your preferences during a chat, that knowledge lives in the conversation, not in the model. The whole conversation is sent back in with each new request, and a fresh chat starts without it. The GPT-3 paper showed in 2020 how far this goes, with a model taking on new tasks from nothing but the instructions and examples in its prompt, the text sent to it, and no change to its weights at all.
A frozen model writes its reply one token at a time, in a loop. It reads everything in front of it, works out how likely each possible next token is, picks one according to those odds and adds it to the end of the text, and then reads the whole thing again, now one token longer, to pick the next. Picking by odds, rather than always taking the single likeliest token, is why the same question can get different answers. The 2017 paper that introduced the , the blueprint behind nearly every language model in use, calls a loop like this, where each token written becomes input for the next, .
Once you see the loop, a lot of model behavior makes sense. A model can't go back and fix a word it already wrote, because the loop only moves forward. A 500-token answer takes 500 trips through the model, which is why long answers are slow and expensive in a way short ones aren't. And because each token depends on every one before it, the steps can't simply be run side by side to save time. That is ironic, since the Transformer's own authors chose their design because it trains in parallel so well, but training is a different situation. When a model trains, the whole text is already known, so every position can be worked on at once. When it writes, the next token doesn't exist until the one before it has been picked.
In short
Inference is every use of a finished model. Its weights stay frozen while it writes one token at a time, feeding each new token back in to pick the next.
- Your text, split into tokens
- The model reads everything so farwith frozen weightsSet during training, then fixed
- It picks the next token
- The token joins the text
- The finished reply
Once for every token in the reply (steps 2 to 4 repeat)
Quick check (optional)
Why does a 500-token answer take noticeably longer than a 50-token one?
Go deeper
The trick that saves trips to the warehouse
If the factory spends its time waiting for trucks, the obvious fix is to send fewer trucks, and that is the idea behind what Horace He calls the most important optimization in deep learning compilers, operator fusion. Normally each small step of a calculation writes its result back to the chip's main memory, only for the next step to read it straight out again. Fusion does several steps at once while the data is already inside the chip, skipping those round trips. In his simple example, the fused version needs only two trips to memory and runs twice as fast, without changing a single result. That is why the same chip can serve a model at very different speeds depending on the software running it.
How a small model speeds up a big one
Speculative decoding works because checking guesses is cheaper than making them one at a time. The paper's authors give an example. A 38-token sentence that would normally take 38 trips through their larger model, one with 97 million parameters, took only 9 trips, because a much smaller model with 6 million parameters drafted tokens ahead and the large model checked several of them in each trip, keeping the ones it agreed with. The output is exactly what the large model would have written on its own, and the authors say the method works on existing models without retraining them or changing their design.
For your work · Generalist
A model doesn't learn from your chats while you use it. What it seems to remember lives in the conversation, so a fresh chat starts from zero. It also writes one token at a time, which is why long answers take longer.
For your career · Knowing the difference between training and inference helps you follow almost any AI news story. Next, Context windows builds on it.
Career paths are getting more detailed and more tailored to you soon.
People to know
Tri Dao
Princeton professor, and co-founder and Chief Scientist of Together AI.
He is lead author of FlashAttention. Its insight is that much of a model's time goes to moving data between levels of a chip's memory, not to the math itself. It connects this lesson to how the teams that serve models actually make them faster.
FlashAttention (the paper) ↗Words in this lesson (17)
- attention
- The part of a language model that, for each token, weighs every earlier token to decide which ones matter for what comes next.
- auto-regressive
- Writing one token at a time, feeding each new token back in as input for the next.
- decode
- The second phase, when the model writes the reply one token at a time. It is usually limited by memory, not computing power.
- GPU
- Short for graphics processing unit, the kind of chip that runs AI models. It does huge numbers of small calculations at once and has its own fast memory.
- inference
- Using a trained model to produce an answer. The model's weights stay fixed while it runs.
- KV cache
- Notes a model keeps in chip memory about every earlier token so it does not reread everything for each new token.
- memory bandwidth
- How fast data can move between a chip's memory and its processors, often the real limit on how fast a model writes.
- model
- A program that has learned from examples to turn an input, such as your question, into an output, such as an answer. Inside, it is a large set of learned numbers.
- prefill
- The first phase of a request, when the model reads the whole prompt at once, in parallel.
- prompt
- Everything the model is given as input for one request, including your message, any instructions, and examples or documents added to it.
- reasoning model
- A model trained to work through a problem in hidden steps before it answers, spending extra time and tokens to do better on hard tasks.
- speculative decoding
- A speed trick where a small model guesses several tokens and the large model checks them all at once, with identical output.
- time to first token
- The wait between sending a request and receiving the first token of the reply.
- tokens
- The small chunks of text a model reads and writes, such as a word, part of a word or a punctuation mark. Prices, limits and speed are all counted in them.
- training
- The stage where a model learns. It guesses on examples with known answers, and its numbers are nudged until the guesses improve. It happens before the model is put to use.
- Transformer
- The design behind nearly every modern language model, introduced in a 2017 paper. Its central part is attention.
- weights
- The numbers inside a model that training sets. Each one decides how much an input counts toward the answer, and they stay fixed while you use the model.
Sources
Original research and official documents used for this version.
arxiv.org ↗ar5iv.labs.arxiv.org ↗arxiv.org ↗arxiv.org ↗arxiv.org ↗artificialanalysis.ai ↗help.openai.com ↗horace.io ↗arxiv.org ↗arxiv.org ↗arxiv.org ↗arxiv.org ↗tridao.me ↗arxiv.org ↗