interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / bin / oal-shrink

77 lines · 3304 bytes Raw Blame History
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.
10import os
11import sys
12sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
13import argparse
14import json
15from oal import manifest, matrix
16
17ap = argparse.ArgumentParser(prog="oal-shrink")
18ap.add_argument("backend")
19ap.add_argument("workload")
20ap.add_argument("--seed", type=int, default=1)
21ap.add_argument("--set", action="append", default=[], metavar="K=V")
22ap.add_argument("--timeout", type=int, default=300)
23a = ap.parse_args()
24m = manifest.load(a.workload)
25start = dict(m["params"])
26start.update({k: int(v) for k, v in (kv.split("=", 1) for kv in a.set)})
27FAILS = ("FAIL", "RUNTIME_FAIL", "TIMEOUT")
28tries = 0
29
30
31def attempt(p):
32 global tries
33 tries += 1
34 return matrix.run_one(a.backend, m, p, a.seed, a.timeout)
35
36
37first = attempt(start)
38if 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)
42cls = first["status"]
43cur, best = dict(start), first
44changed = True
45while 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
64out = {"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)}
70for 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
75if cls != "FAIL":
76 out["log"] = best.get("log", "")[-600:]
77print(json.dumps(out, sort_keys=True))