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

  1. Run warmup iterations before measuring.
  2. Use torch.cuda.Event(enable_timing=True) for timing.
  3. Synchronize before the measurement loop begins and after each measured event pair.
  4. Return a dictionary with median_ms, p10_ms, p90_ms, and samples.
  5. Raise a useful error if CUDA is unavailable.
  6. Do not include tensor allocation in the timed function unless the caller put it inside fn intentionally.

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:

  1. The benchmark function.
  2. The two result dictionaries.
  3. A paragraph explaining why the second benchmark answers a different question.
  4. A sentence describing when including allocation would be the correct thing to do.

Acceptance criteria

The solution is correct if:

  1. Removing synchronization makes the reported time suspiciously tiny.
  2. The function produces stable-ish medians across repeated calls.
  3. 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.