KBench / tools /factory /audit_sizes.py
ZMC2019's picture
Reorganise: group 313 tasks into 17 families under tasks/, generators under tools/ (part 10)
0f775e2 verified
Raw
History Blame Contribute Delete
3.42 kB
"""Audit graded-shape sizing: can a ROOFLINE kernel actually reach peak at these sizes?
A kernel only approaches peak if its runtime is long enough that launch overhead (~5-10us), the wave
quantisation tail, and cache warm-up are amortised. Rule of thumb used here: the implied roofline runtime
at the LARGEST graded shape should be >= ~200us. Anything under ~50us is capped by overhead no matter how
good the kernel is, which silently compresses the top of the leaderboard.
"""
import ast
import pathlib
import sys
# Optional task-name filter: `audit_sizes.py <task> [task...]` audits just those, no args audits all.
# Without this every per-task call from a build agent re-audits the whole lane.
ONLY = set(sys.argv[1:])
H200_TFLOPS = 700.0 # realistic sustained bf16 dense
H200_GBPS = 4800.0 # HBM3e roofline
# Multi-GPU tasks are bounded by the INTERCONNECT, not HBM. Sizing them against 4800 GB/s would demand
# absurd message sizes; the honest bar is the link. ~50 GB/s covers host-staged PCIe (this host, whose
# peer DMA is broken) through to a healthy PCIe gen5 pair; an NVLink box would be ~450.
LINK_GBPS = 50.0
for d in sorted(p for p in pathlib.Path(".").iterdir() if p.is_dir() and not p.name.startswith("_")):
v = d / "tests" / "verify_env.py"
if not v.exists() or (ONLY and d.name not in ONLY):
continue
if "canonical_work" not in v.read_text():
# megakernel family: latency-regime, sized by MegaSpec.floor_us() not a shape-work formula
print(f"{d.name:34s} n/a (megakernel family - use MegaSpec.floor_us(), see _mega_factory)")
continue
src = v.read_text()
ns = {}
try:
tree = ast.parse(src)
for node in tree.body:
# exec EVERY top-level def (not just canonical_work): several tasks compute their work count
# via shape-only helpers (ragged-length generators etc.) that must be in scope when it runs.
if isinstance(node, ast.FunctionDef):
exec(compile(ast.Module([node], []), "<w>", "exec"), ns)
# and top-level constant assignments the helpers may reference (PAGE_SIZE, BLOCK, ...)
elif isinstance(node, ast.Assign):
try:
exec(compile(ast.Module([node], []), "<w>", "exec"), ns)
except Exception:
pass
if isinstance(node, ast.Assign) and getattr(node.targets[0], "id", "") == "GRADER_SHAPES":
shapes = ast.literal_eval(node.value)
if isinstance(node, ast.Assign) and getattr(node.targets[0], "id", "") == "REWARD_DIR":
pass
metric = "GB/s" if 'GB/s' in src else "TFLOP/s"
cw = ns["canonical_work"]
works = [cw(*s) for s in shapes]
big = max(works)
if metric == "TFLOP/s":
us = big / (H200_TFLOPS * 1e12) * 1e6
unit = f"{big/1e9:8.1f} GFLOP"
else:
bw = LINK_GBPS if d.name.startswith("dist-") else H200_GBPS
us = big / (bw * 2**30) * 1e6
unit = f"{big/2**30:8.2f} GiB" + (" [link]" if d.name.startswith("dist-") else "")
flag = " <-- TOO SMALL" if us < 50 else (" <- marginal" if us < 200 else "")
print(f"{d.name:34s} {metric:8s} max {unit} roofline {us:8.1f} us{flag}")
except Exception as e:
print(f"{d.name:34s} audit failed: {type(e).__name__}: {str(e)[:60]}")