What Happens Inside a GPU When an AI Model Generates a Token?
Follow the journey from GPU memory to parallel computation, cached attention and the next generated token.
Trace the next token
Run the illustrated sequence or advance one stage at a time. Change the context, batch and cache precision to investigate the memory footprint.
Choose a stop; the diagram, cache state and token stream will synchronize. Or play the full journey below.
Prompt02
Prefill03
Read04
Compute05
Sample
Prompt awaiting processing
Illustrative output token fragments (not generated by an AI model):
Prefill produces the first token's logits. Each subsequent decode iteration processes the previously selected token.
GPU memory and compute
Select a component to inspect its role independently of the animated execution path.
Hypothetical decoder-only transformer: 32 layers × 8 KV heads × 128 head dimension × 2 tensors (K and V). Batch assumes equally long independent sequences. This is raw unpartitioned payload, excluding weights, allocator padding, metadata and workspaces. Precision options illustrate storage size only, not actual support or quality. Controls reset the trace; the budget indicator is not an actual out-of-memory predictor.
Explore inside the GPU · 4 interactive microarchitecture labs
Memory hierarchy · trace eight illustrative reads
A cache hit can avoid an external HBM read. Set an assumed L2 hit share, then replay eight line requests. This simplified trace is not a GPU cache emulator; registers and shared memory are distinct forms of on-chip working storage, not compulsory next levels of a cache chain.
Tensor arithmetic · matrix multiply
Watch each output cell accumulate a row and column dot product. This exact 2 × 2 example illustrates matrix multiply; real tensor instructions operate on hardware-specific tiles and supported data types.
Attention · query against cached keys
Move the two query components. Four toy cached key vectors are scored by Q·K / √2; softmax normalizes the scores. The returned scalar is the weighted sum of their example values. These are invented vectors, not language-model attention measurements.
Token selection · from logits to probabilities
Choose a temperature for four invented vocabulary logits. Softmax produces a distribution; a greedy selection uses the largest logit. Use sample to draw from this toy distribution. This operates independently of the main illustrative token trace.
Open a lab to investigate the computation in more detail.
Explore performance · GPU inference command centre
Prefill vs. decode
Change the workload, accelerator limits and model weight storage to discover which resource constrains inference. Compare arithmetic and HBM transfer time—not measured GPU utilisation.
Prefill
–Idealised whole-prompt phase timeDecode iteration
–Idealised time per batched output stepEach phase uses its own bar scale (the larger time in that phase). Blue = compute time; gold = estimated HBM time. The maximum is the optimistic roofline-style estimate; latency is not the sum of these bars.
Live bottleneck diagnosis
Set the workload to investigate the limiting resource.
Engineering investigation · improve a memory-limited request
Load the case. Improve the estimated decode step to 10 ms or less while preserving batch, context, KV precision, weight precision, compute and capacity. Only HBM bandwidth may change.
Model assumptions, calculations and limitations
Hypothetical single-device dense 7-billion-parameter decoder, 32 layers, 32 query heads, 8 KV heads, head dimension 128. B=batch, S=prompt positions, P=7×10⁹ parameters. Each parameter is assumed to contribute approximately one multiply-add per token (2 FLOPs). Weight storage uses decimal bytes. KV bytes per position per sequence = 2×32×8×128×stored bytes per element.
Prefill FLOPs = 2PBS + 2×32×32×128×B×S(S+1) (causal attention pair work); decode FLOPs = 2PB + 4×32×32×128×B×(S+1). Illustrative HBM bytes = weights read once per phase/step plus prefill KV write or decode KV read and current-position write. This assumes ideal weight reuse across the batch and ignores activation traffic and additional attention reads during prefill. Real implementations may reuse KV in cache, read weights multiple times, or perform extra work.
Each phase time = max(FLOPs/effective compute ceiling, HBM bytes/effective bandwidth). Effective ceilings are selected assumptions, not vendor specs. Resident footprint = weights + raw KV at S+1 + arbitrary 4 GiB reserve. The main trace KV-only budget is independent of this full-device model. Capacity warnings do not predict actual OOM. No queuing, kernel launches, tokenization, networking, quantization overhead, dynamic batching, distributed transfer or quality impact is modelled. The prompt phase includes neither first-token sampling nor end-to-end TTFT. Rates and times are mathematical examples, not benchmark results and not deployment sizing advice.
Engineering challenge · fit the cache
Try the Memory pressure preset. Without lowering concurrency or changing the budget, change one setting to bring projected raw KV payload at or below the selected budget. Then check your answer.
Why memory matters as much as compute
A language model does not produce an entire answer in one operation. The inference runtime first tokenizes the prompt and processes it through the model. During this prefill phase, the transformer establishes key and value (KV) state for the context and produces logits for the first output token.
The next generation step feeds the previously selected token into the model. Across successive transformer layers, the accelerator performs operations using model weights and the existing KV state. GPU streaming multiprocessors execute parallel work; supported matrix operations can use tensor cores, while other operations use other execution units. Registers, local caches and shared memory can help reuse data. HBM holds large model and cache allocations on suitable accelerators.
Attention uses the cached representations of earlier context. The current token's key and value representations are stored as the cache grows. The model produces new vocabulary logits, and a decoding policy chooses another token. The sequence repeats until a stopping condition is reached.
Unlike prefill, which often has considerable parallel work across the input sequence, single-request decode steps can spend much of their time moving data. Batching, quantization, cache reuse, architecture and implementation all change the balance between memory traffic and arithmetic. This is why a GPU with impressive theoretical compute power does not necessarily deliver the best response time for every inference workload.
The next token is a memory journey as well as a compute operation. Increase context or concurrency in the simulator to see why KV-cache capacity and memory bandwidth become important design constraints.
Three questions to take away
Choose an answer for each. You can revise your selections and get immediate explanations.
Technical references and model assumptions
- NVIDIA Nsight Compute — roofline analysis
- PyTorch — scaled dot-product attention reference
- NVIDIA CUDA Programming Guide — GPU programming model
- NVIDIA CUTLASS — decode-phase attention
- NVIDIA Dynamo — prefill and decode
Simplified teaching model; no real GPU, inference engine, telemetry or performance measurement. Original AI-assisted editorial prepared for Matthew Conran's review. Legal & Editorial Centre.
- Daily Visual Engineering — Edition 001 - December 5, 2026
- Building an AI Data Centre Network Fabric - December 4, 2026
- Linux Virtualisation and Virtual Networking for AI Infrastructure - December 4, 2026
