How a model produces an answer
You hit enter. What happens before the first token, and after it? Today we're looking at the model itself.
Chapters
- 0:00Introduction
- 0:05Where this fits: The AI stack
- 0:09The pause: Waiting for token one
- 0:32KV cache: KV means key-value
- 0:50After the first token: Then the loop
- 1:16Wrap up
Read the transcript
Introduction
You hit enter. What happens before the first token, and after it?
Where this fits: The AI stack
Today we're looking at the model itself.
The pause: Waiting for token one
First, the pause. Your request arrives, and may wait in a queue. Your text is split into tokens, small pieces of words. Then prefill: the model reads your whole prompt, and fills its KV cache. It picks the first token. That whole wait is TTFT, time to first token. Longer prompts usually wait longer. Reusing a saved start can help.
KV cache: KV means key-value
KV stands for key value, so this is the key value cache. Inside the model, each token gets a key, used for matching, and a value, the information it passes on. They're saved, so they aren't worked out again at every step. Longer chats, and more chats, need more memory.
After the first token: Then the loop
After that comes decode. The newest token makes a query. The query is matched against the saved keys, to gather the values that matter. The model scores what could come next, picks one token, and sends it to you. Its key and value join the cache, and round it goes, until it stops. The gap between tokens is inter-token latency, or ITL. Tok/s is how fast they arrive.
Wrap up
Next: speculative decoding, a way to write faster.
Sources
- docs.nvidia.com/nim/benchmarking/llm/latest/metrics.html
- huggingface.co/docs/transformers/cache_explanation
- docs.vllm.ai/en/latest/features/automatic_prefix_caching
- developer.nvidia.com/blog/mastering-llm-techniques-inference-optimization
- arxiv.org/abs/1706.03762