Spaces:
Running on Zero
Running on Zero
Add model filter param to benchmark endpoint
Browse files
app.py
CHANGED
|
@@ -471,10 +471,14 @@ def _ensure_llama_cpp():
|
|
| 471 |
return llama_cli
|
| 472 |
|
| 473 |
|
| 474 |
-
def _run_benchmark():
|
| 475 |
-
"""Run CPU benchmark on
|
|
|
|
|
|
|
|
|
|
|
|
|
| 476 |
global _BENCHMARK_RESULTS
|
| 477 |
-
if _BENCHMARK_RESULTS:
|
| 478 |
return _BENCHMARK_RESULTS
|
| 479 |
|
| 480 |
import subprocess
|
|
@@ -486,7 +490,13 @@ def _run_benchmark():
|
|
| 486 |
results = []
|
| 487 |
prompt = "Explain recursion in programming in 3 sentences."
|
| 488 |
|
| 489 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 490 |
result = {"name": model_info["name"], "status": "error"}
|
| 491 |
try:
|
| 492 |
_log(f"Benchmark: downloading {model_info['name']} (~{model_info['size_gb']} GB)...")
|
|
@@ -500,12 +510,15 @@ def _run_benchmark():
|
|
| 500 |
env = os.environ.copy()
|
| 501 |
bin_dir = os.path.dirname(llama_cli)
|
| 502 |
env["LD_LIBRARY_PATH"] = bin_dir + ":" + env.get("LD_LIBRARY_PATH", "")
|
|
|
|
|
|
|
|
|
|
| 503 |
cmd = [
|
| 504 |
llama_cli,
|
| 505 |
"-m", model_path,
|
| 506 |
"-t", str(n_threads),
|
| 507 |
"-c", "2048",
|
| 508 |
-
"-n",
|
| 509 |
"--temp", "0.7",
|
| 510 |
"--top-p", "0.9",
|
| 511 |
"-p", prompt,
|
|
@@ -518,7 +531,7 @@ def _run_benchmark():
|
|
| 518 |
t0 = time.time()
|
| 519 |
try:
|
| 520 |
proc = subprocess.run(
|
| 521 |
-
cmd, capture_output=True, text=True, timeout=
|
| 522 |
)
|
| 523 |
except subprocess.TimeoutExpired:
|
| 524 |
result["error"] = "Timeout after 300s"
|
|
@@ -585,7 +598,8 @@ def _run_benchmark():
|
|
| 585 |
except Exception:
|
| 586 |
pass
|
| 587 |
|
| 588 |
-
|
|
|
|
| 589 |
return results
|
| 590 |
|
| 591 |
|
|
@@ -613,13 +627,14 @@ def _patched_create_app(blocks, **kwargs):
|
|
| 613 |
return JSONResponse({"error": str(e)}, status_code=502)
|
| 614 |
|
| 615 |
@fa_app.get("/respite/benchmark")
|
| 616 |
-
def _benchmark(force: bool = False):
|
| 617 |
-
"""Run CPU LLM benchmark.
|
| 618 |
try:
|
| 619 |
global _BENCHMARK_RESULTS
|
| 620 |
if force:
|
| 621 |
_BENCHMARK_RESULTS = []
|
| 622 |
-
|
|
|
|
| 623 |
return JSONResponse({
|
| 624 |
"status": "complete",
|
| 625 |
"models": results,
|
|
|
|
| 471 |
return llama_cli
|
| 472 |
|
| 473 |
|
| 474 |
+
def _run_benchmark(model_filter=None):
|
| 475 |
+
"""Run CPU benchmark on models using llama.cpp binary.
|
| 476 |
+
|
| 477 |
+
Args:
|
| 478 |
+
model_filter: If provided, only run this model name (e.g. 'Gemma 4 E4B').
|
| 479 |
+
"""
|
| 480 |
global _BENCHMARK_RESULTS
|
| 481 |
+
if _BENCHMARK_RESULTS and not model_filter:
|
| 482 |
return _BENCHMARK_RESULTS
|
| 483 |
|
| 484 |
import subprocess
|
|
|
|
| 490 |
results = []
|
| 491 |
prompt = "Explain recursion in programming in 3 sentences."
|
| 492 |
|
| 493 |
+
models = _BENCHMARK_MODELS
|
| 494 |
+
if model_filter:
|
| 495 |
+
models = [m for m in _BENCHMARK_MODELS if m["name"] == model_filter]
|
| 496 |
+
if not models:
|
| 497 |
+
raise ValueError(f"Unknown model '{model_filter}'. Available: {[m['name'] for m in _BENCHMARK_MODELS]}")
|
| 498 |
+
|
| 499 |
+
for model_info in models:
|
| 500 |
result = {"name": model_info["name"], "status": "error"}
|
| 501 |
try:
|
| 502 |
_log(f"Benchmark: downloading {model_info['name']} (~{model_info['size_gb']} GB)...")
|
|
|
|
| 510 |
env = os.environ.copy()
|
| 511 |
bin_dir = os.path.dirname(llama_cli)
|
| 512 |
env["LD_LIBRARY_PATH"] = bin_dir + ":" + env.get("LD_LIBRARY_PATH", "")
|
| 513 |
+
# 27B model needs more tokens limit and longer timeout
|
| 514 |
+
n_predict = 128 if model_info["size_gb"] <= 6 else 256
|
| 515 |
+
timeout_sec = 300 if model_info["size_gb"] <= 6 else 600
|
| 516 |
cmd = [
|
| 517 |
llama_cli,
|
| 518 |
"-m", model_path,
|
| 519 |
"-t", str(n_threads),
|
| 520 |
"-c", "2048",
|
| 521 |
+
"-n", str(n_predict),
|
| 522 |
"--temp", "0.7",
|
| 523 |
"--top-p", "0.9",
|
| 524 |
"-p", prompt,
|
|
|
|
| 531 |
t0 = time.time()
|
| 532 |
try:
|
| 533 |
proc = subprocess.run(
|
| 534 |
+
cmd, capture_output=True, text=True, timeout=timeout_sec, env=env,
|
| 535 |
)
|
| 536 |
except subprocess.TimeoutExpired:
|
| 537 |
result["error"] = "Timeout after 300s"
|
|
|
|
| 598 |
except Exception:
|
| 599 |
pass
|
| 600 |
|
| 601 |
+
if not model_filter:
|
| 602 |
+
_BENCHMARK_RESULTS = results
|
| 603 |
return results
|
| 604 |
|
| 605 |
|
|
|
|
| 627 |
return JSONResponse({"error": str(e)}, status_code=502)
|
| 628 |
|
| 629 |
@fa_app.get("/respite/benchmark")
|
| 630 |
+
def _benchmark(force: bool = False, model: str = ""):
|
| 631 |
+
"""Run CPU LLM benchmark. Pass ?model=Gemma 4 E4B to run a single model."""
|
| 632 |
try:
|
| 633 |
global _BENCHMARK_RESULTS
|
| 634 |
if force:
|
| 635 |
_BENCHMARK_RESULTS = []
|
| 636 |
+
model_filter = model.strip() if model.strip() else None
|
| 637 |
+
results = _run_benchmark(model_filter=model_filter)
|
| 638 |
return JSONResponse({
|
| 639 |
"status": "complete",
|
| 640 |
"models": results,
|