advanced25 min studyLesson 480 in Engineering

Kernel harnesses: verify the operation before timing its boundary

Build reference cases, numerical tolerances, memory-safety checks, warmup, synchronized timing, and unit-consistent reports for tensor kernels.

On this page

What you’ll understand

  • Define a reference and finite-value tolerance appropriate to the operation.
  • Cover tails, layouts, masks, aliasing, and numerical edge cases.
  • Separate compilation, launch, GPU execution, transfer, and end-to-end timing while reporting correct units.

A benchmark that times the wrong answer is not useful, and a correctness test that accidentally synchronizes every step is not a representative performance measurement. A good harness states the operation, input distribution, reference precision, allowed error, and timing boundary. It produces evidence about both behavior and cost without letting one convenient shape stand in for every supported input.

A reference must match the intended semantics

Start with small hand-computable cases and a trusted implementation using the same input values, axes, masks, output layout, and precision policy. A higher-precision reference can evaluate represented low-precision inputs more accurately, but it does not restore values discarded before the inputs reached the kernel. Check a range of absolute and relative errors rather than comparing only an average. Cancellation, near-zero references, softmax tails, constant normalization rows, and overflow boundaries need deliberate cases. Nonfinite outputs require explicit handling rather than relying on a comparison that silently treats NaN as acceptable.

Exercise dimensions just below and above tile boundaries, noncontiguous supported views, singleton and zero-size cases under the API contract, and all-masked rows where relevant. Verify unsupported overlap is rejected or handled by its defined path. Memory tools such as NVIDIA Compute Sanitizer can help identify invalid accesses, races, or synchronization mistakes within their documented coverage. Passing numerical comparisons cannot prove that no out-of-range write occurred elsewhere, while a clean memory check does not establish the intended formula. Use both kinds of evidence for the kernels that need them.

∣y^i−yi∣≤atol+rtol ∣yi∣|\widehat y_i-y_i|\leq {\rm atol}+{\rm rtol}\,|y_i|
This finite-value elementwise criterion compares output y-hat with a reference y, using nonnegative absolute and relative tolerances in matching units. It is one declared test rule, not a universal accuracy guarantee. NaN/infinity policy and operation-level invariants require separate checks.↔ Scroll wide equations horizontally.

Warm up and time the intended asynchronous work

Warmup can separate context initialization, compilation, autotuning, allocator setup, and changing device state from steady execution. Include those costs in a cold-start measurement if the product actually pays them; exclude them only from a clearly labeled steady-state boundary. GPU launches are asynchronous, so a host timer around a launch can measure submission rather than completion. CUDA events recorded around the intended work in the appropriate stream can measure a device timeline interval when completion is properly awaited. Other streams, synchronization, and concurrent workloads must remain part of the stated experiment.

Reuse allocations or capture launch sequences only when that reflects the boundary being compared. A captured graph and an ordinary launch path answer different overhead questions unless both are relevant production choices. Use enough repetitions for timer resolution and variability, but avoid reporting a batch average as a per-call latency percentile. Record device model, software versions, compiler flags, precision modes, shapes, strides, and random seeds. Profiling and sanitizer runs can perturb timing, so collect mechanism evidence and representative performance measurements with their instrumentation conditions clearly distinguished.

PGFLOP/s=Ft 109,Beffective,GB/s=Qlogicalt 109P_{\rm GFLOP/s}=\frac{F}{t\,10^9},\qquad B_{\rm effective,GB/s}=\frac{Q_{\rm logical}}{t\,10^9}
F is the stated conventional floating-point work count, Q_logical is a named logical byte boundary, and t is elapsed seconds per operation. Decimal giga units use 10^9. Effective logical bandwidth is not necessarily measured device-memory traffic, and event milliseconds must first be converted to seconds.↔ Scroll wide equations horizontally.

Turn a reported speedup into a falsifiable claim

Compare a tuned library or prior kernel with matched semantics and a representative shape portfolio, including small, thin, odd-sized, and large cases. Report failures and unsupported cases rather than silently omitting them. Check whether the claimed bottleneck is reflected in bytes, instructions, occupancy constraints, stalls, or launch overhead. A roofline ceiling can guide investigation, but a counter using a different byte boundary cannot prove the ceiling was violated. End-to-end model throughput may depend on data preparation, layout conversion, and other kernels that an isolated test excludes.

The roofline lab offers a unit-consistent synthetic reference only. Reset, set I=5 and B=100, predict ceiling 500 GFLOP/s, then Play or Step. Separately Reset, set I=10 and B=100, predict the fixed 1,000 GFLOP/s ceiling, then Play or Step. It does not run a GPU, compile code, measure bytes, or generate benchmark samples. Use its decimal units to check a report's dimensional arithmetic, then require actual synchronized timings and correctness results for a real performance claim.

Convert batch timing into clearly labeled rates

A fictional synchronized device measurement records forty milliseconds for 200 repeated operations. Each operation has 200 million conventional FLOPs and an eight-million-byte logical transfer count. Exclude setup and assume the repetitions are comparable.

  1. Mean duration over this measured batch is 40/200=0.2 milliseconds per operation, or 0.0002 seconds. The batch does not reveal an individual-operation p99.

  2. Conventional arithmetic throughput is 200,000,000/0.0002=10¹² FLOP/s, or 1,000 GFLOP/s. Effective logical bandwidth is 8,000,000/0.0002=40 billion bytes/s, or 40 GB/s.

  3. State the timing and byte boundaries alongside the results. They do not prove measured DRAM bandwidth, end-to-end application speedup, or correctness outside the tested input contract.

The result

The batch implies a 0.2 ms mean, 1,000 conventional GFLOP/s, and 40 logical GB/s under the stated counting rules.

Explore the model

Interactive lab

Interactive lab · roofline

Check units before interpreting a ceiling

Reset, set I=5 and B=100, predict 500 GFLOP/s, then Play or Step. Separately Reset, set I=10 and B=100, predict the fixed 1,000 GFLOP/s compute ceiling, then Play or Step. This synthetic diagram neither executes kernels nor measures actual bandwidth, timer samples, or correctness.

Check units before interpreting a ceilingSynthetic roofline ceiling min(1000, B·I), in GFLOP/s, with B in decimal GB/s and I in FLOP/byte. Playback traces the roof without changing the selected kernel. This upper-bound model is not a measured throughput prediction, hardware comparison or price model. Current readings: Attainable ceiling: 640 GFLOP/s; Ridge intensity: 12.5 FLOP/byte; Ceiling regime: Memory-bound ceiling. All parameters are adjustable with the labeled controls below.Hypothetical roofline upper bound: 640 GFLOP per second at operational intensity 8 FLOP per byte and bandwidth 80 gigabytes per second.0.1110641101001000Operational intensity · FLOP/byte · log scaleCeiling · GFLOP/s · log scalePeak: 1000
Attainable ceiling / kernelBandwidth roof / traceCompute ceiling
Attainable ceiling
640 GFLOP/s
Ridge intensity
12.5 FLOP/byte
Ceiling regime
Memory-bound ceiling
t = 0 s

Synthetic roofline ceiling min(1000, B·I), in GFLOP/s, with B in decimal GB/s and I in FLOP/byte. Playback traces the roof without changing the selected kernel. This upper-bound model is not a measured throughput prediction, hardware comparison or price model. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA valid report needs matching work, byte, time, precision, and input boundaries.

Check your understanding

What can a host timer around an asynchronous kernel launch measure if completion is not synchronized?

Try it yourself

Use atol=0.0001 and rtol=0.0001. Check finite outputs 1,000.1 versus reference 1,000, and 0.001 versus reference zero. Explain why both absolute and relative terms matter.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Compute each error and tolerance independently; the relative contribution vanishes at zero.

Compare with the solution

For reference 1,000, error is 0.1 and tolerance is 0.0001+0.0001×1,000=0.1001, so the stated criterion passes. For reference zero, error is 0.001 while tolerance is only 0.0001, so it fails. A scale-aware rule still needs tolerances justified by the operation and cannot silently absorb invalid nonfinite values.

Take it with you

  • Numerical references and memory checks establish different aspects of correctness.
  • Asynchronous execution needs an explicit synchronized timing boundary.
  • Performance rates require consistent units and a representative supported-input contract.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.