omega-gpu-bench
First-party Omega GPU kernel benchmark harness and receipts
Brought by drakestapleton
omega-gpu-bench / kit / bench.py
| 1 | #!/usr/bin/env python3 |
| 2 | # kit/bench.py -- what ./run executes. Checks the kit's kernels against kernels/SHA256SUMS, asks the |
| 3 | # driver which GPU is device 0, runs every kernel this GPU can load (each in its own process, so a crash |
| 4 | # is a result and not a hang), and writes one receipt JSON under receipts/. |
| 5 | # |
| 6 | # INTERIM host glue (Python). Omega successor: an omc-built `run` that does the same steps, with the |
| 7 | # receipt written by Omega's own JSON and SHA-256 units (omega-mv om/sha256), see DEPENDENCIES.md. |
| 8 | import argparse, datetime, hashlib, json, os, platform, re, subprocess, sys |
| 9 | |
| 10 | KIT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 11 | sys.path.insert(0, os.path.join(KIT, "tools")) |
| 12 | import receipt as R # noqa: E402 |
| 13 | |
| 14 | |
| 15 | def sh(*cmd): |
| 16 | try: |
| 17 | return subprocess.run(cmd, cwd=KIT, capture_output=True, text=True, timeout=60).stdout.strip() |
| 18 | except (OSError, subprocess.TimeoutExpired): |
| 19 | return "" |
| 20 | |
| 21 | |
| 22 | def first_line(path, key=None): |
| 23 | try: |
| 24 | for line in open(path, errors="replace"): |
| 25 | if key is None or line.startswith(key): |
| 26 | return line.strip() |
| 27 | except OSError: |
| 28 | pass |
| 29 | return None |
| 30 | |
| 31 | |
| 32 | def ck(*args, timeout=600): |
| 33 | cmd = [sys.executable, os.path.join(KIT, "kit", "ck.py"), *args] |
| 34 | try: |
| 35 | p = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout) |
| 36 | except subprocess.TimeoutExpired: |
| 37 | return {"verdict": "HARNESS_CRASH", "error": "timed out after %d s" % timeout} |
| 38 | try: |
| 39 | return json.loads(p.stdout.strip().splitlines()[-1]) |
| 40 | except (IndexError, ValueError): |
| 41 | stages = re.findall(r"stage=(\S+)", p.stderr) |
| 42 | return {"verdict": "HARNESS_CRASH", |
| 43 | "error": "exit %d after stage %s" % (p.returncode, stages[-1] if stages else "none")} |
| 44 | |
| 45 | |
| 46 | def main(): |
| 47 | ap = argparse.ArgumentParser(prog="./run", description="Run Omega-encoded kernels on this machine's " |
| 48 | "NVIDIA GPU through libcuda and write a receipt.") |
| 49 | ap.add_argument("--sign-off", help='"Name <email>" (default: git config user.name / user.email)') |
| 50 | ap.add_argument("--provider", default="self-hosted", |
| 51 | help="where the GPU is, e.g. self-hosted (default) or the cloud you rent it from") |
| 52 | ap.add_argument("--out", default=os.path.join(KIT, "receipts"), help="receipt directory") |
| 53 | o = ap.parse_args() |
| 54 | |
| 55 | # 1. the kit itself |
| 56 | sums = R.read_sums(os.path.join(KIT, "kernels", "SHA256SUMS")) |
| 57 | if not sums: |
| 58 | print("NOT_RUN on every GPU: this kit ships no kernel. The six v7 cubins were %s; " |
| 59 | "see README.md. No GPU was touched and no receipt written." % R.WITHDRAWN) |
| 60 | return 2 |
| 61 | for name, want in sums.items(): |
| 62 | got = R.sha256_file(os.path.join(KIT, "kernels", name)) |
| 63 | if got != want: |
| 64 | print("REFUSED: kernels/%s does not match kernels/SHA256SUMS (got %s, want %s)." |
| 65 | % (name, got, want)) |
| 66 | print("Run the kit as cloned.") |
| 67 | return 3 |
| 68 | commit = sh("git", "rev-parse", "HEAD") or None |
| 69 | modified = bool(sh("git", "status", "--porcelain", "--", "run", "kit", "kernels", "tools")) |
| 70 | sign = o.sign_off or ("%s <%s>" % (sh("git", "config", "user.name"), sh("git", "config", "user.email"))) |
| 71 | if not R.SIGNOFF.fullmatch(sign): |
| 72 | print('REFUSED: no sign-off. Pass --sign-off "Your Name <you@example.com>" or set git config ' |
| 73 | "user.name and user.email.") |
| 74 | return 4 |
| 75 | |
| 76 | # 2. the GPU |
| 77 | probe = ck("probe", timeout=120) |
| 78 | if probe.get("verdict") != "PROBED": |
| 79 | print("NOT_RUN: no usable NVIDIA GPU (%s: %s). No receipt written." |
| 80 | % (probe.get("verdict"), probe.get("error", ""))) |
| 81 | return 2 |
| 82 | cc = probe["cc"] |
| 83 | print("GPU 0: %s, compute capability %s, CUDA driver API %d, through %s" |
| 84 | % (probe["device"], cc, probe["cuda_driver_api"], probe["driver"])) |
| 85 | smi = sh("nvidia-smi", "--id=0", "--query-gpu=driver_version", "--format=csv,noheader") |
| 86 | os_name = (first_line("/etc/os-release", "PRETTY_NAME=") or "").split("=", 1)[-1].strip('"') or None |
| 87 | cpu = first_line("/proc/cpuinfo", "model name") |
| 88 | machine = { |
| 89 | "gpu": probe["device"], "cc": cc, "cuda_driver_api": probe["cuda_driver_api"], |
| 90 | "driver_version": smi or None, "nvrm": first_line("/proc/driver/nvidia/version"), |
| 91 | "os": os_name, "kernel": platform.release(), "arch": platform.machine(), |
| 92 | "cpu": cpu.split(":", 1)[1].strip() if cpu else (platform.processor() or None), |
| 93 | } |
| 94 | |
| 95 | # 3. the kernels this GPU can load |
| 96 | want = R.expected_cubins(cc, sums) |
| 97 | results, not_run = [], None |
| 98 | if not want: |
| 99 | not_run = "this kit has no kernel for sm_%s yet" % cc.replace(".", "") |
| 100 | print("NOT_RUN: " + not_run) |
| 101 | for name in want: |
| 102 | kind = name.split("_")[0] |
| 103 | print(" %-22s " % name, end="", flush=True) |
| 104 | row = ck(os.path.join(KIT, "kernels", name), kind) |
| 105 | row = R.result_row(name, sums[name], kind, row) |
| 106 | print(row["verdict"] + (" %d of %d words differ from the CPU reference%s" % ( |
| 107 | row["mismatches"], row["checked"], " (the negative control must differ)" if kind == "neg" else "") |
| 108 | if row["checked"] else " " + (row["error"] or "")) |
| 109 | + (" %.3f ms, %.1f GB/s" % (row["kernel_ms"], row["gbps"]) if row["gbps"] else "")) |
| 110 | results.append(row) |
| 111 | |
| 112 | # 4. the receipt |
| 113 | now = datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0) |
| 114 | rec = R.seal({ |
| 115 | "schema": R.SCHEMA, "origin": "kit-run", "path": R.PATH_LABEL, "provider": o.provider, |
| 116 | "kit": {"repo": R.REPO, "commit": commit, "modified": modified, |
| 117 | "kernels_sha256sums": R.sha256_file(os.path.join(KIT, "kernels", "SHA256SUMS"))}, |
| 118 | "machine": machine, |
| 119 | "run": {"utc": now.strftime("%Y-%m-%dT%H:%M:%SZ"), "driver": probe["driver"], |
| 120 | "python": platform.python_version()}, |
| 121 | "submitter": {"signed_off_by": sign, "statement": R.STATEMENT}, |
| 122 | "results": results, "not_run": not_run, "evidence": None, |
| 123 | }) |
| 124 | os.makedirs(o.out, exist_ok=True) |
| 125 | slug = re.sub(r"[^a-z0-9]+", "-", probe["device"].lower()).strip("-")[:40] |
| 126 | fname = os.path.join(o.out, "%s-%s-%s.json" % (now.strftime("%Y%m%dT%H%M%SZ"), slug, |
| 127 | rec["receipt_sha256"][:8])) |
| 128 | with open(fname, "w") as f: |
| 129 | json.dump(rec, f, indent=1, sort_keys=True) |
| 130 | f.write("\n") |
| 131 | rel = os.path.relpath(fname, KIT) |
| 132 | ok = all(r["verdict"] in ("PASS", "NEG_DETECTED") for r in results) and not modified and not not_run |
| 133 | print("\nreceipt: %s" % rel) |
| 134 | if modified: |
| 135 | print("note: the kit has local changes, so the leaderboard will not accept this receipt.") |
| 136 | print("check it: python3 tools/validate.py %s" % rel) |
| 137 | print("submit it: fork https://github.com/%s, then\n" |
| 138 | " git checkout -b receipt-%s && git add %s && git commit -s -m 'receipt: %s' && git push\n" |
| 139 | " and open a pull request (the commit's Signed-off-by must equal \"%s\")." |
| 140 | % (R.REPO, rec["receipt_sha256"][:8], rel, probe["device"], sign)) |
| 141 | return 0 if ok else 1 |
| 142 | |
| 143 | |
| 144 | if __name__ == "__main__": |
| 145 | sys.exit(main()) |