Inferensys

Glossary

Decode Phase

The decode phase is the token-by-token generation stage of transformer inference, where the model autoregressively predicts the next token using a cached context.
Developer testing AI inference on mobile phone in hand, laptop with optimization code visible, casual tech review moment.
KV CACHE MANAGEMENT

What is the Decode Phase?

The decode phase is the core token-by-token generation stage in transformer inference, directly responsible for the latency and throughput of text generation.

The decode phase is the autoregressive, sequential generation stage of a transformer language model where it predicts the next token based on the entire preceding context. During this phase, the model relies heavily on its KV cache to avoid recomputing attention over past tokens, making each step a memory-bound operation focused on reading cached keys and values and performing a small amount of new computation. This iterative process continues until a stopping condition, like an end-of-sequence token, is met.

Optimizing the decode phase is the primary focus of inference latency reduction. Techniques like continuous batching, PagedAttention, and KV cache quantization are designed to maximize the efficiency of this phase by improving GPU utilization, reducing memory footprint, and minimizing I/O bottlenecks. The performance is often measured in metrics like time-to-first-token (TTFT) and tokens-per-second, which are dominated by the prefill and decode phases, respectively.

KV CACHE MANAGEMENT

Key Characteristics of the Decode Phase

The decode phase is the token-by-token generation stage where the model uses its KV cache to efficiently compute attention over cached context and predict the next token autoregressively. Its performance is dominated by memory bandwidth constraints.

01

Autoregressive Token Generation

The decode phase operates autoregressively, meaning each generated token becomes the input for predicting the next. For a sequence of length L, this requires L serial forward passes. The model's output logits for the final token are used to sample the next token (e.g., via top-p or temperature sampling). This sequential dependency creates a fundamental latency bottleneck, as each step must wait for the previous one to complete, making techniques like speculative decoding critical for acceleration.

02

KV Cache Utilization

The primary optimization of the decode phase is the use of the KV cache. After the prefill phase computes keys and values for all prompt tokens, the cache is populated. During decoding:

  • The query vector is computed only for the new token.
  • The key and value vectors for all previous tokens are read from the cache.
  • The attention scores (Query • Key^T) are computed, followed by the weighted sum of values. This avoids recomputing keys and values for the entire growing sequence, transforming the compute complexity of attention from O(n²) to O(n) per step, but creates a memory-bound workload where performance is limited by cache read speed.
03

Memory-Bound Performance Regime

Decoding is typically memory-bound, not compute-bound. The arithmetic intensity (FLOPs per byte of memory access) is very low because the primary operation is reading the large KV cache for each new token. Performance is limited by the GPU's memory bandwidth (e.g., H100: ~2 TB/s). The latency for a single decoding step can be approximated by: KV_Cache_Size / Memory_Bandwidth. Optimizations focus on:

  • Reducing the size of the KV cache via Multi-Query Attention (MQA) or Grouped-Query Attention (GQA).
  • Improving memory access patterns via PagedAttention.
  • Using lower precision like KV cache quantization (FP8, INT8).
04

Continuous Batching & Scheduling

In production serving systems, the decode phase is executed with continuous batching (also known as iterative batching or dynamic batching). Unlike static batching, this allows:

  • New requests to join the batch as others are still generating.
  • Completed sequences to exit the batch, freeing resources.
  • The batch size and composition to change every decoding step. This maximizes GPU utilization by keeping the hardware busy despite the variable and sequential nature of decoding. Efficient schedulers must manage KV cache memory allocation and context swapping across this dynamic request pool.
05

Context Window Limitations & Management

The maximum sequence length a model can process is defined by its context window, which is physically constrained by GPU memory capacity for the KV cache. For a model with n_layers, n_kv_heads, d_head, and precision b bytes, the cache size per token is: 2 * n_layers * n_kv_heads * d_head * b. Managing long contexts requires techniques like:

  • KV Cache Eviction: Removing less important cached tokens (e.g., LRU).
  • Sliding Window Attention: Only caching the most recent W tokens.
  • KV Cache Offloading: Moving older cache blocks to CPU/NVMe.
  • Attention Sinks: As in StreamingLLM, keeping initial tokens to stabilize attention for infinite generation.
06

Hardware & Kernel Optimization

Low-level kernel optimizations are essential for decode phase performance. This includes:

  • Operator Fusion: Combining the layer normalization, linear projection, and activation functions into a single kernel to reduce memory reads/writes.
  • FlashAttention: While primarily for training, its principles inform decode-optimized attention kernels that minimize HBM accesses.
  • TensorRT-LLM / vLLM: SDKs that provide highly optimized, fused decoding kernels and efficient PagedAttention memory management.
  • Mixed Precision Inference: Using FP16/BF16 for compute while potentially quantizing the KV cache to INT8, requiring efficient dequantization kernels.
TRANSFORMER INFERENCE STAGES

Decode Phase vs. Prefill Phase

A comparison of the two primary computational stages in autoregressive transformer inference, highlighting their distinct roles, performance characteristics, and system resource demands.

Feature / CharacteristicPrefill PhaseDecode Phase

Primary Function

Process the entire input prompt in parallel to compute the initial KV cache.

Generate output tokens sequentially, one per step, using the cached context.

Computational Pattern

Massively parallel (across all prompt tokens).

Inherently sequential and autoregressive.

Dominant Bottleneck

Compute-bound (FLOPs for matrix multiplications).

Memory-bound (bandwidth for reading the KV cache).

KV Cache Role

Populated. The cache is written for all prompt tokens.

Utilized and extended. The cache is read for attention and appended with each new token.

Latency Profile

Single, large operation. Latency scales with prompt length.

Many small, repeated operations. Latency scales with output length.

Throughput Optimization

Large batch sizes to saturate GPU compute units.

Continuous batching to keep the GPU busy despite sequential steps.

Typical GPU Utilization

High (compute cores active).

Often lower (frequent memory stalls).

Key Optimization Techniques

Operator fusion (e.g., FlashAttention), efficient attention masking.

KV cache compression (quantization), PagedAttention, speculative decoding.

DECODE PHASE

Frequently Asked Questions

The decode phase is the token-by-token generation stage of transformer inference. This FAQ addresses common technical questions about its mechanics, optimization, and relationship to the KV cache.

The decode phase is the sequential, autoregressive stage of transformer inference where a language model generates output one token at a time, using its previously generated output as part of the input for the next step.

During this phase, the model's forward pass is dominated by reading the KV cache—the stored key and value tensors from all previous tokens—to compute attention and predict the next token's logits. This phase is inherently sequential and memory-bandwidth bound, as each new token generation depends on the full cached history, making its optimization critical for low-latency text generation.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.