121 lines
5.5 KiB
Python
121 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""GPU-Wahl beim Laden (#1272): anfrage2warteschlange.11 bis .15. Läuft auf dem Entwicklungsrechner.
|
|
|
|
Voraussetzung wie block_cdg.py, dazu auf srv04 die Modelle darkidol-b und darkidol-c (dieselbe Datei wie darkidol)
|
|
und für darkidol, bonsai die GPU-Wahl "auto" unter 0, 1. Aufruf: block_gpu.py ERGEBNIS.jsonl [15 11 12 13 14]
|
|
"""
|
|
import json, subprocess, sys, threading, time
|
|
from block_cdg import BASE, SSH, Monitor, call, client, last, restart, ruhe, ssh, status, summe
|
|
|
|
N = "/tmp/nexus-kern/bin/nexus"; CFG = "--config /tmp/nexus-kern/config.toml"
|
|
LANG = ("idle_secs = 600", "maya_idle_secs = 600")
|
|
|
|
def lage():
|
|
s = status()
|
|
return [(i["model"], i["state"], i["gpus"], i["booked_mb_per_gpu"]) for i in s["instances"]]
|
|
|
|
def smi():
|
|
return [int(x) for x in ssh("nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits").split()]
|
|
|
|
def ereignisse():
|
|
prog = r'''import json
|
|
for l in open("/tmp/nexus-kern/serve.log"):
|
|
try: d=json.loads(l)
|
|
except Exception: continue
|
|
if d.get("ereignis") in ("platzierung","verdraengt","instanz_verschwunden") or d.get("level")=="ERROR":
|
|
print(json.dumps({k:d.get(k) for k in ("timestamp","ereignis","model","gpus","grund","message")}))'''
|
|
p = subprocess.run([*SSH, "python3 -I -"], input=prog, capture_output=True, text=True, timeout=60)
|
|
out = []
|
|
for l in p.stdout.splitlines():
|
|
d = json.loads(l); d["timestamp"] = (d["timestamp"] or "")[11:19]
|
|
out.append({k: v for k, v in d.items() if v is not None and not (k == "message" and d.get("ereignis"))})
|
|
return out
|
|
|
|
def liste(*namen):
|
|
return ssh(f"{N} model list {CFG} | grep -E '^({'|'.join(namen)}) '")
|
|
|
|
def schritt(model, **kw):
|
|
r = call(model, "normal", max_tokens=kw.get("max_tokens", 5))
|
|
time.sleep(4)
|
|
return {"anfrage": r, "lage": lage(), "gpu_mib": smi()}
|
|
|
|
def block_15(emit):
|
|
for m in ("bonsai", "erebus"):
|
|
restart(*LANG)
|
|
erst = schritt(m)
|
|
gemessen = liste(m)
|
|
ev = ereignisse()
|
|
restart(*LANG)
|
|
emit({"fall": "15_" + m, "erstes_laden": erst, "liste": gemessen, "log_erst": ev, "zweiter_start": schritt(m), "log_zweit": ereignisse()})
|
|
|
|
def block_11(emit):
|
|
restart(*LANG)
|
|
emit({"fall": "11_freie_karte", "a": schritt("darkidol"), "b": schritt("darkidol-b"), "a_noch": schritt("darkidol"),
|
|
"log": ereignisse(), "liste": liste("darkidol", "darkidol-b")})
|
|
|
|
def tempo(model, n=3):
|
|
werte = []
|
|
for _ in range(n):
|
|
t = time.time()
|
|
x = client.chat.completions.create(model=model, messages=[{"role": "user", "content": "Zähle von 1 bis 400, nur die Zahlen."}], max_tokens=300)
|
|
werte.append((x.usage.completion_tokens, round(x.usage.completion_tokens / (time.time() - t), 1)))
|
|
return werte
|
|
|
|
def block_12(emit):
|
|
# a) noch nie auf zwei Karten gemessen
|
|
restart(*LANG)
|
|
schritt("darkidol"); schritt("darkidol-b")
|
|
emit({"fall": "12a_ohne_zweikartenmesswert", "vorher": lage(), "c": schritt("darkidol-c"), "log": ereignisse()})
|
|
# Messwert für zwei Karten erzeugen
|
|
ssh(f"{N} model set --name darkidol-c --gpu-wahl fest --gpus 0,1 {CFG}")
|
|
restart(*LANG)
|
|
m = schritt("darkidol-c")
|
|
ssh(f"{N} model set --name darkidol-c --gpu-wahl auto --gpus 0,1 {CFG}")
|
|
restart(*LANG)
|
|
a = schritt("darkidol"); b = schritt("darkidol-b"); c = schritt("darkidol-c")
|
|
emit({"fall": "12b_aufteilen", "messlauf": m, "a": a, "b": b, "c": c, "log": ereignisse(),
|
|
"tempo_eine_karte": tempo("darkidol"), "tempo_zwei_karten": tempo("darkidol-c"), "ruhe": ruhe()})
|
|
|
|
def block_13(emit):
|
|
restart(*LANG)
|
|
klein = ["bonsai", "darkidol", "darkidol-b"]
|
|
for m in klein: schritt(m)
|
|
vorher = lage()
|
|
mon = Monitor()
|
|
gross, start = [], time.time()
|
|
def big():
|
|
for t in (30, 120, 210):
|
|
time.sleep(max(0, start + t - time.time()))
|
|
gross.append(call("cydonia", "normal", max_tokens=30))
|
|
x = threading.Thread(target=big); x.start()
|
|
rs = last(260, [lambda m=m: call(m, "normal", max_tokens=100) for m in klein * 2])
|
|
x.join()
|
|
emit({"fall": "13_gross_unter_kleinlast", "vorher": vorher, "gross": gross, "klein": summe(rs), "anfragen_klein": len(rs),
|
|
"monitor": mon.end(), "ruhe": ruhe(), "log": ereignisse()})
|
|
|
|
def block_14(emit):
|
|
for karte in (0, 1):
|
|
restart(*LANG)
|
|
pid = ssh("G=/srv/models/gguf/DarkIdol-Llama-3.1-8B-Instruct-1.2-Uncensored-Q6_K.gguf; "
|
|
f"CUDA_VISIBLE_DEVICES={karte} setsid nohup /srv/apps/llama.cpp/build/bin/llama-server -m $G -ngl 99 -c 4096 "
|
|
"--host 127.0.0.1 --port 19900 >/tmp/nexus-kern/fremd.log 2>&1 </dev/null & echo $!")
|
|
try:
|
|
ssh("curl -s -o /dev/null --retry 60 --retry-delay 1 --retry-all-errors -f http://127.0.0.1:19900/health", timeout=150)
|
|
time.sleep(15)
|
|
vorher = smi()
|
|
emit({"fall": f"14_fremd_auf_{karte}", "gpu_vorher_mib": vorher, "darkidol": schritt("darkidol"), "bonsai": schritt("bonsai"), "log": ereignisse()})
|
|
finally:
|
|
ssh(f"kill {int(pid)}")
|
|
|
|
def main():
|
|
with open(sys.argv[1], "a") as f:
|
|
def emit(d):
|
|
f.write(json.dumps(d, ensure_ascii=False) + "\n"); f.flush(); print(d["fall"], flush=True)
|
|
for b in sys.argv[2:] or ["15", "11", "12", "13", "14"]:
|
|
try: globals()["block_" + b](emit)
|
|
except Exception as e: emit({"fall": b + "_abbruch_des_skripts", "fehler": repr(e)[:500]})
|
|
ssh("bash /tmp/nexus-kern/remote.sh stop")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|