Workload Profiling
Quick Reference
Pick ONE path based on the workload type:
| Workload |
Approach |
Section |
| Training loop |
Manual torch.cuda.synchronize() + time.perf_counter() with warmup |
Loop Workloads — Manual Timing |
| Single kernel or op |
Write CUDA event benchmark (pre-allocate, warmup, event pairs) |
Non-Loop Workloads — CUDA Event Benchmarking |
| Add timeline labels for nsys |
Use @nvtx.annotate decorator or context manager |
NVTX Reference |
Principles
- Measure, don't guess. Every performance claim must trace back to profiler output or structured measurement data. Never invent metrics.
- Isolate steady-state. Warmup costs (CUDA context init, cuDNN autotuning, JIT compilation) distort measurements. Always exclude warmup iterations before collecting data.
- Use hardware timing. CUDA events measure GPU time precisely. CPU timers (
time.perf_counter()) include host overhead and miss asynchronous execution.
- No sync inside measurement loops. Each
torch.cuda.synchronize() adds 10-50us overhead. Record CUDA events asynchronously, sync once at the end.
- Pre-allocate everything. Tensors, events, compiled kernels — all before the timing loop. For CuTe DSL kernels, pre-compile with
cute.compile().
- Minimize profiler interference. Start with lightweight measurement (manual timing for latency/throughput) and escalate to heavier tools (Kineto, nsys, ncu) only when lighter tools cannot answer the question.
Loop Workloads — Manual Timing
For training loops and iterative workloads, use manual torch.cuda.synchronize() + time.perf_counter() timing with warmup to measure per-iteration latency, throughput, and data load time.
Injection Template
Read the user's training script, understand the dataloader and loop structure, then inject timing code.
import time
import torch
WARMUP = 5
NUM_ITERS = 30
BATCH_SIZE = 128 # global batch size for throughput calculation
iter_times = []
data_times = []
for i, batch in enumerate(dataloader):
if i >= WARMUP + NUM_ITERS:
break
t_data_end = time.perf_counter()
torch.cuda.synchronize()
t_start = time.perf_counter()
# ... existing training loop body ...
torch.cuda.synchronize()
t_end = time.perf_counter()
if i >= WARMUP:
iter_ms = (t_end - t_start) * 1000
iter_times.append(iter_ms)
if i > 0:
data_times.append((t_data_end - prev_iter_end) * 1000)
print(f"[{i:04d}]: iter {iter_ms:.2f} ms, fps {BATCH_SIZE / (iter_ms / 1000):.2f}")
prev_iter_end = t_end
import statistics
print(f"Average: iter {statistics.mean(iter_times):.2f} ms, "
f"fps {BATCH_SIZE / (statistics.mean(iter_times) / 1000):.2f}")
Interpreting Results
- iter (ms): Wall-clock time per iteration (compute + communication, excluding data loading)
- data (ms): Time spent in dataloader between iterations. If
data / iter > 0.2, data loading is a bottleneck.
- fps: Global throughput in samples/second. Use with known FLOPs-per-sample to compute MFU.
Limitations
Manual timing reports aggregate iteration timing — not per-sub-phase breakdown (forward, backward, optimizer). When the user asks where time is spent within compute:
- Add
torch.cuda.synchronize() + time.perf_counter() around each sub-phase for a one-off diagnosis, OR
- Add NVTX annotations and run with
nsys profile for timeline visualization.
Non-Loop Workloads — CUDA Event Benchmarking
For single kernels, one-shot inference, or standalone operations, write CUDA event benchmarking code directly.
PyTorch: Simple (Mean Only)
import torch
def benchmark(fn, warmup=50, iters=100):
for _ in range(warmup):
fn()
torch.cuda.synchronize()
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
for _ in range(iters):
fn()
end.record()
torch.cuda.synchronize()
return start.elapsed_time(end) / iters # ms per iteration
PyTorch: Detailed (Per-Iteration Stats)
import torch
import statistics
def benchmark_detailed(fn, warmup=50, iters=100):
for _ in range(warmup):
fn()
torch.cuda.synchronize()
starts = [torch.cuda.Event(enable_timing=True) for _ in range(iters)]
ends = [torch.cuda.Event(enable_timing=True) for _ in range(iters)]
for i in range(iters):
starts[i].record()
fn()
ends[i].record()
torch.cuda.synchronize()
times = [starts[i].elapsed_time(ends[i]) for i in range(iters)]
return {
"mean_ms": statistics.mean(times),
"median_ms": statistics.median(times),
"std_ms": statistics.stdev(times) if len(times) > 1 else 0,
"min_ms": min(times),
"max_ms": max(times),
}
Anti-Patterns
| Anti-Pattern |
Problem |
torch.cuda.synchronize() before AND after each iteration |
Adds ~10-50us overhead per iteration |
time.perf_counter() for GPU timing |
Measures CPU time, misses async GPU execution |
| Missing warmup |
First iterations include JIT, clock ramp-up, context init |
| Allocating tensors inside measurement loop |
Allocation overhead pollutes timing |
| Reporting only mean |
Hides variance, outliers, bimodal distributions |
For additional benchmarking templates (CUDA Graph, CuTe DSL, Triton, Raw CUDA), see references/benchmarking-patterns.md.
NVTX Reference
NVTX (NVIDIA Tools Extension) adds named annotations to profiler timelines. Use NVTX to label phases (forward, backward, optimizer) for readability in nsys — not for measurement.
import nvtx
# Decorator — annotates every call
@nvtx.annotate("training_step", color="blue")
def training_step():
...
# Context manager — annotates a code block
with nvtx.annotate("data_loading", color="green"):
batch = next(dataloader)
- Do annotate training phases (forward, backward, optimizer, data loading) for nsys timeline clarity.
- Do not annotate for measurement — use CUDA events or manual timing instead.
- Do not over-annotate — too many fine-grained ranges add visual clutter and minor overhead.
For NVTX domains, categories, payloads, and legacy API details, see references/nvtx-api.md.
References
- references/benchmarking-patterns.md — CUDA Graph, CuTe DSL, Triton, Raw CUDA templates; warmup guidance; GPU hardware properties; reporting format
- references/nvtx-api.md — Domains, categories, payloads, legacy push/pop API
- references/pytorch-profiler-api.md — PyTorch 2.0+ profiler API changes (
device_time vs deprecated cuda_time)
1---2name: nvidia-tensorrt-llm-perf-workload-profiling3description: 'Code instrumentation for timing workloads. Two scenarios: (1) Training4 loop — inject manual timing to report per-iteration latency, throughput (samples/sec),5 and data load time. (2) Standalone kernel/op — write CUDA event timing code with6 warmup, per-iteration statistics, and anti-pattern avoidance. Also covers NVTX annotation7 for labeling profiler timelines. NOT for: running or analyzing profiler tools (nsys,8 ncu, Nsight Systems, Nsight Compute), writing kernels (Triton, CuTe, CUDA), applying9 optimizations (CUDA Graphs, gradient checkpointing, fusion), or interpreting roofline/SOL%10 metrics. Triggers: "measure throughput", "benchmark this function", "time my training11 loop", "samples per second", "NVTX annotate", "instrument my dataloader", "data12 load time", "kernel timing", "how do I time".1314 '15license: Apache-2.016metadata:17 author: NVIDIA Corporation18---1920# Workload Profiling2122## Quick Reference2324Pick ONE path based on the workload type:2526| Workload | Approach | Section |27|----------|----------|---------|28| Training loop | Manual `torch.cuda.synchronize()` + `time.perf_counter()` with warmup | Loop Workloads — Manual Timing |29| Single kernel or op | Write CUDA event benchmark (pre-allocate, warmup, event pairs) | Non-Loop Workloads — CUDA Event Benchmarking |30| Add timeline labels for nsys | Use `@nvtx.annotate` decorator or context manager | NVTX Reference |3132## Principles3334- **Measure, don't guess.** Every performance claim must trace back to profiler output or structured measurement data. Never invent metrics.35- **Isolate steady-state.** Warmup costs (CUDA context init, cuDNN autotuning, JIT compilation) distort measurements. Always exclude warmup iterations before collecting data.36- **Use hardware timing.** CUDA events measure GPU time precisely. CPU timers (`time.perf_counter()`) include host overhead and miss asynchronous execution.37- **No sync inside measurement loops.** Each `torch.cuda.synchronize()` adds 10-50us overhead. Record CUDA events asynchronously, sync once at the end.38- **Pre-allocate everything.** Tensors, events, compiled kernels — all before the timing loop. For CuTe DSL kernels, pre-compile with `cute.compile()`.39- **Minimize profiler interference.** Start with lightweight measurement (manual timing for latency/throughput) and escalate to heavier tools (Kineto, nsys, ncu) only when lighter tools cannot answer the question.4041## Loop Workloads — Manual Timing4243For training loops and iterative workloads, use manual `torch.cuda.synchronize()` + `time.perf_counter()` timing with warmup to measure per-iteration latency, throughput, and data load time.4445### Injection Template4647Read the user's training script, understand the dataloader and loop structure, then inject timing code.4849```python50import time51import torch5253WARMUP = 554NUM_ITERS = 3055BATCH_SIZE = 128 # global batch size for throughput calculation5657iter_times = []58data_times = []5960for i, batch in enumerate(dataloader):61 if i >= WARMUP + NUM_ITERS:62 break6364 t_data_end = time.perf_counter()6566 torch.cuda.synchronize()67 t_start = time.perf_counter()6869 # ... existing training loop body ...7071 torch.cuda.synchronize()72 t_end = time.perf_counter()7374 if i >= WARMUP:75 iter_ms = (t_end - t_start) * 100076 iter_times.append(iter_ms)77 if i > 0:78 data_times.append((t_data_end - prev_iter_end) * 1000)79 print(f"[{i:04d}]: iter {iter_ms:.2f} ms, fps {BATCH_SIZE / (iter_ms / 1000):.2f}")8081 prev_iter_end = t_end8283import statistics84print(f"Average: iter {statistics.mean(iter_times):.2f} ms, "85 f"fps {BATCH_SIZE / (statistics.mean(iter_times) / 1000):.2f}")86```8788### Interpreting Results8990- **iter (ms)**: Wall-clock time per iteration (compute + communication, excluding data loading)91- **data (ms)**: Time spent in dataloader between iterations. If `data / iter > 0.2`, data loading is a bottleneck.92- **fps**: Global throughput in samples/second. Use with known FLOPs-per-sample to compute MFU.9394### Limitations9596Manual timing reports **aggregate** iteration timing — not per-sub-phase breakdown (forward, backward, optimizer). When the user asks **where time is spent within compute**:97981. Add `torch.cuda.synchronize()` + `time.perf_counter()` around each sub-phase for a one-off diagnosis, OR992. Add NVTX annotations and run with `nsys profile` for timeline visualization.100101## Non-Loop Workloads — CUDA Event Benchmarking102103For single kernels, one-shot inference, or standalone operations, write CUDA event benchmarking code directly.104105### PyTorch: Simple (Mean Only)106107```python108import torch109110def benchmark(fn, warmup=50, iters=100):111 for _ in range(warmup):112 fn()113 torch.cuda.synchronize()114115 start = torch.cuda.Event(enable_timing=True)116 end = torch.cuda.Event(enable_timing=True)117118 start.record()119 for _ in range(iters):120 fn()121 end.record()122 torch.cuda.synchronize()123124 return start.elapsed_time(end) / iters # ms per iteration125```126127### PyTorch: Detailed (Per-Iteration Stats)128129```python130import torch131import statistics132133def benchmark_detailed(fn, warmup=50, iters=100):134 for _ in range(warmup):135 fn()136 torch.cuda.synchronize()137138 starts = [torch.cuda.Event(enable_timing=True) for _ in range(iters)]139 ends = [torch.cuda.Event(enable_timing=True) for _ in range(iters)]140141 for i in range(iters):142 starts[i].record()143 fn()144 ends[i].record()145146 torch.cuda.synchronize()147 times = [starts[i].elapsed_time(ends[i]) for i in range(iters)]148149 return {150 "mean_ms": statistics.mean(times),151 "median_ms": statistics.median(times),152 "std_ms": statistics.stdev(times) if len(times) > 1 else 0,153 "min_ms": min(times),154 "max_ms": max(times),155 }156```157158### Anti-Patterns159160| Anti-Pattern | Problem |161|--------------|---------|162| `torch.cuda.synchronize()` before AND after each iteration | Adds ~10-50us overhead per iteration |163| `time.perf_counter()` for GPU timing | Measures CPU time, misses async GPU execution |164| Missing warmup | First iterations include JIT, clock ramp-up, context init |165| Allocating tensors inside measurement loop | Allocation overhead pollutes timing |166| Reporting only mean | Hides variance, outliers, bimodal distributions |167168For additional benchmarking templates (CUDA Graph, CuTe DSL, Triton, Raw CUDA), see [references/benchmarking-patterns.md](references/benchmarking-patterns.md).169170## NVTX Reference171172NVTX (NVIDIA Tools Extension) adds named annotations to profiler timelines. Use NVTX to label phases (forward, backward, optimizer) for readability in nsys — not for measurement.173174```python175import nvtx176177# Decorator — annotates every call178@nvtx.annotate("training_step", color="blue")179def training_step():180 ...181182# Context manager — annotates a code block183with nvtx.annotate("data_loading", color="green"):184 batch = next(dataloader)185```186187- **Do** annotate training phases (forward, backward, optimizer, data loading) for nsys timeline clarity.188- **Do not** annotate for measurement — use CUDA events or manual timing instead.189- **Do not** over-annotate — too many fine-grained ranges add visual clutter and minor overhead.190191For NVTX domains, categories, payloads, and legacy API details, see [references/nvtx-api.md](references/nvtx-api.md).192193## References194195- [references/benchmarking-patterns.md](references/benchmarking-patterns.md) — CUDA Graph, CuTe DSL, Triton, Raw CUDA templates; warmup guidance; GPU hardware properties; reporting format196- [references/nvtx-api.md](references/nvtx-api.md) — Domains, categories, payloads, legacy push/pop API197- [references/pytorch-profiler-api.md](references/pytorch-profiler-api.md) — PyTorch 2.0+ profiler API changes (`device_time` vs deprecated `cuda_time`)