Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/openaccel-lab/blob/main/oal/check.py # openaccel-lab aien-dev/openaccel-lab Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `openaccel-lab / oal / check.py` 81 lines ยท 3958 bytes Raw Blame History 1 # SPDX-License-Identifier: Apache-2.0 2 """The tolerance contract (docs/TOLERANCE.md). 3 4 The reference (workloads//reference.py) computes in float64 from the exact input values and 5 returns, per output, the value and optionally a scale array. The contract kinds are: 6 7 exact every element equals the reference rounded once to the output dtype (bitwise for floats, 8 with -0.0 == 0.0); used where one correctly rounded operation, or none, makes each element. 9 bound |got - ref| <= rtol * scale + atol elementwise; scale defaults to |ref|. rtol and atol are 10 manifest expressions (gamma(n), u, parameters), so the bound follows the shape. 11 int |got - ref| <= atol on integers (atol 0 is exact). 12 13 NaN or infinity in an output fails unless the reference has the same value at that element. 14 The verdict carries err_ratio = max(|got - ref| / bound) so the board can show how much of the 15 bound each entrant used (1.0 = at the limit). 16 17 INTERIM host glue (Python + numpy). Successor: oal-check in seeded Omega (f32/f64 words exist). 18 """ 19 import numpy as np 20 21 from . import manifest 22 23 24 def _bound_arrays(contract, params, ref, scale): 25 rtol = float(manifest.evaluate(contract.get("rtol", 0.0), params)) 26 atol = float(manifest.evaluate(contract.get("atol", 0.0), params)) 27 sc = np.abs(ref) if scale is None else np.asarray(scale, dtype=np.float64) 28 return rtol * sc + atol, rtol, atol 29 30 31 def compare(m, params, refs, got): 32 """refs: name -> (value float64 array, scale or None). got: name -> array. Returns verdict dict.""" 33 worst = 0.0 34 details = [] 35 ok = True 36 for spec in m["outputs"]: 37 name = spec["name"] 38 contract = m["tolerance"][name] 39 ref, scale = refs[name] 40 ref = np.asarray(ref, dtype=np.float64) 41 g = np.asarray(got[name]) 42 if g.shape != ref.shape: 43 details.append({"output": name, "pass": False, "why": "shape %s vs %s" % (g.shape, ref.shape)}) 44 ok = False 45 continue 46 kind = contract["kind"] 47 gd = g.astype(np.float64) 48 bad_nonfinite = ~np.isfinite(gd) & ~(np.isnan(gd) & np.isnan(ref)) & ~(gd == ref) 49 if kind == "exact": 50 want = ref.astype(manifest.DTYPES[spec["dtype"]][0]).astype(np.float64) 51 err = np.abs(gd - want) 52 err[np.isnan(gd) & np.isnan(want)] = 0.0 53 bound = np.zeros_like(err) 54 fail = (err != 0) | bad_nonfinite 55 ratio = float(np.nanmax(np.where(err > 0, np.inf, 0.0))) if err.size else 0.0 56 elif kind in ("bound", "int"): 57 bound, rtol, atol = _bound_arrays(contract, params, ref, scale) 58 err = np.abs(gd - ref) 59 fail = ~(err <= bound) | bad_nonfinite 60 with np.errstate(divide="ignore", invalid="ignore"): 61 r = np.where(bound > 0, err / bound, np.where(err > 0, np.inf, 0.0)) 62 ratio = float(np.nanmax(r)) if r.size else 0.0 63 else: 64 raise ValueError("%s: unknown tolerance kind %r" % (name, kind)) 65 nfail = int(np.count_nonzero(fail)) 66 d = {"output": name, "pass": nfail == 0, "kind": kind, "elements": int(fail.size), 67 "failed": nfail, "err_ratio": ratio if np.isfinite(ratio) else None, 68 "max_abs_err": float(np.nanmax(err)) if err.size else 0.0} 69 if nfail: 70 flat = np.flatnonzero(fail.ravel()) 71 i = int(flat[0]) 72 d["first_bad"] = {"index": [int(x) for x in np.unravel_index(i, fail.shape)], 73 "got": float(gd.ravel()[i]), "ref": float(ref.ravel()[i]), 74 "bound": float(np.asarray(bound).ravel()[i]) if np.size(bound) else 0.0} 75 ok = False 76 if np.isfinite(ratio): 77 worst = max(worst, ratio) 78 else: 79 worst = float("inf") 80 details.append(d) 81 return {"pass": ok, "err_ratio": worst if np.isfinite(worst) else None, "outputs": details}