Trinity / tools /colab_k3_eval.py
STAR-future's picture
Trinity: public release (history squashed)
f80e81a
Raw History Blame Contribute Delete
2.91 kB
"""Colab GPU cell driver: full-model evaluation of LittleBoy and its base with the k3ref reference.
from google.colab import userdata; import huggingface_hub as h, os; os.environ['HF_TOKEN'] = userdata.get('STARHUGGIN')
exec(open(h.hf_hub_download('STAR-future/Trinity', 'tools/colab_k3_eval.py', token=os.environ['HF_TOKEN'],
force_download=True)).read())
K3_LAYERS (env, default 93) runs only the first layers (smoke test). Results go to /content/k3eval and are uploaded
to the LittleBoy repo under eval/ (private until release).
"""
import json
import os
import subprocess
import sys
from pathlib import Path
from huggingface_hub import HfApi, hf_hub_download
TOK = os.environ["HF_TOKEN"]
REPO = "STAR-future/Trinity"
W = Path("/content/k3eval")
W.mkdir(parents=True, exist_ok=True)
subprocess.run([sys.executable, "-m", "pip", "install", "-q", "tiktoken", "datasets"], check=True)
for f in ("k3ref.py", "k3stream.py"):
p = hf_hub_download(REPO, f"tools/{f}", token=TOK, force_download=True)
(W / f).write_bytes(Path(p).read_bytes())
texts = W / "eval.jsonl"
if not texts.exists():
from datasets import load_dataset
ds = load_dataset("wikimedia/wikipedia", "20231101.en", split="train", streaming=True)
rows = []
for r in ds:
if len(r["text"]) > 16000:
rows.append({"id": r["title"], "text": r["text"], "set": "wikipedia"})
if len(rows) >= 16:
break
texts.write_text("".join(json.dumps(r) + "\n" for r in rows), encoding="utf-8")
layers = os.environ.get("K3_LAYERS", "93")
out = W / f"out_L{layers}"
cmd = [sys.executable, "-u", str(W / "k3stream.py"), "--texts", str(texts), "--seq", os.environ.get("K3_SEQ", "2048"),
"--batch", os.environ.get("K3_BATCH", "8"), "--max-seqs", os.environ.get("K3_MAXSEQS", "16"),
"--out", str(out), "--layers", layers]
print(" ".join(cmd), flush=True)
log = W / f"k3stream_L{layers}.log"
with subprocess.Popen(cmd, cwd=W, env=dict(os.environ, PYTHONPATH=str(W)), stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True, bufsize=1) as p, open(log, "w") as fo:
for line in p.stdout: # Colab does not show a child's output unless it is relayed
print(line, end="", flush=True)
fo.write(line)
print("exit", p.returncode, flush=True)
HfApi(token=TOK).upload_file(path_or_fileobj=str(log), path_in_repo=f"eval/k3stream_L{layers}.log", repo_id=REPO,
commit_message=f"k3ref run log, {layers} layers")
if p.returncode == 0 and (out / "summary.json").exists():
HfApi(token=TOK).upload_file(path_or_fileobj=str(out / "summary.json"), path_in_repo=f"eval/k3ref_summary_L{layers}.json",
repo_id=REPO, commit_message=f"k3ref evaluation, {layers} layers")
print(json.loads((out / "summary.json").read_text())["modes"], flush=True)