Unit 1 of 12 · about 4 hours · explain, measure
Timed boundaries, asynchronous launch and event timing
A host call can return before its GPU work finishes. A speedup is meaningful only when both variants time the same completed work.
You'll be able to
- Define a timed boundary that separates kernel execution, host dispatch, transfers and setup.
- Measure completed work with stream-ordered events and a synchronized host timer.
- Repair a launch-only timing claim and explain any remaining timer disagreement.
Covers
Read
In this order, only the parts listed
0 of 2 done
- CS336 Assignment 2: end-to-end benchmarkingCS336 Assignment 2 - Section 2.1 Profiling
Section 2.1.3, End-to-End Benchmarking, and Problem (
benchmarking_script), parts (a) and (b), pp. 3 to 4 - Companion code: event-timed coalesced copyAI Systems Performance Engineering companion code - Chapters 6 to 8
Commit b57e4c6:
coalesced_stream, thenmainfrom allocation through event destructionRead the timing sequence. Do not treat its average over 40 launches as a distribution of individual-call latency.
Focus on
- Write the benchmark question first: isolated kernel time, completed operator latency, or end-to-end step time. Name every included and excluded cost.
- Record start and stop events on the work's stream, then wait for the stop event before reading elapsed time. Other streams need explicit dependencies.
- For host wall time, complete prior work before starting and included work before stopping. Validation and input generation stay outside a kernel-only interval.
- An event interval can include GPU idle gaps while the host supplies launches. Dividing a batch interval by its launch count measures amortized service time, not a latency percentile.
Common mistakes · 3
- Timing only the return from
torch.matmuland calling that kernel duration. - Recording events on the default stream while the measured work runs on another stream.
- Calling
.item()inside the loop and accidentally including a device-to-host synchronization.
Try it yourself
Closed-book, before checking anything
torch.float32 CUDA matrices. Warm up 10 matrix multiplies, synchronize, then time 20 multiplies with events and with a host timer spanning the same batch and final synchronization. Validate the last output against a CPU double-precision reference. Explain the two boundaries without assuming equal times or a fixed speedup.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why synchronize before starting a host timer as well as after the work?Answer
The first wait excludes queued earlier work. The final wait includes completion of the work being measured.
2.Does waiting for a stop event prove all device work has completed?Answer
Only work ordered before that event, including explicit dependencies. Unrelated streams can still be running.
3.Why can a batch event interval exceed the sum of kernel durations in a trace?Answer
The interval includes time between its event timestamps, including launch-supply gaps and interference. Kernel durations alone exclude those gaps.
Practice
- CS336 Assignment 2:
benchmarking_script(a) and (b)Problem (
benchmarking_script), pp. 3 to 4Part (b) times the Table 1 sizes at context 512; time the sizes that fit your GPU's memory and record every change from the handout. For example, on an RTX 3050 6 GB with PyTorch 2.14, one full AdamW step of the small model at batch 4 and context 256 peaked at 2.93 GiB, while context 512 needs 4.83 GiB and did not fit beside a desktop session; a 24 GB GPU runs small and medium at context 512. Time forward-only and full training steps separately. These are written deliverables with no public timing tests; add your own correctness checks.
You're done when
Your harness reports both completed host latency and event time with explicit boundaries, validates the output, and rejects an unsynchronized launch-only result as a kernel-time claim.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 10 more in the section
- AI Systems Performance Engineering companion code - Chapters 6 to 8Chris Fregly · required
Use selected baseline and optimized pairs from
code/ch06,code/ch07andcode/ch08plus the shared benchmark and verification machinery. Run only hardware-compatible paths, inspect the tests first, retain raw artifacts and document any guard or unavailable feature. - CS336 Assignment 2 - Section 2.1 ProfilingStanford University · support
Do the
benchmarking_script,nsys_profile,mixed_precision_accumulationandmemory_profilingproblems on a model size that fits your GPU instead of the handout's B200 settings. Keep raw timings, traces and memory snapshots.
Also in this section
- AI Systems Performance Engineering - Chapters 6 to 8Chris Fregly · primary
Read Chapters 6 to 8: GPU architecture and occupancy, profiling and tuning memory access, and occupancy, warp efficiency and instruction-level parallelism, with their Nsight Systems, Nsight Compute and roofline workflows. Treat every quoted speedup as a hypothesis for your own hardware.
- Nsight Systems User GuideNVIDIA · required
Learn capture, statistics, CUDA API and GPU timeline views, NVTX ranges and report export. Produce one trace that attributes a host gap, transfer or synchronization cost without using the kernel profiler first.
- Nsight Compute Profiling GuideNVIDIA · required
Study the profiling model, replay, metric collection, sections, roofline and source correlation. Collect the smallest counter set that can falsify your bottleneck hypothesis and record profiler overhead.
- Compute SanitizerNVIDIA · required
Run memcheck plus applicable racecheck, initcheck and synccheck cases against seeded defects. Preserve minimal reproducers and regression tests, and distinguish unsupported checks from a clean result.
- Roofline: An Insightful Visual Performance ModelUC Berkeley · required
Read the original model and construct one roofline plot with explicit peak assumptions, useful operations and bytes. Add measured points and explain gaps from launch, resource, cache or instruction effects.
- CUTLASS GEMM Performance Measurement MethodologyNVIDIA · support
Apply the environment, clock, warmup, timing, verification and reporting guidance to the benchmark harness. Generalize the method beyond GEMM and document every intentional deviation.
- TensorTonic Language Modeling from Scratch - Measure Kernel Timeline UtilizationTensorTonic · support
Solve the timeline-utilization problem after the timeline unit, then repeat the calculation on a trace you captured yourself.
- GPU Microbenchmark and Kernel Suite - Harness and InstrumentationRoadmap-Final · support
Complete Milestones A and B: correct references, synchronized timing, raw samples, noise characterization, environment capture, operation and byte models, timeline traces and profiler evidence.
- PyTorch 2.14 numerical accuracy and tensor testingPyTorch · reference
Read the numerical-accuracy introduction, Extremal values, non-finite inputs and Ampere precision sections, plus the
assert_closecontract andmake_tensorparameters. These supply current comparison semantics missing from the pool's project checklist. - CUDA 13.4 Floating Point and IEEE 754NVIDIA · reference
Read sections 2.2, 3.1 to 3.2 and 4.4 for reduction order and single-precision flush-to-zero flags. Do not use the historical CPU-has-no-FMA statements as current hardware facts.