96 lines
3.4 KiB
Python
Executable File
96 lines
3.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Compare CPU/Vulkan on one canonical WAV without generating a protocol."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
import wave
|
|
from pathlib import Path
|
|
|
|
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.transcription.backend import file_sha256
|
|
from src.meeting_lab.transcription.whisper import (
|
|
TranscriptionError,
|
|
transcribe_audio,
|
|
)
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("audio", type=Path)
|
|
parser.add_argument("--model", type=Path, required=True)
|
|
parser.add_argument("--cpu-executable", required=True)
|
|
parser.add_argument("--vulkan-executable", required=True)
|
|
parser.add_argument("--output-dir", type=Path, required=True)
|
|
parser.add_argument("--language", default="en")
|
|
parser.add_argument("--threads", default="auto")
|
|
parser.add_argument("--repeats", type=int, default=3)
|
|
args = parser.parse_args()
|
|
if args.repeats < 1:
|
|
parser.error("--repeats must be positive")
|
|
with wave.open(str(args.audio), "rb") as source:
|
|
if (source.getnchannels(), source.getframerate(), source.getsampwidth()) != (
|
|
1,
|
|
16000,
|
|
2,
|
|
):
|
|
parser.error("Input must be mono 16 kHz PCM16 WAV")
|
|
duration = source.getnframes() / source.getframerate()
|
|
args.output_dir.mkdir(parents=True, exist_ok=False)
|
|
report = {
|
|
"audio_sha256": file_sha256(args.audio),
|
|
"model_sha256": file_sha256(args.model),
|
|
"audio_duration_seconds": duration,
|
|
"language": args.language,
|
|
"threads": args.threads,
|
|
"runs": [],
|
|
"errors": [],
|
|
"notes": "First run per backend is cold-start candidate; later runs are warm candidates.",
|
|
}
|
|
for mode, executable in [
|
|
("cpu", args.cpu_executable),
|
|
("vulkan", args.vulkan_executable),
|
|
]:
|
|
for index in range(args.repeats):
|
|
output = args.output_dir / f"{mode}-{index + 1}"
|
|
try:
|
|
result = transcribe_audio(
|
|
args.audio,
|
|
args.model,
|
|
output,
|
|
args.language,
|
|
executable=executable,
|
|
threads=args.threads,
|
|
backend=mode,
|
|
)
|
|
metadata = json.loads(result.runtime_metadata.read_text())
|
|
report["runs"].append(
|
|
{
|
|
"backend": mode,
|
|
"iteration": index + 1,
|
|
"runtime_seconds": result.runtime_seconds,
|
|
"rtf": result.runtime_seconds / duration,
|
|
"device_name": metadata["device_name"],
|
|
"actual_backend": metadata["actual_backend"],
|
|
"output": str(output.resolve()),
|
|
}
|
|
)
|
|
except TranscriptionError as exc:
|
|
report["errors"].append({"backend": mode, "error": str(exc)})
|
|
break
|
|
finally:
|
|
(args.output_dir / "benchmark.json").write_text(
|
|
json.dumps(report, indent=2) + "\n"
|
|
)
|
|
print(args.output_dir / "benchmark.json")
|
|
return 1 if report["errors"] else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|