nki-kernel-bench / tools /make_ifmaps.py
jburtoft's picture
nki-kernel-bench v0.1
0fef070
Raw History Blame Contribute Delete
2.38 kB
"""Recreate results/ifmaps/<shape>/*.npy (the exact profiled inputs) for a version, so the shipped
NEFF + profile.ntff can be reloaded in neuron-explorer or re-run.
python tools/make_ifmaps.py <version dir> [...] (needs the pinned nki for adapters that import the kernel)
Inputs come from the family's inputs.py with seed 0, passed through the version's call adapter
exactly as the harness does; names match the NEFF's input tensors. Each file is checked against
results/ifmaps.sha256.json.
"""
import hashlib, json, os, sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(HERE), "harness"))
import run # noqa: E402
def main(vdirs):
import numpy as np, yaml
bad = 0
for vd in vdirs:
vd = os.path.abspath(vd); fd = os.path.dirname(vd)
man = yaml.safe_load(open(os.path.join(vd, "manifest.yaml")))
want = json.load(open(os.path.join(vd, "results", "ifmaps.sha256.json")))
sys.path.insert(0, fd)
inputs = run.load_mod(os.path.join(fd, "inputs.py"), f"mi_{abs(hash(vd))}")
k = run.load_kernel(vd, man, man["id"] + "_mi")
ad = run.make_adapter(fd, vd, man)
for sid, files in want.items():
args, kw = inputs.make_inputs(0, sid)
ad_args, ad_kw = args, kw
if ad is not None:
try:
ad.last = None
ad(lambda *a, **k2: None, args, kw) # run the adapter only, not the kernel
ad_args, ad_kw = ad.last
except Exception:
pass
arrays = dict(run.named_arrays(k, ad_args, ad_kw))
od = os.path.join(vd, "results", "ifmaps", sid); os.makedirs(od, exist_ok=True)
for name, h in files.items():
base = name.replace(".must_alias_input", "")
a = arrays.get(base)
if a is None:
print("MISSING", man["id"], sid, name); bad += 1; continue
p = os.path.join(od, name + ".npy"); np.save(p, np.ascontiguousarray(a))
if hashlib.sha256(open(p, "rb").read()).hexdigest() != h:
print("HASH MISMATCH", man["id"], sid, name); bad += 1
print("ok" if not bad else "issues", man["id"])
sys.exit(1 if bad else 0)
if __name__ == "__main__":
main(sys.argv[1:])