168 lines
5.9 KiB
Python
Executable File
168 lines
5.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Benchmark two qwen3.8:27b requests alongside strict Vulkan Whisper."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
import wave
|
|
from collections.abc import Callable
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[1]
|
|
if str(REPO_ROOT) not in sys.path:
|
|
sys.path.insert(0, str(REPO_ROOT))
|
|
|
|
from src.meeting_lab.benchmarking.concurrency import run_load_case
|
|
from src.meeting_lab.llm.ollama import generate_once, require_model
|
|
from src.meeting_lab.protocol.generate_direct_protocol import estimate_input_tokens
|
|
from src.meeting_lab.transcription.backend import file_sha256
|
|
from src.meeting_lab.transcription.whisper import transcribe_audio
|
|
|
|
MODEL = "qwen3.8:27b"
|
|
CONTEXT = 32768
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument(
|
|
"audio", type=Path, help="Fresh canonical WAV for a realistic Whisper workload"
|
|
)
|
|
parser.add_argument(
|
|
"--prompt-file",
|
|
type=Path,
|
|
required=True,
|
|
help="Complete realistic protocol prompt",
|
|
)
|
|
parser.add_argument(
|
|
"--second-prompt-file", type=Path, help="Defaults to reusing the first prompt"
|
|
)
|
|
parser.add_argument("--whisper-model", type=Path, required=True)
|
|
parser.add_argument("--whisper-executable", required=True)
|
|
parser.add_argument("--output-dir", type=Path, required=True)
|
|
parser.add_argument("--endpoint", default="http://127.0.0.1:11434")
|
|
parser.add_argument("--language", default="de")
|
|
parser.add_argument("--threads", default="auto")
|
|
parser.add_argument("--timeout", type=int, default=3600)
|
|
args = parser.parse_args()
|
|
paths = [args.prompt_file, args.second_prompt_file or args.prompt_file]
|
|
prompts = [path.read_text(encoding="utf-8") for path in paths]
|
|
if any(
|
|
not prompt.strip() or estimate_input_tokens(prompt) > 29000
|
|
for prompt in prompts
|
|
):
|
|
parser.error(
|
|
"Prompts must be non-empty and within the 29,000 estimated input-token budget"
|
|
)
|
|
with wave.open(str(args.audio), "rb") as audio:
|
|
if (audio.getnchannels(), audio.getframerate(), audio.getsampwidth()) != (
|
|
1,
|
|
16000,
|
|
2,
|
|
):
|
|
parser.error("Audio must be canonical mono 16 kHz PCM16 WAV")
|
|
duration = audio.getnframes() / audio.getframerate()
|
|
args.output_dir.mkdir(parents=True, exist_ok=False)
|
|
report = {
|
|
"model": MODEL,
|
|
"num_ctx": CONTEXT,
|
|
"llm_request_count_planned": 6,
|
|
"whisper_request_count_planned": 2,
|
|
"audio_duration_seconds": duration,
|
|
"audio_sha256": file_sha256(args.audio),
|
|
"whisper_model_sha256": file_sha256(args.whisper_model),
|
|
"prompts": [
|
|
{
|
|
"sha256": file_sha256(path),
|
|
"estimated_tokens": estimate_input_tokens(prompt),
|
|
}
|
|
for path, prompt in zip(paths, prompts, strict=True)
|
|
],
|
|
"server_policy_expected": {
|
|
"OLLAMA_MAX_LOADED_MODELS": 1,
|
|
"OLLAMA_NUM_PARALLEL": 2,
|
|
},
|
|
"cases": {},
|
|
}
|
|
result_file = args.output_dir / "concurrency.json"
|
|
|
|
def save() -> None:
|
|
result_file.write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8")
|
|
|
|
save()
|
|
try:
|
|
require_model(args.endpoint, MODEL)
|
|
except Exception as exc: # noqa: BLE001
|
|
report["blocked"] = str(exc)
|
|
save()
|
|
return 1
|
|
|
|
def jobs(
|
|
case: str, include_whisper: bool
|
|
) -> dict[str, Callable[[], dict[str, Any]]]:
|
|
folder = args.output_dir / case
|
|
folder.mkdir()
|
|
|
|
def llm(index: int) -> dict[str, Any]:
|
|
generated = generate_once(
|
|
args.endpoint,
|
|
MODEL,
|
|
prompts[index],
|
|
timeout=args.timeout,
|
|
num_ctx=CONTEXT,
|
|
num_predict=8192,
|
|
)
|
|
(folder / f"ollama-{index + 1}.json").write_text(
|
|
json.dumps(generated.raw_response, indent=2) + "\n"
|
|
)
|
|
return {
|
|
"runtime_seconds": generated.client_wall_time_seconds,
|
|
"model": MODEL,
|
|
"num_ctx": CONTEXT,
|
|
"prompt_eval_count": generated.raw_response.get("prompt_eval_count"),
|
|
"eval_count": generated.raw_response.get("eval_count"),
|
|
"total_duration_ns": generated.raw_response.get("total_duration"),
|
|
"load_duration_ns": generated.raw_response.get("load_duration"),
|
|
"prompt_eval_duration_ns": generated.raw_response.get("prompt_eval_duration"),
|
|
"eval_duration_ns": generated.raw_response.get("eval_duration"),
|
|
}
|
|
|
|
calls = {"ollama-1": lambda: llm(0), "ollama-2": lambda: llm(1)}
|
|
if include_whisper:
|
|
|
|
def whisper() -> dict[str, Any]:
|
|
result = transcribe_audio(
|
|
args.audio,
|
|
args.whisper_model,
|
|
folder / "whisper",
|
|
args.language,
|
|
executable=args.whisper_executable,
|
|
threads=args.threads,
|
|
backend="vulkan",
|
|
)
|
|
return json.loads(result.runtime_metadata.read_text())
|
|
|
|
calls["whisper"] = whisper
|
|
return calls
|
|
|
|
for case, parallel, include_whisper in [
|
|
("serial_baseline", False, True),
|
|
("ollama_parallel", True, False),
|
|
("ollama_and_whisper", True, True),
|
|
]:
|
|
report["cases"][case] = run_load_case(
|
|
jobs(case, include_whisper), parallel=parallel, endpoint=args.endpoint
|
|
)
|
|
save()
|
|
if any(job["status"] == "failed" for job in report["cases"][case]["jobs"]):
|
|
print(f"Benchmark failed in {case}; diagnostics: {result_file}")
|
|
return 1
|
|
print(result_file)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|