openaccel-lab
aien-dev/openaccel-lab
Brought by drakestapleton
openaccel-lab / bin / oal-shrink
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # oal-shrink BACKEND WORKLOAD [--seed S] [--set k=v ...] |
| 4 | # Failure minimisation: starting from a failing case, reduce each parameter toward its manifest |
| 5 | # "shrink" minimum (respecting multiple_of) while the entrant still fails the same way, then print |
| 6 | # one JSON line with the original and minimal parameters and the minimal failure. The minimal case |
| 7 | # reproduces with: oal-gen WORKLOAD --seed S --set k=v ... | oal-run BACKEND WORKLOAD |
| 8 | # Exit 0 when a minimal failing case was printed, 1 when the starting case does not fail, 2 on usage. |
| 9 | # INTERIM host glue (Python). Successor: oal-shrink in seeded Omega. |
| 10 | import os |
| 11 | import sys |
| 12 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) |
| 13 | import argparse |
| 14 | import json |
| 15 | from oal import manifest, matrix |
| 16 | |
| 17 | ap = argparse.ArgumentParser(prog="oal-shrink") |
| 18 | ap.add_argument("backend") |
| 19 | ap.add_argument("workload") |
| 20 | ap.add_argument("--seed", type=int, default=1) |
| 21 | ap.add_argument("--set", action="append", default=[], metavar="K=V") |
| 22 | ap.add_argument("--timeout", type=int, default=300) |
| 23 | a = ap.parse_args() |
| 24 | m = manifest.load(a.workload) |
| 25 | start = dict(m["params"]) |
| 26 | start.update({k: int(v) for k, v in (kv.split("=", 1) for kv in a.set)}) |
| 27 | FAILS = ("FAIL", "RUNTIME_FAIL", "TIMEOUT") |
| 28 | tries = 0 |
| 29 | |
| 30 | |
| 31 | def attempt(p): |
| 32 | global tries |
| 33 | tries += 1 |
| 34 | return matrix.run_one(a.backend, m, p, a.seed, a.timeout) |
| 35 | |
| 36 | |
| 37 | first = attempt(start) |
| 38 | if first["status"] not in FAILS: |
| 39 | print(json.dumps({"workload": a.workload, "backend": a.backend, "seed": a.seed, "params": start, |
| 40 | "status": first["status"], "minimised": False})) |
| 41 | sys.exit(1) |
| 42 | cls = first["status"] |
| 43 | cur, best = dict(start), first |
| 44 | changed = True |
| 45 | while changed: |
| 46 | changed = False |
| 47 | for k, rule in m.get("shrink", {}).items(): |
| 48 | lo, mult = int(rule.get("min", 1)), int(rule.get("multiple_of", 1)) |
| 49 | v = cur[k] |
| 50 | # smallest first: the minimum, the midpoint, one step down; repeat until nothing smaller fails |
| 51 | cands = {lo, (lo + v) // 2, v - mult} |
| 52 | for c in sorted(x - x % mult for x in cands): |
| 53 | if c < lo or c >= v: |
| 54 | continue |
| 55 | trial = dict(cur) |
| 56 | trial[k] = c |
| 57 | try: |
| 58 | r = attempt(trial) |
| 59 | except (ValueError, ZeroDivisionError): |
| 60 | continue |
| 61 | if r["status"] == cls: |
| 62 | cur, best, changed = trial, r, True |
| 63 | break |
| 64 | out = {"workload": a.workload, "backend": a.backend, "seed": a.seed, "status": cls, "minimised": True, |
| 65 | "original_params": start, "params": cur, "tries": tries, "case_sha256": best["case_sha256"], |
| 66 | "reproduce": "bin/oal-gen %s --seed %d %s > case.bin && bin/oal-run %s %s < case.bin > out.bin; " |
| 67 | "bin/oal-check %s case.bin out.bin" % ( |
| 68 | a.workload, a.seed, " ".join("--set %s=%d" % kv for kv in cur.items()), a.backend, a.workload, |
| 69 | a.workload)} |
| 70 | for o in best.get("outputs", []): |
| 71 | if not o.get("pass", True): |
| 72 | out["first_bad"] = dict(o.get("first_bad", {}), output=o["output"], failed=o.get("failed"), |
| 73 | elements=o.get("elements")) |
| 74 | break |
| 75 | if cls != "FAIL": |
| 76 | out["log"] = best.get("log", "")[-600:] |
| 77 | print(json.dumps(out, sort_keys=True)) |