Unit 8 of 12 · about 4 hours · measure

CPU-GPU timelines, NVTX ranges and trace-to-source correlation

A kernel profiler cannot explain time spent waiting for the host to launch work. The timeline connects source phases to device activity and exposes hidden boundaries.

You'll be able to

  • Capture CUDA API, kernel and transfer timelines with source-correlated NVTX ranges.
  • Attribute one host gap, transfer or synchronization cost before collecting kernel counters.
  • Reconcile timeline intervals with completed wall time without double-counting overlap.

Covers

CPU-GPU timelines, API calls, copies, kernels and synchronizationNVTX ranges and trace-to-source correlation
1

Read

In this order, only the parts listed

0 of 3 done

  1. System timeline before kernel analysisAI Systems Performance Engineering - Chapters 6 to 8

    Chapter 8, Profiling and Diagnosing GPU Bottlenecks: Nsight Systems Timeline View and Profiling and Tuning the Data Pipeline

  2. Nsight Systems 2026.3: capture and correlationNsight Systems User Guide

    Preparing Your Application for Profiling - Focused Profiling and Marking and Labeling Regions; CUDA Trace - Basic CUDA trace; NVTX Trace; Reading Your Report in GUI - Timeline Correlation; Profiling from the CLI - CLI Stats Command Switch Options

    Start with CUDA and NVTX tracing only: --sample=none --cpuctxsw=none keeps the capture small. Turn on CPU sampling, GPU metric sampling or backtraces only when a question needs them.

  3. CS336: profile a completed model stepCS336 Assignment 2 - Section 2.1 Profiling

    Section 2.1.4, Nsight Systems Profiler, and Problem (nsys_profile), pp. 4 to 6

2

Focus on

  • Label warmup, forward, backward, optimizer and validation phases. CPU NVTX range duration is not automatically the duration of asynchronous GPU work it launched.
  • Correlate a kernel to its CUDA API call and enclosing source range. Inspect stream order, copies, dependencies and GPU idle intervals, not just cumulative kernel rankings.
  • Busy time is the union of intervals on the chosen device and capture boundary. Summing overlapping kernels can exceed wall time and is not a utilization percentage.
  • Compare unprofiled and traced completed wall time. Trace overhead and different capture boundaries can explain discrepancies; a synchronization wait may represent completion of useful work rather than wasted time.
Common mistakes · 3
  • Calling a long cudaDeviceSynchronize entry pure synchronization overhead when it is waiting for unfinished kernels.
  • Calling a device busy because any kernel exists in the trace, without defining the denominator interval.
  • Inferring per-SM utilization from kernel start and end timestamps. A device-wide timeline does not reveal how many SMs were active.
3

Try it yourself

Closed-book, before checking anything

Add an NVTX range around the timing unit's matrix-multiply batch and capture a short CUDA/NVTX trace without CPU sampling. Follow one API call to its kernel, identify the final synchronization and compute the union of kernel intervals within the batch. Compare that busy duration, the event interval and completed wall time; label any gap whose cause the trace does not establish.
4

Quiz yourself

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

0 got · 0 missed

  1. 1.Two kernels each run for 5 ms and overlap completely. How much device busy time do they establish?Answer

    5 ms, the union of their intervals, not 10 ms. This does not establish full utilization of every SM.

  2. 2.Does an NVTX range ending mean the launched kernel has finished?Answer

    No. A CPU range ends when its host code ends. GPU completion needs synchronization or trace-based correlation.

  3. 3.What should you investigate when event time is small but completed step latency is large?Answer

    Host preparation, dispatch gaps, transfers, allocations, other streams and mismatched boundaries, using the timeline before blaming the kernel.

5

Practice

  • CS336 Assignment 2: nsys_profile

    Problem (nsys_profile), parts (a) to (e), p. 6

    The problem asks for two Table 1 sizes and three power-of-two contexts above 128. One full AdamW step at batch 4 peaked at 2.93, 4.83 and 10.74 GiB for the small model at contexts 256, 512 and 1024, and at 8.41 and 13.55 GiB for the medium model at 256 and 512; medium at 1024 did not fit in 24 GB (measured on an RTX A5000 with PyTorch 2.11). Profile the sizes that fit your GPU's memory: on a 24 GB GPU, profile small and medium at 256 and 512 and small also at 1024; on a 6 GB GPU, profile small training at 256, small forward passes at 512 and 1024 and medium forward passes at 256 and 512, and record the deviation. Keep captures short. These write-up questions have no public autograder.

You're done when

Your annotated trace connects a measured phase to source, kernels and dependencies, attributes one observed gap or transfer, and reconciles busy time with event and completed wall intervals.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 9 more in the 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.

  • CS336 Assignment 2 - Section 2.1 ProfilingStanford University · support

    Do the benchmarking_script, nsys_profile, mixed_precision_accumulation and memory_profiling problems 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 companion code - Chapters 6 to 8Chris Fregly · required

    Use selected baseline and optimized pairs from code/ch06, code/ch07 and code/ch08 plus 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.

  • 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_close contract and make_tensor parameters. 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.