okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-3v (aec86299d) into f-own-int: our own Qwen vision tower (mtmd stays the default)0479b72e9

main
3.8 KiB86 linesraw
1#!/usr/bin/env python3
2"""Alternating-pairs speed test of two picture readers of the same helper (default: our fast CPU tower against mtmd): per picture one warm-up encode of each, then N pairs
3in the order A, B, A, B, ... so that drift of the machine (clocks, other jobs) hits both the same way.  Reports the median, the minimum and the maximum of each (the `ms` the
4helper prints) and the ratio of the medians; also the ratio of every single pair (min / median / max), the spread that decides whether a difference is real.
5
6    python3 speed_pairs.py --helper <strata-vision> --mmproj <mmproj> --model <text model> [--threads N] [--pairs 5] [--max-tokens 1024]
7                           [--a own --env-a STRATA_VIT_CPU=fast] [--b mtmd] <image> ...
8"""
9import argparse
10import os
11import statistics
12import subprocess
13import sys
14
15
16def start(helper, a, tower, envs, label):
17    args = [helper, "--mmproj", a.mmproj, "--model", a.model, "--tower", tower, "--max-tokens", str(a.max_tokens)]
18    if a.threads:
19        args += ["--threads", str(a.threads)]
20    env = dict(os.environ)
21    for kv in envs:
22        k, _, v = kv.partition("=")
23        env[k] = v
24    p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True, env=env)
25    if not p.stdout.readline().startswith("READY"):
26        raise SystemExit("%s reader did not start" % label)
27    return p
28
29
30def enc(p, img, out):
31    p.stdin.write("ENC %s %s\n" % (img, out))
32    p.stdin.flush()
33    r = p.stdout.readline().split()
34    if not r or r[0] != "OK":
35        raise SystemExit("encode failed: %s" % " ".join(r))
36    return float(r[4]), int(r[1])
37
38
39def main():
40    ap = argparse.ArgumentParser()
41    ap.add_argument("--helper", required=True)
42    ap.add_argument("--mmproj", required=True)
43    ap.add_argument("--model", required=True)
44    ap.add_argument("--threads", type=int, default=0)
45    ap.add_argument("--pairs", type=int, default=5)
46    ap.add_argument("--max-tokens", type=int, default=1024)
47    ap.add_argument("--a", default="own")
48    ap.add_argument("--b", default="mtmd")
49    ap.add_argument("--env-a", action="append", default=["STRATA_VIT_CPU=fast"])
50    ap.add_argument("--env-b", action="append", default=[])
51    ap.add_argument("--tmp", default="/tmp")
52    ap.add_argument("images", nargs="+")
53    a = ap.parse_args()
54    pa = start(a.helper, a, a.a, a.env_a, "A")
55    pb = start(a.helper, a, a.b, a.env_b, "B")
56    print("A = %s %s, B = %s %s, %s threads, %d alternating pairs after one warm-up each (ms of the helper's OK line)" % (a.a, a.env_a, a.b, a.env_b, a.threads or "default", a.pairs))
57    print("%-24s %6s | %-26s | %-26s | %6s | %s" % ("picture", "tokens", "A median (min..max)", "B median (min..max)", "A/B", "pair ratios min / median / max"))
58    worst = 0.0
59    for img in a.images:
60        name = os.path.basename(img)
61        oa, ob = os.path.join(a.tmp, "sp_a_" + name + ".sve"), os.path.join(a.tmp, "sp_b_" + name + ".sve")
62        _, n = enc(pa, img, oa)
63        enc(pb, img, ob)
64        ta, tb = [], []
65        for _ in range(a.pairs):
66            ta.append(enc(pa, img, oa)[0])
67            tb.append(enc(pb, img, ob)[0])
68        ra = [x / y for x, y in zip(ta, tb)]
69        ma, mb = statistics.median(ta), statistics.median(tb)
70        worst = max(worst, ma / mb)
71        print("%-24s %6d | %8.0f (%6.0f..%6.0f) | %8.0f (%6.0f..%6.0f) | %6.3f | %.3f / %.3f / %.3f" %
72              (name, n, ma, min(ta), max(ta), mb, min(tb), max(tb), ma / mb, min(ra), statistics.median(ra), max(ra)), flush=True)
73        for f in (oa, ob):
74            try:
75                os.remove(f)
76            except OSError:
77                pass
78    print("largest A/B ratio of medians: %.3f" % worst)
79    for p in (pa, pb):
80        p.stdin.write("QUIT\n")
81        p.stdin.flush()
82        p.wait(timeout=60)
83
84
85if __name__ == "__main__":
86    sys.exit(main())