all-MiniLM-L6-v2 / code /scripts /eval_gates.py
changh95's picture
tt-model push all-minilm-l6-v2 (container)
1e8fbed verified
Raw History Blame Contribute Delete
3.22 kB
# SPDX-License-Identifier: Apache-2.0
"""SPEC section 3 gates G2-G6 for one engine on the goldens (device engines: run through bin/devrun).
MINILM_GOLDEN_DIR=<goldens> bin/devrun -t 900 -- python -u code/scripts/eval_gates.py --engine mk --out gates_mk.json
MINILM_GOLDEN_DIR=<goldens> python code/scripts/eval_gates.py --engine cpu # no device
Goldens (``MINILM_GOLDEN_DIR``): ``manifest.json`` + ``golden_fp32.safetensors`` of the official float32 code."""
from __future__ import annotations
import argparse
import json
import os
import sys
import time
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.dirname(HERE))
def build(engine: str, model_dir: str, device=None, **kw):
from tt_minilm.host import HostEmbeddings
from tt_minilm.weights import load_weights
W = load_weights(model_dir)
he = HostEmbeddings(W)
if engine == "cpu":
from tt_minilm.reference.model import CpuEngine
return CpuEngine(W, he, threads=2)
if engine == "ttnn":
from tt_minilm.tt.ttnn_engine import TtnnEngine
return TtnnEngine(device, W, he, **kw)
if engine == "mk":
from tt_minilm.tt.mk.engine import MkEngine
return MkEngine(device, W, he, **kw)
raise ValueError(engine)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--engine", default="mk", choices=["mk", "ttnn", "cpu"])
ap.add_argument("--model", default=os.environ.get("MINILM_MODEL_DIR"),
help="weights dir (default: $MINILM_MODEL_DIR, else the pinned revision from the Hub / HF cache)")
ap.add_argument("--out", default=None)
ap.add_argument("--det", type=int, default=200, help="G6 determinism replays (0: skip)")
ap.add_argument("--act", default="bf16", help="ttnn residual dtype")
ap.add_argument("--no-batches", action="store_true")
ap.add_argument("--fidelity", default="HiFi4", help="mk matmul fidelity")
a = ap.parse_args()
if not a.model:
from tt_minilm.api import resolve_weights
a.model = resolve_weights()
from tt_minilm.tests.gates import determinism, evaluate, load_golden
man, gold = load_golden()
dev = None
if a.engine != "cpu":
from tt_minilm.device import open_device
dev = open_device(allow_fallback=False)
try:
kw = {"act_dtype": a.act} if a.engine == "ttnn" else ({"fidelity": a.fidelity} if a.engine == "mk" else {})
t0 = time.perf_counter()
eng = build(a.engine, a.model, dev, **kw)
t1 = time.perf_counter()
res = evaluate(eng, man, gold, batches=not a.no_batches)
res["build_s"] = t1 - t0
res["eval_s"] = time.perf_counter() - t1
if a.det:
ids = [c["ids"] for c in man["cases"][:8]]
res["determinism"] = determinism(eng, ids, a.det)
res["engine"] = eng.describe()
print(json.dumps(res, indent=1))
if a.out:
with open(a.out, "w") as f:
json.dump(res, f, indent=1)
eng.close()
finally:
if dev is not None:
from tt_minilm.device import close_device
close_device(dev)
if __name__ == "__main__":
main()