Unit 1 of 11 · about 4 hours · measure
Operator dispatch, trace correlation and fusion groups
A faster kernel changes nothing when the framework dispatches a different path. The trace shows which path ran and where time actually went.
You'll be able to
- Trace one framework operator from dispatcher through backend to executed kernels and name each boundary.
- Capture correlated PyTorch-profiler and NVTX timelines and attribute one host gap before reading kernel counters.
- Identify one fusion group and state which launches and materializations it removes.
Covers
Read
In this order, only the parts listed
0 of 3 done
- PyTorch profiler, NVTX and Nsight timelinesAI Systems Performance Engineering - Chapters 13, 15 and 16
Chapter 13, NVTX Markers and Profiling Tools; Profiling PyTorch to Identify Bottlenecks: Using PyTorch Profiler; System Profiling with Nsight Systems and NVTX Timelines
- Exact profiler and range call contractsPyTorch 2.14 Profiler
Overview example with
activities,scheduleandon_trace_ready; API reference fortorch.profiler.profileparametersactivities,record_shapesandwith_stack,torch.profiler.record_functionandexport_chrome_trace - Whole-graph versus regional compile boundaryAI Systems Performance Engineering companion code - Chapters 12 to 16
TinyTransformerBlockandBaselineFullGraphCompileBenchmarkinbaseline_regional_compile.pyversusRegionalMLPwithcompile_mlpandOptimizedRegionalCompileBenchmarkinoptimized_regional_compile.py
Focus on
- Follow one
atenoperator to its CUDA API call and kernel in both the profiler table and thensystimeline. Name the dispatcher, backend, launch and kernel boundaries. - Label each phase with
torch.profiler.record_functionandtorch.cuda.nvtx.range. A CPU range ending does not prove its GPU work finished. - Compare the whole-block and MLP-only compile pair: regional compile keeps dynamic-shape regions eager to avoid recompilation while fusing the compute-heavy subgraph.
Common mistakes · 3
- Reading profiler CUDA time as kernel latency when CUPTI is unavailable. The table then falls back to legacy timing without a device trace.
- Treating
record_shapes=Trueorwith_stack=Trueas free. Both add overhead and hold tensor references during capture. - Calling a fused graph faster without checking which materialization it removed. Count launches and intermediates before and after.
Try it yourself
Closed-book, before checking anything
torch.float32 CUDA matrix multiply wrapped in torch.profiler.record_function("mm") and torch.cuda.nvtx.range("mm"). Capture a short nsys profile --trace=cuda,nvtx --sample=none --cpuctxsw=none trace, follow one aten::mm entry to its kernel, and reconcile profiler CUDA time with the union of kernel intervals and completed wall time.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Does the end of an NVTX range prove the kernel it launched has finished?Answer
No. The range ends when host code ends. GPU completion needs event synchronization or trace correlation.
2.Two kernels each run 5 ms fully overlapped. What device busy time do they establish?Answer
5 ms, the union of their intervals. Summing them to 10 ms double-counts overlap.
3.Why does the regional-compile pair keep attention and LayerNorm eager?Answer
Those regions carry dynamic shapes and masks that would trigger recompilation. Fusing only the MLP keeps the gain without the recompile cost.
4.Profiler shows one
aten::addbut the timeline shows no kernel. What is the most likely cause?AnswerThe op ran on the CPU (a CPU tensor or scalar path), or the CUDA activity was not captured: no
ProfilerActivity.CUDA, CUPTI unavailable, or the kernel fell outside the capture window. Check the device and the profiler activities before blaming the kernel.
Practice
- Companion code ch13: whole-graph versus regional compile trace
Run
python -m cli.aisp bench run --targets ch13:regional_compile --profile minimal(plainpython -m ch13.compareruns every pair in the chapter), then capture profiler andnsystraces for both variants and attribute one dispatch or fusion differenceKeep runs short enough to fit your GPU's memory. No public autograder checks the trace; retain both traces and your boundary notes.
You're done when
Your trace connects one operator to source, API call and kernel, attributes one host gap, and names the fusion boundary with its removed launches.
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/ch12throughcode/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. - 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_functionandexport_chrome_trace. Use it for exact profiler and NVTX-range call contracts missing from the book.
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.
- 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.
- 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.