Learn · Inference 101

Inference engineering – 101

A 1,000-foot overview of what inference engineering looks like: how a request moves through an LLM serving stack, what to measure, where the bottlenecks are, and which levers move them.

01

Request flow through the serving stack

InputRequest

Prompt plus parameters: max_new_tokens, sampling settings.

CPUTokenization

Text to token IDs. Apply chat template, add special tokens.

AdmitQueue / scheduler

Admit, prioritize, form batches with continuous batching.

Prompt-parallelPrefill

Builds K/V for each layer and produces the first token. Often compute-bound.

StateKV cache

Per-layer K/V for every active sequence. Grows with prompt plus generated length.

One token / stepDecode loop

Read prior KV and weights, append new KV. Often bandwidth-bound.

OutputStreamed output

Tokens go to the client as they are generated.

Prefill pressure shows up in TTFTDecode pressure shows up in ITL / TPOT
02

Key metrics

TTFTTime to first token: request arrival to first token emitted.
ITLTime between streamed tokens.
TPOT≈ (E2E latency − TTFT) / max(Nout − 1, 1)
ThroughputTotal output tokens per second across all requests.
User rate≈ 1 / TPOT, tokens per second per user.
GoodputRequest rate that still meets the latency SLOs.
Throughput ≠ goodput

High throughput can still mean poor latency, and low goodput.

03

Roofline and bottlenecks

Attainable ≤ min(peak compute, bandwidth × intensity)

Decode low intensity · bandwidth Prefill high intensity · compute peak compute low FLOPs / byte →
Bandwidth-bound decode can show low FLOP utilization even when it is well optimized.
04

Memory requirements

Weight memory

≈ parameters × bytes per parameter

KV cache memory

2 × L × T × Hkv × Dhead × b

1.00 GiB1,073,741,824 bytes / sequence
05

Attention variants and cache

MHA · multi-head GQA · grouped MQA · multi-query QK/V 4 KV heads 2 KV heads 1 shared KV head Fewer KV heads (lower H_kv) → less KV storage, less decode bandwidth
Lower HkvReduces KV storage and decode bandwidth.
Prefix cachingReuses KV for identical prompt prefixes. Helps prefill and TTFT, not decode TPOT.
PagedAttentionPaged KV memory management with non-contiguous allocation.
FlashAttentionExact attention kernel that reduces HBM traffic. Distinct from PagedAttention.
ComplexityFlashAttention does not make dense attention O(T). It stays O(T²), with better constant factors.
06

Scheduling techniques

Continuous batching

Batch in-flight requests dynamically and add new ones as sequences finish. Improves GPU utilization and throughput.

prefilldecodejoins when A frees a slot

Chunked prefill

Split long prompts into chunks so prefill interferes less with decode.

Chunk 1Chunk 2Chunk 3…Decode

Prefill / decode disaggregation

Separate GPU pools for prefill and decode when their resource profiles differ. Adds KV-transfer overhead.

Prefill pool Decode pool transfer KV across network
07

Optimization levers

Quantization

  • Weight quantization cuts weight memory and bandwidth (INT8, INT4).
  • KV quantization cuts cache memory and bandwidth (FP8, INT8).
TargetEffectTypical formats
Weights↓ memory, ↓ bandwidthINT8, INT4
KV cache↓ memory, ↓ bandwidthFP8, INT8

Speculative decoding

  • A small draft model proposes tokens and the larger model verifies them.
  • Speedup depends on draft cost, acceptance rate and verification efficiency.
Prompt Draft modelcheap k tokens Verifytarget model acceptedtokens
08

Parallelism strategies

TP

Tensor parallelism

Shard tensors within layers, such as matmuls and attention.

PP

Pipeline parallelism

Split layers across pipeline stages.

DP

Data parallelism

Replicate the full model across GPUs; route different requests or batches.

EP

Expert parallelism

Shard MoE experts across devices. Adds token dispatch and all-to-all.

09

What to watch in production

  • TTFT, ITL, TPOT, E2E at p50 to p99
  • Queue time in the scheduler
  • Active sequences (concurrency)
  • KV occupancy per GPU
  • Prefix hit rate
  • Offload bytes to CPU or disk
  • Achieved HBM bandwidth
  • Collective latency: all-reduce, all-to-all
  • Network and KV-transfer time
10

Fast diagnosis

High TTFTQueue or prefill bottleneck.
Poor TPOT / ITLDecode bandwidth or KV traffic.
OOM at concurrencyWeights plus KV plus workspace pressure.
Poor scale-outCommunication or topology issue.
By turingfold · Published ← Back to Learn
TURINGFOLD / RESEARCH