File size: 2,621 Bytes
e6ea215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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)