Unit 7 of 12 · about 4 hours · explain, measure
Launch and host overhead, streams, events and asynchronous execution
Pinned memory, streams, events and copy-compute overlap supplied the CUDA mechanics. A serving loop must hide host preparation without exposing unfinished tokens or recycling KV before its consumers finish.
You'll be able to
- Separate host scheduling and launch supply from GPU execution on a decode timeline.
- Preserve producer, consumer and host-read ordering with stream events and buffer lifetimes.
- Measure a completed serving step without inserting a device-wide wait after every operator.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Host work on the decode critical pathUCSD CSE 291A (Spring 2026) - Guest lecture: Engineering vLLM
May 7, 2026 scribe notes: Sections 4.1, 4.3 and 4.4, CPU overhead, asynchronous scheduling and GPU-native input preparation
Read the dependencies and preparation boundaries. Do not inherit lecture timing examples or treat all host work as removed by asynchronous scheduling.
- Sampled-token copy completionMini-SGLang - Kernel, Model Runner and Overlap Boundaries
ForwardOutputandEngine.forward_batch: active stream, sampling, CPU copy and completion event - Overlap and safe host consumptionMini-SGLang - Kernel, Model Runner and Overlap Boundaries
overlap_loop,normal_loop,_process_last_dataandshutdown
Focus on
- A short GPU step can wait for host launches, scheduler work or input preparation. Locate gaps on the timeline before choosing a faster arithmetic kernel.
- One stream orders its operations; another stream needs an explicit dependency. Event waiting establishes readiness, while retaining storage or recording its use prevents allocator reuse. These are separate obligations.
- A nonblocking copy return is not proof that the CPU tensor is ready or that transfer overlaps compute. Wait for completion before host inspection and verify overlap in the trace.
- Overlap can prepare work for a request that finishes or is cancelled. Discard its output and defer storage reuse until in-flight consumers finish; check this ordering in a deterministic CPU event simulation.
Common mistakes · 3
- Timing Python enqueue duration and calling it completed decode latency.
- Replacing an event wait with a global synchronization at every token, thereby changing the overlap being measured.
- Freeing a cancelled request's slots while previously enqueued kernels still read them. Cancellation changes ownership, not completion of GPU work.
Try it yourself
Closed-book, before checking anything
torch.float32 vector of ones and a pinned host output. Have a producer stream add one, record a ready event, and a consumer stream wait on it before multiplying by three and copying to the host. Record completion after the copy and wait before reading; every host element must be 6. Keep all buffers alive. Capture a short timeline and identify both dependencies; overlap is an observation, not a requirement. Repeat the ownership sequence in a CPU event simulation where cancellation prevents early slot reuse.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Does a stream wait block the host until the producer finishes?Answer
No. It orders future work on the waiting stream. A host event synchronization is needed before the CPU consumes unfinished results.
2.Why retain a buffer after adding a cross-stream event dependency?Answer
The dependency establishes execution order, not storage lifetime. The allocator must not reuse storage while another stream still uses it.
3.What disproves a host-launch-bound hypothesis?Answer
A timeline with sufficient queued GPU work and device execution occupying the critical path, together with no useful improvement when host launch supply is reduced.
You're done when
The two-stream result is correct, the trace shows the required ordering, and cancellation cannot return storage before its final consumer completes.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 12 more in the section
- Mini-SGLang - Kernel, Model Runner and Overlap BoundariesSGLang Project · support
Trace how FlashAttention or FlashInfer, KV layouts, tensor parallel communication and overlap scheduling meet the model runner. Identify the fallback and state-reset boundary before attempting a change.
- UCSD CSE 291A (Spring 2026) - Guest lecture: Engineering vLLMUC San Diego · support
Read the scribe notes, Sections 4 and 5: CPU overhead, the API server and renderer, synchronous and asynchronous engine-core scheduling, GPU-side input preparation, piecewise CUDA graphs and the hybrid kernel strategy.
Also in this section
- FlashInfer: Efficient and Customizable Attention Engine for LLM Inference ServingFlashInfer Project · primary
Read the storage heterogeneity, plan and run separation, load-balanced scheduling, customization, CUDA Graph compatibility and end-to-end evaluation. Use it to connect serving-shaped attention kernels to dynamic workloads and dispatch constraints.
- AI Systems Performance Engineering - Chapters 18 and 19, decode kernels and engine optimizationsChris Fregly (O'Reilly) · required
Read "Optimized Decode Kernels" in Chapter 18 (FlashMLA, ThunderMLA, FlexDecoding), then the Chapter 19 sections on kernel autotuning, occupancy-aware kernel selection, hot-swappable kernels and CUDA-graph prewarming. Choose one bounded critical path to verify.
- AI Systems Performance Engineering companion code - Chapter 18Chris Fregly · required
Start with paged_attn_common.py, the dense versus paged layout pair, CUDA graph bucketing and the vLLM integration pair. Run correctness tests first, then a hardware-compatible benchmark with newly captured artifacts.
- FlashAttention-2: Faster Attention with Better Parallelism and Work PartitioningTri Dao · required
Derive online-softmax state, memory traffic and work partitioning. Implement only a small oracle-backed version or use the CS336 assignment; production kernel tuning belongs to the GPU track.
- FlashInfer DocumentationFlashInfer Project · required
Use the current attention, PageAttention, sampling, JIT and CUDA Graph interfaces while implementing. Pin the version and supported GPU architecture, and validate dispatch and fallback over the real workload shape distribution.
- CS336 Assignment 2 - FlashAttention and Triton SectionsStanford University · required
Complete the relevant derivation, PyTorch reference and Triton sections using your own code. Follow assignment integrity: use external help for concepts, invariants, tests and profiling, not copied solutions or edited answer code.
- vLLM CUDA Graphs design (v0.31.0)vLLM Project · support
Use the capture modes, batch-size buckets and piecewise splitting around attention to explain what a graph replay can and cannot include.
- LLM Inference: From scratch, one token at a time - Module 07Tushar Amrit · support
Work the Attention Kernels module for the decode-attention memory traffic and the paged-attention gather before the matching units.
- AI Performance Engineering - Kernel Optimization, Profiling and AttentionWafer · reference
A curated index. Use Sections 2 and 3 only to find a kernel or profiler source the other entries lack.
- TensorRT-LLM Architecture OverviewNVIDIA · support
Use the runtime-optimization section to compare graph replay, overlap scheduling and backend dispatch with your selected engine. Record current version and hardware support before treating a path as available.
- vLLM structured outputs (v0.31.0)vLLM Project · required
Read Offline Inference and the API-field migration warning; trace grammar bitmask application before sampling in the versioned model-runner API.
- PyTorch compiler troubleshooting guide (2.14)PyTorch · reference
Read Graph break, Guards, Recompilation and Dynamic Shapes; run
TORCH_LOGSset tograph_breaksandrecompileson a small serving-shaped function.