"""Time per image of every deployment path, measured interleaved in one process: on the shared server the time per run drifts between runs (one model: 5.6-11.1 ms), so each round times every path back to back and the report is the median over rounds of each path minus the plain model in that round. python bench_latency.py --base s2.pt --site lt_merged.pt --lora lt.pt --data br.yaml --device 0 [--batch 32] [--cpu] --ref times one more plain model in the same rounds, e.g. the unpruned site model next to a pruned pair. """ import argparse import copy import statistics import time import torch from deploy_route import BaseRouter, Router, fuse_all, run, stats from gridkd import * # noqa: F401,F403 def main(): p = argparse.ArgumentParser() p.add_argument("--base", required=True) p.add_argument("--site", required=True, help="new site's merged model") p.add_argument("--lora", required=True, help="new site's LoRA checkpoint: names the card layers") p.add_argument("--data", required=True, help="any data.yaml: its val split only sizes the cards (timing does not depend on their values)") p.add_argument("--rounds", type=int, default=15) p.add_argument("--n", type=int, default=50) p.add_argument("--device", default="0", help="GPU for the val that sizes the cards (and for timing without --cpu)") p.add_argument("--batch", type=int, default=1) p.add_argument("--cpu", action="store_true", help="time on the CPU in fp32 (threads: OMP_NUM_THREADS)") p.add_argument("--ref", help="plain model to time alongside") a = p.parse_args() names = [n for n, x in load_plain(a.lora).model.named_modules() if isinstance(x, MultiLoRAConv)][:8] sf = stats(load_plain(a.base), names, a.data, "val", a.device, 10) fused_cards = [DiagGMM.fit(s, 4) for s in sf] early_cards = fused_cards[:-1] dev, dt = (torch.device("cpu"), torch.float) if a.cpu else (torch.device("cuda"), torch.half) # cuda: pinned by the val above load = lambda w: fuse_all(load_plain(w).model).to(dev, dt).eval() # noqa: E731 m0, m1 = load(a.base), load(a.site) early = Router((copy.deepcopy(m0), copy.deepcopy(m1)), names, [early_cards, early_cards], 0.0) # own copies: hooks stay apart fused = BaseRouter((copy.deepcopy(m0), copy.deepcopy(m1)), names, [fused_cards, fused_cards], 0.0) x = torch.rand(a.batch, 3, 640, 640, device=dev, dtype=dt) sync = (lambda: None) if a.cpu else torch.cuda.synchronize def kept(t): fused.force = False return fused(t) def sent(t): fused.force = True return fused(t) paths = {"one model": lambda t: run(m1, t, [], 0, len(m1.model)), "early-layer cards": early, "fused, kept on old site": kept, "fused, sent to new site": sent, "both models in full": lambda t: (run(m0, t, [], 0, len(m0.model)), run(m1, t, [], 0, len(m1.model)))} if a.ref: r = load(a.ref) paths["reference model"] = lambda t: run(r, t, [], 0, len(r.model)) ms = {k: [] for k in paths} with torch.no_grad(): for f in paths.values(): # warm-up for _ in range(20): f(x) for _ in range(a.rounds): for k, f in paths.items(): sync() t0 = time.perf_counter() for _ in range(a.n): f(x) sync() ms[k].append((time.perf_counter() - t0) / a.n / a.batch * 1e3) base = ms["one model"] print(f"{a.site} | {'cpu fp32' if a.cpu else 'gpu fp16'} | {a.rounds} rounds x {a.n} batches of {a.batch} | ms per image" + (f" | reference {a.ref}" if a.ref else "")) for k, v in ms.items(): d = [x - y for x, y in zip(v, base)] print(f" {k:26s} median {statistics.median(v):6.2f} ms (min {min(v):6.2f}, max {max(v):6.2f}) minus one model: median {statistics.median(d):+5.2f} ms") if __name__ == "__main__": main()