Download code/harness/speed.py from quocbao747/embedding-model-benchmark: direct link, hf CLI and curl.
- Browser
- Download file 3.14 kB
-
https://huggingface.co/quocbao747/embedding-model-benchmark/resolve/main/code/harness/speed.py
- Command line
-
hf download hf://quocbao747/embedding-model-benchmark/code/harness/speed.py
-
curl -L -o speed.py https://huggingface.co/quocbao747/embedding-model-benchmark/resolve/main/code/harness/speed.py
3.14 kB
| """Throughput + peak-VRAM benchmark, one model per process, on one otherwise idle GPU. | |
| Fixed workload for every model: 2,048 Zalo Legal documents (every n-th, deterministic) + 1,024 Zalo Legal queries, | |
| each model with its own benchmark settings (prompt format, 512-token window, batch size, bf16; BGE-M3 fp16). | |
| Usage: python speed.py <model_key> -> appends one JSON line per mode to $SPEED_OUT (default ~/embed/results/speed/speed.jsonl)""" | |
| import os, sys, json, time | |
| from pathlib import Path | |
| import numpy as np, torch | |
| import data, zoo | |
| key = sys.argv[1] | |
| OUT = Path(os.path.expanduser(os.environ.get("SPEED_OUT", "~/embed/results/speed/speed.jsonl"))); OUT.parent.mkdir(parents=True, exist_ok=True) | |
| corpus, queries, _ = data.load_task("ZacLegalTextRetrieval") | |
| docs = corpus[:: max(1, len(corpus) // 2048)][:2048]; qs = [q["text"] for q in queries[:: max(1, len(queries) // 1024)][:1024]] | |
| sync = torch.cuda.synchronize | |
| GiB = 2 ** 30 | |
| def emit(rec): | |
| rec = {"model": key, **rec, "n_docs": len(docs), "n_queries": len(qs), "torch": torch.__version__, "ts": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())} | |
| with open(OUT, "a") as f: | |
| f.write(json.dumps(rec) + "\n") | |
| print(json.dumps(rec), flush=True) | |
| t0 = time.time() | |
| if key == "bge-m3": | |
| import m3 | |
| enc = m3.M3(max_len=512, bs=128); sync(); load_s = time.time() - t0 | |
| texts = [data.doc_plain(d) for d in docs] | |
| ntok = sum(min(len(x), 512) for x in enc.tok(texts, add_special_tokens=True)["input_ids"]) | |
| for mode, kw in (("dense", {}), ("all", {"sparse": True, "colbert": True})): | |
| kw = {"sparse": False, "colbert": False, **kw} | |
| enc.encode(texts[:128], **kw); sync(); torch.cuda.empty_cache(); torch.cuda.reset_peak_memory_stats() | |
| t = time.time(); out = enc.encode(texts, **kw); sync(); td = time.time() - t | |
| t = time.time(); enc.encode(qs, **kw); sync(); tq = time.time() - t | |
| emit({"model": "bge-m3" if mode == "dense" else "bge-m3-all", "hf": "BAAI/bge-m3", "mode": mode, "dtype": "fp16", "batch": enc.bs, "max_len": 512, | |
| "load_s": load_s, "docs_per_s": len(docs) / td, "tok_per_s": ntok / td, "queries_per_s": len(qs) / tq, | |
| "peak_vram_gib": torch.cuda.max_memory_allocated() / GiB, "weights_vram_gib": None}) | |
| del out | |
| else: | |
| enc = zoo.Enc(key); sync(); load_s = time.time() - t0 | |
| w = torch.cuda.memory_allocated() / GiB | |
| toks = enc.count_tokens([enc.dtext(d) for d in docs]) | |
| enc.encode_docs(docs[:128]); sync(); torch.cuda.empty_cache(); torch.cuda.reset_peak_memory_stats() | |
| t = time.time(); enc.encode_docs(docs); sync(); td = time.time() - t | |
| t = time.time(); enc.encode_queries(qs, "ZacLegalTextRetrieval"); sync(); tq = time.time() - t | |
| emit({"hf": zoo.MODELS[key][0], "mode": "dense" if enc.kind != "multivec" else "multivector", "dtype": enc.dtype_name, "batch": enc.bs, "max_len": enc.max_len, | |
| "load_s": load_s, "docs_per_s": len(docs) / td, "tok_per_s": (float(toks.sum()) / td) if toks is not None else None, "queries_per_s": len(qs) / tq, | |
| "peak_vram_gib": torch.cuda.max_memory_allocated() / GiB, "weights_vram_gib": w}) | |