KBench / tools /factory /headroom.py
ZMC2019's picture
Reorganise: group 313 tasks into 17 families under tasks/, generators under tools/ (part 2)
2e4c7fe verified
Raw
History Blame Contribute Delete
3.56 kB
"""Measure the REAL headroom of a task: what is left after the best readily-available tooling.
The difficulty of a kernel task is not how complex it looks, it is how much performance remains once a
competent-but-standard approach has been applied. Measuring that against the shipped reference is
useless -- the references here are deliberately slow fp32 specs, so every task looks like it has 20x
available. Measured that way not one task in the suite exceeded 17% of roofline, including dense GEMMs.
So the incumbent is `torch.compile(mode="max-autotune")`: it dispatches to cuBLAS/CUTLASS for matmuls,
picks Triton templates, and fuses elementwise chains. Whatever IT leaves on the table is the honest
headroom an agent is competing for.
headroom = compiled_time / roofline_time
T4 headroom < 2x the standard path is already near the limit -- dense GEMM against cuBLAS.
Beating it means out-engineering a vendor kernel team.
T3 2x .. 4x real headroom, but only via async pipelining, warp specialisation and
hand-written MMA with correct fragment layouts.
T2 4x .. 10x reachable with shared-memory tiling, warp reductions, an online single-pass
reformulation, or a layout change.
T1 > 10x the standard path leaves it in several passes over memory; the win is fusing.
Run inside a task container with its tests/ mounted.
"""
import os
import sys
import time
import torch
sys.path.insert(0, "/app")
V = {"__file__": "/tests/verify_env.py", "__name__": "_g"}
_src = open("/tests/verify_env.py").read().split("def _bench_fresh")[0]
exec(compile(_src.replace('sys.path.insert(0, "/app")', ""), "<g>", "exec"), V)
MK = V.get("_mk") or V.get("_make")
REF = V.get("_ref")
if REF is None:
for k in ("ref_fp32", "ref_mla"):
if callable(V.get(k)): REF = V[k]; break
if REF is None:
for k, f in V.items():
if k.startswith("ref_") and callable(f): REF = f; break
CW = V.get("canonical_work")
SH = V.get("GRADER_SHAPES")
if not (MK and REF and CW and SH):
print("HEADROOM: unsupported grader layout"); raise SystemExit(0)
METRIC = "GB/s" if "GB/s" in _src else "TFLOP/s"
PEAK = 700.0e12 if METRIC == "TFLOP/s" else 4800.0 * 2 ** 30
shp = max(SH, key=lambda s: CW(*s))
work = CW(*shp)
roof_s = work / PEAK
def bench(fn, args, reps=5, warm=3):
for _ in range(warm):
fn(*args)
torch.cuda.synchronize()
best = float("inf")
for _ in range(reps):
e0, e1 = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True)
e0.record(); fn(*args); e1.record(); torch.cuda.synchronize()
best = min(best, e0.elapsed_time(e1) / 1e3)
return best
args = MK(*shp, seed=7)
eager = bench(REF, args)
comp_s, note = None, ""
try:
torch._dynamo.reset()
c = torch.compile(REF, mode="max-autotune", fullgraph=False, dynamic=False)
comp_s = bench(c, args, reps=5, warm=5)
except Exception as e:
note = f"compile failed: {type(e).__name__}"
best_s = min(x for x in (eager, comp_s) if x is not None)
head = best_s / roof_s
tier = "T4" if head < 2 else "T3" if head < 4 else "T2" if head < 10 else "T1"
print(f"HEADROOM shape={shp} metric={METRIC}")
print(f"HEADROOM roofline={roof_s*1e6:.1f}us eager={eager*1e6:.1f}us "
f"compiled={comp_s*1e6:.1f}us" if comp_s else
f"HEADROOM roofline={roof_s*1e6:.1f}us eager={eager*1e6:.1f}us compiled=NA {note}")
print(f"HEADROOM best={best_s*1e6:.1f}us headroom={head:.2f}x tier={tier}")