tools: session_pressure_test.py and batch_groups_cache_test.py
This commit is contained in:
1 parent
0650f31670
commit
31ffa153c6
2 files changed
+190
No files matched your search
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
"""#1604: with --batch N --batch-groups N on a layer split, two long conversations that alternate must reuse their slot
|
||||
(or the conversation cache) on every later turn instead of reading their prompt from token 0.
|
||||
|
||||
python3 tools/batch_groups_cache_test.py CFG OUT.json
|
||||
STRATA_EXTRA_ARGS="--batch 2 --batch-groups 2 --prompt-cache 4 --conversation-cache-mib 4096 --conversation-cache-slots 3"
|
||||
|
||||
Three rounds; each round runs conversation A and conversation B at the same time (so both go through the batch slots),
|
||||
the next round continues each one (prompt + its answer + a new user turn). The engine log says per request how many
|
||||
prompt tokens were reused. PASS when every request of rounds 2 and 3 reused at least 90 % of the previous round's
|
||||
prompt. The answers (token ids) are written to OUT.json to compare two runs.
|
||||
"""
|
||||
import json, os, re, sys, threading
|
||||
from pathlib import Path
|
||||
|
||||
REPO = Path(os.environ.get("STRATA_REPO", Path(__file__).resolve().parent.parent))
|
||||
sys.path.insert(0, str(REPO / "tools")); sys.path.insert(0, str(REPO))
|
||||
import strata_tokenizer as ST
|
||||
from serve.server import StrataEngine, child_env, engine_args
|
||||
|
||||
cfg = json.load(open(sys.argv[1]))
|
||||
tp = Path(cfg["tokenizer"])
|
||||
vocab = json.loads((tp / "vocab.json").read_text())
|
||||
toks = [None] * len(vocab)
|
||||
for t, i in vocab.items():
|
||||
toks[i] = t
|
||||
tok = ST.Tokenizer(toks, (tp / "merges.txt").read_text().split("\n"), json.loads((tp / "token_type.json").read_text()))
|
||||
if os.environ.get("GPUS"): # e.g. GPUS=0,1: a layer split over those cards (SPLIT=auto)
|
||||
cfg["gpu"] = [int(x) for x in os.environ["GPUS"].split(",")]
|
||||
cfg["layer_split"] = os.environ.get("SPLIT", "auto")
|
||||
args = engine_args(cfg) + os.environ.get("STRATA_EXTRA_ARGS", "").split()
|
||||
log = os.environ.get("SMOKE_LOG", cfg["log"])
|
||||
eng = StrataEngine(os.environ.get("STRATA_EXE", cfg["exe"]), args, cwd=cfg.get("cwd"), log=log, env=child_env(cfg))
|
||||
GEN = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
|
||||
|
||||
|
||||
def enc(text):
|
||||
return tok.encode(text, parse_special=True)
|
||||
|
||||
|
||||
def first(tag, n):
|
||||
return enc(f"<|im_start|>user\n[{tag}] " + " ".join(f"Entry {i} of {tag}: value {i * 13 % 97}." for i in range(n))
|
||||
+ "\nCount from 1 to 80 separated by commas." + GEN)
|
||||
|
||||
|
||||
conv = {"A": first("alpha", 250), "B": first("bravo", 250)}
|
||||
answers = {"A": [], "B": []}
|
||||
prev_len = {}
|
||||
rounds = []
|
||||
|
||||
|
||||
def one(name, ids, out):
|
||||
res = [t for t in eng.generate(ids, 80, {"temperature": 0}, threading.Event()) if t is not None]
|
||||
out[name] = res
|
||||
|
||||
|
||||
for rnd in range(3):
|
||||
lens = {k: len(v) for k, v in conv.items()}
|
||||
out = {}
|
||||
ts = [threading.Thread(target=one, args=(k, conv[k], out)) for k in ("A", "B")]
|
||||
for t in ts:
|
||||
t.start()
|
||||
for t in ts:
|
||||
t.join()
|
||||
rounds.append({"prompt_lens": lens, "answers": out})
|
||||
print(f"round {rnd + 1}: prompts {lens}, answers {[len(v) for v in out.values()]}", flush=True)
|
||||
for k in conv:
|
||||
answers[k].append(out[k])
|
||||
conv[k] = conv[k] + out[k] + enc(f"<|im_end|>\n<|im_start|>user\nNow count from {rnd + 2} to {rnd + 60}." + GEN)
|
||||
eng.proc.stdin.close()
|
||||
try:
|
||||
eng.proc.wait(60)
|
||||
except Exception:
|
||||
eng.proc.kill()
|
||||
json.dump(rounds, open(sys.argv[2], "w"))
|
||||
|
||||
text = open(log, errors="replace").read()
|
||||
reqs = [(int(a), int(b)) for a, b in re.findall(r"strata serve: prompt (\d+) tokens = (\d+) reused", text)]
|
||||
reqs += [(int(a), int(b)) for a, b in re.findall(r"strata batch: slot \d+ \w+ (\d+) tokens", text) if False]
|
||||
print("requests (prompt tokens, reused):", reqs)
|
||||
print("slot gave back lines:", len(re.findall(r"gave back", text)), " conversation cache restores:",
|
||||
len(re.findall(r"conversation cache: restored", text)))
|
||||
# rounds 2 and 3 are the last 4 requests of the log
|
||||
ok = len(reqs) >= 6
|
||||
for p, r in reqs[2:]:
|
||||
if r < 0.9 * (p - 140): # the previous prompt + answer is ~all of it but the new turn
|
||||
ok = False
|
||||
print("PASS" if ok else "FAIL")
|
||||
sys.exit(0 if ok else 1)
|
||||
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env python3
|
||||
"""SAVE / RESTORE round trip with parked conversations holding the host RAM (#1572 / #1573).
|
||||
|
||||
python3 tools/session_pressure_test.py CFG OUTFILE
|
||||
STRATA_EXTRA_ARGS="--prompt-cache 4 --conversation-cache-mib 3072 --conversation-cache-slots 4
|
||||
--conversation-cache-min-free-mib <floor> [--session-save-reclaim]"
|
||||
|
||||
Parks three conversations, runs a fourth (D), SAVEs it, runs an unrelated one, RESTOREs, and checks that D's next turn
|
||||
gives the same token ids after the restore as it does when continued live. Prints SAVE: ok|refused (...) and PASS/FAIL.
|
||||
"""
|
||||
import json, os, sys, threading, time
|
||||
from pathlib import Path
|
||||
|
||||
REPO = Path(os.environ.get("STRATA_REPO", Path(__file__).resolve().parent.parent))
|
||||
sys.path.insert(0, str(REPO / "tools")); sys.path.insert(0, str(REPO))
|
||||
import strata_tokenizer as ST
|
||||
from serve import server as S
|
||||
from serve.server import StrataEngine, child_env
|
||||
|
||||
cfg = json.load(open(sys.argv[1]))
|
||||
out_file = sys.argv[2]
|
||||
tp = Path(cfg["tokenizer"])
|
||||
vocab = json.loads((tp / "vocab.json").read_text())
|
||||
toks = [None] * len(vocab)
|
||||
for t, i in vocab.items():
|
||||
toks[i] = t
|
||||
tok = ST.Tokenizer(toks, (tp / "merges.txt").read_text().split("\n"), json.loads((tp / "token_type.json").read_text()))
|
||||
args = cfg["args"] + os.environ.get("STRATA_EXTRA_ARGS", "").split()
|
||||
eng = StrataEngine(os.environ.get("STRATA_EXE", cfg["exe"]), args, cwd=cfg.get("cwd"),
|
||||
log=os.environ.get("SMOKE_LOG", cfg["log"]), env=child_env(cfg))
|
||||
GEN = "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
|
||||
|
||||
|
||||
def enc(text):
|
||||
return tok.encode(text, parse_special=True)
|
||||
|
||||
|
||||
def run(ids, max_new=60):
|
||||
out = [t for t in eng.generate(ids, max_new, {"temperature": 0}, threading.Event()) if t is not None]
|
||||
return out, int(eng.last.get("reused") or 0)
|
||||
|
||||
|
||||
def chat(tag, n):
|
||||
return enc(f"<|im_start|>user\n[{tag}] " + " ".join(f"Entry {i} of {tag}: value {i * 13 % 97}." for i in range(n))
|
||||
+ "\nSummarize in one line." + GEN)
|
||||
|
||||
|
||||
for tag in ("alpha", "bravo", "charlie"):
|
||||
p = chat(tag, 70)
|
||||
run(p, 20)
|
||||
D = chat("delta", 80)
|
||||
outD, _ = run(D, 60)
|
||||
print(f"D: prompt {len(D)} tokens, {len(outD)} generated", flush=True)
|
||||
nxt = enc("<|im_end|>\n<|im_start|>user\nNow say only the word done." + GEN)
|
||||
saved = False
|
||||
hog = None
|
||||
if os.environ.get("HOG_TARGET_MIB"): # take the RAM so that only HOG_TARGET_MIB stay available: a SAVE needs more
|
||||
import psutil
|
||||
extra = psutil.virtual_memory().available - int(os.environ["HOG_TARGET_MIB"]) * 2**20
|
||||
if extra > 0:
|
||||
hog = bytearray(extra)
|
||||
for i in range(0, len(hog), 4096):
|
||||
hog[i] = 1
|
||||
print("available now: %d MiB" % (psutil.virtual_memory().available >> 20), flush=True)
|
||||
try:
|
||||
r = eng.session_file("save", out_file)
|
||||
saved = True
|
||||
print("SAVE: ok", r, flush=True)
|
||||
except S.SessionRefused as e:
|
||||
print("SAVE: refused", e.kind, str(e)[:300], flush=True)
|
||||
if not saved and "--session-save-reclaim" in args:
|
||||
# the server's bounded retry on the same engine
|
||||
class Svc:
|
||||
engine = eng
|
||||
from serve.session_save_retry import session_file as retry
|
||||
try:
|
||||
r = retry(Svc, "save", out_file, S.SessionRefused)
|
||||
saved = True
|
||||
print("SAVE after the bounded wait: ok", r, flush=True)
|
||||
except S.SessionRefused as e:
|
||||
print("SAVE after the bounded wait: refused", e.kind, str(e)[:300], flush=True)
|
||||
hog = None # the RAM goes back
|
||||
if not saved:
|
||||
print("NOSAVE")
|
||||
eng.proc.stdin.close()
|
||||
sys.exit(2)
|
||||
live_next, reused_live = run(D + outD + nxt, 20)
|
||||
print(f"D live next turn: reused {reused_live}", flush=True)
|
||||
run(chat("echo", 70), 20) # something else takes the session
|
||||
r = eng.session_file("restore", out_file)
|
||||
print("RESTORE:", r, flush=True)
|
||||
again, reused_r = run(D + outD + nxt, 20)
|
||||
print(f"D after RESTORE: reused {reused_r}", flush=True)
|
||||
eng.proc.stdin.close()
|
||||
try:
|
||||
eng.proc.wait(60)
|
||||
except Exception:
|
||||
eng.proc.kill()
|
||||
ok = again == live_next and reused_r > 0
|
||||
print("PASS" if ok else "FAIL (ids %s)" % ("equal" if again == live_next else "differ"))
|
||||
sys.exit(0 if ok else 1)
|
||||
Reference in new issue
Block a user