interplane.Get Omega
Read in your language

openaccel-lab

aien-dev/openaccel-lab

Brought by drakestapleton

branch main Branches Tags History

openaccel-lab / oal / ref.py

40 lines · 1506 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0
2"""Independent reference: workloads/<name>/reference.py defines reference(p, x) -> {output: value or
3(value, scale)}, computed in float64 numpy from the exact input values. The reference is the oracle,
4not an entrant; it never appears on the board as a backend. A control may borrow the reference of
5the workload it breaks with "reference_from".
6"""
7import importlib.util
8import os
9
10import numpy as np
11
12from . import manifest
13
14
15def load(m):
16 src_name = m.get("reference_from", m["name"])
17 d = manifest.workload_dir(src_name)
18 path = os.path.join(d, "reference.py")
19 spec = importlib.util.spec_from_file_location("oal_ref_" + src_name, path)
20 mod = importlib.util.module_from_spec(spec)
21 spec.loader.exec_module(mod)
22 return mod.reference
23
24
25def compute(m, params, inputs):
26 fn = load(m)
27 x = {k: np.asarray(v) for k, v in inputs.items()}
28 out = fn(dict(params), x)
29 refs = {}
30 for spec in m["outputs"]:
31 v = out[spec["name"]]
32 if isinstance(v, tuple):
33 refs[spec["name"]] = (np.asarray(v[0], dtype=np.float64), np.asarray(v[1], dtype=np.float64))
34 else:
35 refs[spec["name"]] = (np.asarray(v, dtype=np.float64), None)
36 want = tuple(manifest.shape(spec, params))
37 if refs[spec["name"]][0].shape != want:
38 raise ValueError("%s reference %s has shape %s, manifest says %s" % (
39 m["name"], spec["name"], refs[spec["name"]][0].shape, want))
40 return refs