Home
ArenaGraphSignalTopics
/Large Language Model Infrastructure: Building and Deploying Production AI Systems
Chapter 1 • Module 4 9 min breakdown +15 XP Module

The Two Phases of LLM Inference: Compute-Bound Prefill vs Memory-Bound Decode

The single most important concept in Large Language Model systems engineering is the fundamental physical asymmetry between the Prefill Phase and the Decode Phase.

Unlike traditional computer vision or embedding models that execute a single static forward pass over an input tensor, an LLM execution lifecycle is divided into two radically distinct operating regimes with opposite hardware bottlenecks:

  1. The Prefill Phase (Prompt Processing): Highly parallel, compute-intensive, and Compute-Bound (Matrix-Matrix operations / GEMM).
  2. The Decode Phase (Token Generation): Sequential, bandwidth-starved, and Memory-Bound (Matrix-Vector operations / GEMV).

Understanding this division—and applying the Roofline Model to analyze operational intensity—is essential for optimizing GPU utilization, batching strategies, and serving engine latency.

Interactive Blueprint
Rendering diagram...

1. Phase 1: The Prefill Phase (GEMM)

When a user submits a prompt containing tokens (e.g., ), the LLM processes all tokens concurrently in a single forward pass.

A. Mathematical Formulation: General Matrix Multiply (GEMM)

Let be the input activation matrix for all prompt tokens simultaneously. The projection of these tokens against a weight matrix is computed as:

This operation is a standard GEMM (General Matrix Multiply):

  • Total Floating Point Operations (FLOPs):
  • Total Weight Bytes Read from VRAM:

B. Arithmetic Intensity of Prefill

Arithmetic Intensity () is defined as the ratio of compute operations executed per byte of data transferred from memory:

(assuming FP16 where each weight is 2 bytes and activations are negligible compared to weights).

If the prompt length , the arithmetic intensity is .

Because modern GPUs (like the Nvidia H100) require an arithmetic intensity of to fully saturate their Tensor Cores, prompt prefill easily saturates the compute units, keeping the GPU operating at peak FLOP capacity.


2. Phase 2: The Decode Phase (GEMV)

Once prefill completes and the first token is generated, the model enters the sequential decode loop. To generate token , the model passes only the single newest token () into the network:

A. Mathematical Formulation: General Matrix-Vector (GEMV)

The forward pass is now a Matrix-Vector Multiplication (GEMV):

  • Total FLOPs:
  • Total Weight Bytes Read from VRAM:

B. Arithmetic Intensity of Decode

At an arithmetic intensity of , the GPU must read every single byte of the multi-billion parameter model from High Bandwidth Memory (HBM) into on-chip SRAM just to perform FLOPs per parameter!

Interactive Blueprint
Rendering diagram...

3. The Roofline Model Analysis

The Roofline Model graphically illustrates the hardware boundaries governing these two phases:

Interactive Blueprint
Rendering diagram...

Hardware Comparison: Nvidia H100 vs A100

GPU Hardware SpecNvidia A100 SXM (80GB)Nvidia H100 SXM (80GB)
Peak FP16 Tensor Core Compute () ()
HBM Memory Bandwidth () ()
Machine Balance (Knee Point)
Decode (BS=1) Attainable Speed ( Compute Saturation) ( Compute Saturation)
Prefill () Attainable Speed ( Compute Saturation) ( Compute Saturation)

During single-user decoding on an H100, over of the GPU's raw compute power sits completely idle, throttled entirely by the time required to pull weights across the VRAM memory bus.


4. The Maximum Token Speed Formula

We can mathematically calculate the theoretical physical speed limit for single-stream generation:

Case Study: Llama 3 70B in FP16 on a Single 8-GPU H100 Node

  • Model Size:
  • Total Memory Bandwidth (8 GPUs with Tensor Parallelism):

No algorithmic optimization, PyTorch compiler, or CUDA kernel can ever exceed this number for FP16 weights without:

  1. Quantization: Compressing weights to 4-bit (AWQ/GPTQ) cuts model size to , boosting theoretical speed to .
  2. Batching: Grouping concurrent users so the memory read is shared across tokens simultaneously.
  3. Speculative Decoding: Using a 1B draft model to guess multiple tokens per memory read.

5. How Continuous Batching Bridges GEMV to GEMM

When batch size increases from to , the decode phase changes from Matrix-Vector multiplication back into Matrix-Matrix multiplication:

At , arithmetic intensity jumps to , moving the decode phase up the roofline slope towards compute saturation and dramatically increasing aggregate system throughput.

Interactive Blueprint
Rendering diagram...

6. Failure Modes & Production Debugging Walkthrough

Failure Mode: False "Under-Utilization" Alarms in Monitoring Dashboards

Symptom

A DevOps team inspects nvidia-smi or Prometheus GPU metrics for an inference cluster under live traffic. GPU Compute Utilization (GPU-Util) reads , prompting the infrastructure team to downscale the instance count to save costs. Immediately upon downscaling, user latency spikes catastrophically and requests begin timing out.

text
Loading code editor...

Root Cause: GPU-Util Measures SM Time, Not Memory Bandwidth

nvidia-smi's GPU-Util metric reports the percentage of time that at least one CUDA kernel was active on a Streaming Multiprocessor. It does not measure Memory Bandwidth utilization. During the decode phase, the memory controller and HBM buses were saturated streaming weights, even though the compute SMs finished their math in and sat stalled for the remaining waiting for memory.

Production Solution

  1. Monitor Memory Bandwidth Utilization via NVIDIA DCGM (Data Center GPU Manager) metrics (DCGM_FI_DEV_FB_USED and DCGM_FI_PROF_DRAM_ACTIVE).
  2. Treat DRAM_ACTIVE > 85% as cluster saturation, regardless of whether compute utilization reads low.

7. Summary: Prefill vs Decode Comparison Matrix

DimensionPrefill Phase (Prompt)Decode Phase (Generation)
Input DimensionsMatrix: ()Vector: (or )
Mathematical KernelGEMM (Matrix-Matrix Multiply)GEMV (Matrix-Vector Multiply)
Arithmetic IntensityHigh ( FLOPs/Byte)Low ( to FLOPs/Byte)
Hardware BottleneckCompute Bound (Tensor Core TFLOPs)Memory Bound (HBM Bandwidth)
Optimized ByFlashAttention, FP8 Tensor CoresQuantization (AWQ), Speculative Decoding, High Concurrency
Impacted MetricTTFT (Time to First Token)TPOT (Time Per Output Token)
Milestone Verification

Ready for the next lesson?

Mark this module complete to record verified progress and earn +15 XP toward your architect profile.