Unit 3 of 11 · about 4 hours · explain, measure

Request lifecycle, continuous batching and the kernel shapes it creates

Serving performance comes from the live request mix, not one kernel shape. The lifecycle decides which kernels run at which width every iteration.

You'll be able to

  • Explain the queue, prefill, decode, sample and completion lifecycle at performance depth.
  • Predict how continuous batching changes live batch size, kernel shapes and occupancy for a stated arrival mix.
  • Measure one fair-scheduling effect with heterogeneous request sizes.

Covers

Queue, prefill, decode, sample and completion lifecycle at performance depthContinuous batching effects on kernel shapes and occupancyFair scheduling and heterogeneous request sizes
1

Read

In this order, only the parts listed

0 of 3 done

  1. Dynamic, continuous and scheduled batchingAI Systems Performance Engineering - Chapters 13, 15 and 16

    Chapter 16, Dynamic Batching; Continuous Batching; Continuous Scheduling

  2. Iteration-level scheduling mechanismOrca (OSDI 2022)

    Sections 3 to 4, iteration-level scheduling and selective batching with Algorithm 1 and Figures 2 to 5

  3. Static draining versus dynamic insertAI Systems Performance Engineering companion code - Chapters 12 to 16

    BaselineContinuousBatchingBenchmark with dynamic=False in baseline_continuous_batching.py versus dynamic=True in optimized_continuous_batching.py on ContinuousBatchingBase

2

Focus on

  • A request moves through queue, prefill, decode steps, sampling and completion. Continuous batching refills finished slots every iteration instead of waiting for the longest sequence.
  • Selective batching splits the work: linear and norm layers batch broadly while attention runs per request with its own KV. Kernel shapes therefore track the live mix, not the configured maximum.
  • Heterogeneous lengths set occupancy and latency: one long prefill can stall short decodes in the same iteration, which fair scheduling and token budgets bound.
Common mistakes · 3
  • Calling any batching continuous because requests share a server. Only per-iteration refill with independent completion counts.
  • Assuming live batch size equals configured maximum. Finished slots empty the kernels below their configured width.
  • Reading higher throughput as lower latency for every request. Batching can delay a single request while raising aggregate tokens per second.
3

Try it yourself

Closed-book, before checking anything

Closed-book, work a four-request mix with decode lengths 10, 10, 10 and 100 under static batching versus continuous batching. Show static batching holds all slots until length 100 while continuous batching frees three slots at length 10, and state which kernel dimensions shrink when the live batch drops from 4 to 1.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.What makes batching continuous rather than static?Answer

    Per-iteration membership: finished requests leave and waiting ones join every token step, without waiting for the longest sequence.

  2. 2.Why do linear layers and attention see different batch shapes in one iteration?Answer

    They batch across requests while attention needs per-request KV and masks. The batched layers see the live batch width; attention sees per-request sequence state.

  3. 3.How does one long request hurt short ones in a shared iteration?Answer

    A long prefill occupies the iteration and delays short decodes sharing it. Token budgets and scheduling priorities bound that stall.

5

Practice

  • Companion code ch15: continuous batching pair

    Run python -m cli.aisp bench run --targets ch15:continuous_batching --profile minimal (plain python -m ch15.compare runs every pair in the chapter); compare padded static draining against dynamic insert-on-completion on one arrival mix and report tokens per second and queue wait

    Single-GPU run is enough. No public autograder scores the schedule; retain the arrival mix, both throughputs and the kernel-shape notes.

You're done when

You can walk one request through every lifecycle stage and show measured throughput and queue-wait differences for static versus continuous draining on one mix.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 11 more in the section
  • AI Systems Performance Engineering - Chapters 13, 15 and 16Chris Fregly · primary

    Read Chapter 13 on profiling and tuning PyTorch, Chapter 15 on inference parallelism, decoding and routing, and Chapter 16 on profiling and debugging inference. Keep Chapters 17 to 19 for GPU-9 and depth.

  • AI Systems Performance Engineering companion code - Chapters 12 to 16Chris Fregly · required

    Use compatible examples from code/ch12 through code/ch16. Trace whether each optimized path changes a kernel, graph, compiler choice, scheduler or library dispatch, then integrate one bounded custom path into a realistic operator or small endpoint.

  • Orca (OSDI 2022)USENIX · support

    Read Sections 3 to 4 for iteration-level scheduling and selective batching, and Section 6 for the engine microbenchmark and end-to-end latency-throughput method. Use it as the continuous-batching mechanism behind the book summary.

Also in this section

  • AI Performance Engineering - Inference EnginesWafer · reference

    A curated index. Use Section 4 only to find the introducing paper and implementation for the mechanism your measurement points to.

  • PagedAttention and vLLMvLLM Authors · required

    Study block-based key-value allocation, sharing and scheduling interaction. Trace logical token positions to physical blocks in one engine run and quantify fragmentation and memory-capacity effects.

  • SGLangSGLang Authors · support

    Read the runtime and RadixAttention design, then inspect the repository path for one kernel or prefix-reuse interaction. Use it as an alternative architecture in the integration defense.

  • LeetGPU - Inference Kernel SetAlphaGPU · required

    Choose three bottleneck-matched challenges from 74 GPT-2 Block, 83 Fused Residual Add RMS Norm, 84 SwiGLU MLP Block, 87 Speculative Decoding Verification, 93 Llama Transformer Block and 96 INT8 KV Cache Attention. Integrate one result locally rather than stopping at the hosted submission.

  • FlashInferFlashInfer Project · support

    Use the repository as the serving-kernel baseline and interface reference. Reconstruct workspace planning, layouts, dispatch and stream behavior for one selected operation before comparing your path.

  • GPU Microbenchmark and Kernel Suite - IntegrationRoadmap-Final · support

    Complete the real tensor or model-operation integration, dispatch and fallback requirements. Report compilation and invocation overhead, workload frequency, correctness or quality and the end-to-end no-benefit regime.

  • PyTorch 2.14 ProfilerPyTorch · reference

    Read the Overview example plus the API reference for torch.profiler.profile (activities, schedule, on_trace_ready, record_shapes, with_stack), torch.profiler.record_function and export_chrome_trace. Use it for exact profiler and NVTX-range call contracts missing from the book.

  • Sarathi-ServearXiv · support

    Read Sections 3 to 4 for chunked-prefill stall-free batching and token-budget selection, and Section 5 for capacity and overhead method. Use it as the chunked-prefill mechanism behind the book summary.

  • Fast Inference from Transformers via Speculative DecodingPMLR · support

    Read Sections 2 to 3 for the draft-verify-accept loop, the acceptance rule and the walltime-versus-operations analysis. Use it for the verification cost model behind the engine guides.

  • vLLM Documentation - Automatic Prefix CachingvLLM Project · reference

    Read Automatic Prefix Caching (vLLM v0.31.0) for hash-based block reuse, full-block caching, reverse-order freeing and the multimodal hashing example. Use it for engine behavior the papers do not version.

  • Transformer Inference ArithmeticKipply · reference

    Read kv cache, capacity, latency calculations, batch sizes and flops counting for per-token KV bytes, weight-capacity arithmetic and memory-bound versus compute-bound latency floors. Use it to derive endpoint bounds before measuring.