"""speed.py workload (2,048 Zalo Legal docs corpus[::len//2048][:2048] + queries[::len//1024][:1024]) for one model on one idle GPU, plus nvidia-smi sampled memory. Usage: CUDA_VISIBLE_DEVICES=0 python speed_n35.py Appends one JSON line to $SPEED_OUT.""" import os, sys, json, time, threading, subprocess from pathlib import Path import numpy as np, torch import data, zoo key, smi_idx = sys.argv[1], sys.argv[2] OUT = Path(os.environ["SPEED_OUT"]) 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]] GiB = 2 ** 30 peak_smi = [0]; stop = [False] def sampler(): while not stop[0]: try: v = subprocess.run(["nvidia-smi", "-i", smi_idx, "--query-gpu=memory.used", "--format=csv,noheader,nounits"], capture_output=True, text=True, timeout=5).stdout.strip() peak_smi[0] = max(peak_smi[0], int(v)) except Exception: pass time.sleep(0.2) th = threading.Thread(target=sampler, daemon=True); th.start() sync = torch.cuda.synchronize t0 = time.time(); 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 stop[0] = True; th.join() rec = {"model": key, "hf": zoo.MODELS[key][0], "mode": "dense", "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, "queries_per_s": len(qs) / tq, "peak_vram_gib": torch.cuda.max_memory_allocated() / GiB, "weights_vram_gib": w, "nvidia_smi_peak_gib": peak_smi[0] / 1024, "n_docs": len(docs), "n_queries": len(qs), "doc_tokens": int(toks.sum()), "torch": torch.__version__, "ts": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())} if enc.kind.startswith("nemo35"): rec["batching"] = f"length-sorted, token budget {enc.m.tok_budget} per batch" with open(OUT, "a") as f: f.write(json.dumps(rec) + "\n") print(json.dumps(rec), flush=True)