Prompt to streamed tokens · 12 min
Inference on one GPU
Tokenization, prefill, KV-cache creation, iterative decode, sampling, and streaming turn a prompt into output tokens.
Plain language, the core causal flow, and required checks.
Read the prompt, then extend it
Tokenization produces input IDs. During prefill, the model processes the prompt positions in parallel and creates attention keys and values for every layer. Those reusable values form the KV cache.
Decode is iterative. Each active sequence produces one next-token prediction, the sampling policy chooses a token, its new key and value are appended, and the sequence returns to the scheduler unless a stopping condition has fired.
Time to first token includes queueing and prefill. Inter-token latency describes the cadence after generation starts. They can have different bottlenecks.
Interactive explanation
Follow single-GPU inference
Single-GPU inference
Follow one prompt until a complete response is streamed
The prompt, output text, KV cache, and GPU work share one state. Decode repeats until a stop condition completes the response.
How do GPUs work ?
No output token yet
Waiting for first tokenWhat one decode iteration does
The scheduler selects active sequences. Their latest token IDs enter the model. Layers read the fixed weights and the previously cached keys and values. The final layer produces logits, the decoding policy selects a token, and the new key/value entries are appended. Finished sequences leave; unfinished ones return for another iteration.
- Trace the complete single-GPU inference lifecycle.
- Distinguish time to first token from inter-token latency.
- Tokenization converts text to token IDs.
- Prefill processes the prompt in parallel.
- The KV cache preserves reusable attention state.
- Decode predicts one next token per sequence iteration.
- Sampling chooses and streams the next token.
Core check
What does the KV cache avoid recomputing during decode?
Terms in this chapter
- Tokenizer
- The rules and vocabulary that map text to token IDs and token IDs back to text.
- KV cache
- Per-layer attention keys and values retained so decode can reuse earlier token context.
Primary references
- Tokenizer API ↗Hugging Face
Primary reference for producing model input IDs and attention metadata.
- Cache strategies ↗Hugging Face
Primary reference for autoregressive generation and reuse of attention key/value state.
- Triton batchers ↗NVIDIA
Primary reference for request queues, dynamic batching, and iterative inference scheduling.