okami.mom/strata

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

Niko1221Merge commit 'b8d604d28' into f-clean-1ab0fc432ed

main
4.8 KiB94 linesraw
1"""vision_proto.py - pictures over the engine protocol (GENI / BGENI), no server: the ids come from llama.cpp's own tokenizer
2output (vc1/*.llama.log of vcmp.py), the rows from strata-vision (<picture>.sve).
3
4  python q36_vision_proto.py --exe E --args "--model M --max-context 8192 --vision --batch 4" --vc DIR --pics DIR
5
6Checks: 1. a solo GENI answers like llama.cpp's greedy (the first 24 tokens); 2. two pictures and a text prompt decoding
7together (BGENI / BGEN) give each the tokens of its solo run; 3. the conversation cache: the same request again and a
8longer one (the first, its answer and more text) reuse the held state, a different picture does not; a text request after
9a picture request is not disturbed (the rotary offset is back at 0).
10
11Start the engine without --mtp for the cache check: the held tokens of a request that ran with drafts include the drafts accepted past
12its last emitted token, so a prompt that does not repeat them reads from the start (correct, but the check wants the reuse)."""
13from pathlib import Path
14import argparse, os, shlex, sys
15sys.path.insert(0, __file__.rsplit("/", 1)[0] if "/" in __file__ else ".")
16from q36_harness import Eng, load_prompts
17
18PAD = 248056
19
20
21def case(vc, pics, name, q):
22    log = open(os.path.join(vc, "%s.q%d.llama.log" % (name, q))).read()
23    ids, gen = [], []
24    for l in log.splitlines():
25        if l.startswith("text:"):
26            ids += [int(x) for x in l.split(":", 1)[1].split()]
27        elif l.startswith("image:"):
28            ids += [PAD] * int(l.split()[1])
29        elif l.startswith("gen:"):
30            gen = [int(x) for x in l.split(":", 1)[1].split()]
31    return ids, gen, os.path.join(pics, name + ".sve")
32
33
34ap = argparse.ArgumentParser()
35ap.add_argument("--exe", required=True)
36ap.add_argument("--args", default="")
37ap.add_argument("--vc", required=True)
38ap.add_argument("--pics", required=True)
39ap.add_argument("--n", type=int, default=24)
40ap.add_argument("--log", default=None)
41a = ap.parse_args()
42eng = Eng(a.exe, shlex.split(a.args), log=a.log)
43print("ready", eng.ready, eng.info, flush=True)
44ok = True
45S0, G0, E0 = case(a.vc, a.pics, "shapes.png", 0)
46S1, G1, E1 = case(a.vc, a.pics, "shapes.png", 1)
47C0, GC0, EC0 = case(a.vc, a.pics, "code4217.png", 0)
48C1, GC1, EC1 = case(a.vc, a.pics, "code4217.png", 1)
49
50# 1. solo vs llama.cpp's greedy
51solo = {}
52for tag, (ids, gen, emb) in {"shapes.q0": (S0, G0, E0), "shapes.q1": (S1, G1, E1), "code.q0": (C0, GC0, EC0),
53                              "code.q1": (C1, GC1, EC1)}.items():
54    toks, done, ttft, wall = eng.solo(ids, a.n, emb=emb)
55    solo[tag] = toks
56    k = next((i for i in range(min(len(toks), len(gen))) if toks[i] != gen[i]), None)
57    print("solo %-10s %d tokens, vs llama.cpp greedy: %s | %s" % (tag, len(toks), "same %d tokens" % len(toks) if k is None else "first difference at %d" % k, done[:60]), flush=True)
58
59# 2. batch: two pictures + a text prompt
60text = load_prompts(str(Path(__file__).resolve().parent / "parity_prompts.ids"))[0]
61t_toks, _, _, _ = eng.solo(text, a.n)
62reqs = [{"ids": S1, "n": a.n, "slot": 0, "emb": E1}, {"ids": C1, "n": a.n, "slot": 1, "emb": EC1},
63        {"ids": text, "n": a.n, "slot": 2}]
64r = eng.batch(reqs)
65eq = [r["tokens"][0] == solo["shapes.q1"], r["tokens"][1] == solo["code.q1"], r["tokens"][2] == t_toks]
66print("batch (2 pictures + 1 text): identical to solo %s" % eq, flush=True)
67ok &= all(eq)
68# slot reuse: the same again into other slots
69r2 = eng.batch([{"ids": C1, "n": a.n, "slot": 3, "emb": EC1}, {"ids": S1, "n": a.n, "slot": 0, "emb": E1}])
70eq2 = [r2["tokens"][0] == solo["code.q1"], r2["tokens"][1] == solo["shapes.q1"]]
71print("batch again (slot 3 new, slot 0 held): identical %s" % eq2, flush=True)
72ok &= all(eq2)
73
74# 3. the conversation cache (solo path)
75def reused(done):
76    return int(done.split()[8])
77
78toks1, done1, _, _ = eng.solo(S0, 16, emb=E0)
79toks1b, done1b, _, _ = eng.solo(S0, 16, emb=E0)
80print("same request again (a state that has moved past the prompt cannot step back: no reuse): reused %d of %d, tokens %s" % (reused(done1b), len(S0), "same" if toks1b == toks1 else "DIFFERENT"), flush=True)
81ok &= toks1b == toks1
82longer = S0 + toks1 + text[:12]
83toks2, done2, _, _ = eng.solo(longer, 16, emb=E0)
84print("longer request (picture + answer + more): reused %d of %d" % (reused(done2), len(longer)), flush=True)
85ok &= reused(done2) >= len(S0)
86toks3, done3, _, _ = eng.solo(C0, 16, emb=EC0)   # a different picture
87print("a different picture: reused %d" % reused(done3), "tokens vs its solo: %s" % ("same" if toks3 == solo["code.q0"][:16] else "DIFFERENT"), flush=True)
88ok &= toks3 == solo["code.q0"][:16]
89t2, done4, _, _ = eng.solo(text, a.n)
90print("a text request after pictures: tokens vs the text solo: %s" % ("identical" if t2 == t_toks else "DIFFERENT"), flush=True)
91ok &= t2 == t_toks
92eng.close()
93print("GATE", "PASS" if ok else "FAIL")
94sys.exit(0 if ok else 1)