sedweave-7b / evaluation /benchmark_gguf.py
asterbraid's picture
Sedweave 7B: 59.5% dev and 64.9% frozen correctness with GGUF, Ollama and CPU benchmarks
a55876e verified
Raw History Blame Contribute Delete
4.19 kB
from __future__ import annotations
import argparse
import json
import statistics
import subprocess
import time
import urllib.request
from pathlib import Path
from transformers import AutoTokenizer
from sedweave.prompts import messages
REQUEST = "Replace every occurrence of amber with delta."
INPUT = "amber birch\ncobalt amber\n"
EXPECTED = "s/amber/delta/g"
OPENER = urllib.request.build_opener(urllib.request.ProxyHandler({}))
def completion(prompt: str, port: int) -> tuple[str, float]:
request = urllib.request.Request(
f"http://127.0.0.1:{port}/completion",
data=json.dumps({"prompt": prompt, "n_predict": 64, "temperature": 0, "cache_prompt": False}).encode(),
headers={"Content-Type": "application/json"},
)
started = time.perf_counter()
with OPENER.open(request, timeout=120) as response:
data = json.load(response)
return data["content"], time.perf_counter() - started
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--server", type=Path, required=True)
parser.add_argument("--models", nargs="+", required=True)
parser.add_argument("--tokenizer", required=True)
parser.add_argument("--output", type=Path, required=True)
parser.add_argument("--runs", type=int, default=5)
parser.add_argument("--port", type=int, default=8080)
args = parser.parse_args()
tokenizer = AutoTokenizer.from_pretrained(args.tokenizer)
prompt = tokenizer.apply_chat_template(messages(REQUEST, INPUT), tokenize=False, add_generation_prompt=True)
results = []
for model_path in args.models:
started = time.perf_counter()
log_path = args.output.parent / f"{Path(model_path).stem}.server.log"
log = log_path.open("w", encoding="utf-8")
process = subprocess.Popen(
[str(args.server), "-m", model_path, "--host", "127.0.0.1", "--port", str(args.port), "-c", "4096", "-t", "8"],
stdout=log,
stderr=log,
)
try:
for _ in range(240):
if process.poll() is not None:
raise RuntimeError(f"llama-server exited with {process.returncode}")
try:
OPENER.open(f"http://127.0.0.1:{args.port}/health", timeout=1).close()
break
except Exception:
time.sleep(0.5)
else:
raise TimeoutError("llama-server did not become ready")
startup_seconds = time.perf_counter() - started
content, cold_generation = completion(prompt, args.port)
if EXPECTED not in content:
raise ValueError(f"Unexpected generation for {model_path}: {content}")
warm = []
for _ in range(args.runs):
content, elapsed = completion(prompt, args.port)
warm.append(elapsed)
if EXPECTED not in content:
raise ValueError(f"Unexpected generation for {model_path}: {content}")
status = Path(f"/proc/{process.pid}/status").read_text()
peak_kib = int(next(line.split()[1] for line in status.splitlines() if line.startswith("VmHWM:")))
result = {
"model": model_path,
"startup_seconds": startup_seconds,
"cold_generation_seconds": cold_generation,
"cold_total_seconds": startup_seconds + cold_generation,
"warm_seconds": warm,
"warm_median_seconds": statistics.median(warm),
"max_rss_gib": peak_kib / 1024 / 1024,
}
results.append(result)
print(json.dumps(result), flush=True)
finally:
process.terminate()
try:
process.wait(timeout=10)
except subprocess.TimeoutExpired:
process.kill()
process.wait()
log.close()
payload = {"request": REQUEST, "input": INPUT, "runs": args.runs, "threads": 8, "cache_prompt": False, "results": results}
args.output.write_text(json.dumps(payload, indent=2), encoding="utf-8")
if __name__ == "__main__":
main()