freestream / tests /debug_perf.py
LoveLogicAI's picture
v0.1.0
e6ea215 verified
Raw History Blame Contribute Delete
2.62 kB
"""Microbenchmark: is this container CPU-throttled? Whisper tiny + piper RTFs
in isolation on the e2e flavor, plus piper's ORT session options."""
import json
import os
import time
import urllib.request
from pathlib import Path
ROOT = Path("/tmp/dbgperf")
VOICE_DIR = ROOT / "voices"
def fetch(url, dest):
dest.parent.mkdir(parents=True, exist_ok=True)
with urllib.request.urlopen(url, timeout=120) as r:
dest.write_bytes(r.read())
print("os.cpu_count():", os.cpu_count(), flush=True)
try:
print("cgroup cpu.max:", open("/sys/fs/cgroup/cpu.max").read().strip(), flush=True)
except OSError:
print("no cgroup v2 cpu.max", flush=True)
for name in ("en/en_US/amy/medium/en_US-amy-medium.onnx",
"en/en_US/amy/medium/en_US-amy-medium.onnx.json"):
fetch(f"https://huggingface.co/rhasspy/piper-voices/resolve/main/{name}",
VOICE_DIR / Path(name).name)
from piper import PiperVoice # noqa: E402
import inspect # noqa: E402
print("PiperVoice.load signature:", inspect.signature(PiperVoice.load), flush=True)
try:
src = inspect.getsource(PiperVoice.load)
print("--- PiperVoice.load source ---")
print(src[:2000], flush=True)
except Exception as e:
print("no source:", e, flush=True)
voice = PiperVoice.load(str(VOICE_DIR / "en_US-amy-medium.onnx"))
SENT = "Hi everyone, I'm Streamy, an AI assistant."
for run in range(3):
t0 = time.perf_counter()
chunks = list(voice.synthesize(SENT))
n = sum(len(c.audio_int16_bytes) for c in chunks)
dt = time.perf_counter() - t0
print(f"piper synth {run}: {dt*1000:.0f}ms for {n/2/22050:.2f}s audio "
f"(RTF {dt / (n/2/22050):.2f})", flush=True)
from piper.voice import AudioChunk # noqa: E402
import numpy as np # noqa: E402
pcm = np.concatenate([np.frombuffer(c.audio_int16_bytes, dtype=np.int16)
for c in chunks]).astype(np.float32) / 32768.0
np.save("/tmp/dbg_perf_speech.npy", pcm.astype(np.float32))
from faster_whisper import WhisperModel # noqa: E402
for threads in (1, 2, 4, 8):
model = WhisperModel("tiny", device="cpu", compute_type="int8", cpu_threads=threads)
for rep in range(2):
t0 = time.perf_counter()
segs, _ = model.transcribe(pcm, language="en", beam_size=1,
condition_on_previous_text=False)
text = " ".join(s.text.strip() for s in segs).strip()
dt = time.perf_counter() - t0
print(f"whisper tiny threads={threads} rep={rep}: {dt*1000:.0f}ms "
f"(RTF {dt / (len(pcm)/16000):.2f}) '{text[:40]}'", flush=True)
print("[dbg] done", flush=True)