Download code/scripts/dev_api.py from changh95/METEOR-2-p150: direct link, hf CLI and curl.
- Browser
- Download file 2.44 kB
-
https://huggingface.co/changh95/METEOR-2-p150/resolve/main/code/scripts/dev_api.py
- Command line
-
hf download hf://changh95/METEOR-2-p150/code/scripts/dev_api.py
-
curl -L -o dev_api.py https://huggingface.co/changh95/METEOR-2-p150/resolve/main/code/scripts/dev_api.py
2.44 kB
| #!/usr/bin/env python3 | |
| # SPDX-License-Identifier: Apache-2.0 | |
| """The Python API on the device (bin/devrun): from_pretrained, N calls of the shipped sample, agreement with the | |
| stored CPU-reference body (the smoke test's comparison), timing_ms. | |
| bin/devrun python scripts/dev_api.py --variant gather [--calls 10] | |
| """ | |
| import argparse | |
| import json | |
| import os | |
| import sys | |
| import time | |
| from pathlib import Path | |
| import numpy as np | |
| sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")) | |
| HERE = Path(__file__).resolve().parents[1] / "tt_meteor2" | |
| sys.path.insert(0, str(HERE / "ttaw" / "server")) | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--variant", default="gather") | |
| ap.add_argument("--calls", type=int, default=10) | |
| a = ap.parse_args() | |
| import smoke as ttaw_smoke | |
| from tt_meteor2 import METEOR2, load_sample | |
| t = time.time() | |
| model = METEOR2.from_pretrained(variant=a.variant, weights_dir=os.environ.get("METEOR2_WEIGHTS_DIR", "/weights")) | |
| print(f"from_pretrained {time.time() - t:.1f} s warmup_ms {model.warmup_ms}", flush=True) | |
| kw = load_sample(HERE / "samples" / "synthetic_8cam.json") | |
| timings = [] | |
| for i in range(a.calls): | |
| out = model(**kw) | |
| timings.append(dict(out.timing_ms)) | |
| body = out.to_dict() | |
| ref = json.loads((HERE / "samples" / f"synthetic_8cam.{a.variant}.reference.json").read_text()) | |
| import importlib.util | |
| spec = importlib.util.spec_from_file_location("smoke_test", HERE / "server" / "smoke_test.py") | |
| st = importlib.util.module_from_spec(spec) | |
| spec.loader.exec_module(st) | |
| metrics, fails = ttaw_smoke.compare_with_reference(st.drop_threshold_edge(body), st.drop_threshold_edge(ref), gates={"max_ade": 0.3, "max_fde": 0.6, | |
| "min_label_agreement": 0.97}) | |
| med = {k: round(float(np.median([t[k] for t in timings[1:] or timings])), 1) for k in timings[0]} | |
| print("timing_ms median", med, flush=True) | |
| print("vs CPU reference:", ttaw_smoke.describe_metrics(metrics), flush=True) | |
| print("FAIL" if fails else "PASS", fails, flush=True) | |
| print("repr", repr(out), "info form", model.info.get("form"), flush=True) | |
| json.dump({"timing_ms": med, "metrics": metrics, "fails": fails}, open(f"/logs/dev_api_{a.variant}.json", "w"), | |
| indent=1, default=str) | |
| model.close() | |
| if __name__ == "__main__": | |
| main() | |