Download code/scripts/bench.py from changh95/all-MiniLM-L6-v2: direct link, hf CLI and curl.
- Browser
- Download file 5.98 kB
-
https://huggingface.co/changh95/all-MiniLM-L6-v2/resolve/main/code/scripts/bench.py
- Command line
-
hf download hf://changh95/all-MiniLM-L6-v2/code/scripts/bench.py
-
curl -L -o bench.py https://huggingface.co/changh95/all-MiniLM-L6-v2/resolve/main/code/scripts/bench.py
5.98 kB
| # SPDX-License-Identifier: Apache-2.0 | |
| """Latency / throughput of the device engines (run through bin/devrun): | |
| bin/devrun -t 900 -- python -u code/scripts/bench.py --engine mk --out bench_mk.json | |
| bin/devrun -t 900 -- python -u code/scripts/bench.py --engine ttnn --out bench_ttnn.json | |
| Per configuration (batch B x tokens n): ``device_ms`` = back-to-back trace replays of the prepared request (one sync | |
| at the end; pure device time per request), ``call_ms`` = ``engine.run`` (plan / input upload, replay, readback of the | |
| pooled sums), ``encode_ms`` = ``MiniLM.encode``-equivalent (+ tokenization of the texts), p50 over the iterations; | |
| throughput = B / call_ms. Inputs are natural text cut to exactly n tokens.""" | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import os | |
| import sys | |
| import time | |
| import numpy as np | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| sys.path.insert(0, os.path.dirname(HERE)) | |
| CONFIGS = [(1, 8), (1, 16), (1, 32), (1, 64), (1, 128), (1, 256), (8, 32), (8, 128), (32, 16), (32, 32), (32, 64), | |
| (32, 128), (32, 256)] | |
| def ids_of(tok, n: int, b: int, para: str): | |
| pieces = tok.pieces([para])[0] | |
| assert len(pieces) >= n + 64, len(pieces) | |
| out = [] | |
| for i in range(b): | |
| s = (i * 37) % (len(pieces) - n) | |
| out.append([101] + pieces[s:s + n - 2] + [102]) | |
| assert len(out[-1]) == n | |
| return out | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--engine", default="mk", choices=["mk", "ttnn"]) | |
| 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("--iters", type=int, default=30) | |
| ap.add_argument("--configs", default=None, help="BxN,BxN,...") | |
| ap.add_argument("--out", default=None) | |
| ap.add_argument("--fidelity", default="HiFi4") | |
| ap.add_argument("--act", default="bf16", help="ttnn residual dtype") | |
| a = ap.parse_args() | |
| if not a.model: | |
| from tt_minilm.api import resolve_weights | |
| a.model = resolve_weights() | |
| from tt_minilm.device import close_device, describe_device, open_device | |
| from tt_minilm.host import HostEmbeddings, finish | |
| from tt_minilm.tokenizer import Tokenizer | |
| from tt_minilm.weights import load_weights | |
| cfgs = CONFIGS if not a.configs else [tuple(int(v) for v in c.split("x")) for c in a.configs.split(",")] | |
| W = load_weights(a.model) | |
| he = HostEmbeddings(W) | |
| tok = Tokenizer(a.model) | |
| para = ("The harbor town woke slowly on winter mornings. Fishing boats left before dawn, their engines coughing " | |
| "in the cold, while the bakery on the corner pulled the first trays of bread from an oven that had been " | |
| "burning since four. By seven the school bus made its loop past the lighthouse, collecting children who " | |
| "argued about football and homework in equal measure. The old ferry, repainted every spring in the same " | |
| "faded blue, carried commuters across the bay to the city, where glass towers caught the low sun and " | |
| "offices filled with the noise of keyboards and coffee machines. In the afternoon the wind usually turned, " | |
| "pushing clouds in from the sea, and by evening the streets smelled of rain and frying fish. ") * 4 | |
| import ttnn | |
| dev = open_device(allow_fallback=False) | |
| res = {"engine": a.engine, "device": describe_device(dev), "configs": []} | |
| try: | |
| if a.engine == "mk": | |
| from tt_minilm.tt.mk.engine import MkEngine | |
| eng = MkEngine(dev, W, he, trace=True, fidelity=a.fidelity) | |
| else: | |
| from tt_minilm.tt.ttnn_engine import TtnnEngine | |
| eng = TtnnEngine(dev, W, he, trace=True, act_dtype=a.act) | |
| for B, n in cfgs: | |
| ids = ids_of(tok, n, B, para) | |
| texts = [" ".join(tok._tok.decode(x[1:-1]).split()) for x in ids] | |
| eng.run(ids) # compile / capture / warm | |
| eng.run(ids) | |
| # device time: back-to-back replays of the prepared request | |
| if a.engine == "mk": | |
| eng.prepare(ids) | |
| tid = eng._tid[eng.variant] | |
| else: | |
| Bb, S, e, mask, pw = eng.host_inputs(ids) | |
| tid = eng._get(Bb, S)["tid"] | |
| ttnn.synchronize_device(dev) | |
| t0 = time.perf_counter() | |
| for _ in range(a.iters): | |
| ttnn.execute_trace(dev, tid, cq_id=0, blocking=False) | |
| ttnn.synchronize_device(dev) | |
| dev_ms = 1e3 * (time.perf_counter() - t0) / a.iters | |
| calls, encs = [], [] | |
| for _ in range(a.iters): | |
| t0 = time.perf_counter() | |
| pooled, _ = eng.run(ids) | |
| calls.append(time.perf_counter() - t0) | |
| for _ in range(max(5, a.iters // 3)): | |
| t0 = time.perf_counter() | |
| x = tok.encode(texts) | |
| pooled, _ = eng.run(x) | |
| finish(pooled, [len(v) for v in x]) | |
| encs.append(time.perf_counter() - t0) | |
| r = {"batch": B, "tokens": n, "device_ms": round(dev_ms, 4), | |
| "call_ms_p50": round(1e3 * float(np.median(calls)), 4), | |
| "call_ms_min": round(1e3 * float(np.min(calls)), 4), | |
| "encode_ms_p50": round(1e3 * float(np.median(encs)), 4), | |
| "seq_per_s": round(B / float(np.median(calls)), 1), | |
| "seq_per_s_device": round(B / (dev_ms / 1e3), 1)} | |
| if a.engine == "mk": | |
| r["chunks"] = eng.last_plan.nchunk | |
| r["allgather"] = eng.variant | |
| print(json.dumps(r), flush=True) | |
| res["configs"].append(r) | |
| res["engine_detail"] = eng.describe() | |
| eng.close() | |
| finally: | |
| close_device(dev) | |
| if a.out: | |
| with open(a.out, "w") as f: | |
| json.dump(res, f, indent=1) | |
| if __name__ == "__main__": | |
| main() | |