openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / oal / matrix.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | """One result record per (workload, case, backend): the JSONL schema in docs/RESULTS.md. |
| 3 | |
| 4 | INTERIM host glue (Python). Successor: oal-matrix in seeded Omega. |
| 5 | """ |
| 6 | import os |
| 7 | import platform |
| 8 | import subprocess |
| 9 | import time |
| 10 | |
| 11 | from . import case, check, manifest, ref, run |
| 12 | |
| 13 | SCHEMA = "oal-result/1" |
| 14 | |
| 15 | |
| 16 | def runner_info(): |
| 17 | cpu = platform.processor() or "" |
| 18 | try: |
| 19 | with open("/proc/cpuinfo") as f: |
| 20 | for line in f: |
| 21 | if line.lower().startswith(("model name", "cpu part", "hardware")): |
| 22 | cpu = line.split(":", 1)[1].strip() |
| 23 | break |
| 24 | except OSError: |
| 25 | pass |
| 26 | return {"os": platform.system(), "arch": platform.machine(), "cpu": cpu, "ncpu": os.cpu_count(), |
| 27 | "ci": os.environ.get("GITHUB_ACTIONS") == "true", |
| 28 | "runner": os.environ.get("OAL_RUNNER_LABEL", os.environ.get("RUNNER_NAME", "local")), |
| 29 | "run_url": ("%s/%s/actions/runs/%s" % (os.environ["GITHUB_SERVER_URL"], os.environ["GITHUB_REPOSITORY"], |
| 30 | os.environ["GITHUB_RUN_ID"]) if os.environ.get("GITHUB_RUN_ID") else None)} |
| 31 | |
| 32 | |
| 33 | def lab_commit(): |
| 34 | try: |
| 35 | return subprocess.run(["git", "-C", manifest.ROOT, "rev-parse", "HEAD"], capture_output=True, |
| 36 | text=True).stdout.strip() or None |
| 37 | except OSError: |
| 38 | return None |
| 39 | |
| 40 | |
| 41 | def cases_of(m): |
| 42 | return [dict(c) for c in m.get("cases", [{}])] |
| 43 | |
| 44 | |
| 45 | def make_case(m, overrides, seed): |
| 46 | params = manifest.params_of(m, overrides) |
| 47 | tensors = case.inputs(m, params, seed) |
| 48 | t = m["timing"] |
| 49 | blob = case.pack(m, params, tensors, t.get("warmup", 2), t.get("repeats", 10)) |
| 50 | return params, tensors, blob |
| 51 | |
| 52 | |
| 53 | def run_one(backend, m, overrides, seed, timeout=600, refs_cache=None): |
| 54 | params, tensors, blob = make_case(m, overrides, seed) |
| 55 | rec = {"schema": SCHEMA, "workload": m["name"], "family": m.get("family"), "control": m["_control"], |
| 56 | "expect": m.get("expect", "pass"), "backend": backend, "params": params, "seed": seed, |
| 57 | "case_sha256": case.sha256(blob), "case_bytes": len(blob), "when": int(time.time()), |
| 58 | "lab_commit": lab_commit(), "machine": runner_info(), |
| 59 | "timing_label": os.environ.get("OAL_TIMING_LABEL") or m["timing"].get( |
| 60 | "label", "host CPU wall clock; not GPU performance"), |
| 61 | "evidence": os.environ.get("OAL_EVIDENCE", "EXERCISED: CPU runner")} |
| 62 | r = run.execute(backend, m, params, blob, timeout) |
| 63 | rec.update({k: r[k] for k in ("status", "times_ns", "versions", "log") if k in r}) |
| 64 | for k in ("toolchain", "build", "exit"): |
| 65 | if k in r: |
| 66 | rec[k] = r[k] |
| 67 | if r["status"] != "RAN": |
| 68 | return rec |
| 69 | rec["outputs_sha256"] = case.sha256(r["outputs"]) |
| 70 | try: |
| 71 | got = case.split_outputs(m, params, r["outputs"]) |
| 72 | except ValueError as e: |
| 73 | rec["status"] = "RUNTIME_FAIL" |
| 74 | rec["log"] = "output stream: %s" % e |
| 75 | return rec |
| 76 | key = (m["name"], case.sha256(blob)) |
| 77 | if refs_cache is not None and key in refs_cache: |
| 78 | refs = refs_cache[key] |
| 79 | else: |
| 80 | refs = ref.compute(m, params, tensors) |
| 81 | if refs_cache is not None: |
| 82 | refs_cache[key] = refs |
| 83 | v = check.compare(m, params, refs, got) |
| 84 | rec["status"] = "PASS" if v["pass"] else "FAIL" |
| 85 | rec["err_ratio"] = v["err_ratio"] |
| 86 | rec["outputs"] = v["outputs"] |
| 87 | return rec |