← [ ABORT TO HUD ]
SEQ. 1
SEQ. 2
The Lifecycle of an Inference Request
Step-by-Step Anatomy of a Token Generation Loop
Every token generated in a modern LLM follows a strictly choreographed lifecycle across CPU host memory and GPU High-Bandwidth Memory (HBM).
- Tokenization & Validation: Client UTF-8 string is converted into token IDs using Byte-Pair Encoding (BPE) or SentencePiece. Length is checked against context window limits.
- Prefill Phase (Prompt Evaluation): All prompt tokens are processed simultaneously in parallel. Compute-bound GEMM operations compute initial Key and Value matrices across all attention heads.
- KV Cache Allocation: Non-contiguous virtual memory pages are provisioned in GPU VRAM to store attention keys and values.
- Decode Phase (Autoregressive Generation): Memory-bound memory fetches. Tokens are generated one by one. The model reads the accumulated KV cache and computes the next single token logit distribution.
- Sampling & Grammar Constraint: Temperature, top-p, min-p, and grammar bitmasks filter illegal token IDs before argmax/sampling selects the final token.
- De-tokenization & SSE Streaming: The chosen token ID is decoded back into UTF-8 text and pushed through Server-Sent Events (SSE) to the client.
// Minimalist Rust representation of an inference harness step
pub struct TokenGenerationStep {
pub step_id: usize,
pub input_token: u32,
pub logits: Vec,
pub sampled_token: u32,
pub is_eos: bool,
}
⌨ HANDS-ON LABTrace Request Token Lifecycle
⭐ +150 XPStep through the request lifecycle stages from BPE tokenization to SSE token streaming.
1Tokenize an incoming test prompt and inspect token IDs.
2Simulate prefill phase and monitor initial TTFT latency.
OBJECTIVE 1 / 2 — type "hint" if stuck
SYNAPSE VERIFICATION
QUERY 1 // 1
Why is the Prefill phase computationally different from the Decode phase?
Prefill generates tokens randomly, while Decode uses beam search
Prefill uses CPU only, while Decode runs on GPU
There is no difference in hardware bottlenecks
Prefill is compute-bound (processing prompt tokens in parallel), while Decode is memory-bandwidth bound (sequential generation)