File size: 4,345 Bytes
b48c9f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Serve each small model with vLLM on Kaggle's two T4s, run the stepmold bench, keep the results.

Runs top to bottom with no input, so it works as a "Save & Run All" commit. Each model's
results go to /kaggle/working/results/<label>.jsonl, appended and resumable, so a rerun only
does the case-runs that are missing.
"""

import hashlib
import json
import os
import signal
import subprocess
import sys
import tarfile
import time
import urllib.request

WORK = "/kaggle/working"
OUT = os.path.join(WORK, "results")
BENCH = os.path.join(WORK, "stepmold-bench")
STEPMOLD = os.path.join(WORK, "stepmold")  # a clone at 38235ab: pip installs leave out examples/
PORT = 8000

# One sampling setup for every model, so three runs of a case show how consistent it is.
# 0.7 and top_p 0.8 are Qwen's recommended non-thinking settings. Qwen3 runs with thinking
# off, the same way the stepmold GPU Space runs Qwen3 8B.
QWEN = {"temperature": 0.7, "top_p": 0.8}
NO_THINK = {"chat_template_kwargs": {"enable_thinking": False}}

MODELS = [
    # label, Hugging Face id, tensor-parallel size, request extras
    ("qwen2.5-0.5b-instruct", "Qwen/Qwen2.5-0.5B-Instruct", 1, QWEN),
    ("qwen2.5-1.5b-instruct", "Qwen/Qwen2.5-1.5B-Instruct", 1, QWEN),
    ("qwen3-4b", "Qwen/Qwen3-4B", 1, dict(QWEN, **NO_THINK)),
    ("qwen2.5-7b-instruct", "Qwen/Qwen2.5-7B-Instruct", 2, QWEN),
    ("qwen3-8b", "Qwen/Qwen3-8B", 2, dict(QWEN, **NO_THINK)),
]


def wait_ready(proc, timeout=1500):
    deadline = time.time() + timeout
    while time.time() < deadline:
        if proc.poll() is not None:
            raise RuntimeError("vLLM exited with code %s" % proc.returncode)
        try:
            with urllib.request.urlopen("http://localhost:%d/v1/models" % PORT, timeout=5) as reply:
                if reply.status == 200:
                    return
        except Exception:
            pass
        time.sleep(5)
    raise TimeoutError("vLLM did not come up within %ds" % timeout)


def serve(model, tp, log):
    cmd = [
        "vllm", "serve", model,
        "--dtype", "half",  # a T4 has no bf16
        "--tensor-parallel-size", str(tp),
        "--max-model-len", "4096",
        "--gpu-memory-utilization", "0.90",
        "--port", str(PORT),
    ]
    return subprocess.Popen(cmd, stdout=log, stderr=subprocess.STDOUT, start_new_session=True)


def stop(proc):
    try:
        os.killpg(proc.pid, signal.SIGTERM)
        proc.wait(timeout=120)
    except subprocess.TimeoutExpired:
        os.killpg(proc.pid, signal.SIGKILL)
    except ProcessLookupError:
        pass
    time.sleep(15)  # let the GPUs release their memory before the next model loads


def run_model(label, model, tp, extra):
    started = time.time()
    with open(os.path.join(OUT, label + ".vllm.log"), "w") as log:
        proc = serve(model, tp, log)
        try:
            wait_ready(proc)
            print("%s: server ready in %.0fs" % (label, time.time() - started), flush=True)
            subprocess.run([
                sys.executable, os.path.join(BENCH, "bench.py"),
                "--stepmold", STEPMOLD,
                "--model", "openai:http://localhost:%d/v1#%s" % (PORT, model),
                "--label", label,
                "--arms", "stepmold,plain_schema,plain",
                "--runs", "3",
                "--packs", "all",
                "--concurrency", "16",
                "--extra-body", json.dumps(extra),
                "--out", os.path.join(OUT, label + ".jsonl"),
            ], check=False)
        except Exception as exc:
            print("%s: FAILED: %s" % (label, exc), flush=True)
        finally:
            stop(proc)
    print("%s: finished in %.0fs" % (label, time.time() - started), flush=True)


def main(only=None):
    os.makedirs(OUT, exist_ok=True)
    digest = hashlib.sha256(open(os.path.join(BENCH, "bench.py"), "rb").read()).hexdigest()[:12]
    print("bench.py sha256", digest, flush=True)
    for label, model, tp, extra in MODELS:
        if not only or label in only:
            run_model(label, model, tp, extra)
    with tarfile.open(os.path.join(WORK, "stepmold-bench-kaggle-results.tar.gz"), "w:gz") as tar:
        tar.add(OUT, arcname="results")
    print("packed", os.path.join(WORK, "stepmold-bench-kaggle-results.tar.gz"), flush=True)


if __name__ == "__main__":
    main(set(sys.argv[1:]) or None)