M01.02·Benchmarking·Core·45 minutes·2 min read
Honest GPU Timer
Build a tiny benchmarking harness that warms up, uses CUDA events, repeats, reports medians, and refuses to benchmark CPU fallbacks by accident.
- Module
- PyTorch and the memory wall
- Objective
- Write a benchmark function that measures GPU work instead of queueing time.
Prompt
Write a function:
def bench_cuda(fn, *, warmup=20, repeat=100):
...
It should benchmark a zero-argument callable fn that launches CUDA work.
Requirements
- Run warmup iterations before measuring.
- Use
torch.cuda.Event(enable_timing=True)for timing. - Synchronize before the measurement loop begins and after each measured event pair.
- Return a dictionary with
median_ms,p10_ms,p90_ms, andsamples. - Raise a useful error if CUDA is unavailable.
- Do not include tensor allocation in the timed function unless the caller put it inside
fnintentionally.
Test workload
Use the harness on:
x = torch.randn(4096, 4096, device="cuda", dtype=torch.float16)
w = torch.randn(4096, 4096, device="cuda", dtype=torch.float16)
def matmul():
return x @ w
Then test a deliberately bad version:
def alloc_and_matmul():
x = torch.randn(4096, 4096, device="cuda", dtype=torch.float16)
w = torch.randn(4096, 4096, device="cuda", dtype=torch.float16)
return x @ w
Deliverable
Submit:
- The benchmark function.
- The two result dictionaries.
- A paragraph explaining why the second benchmark answers a different question.
- A sentence describing when including allocation would be the correct thing to do.
Acceptance criteria
The solution is correct if:
- Removing synchronization makes the reported time suspiciously tiny.
- The function produces stable-ish medians across repeated calls.
- The bad workload is called bad because its timed region changed, not because allocation is morally wrong.
Stretch
Add an optional bytes_moved argument and report effective bandwidth in GB/s when it is provided.
Debrief
The harness is a ritual. Warm up, measure on the device clock, repeat, summarize. Everything later in the track assumes this reflex.