LoveLogicAI commited on
Commit
e6ea215
·
verified ·
1 Parent(s): bb18e70
Files changed (1) hide show
  1. tests/debug_perf.py +72 -0
tests/debug_perf.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Microbenchmark: is this container CPU-throttled? Whisper tiny + piper RTFs
2
+ in isolation on the e2e flavor, plus piper's ORT session options."""
3
+
4
+ import json
5
+ import os
6
+ import time
7
+ import urllib.request
8
+ from pathlib import Path
9
+
10
+ ROOT = Path("/tmp/dbgperf")
11
+ VOICE_DIR = ROOT / "voices"
12
+
13
+
14
+ def fetch(url, dest):
15
+ dest.parent.mkdir(parents=True, exist_ok=True)
16
+ with urllib.request.urlopen(url, timeout=120) as r:
17
+ dest.write_bytes(r.read())
18
+
19
+
20
+ print("os.cpu_count():", os.cpu_count(), flush=True)
21
+ try:
22
+ print("cgroup cpu.max:", open("/sys/fs/cgroup/cpu.max").read().strip(), flush=True)
23
+ except OSError:
24
+ print("no cgroup v2 cpu.max", flush=True)
25
+
26
+ for name in ("en/en_US/amy/medium/en_US-amy-medium.onnx",
27
+ "en/en_US/amy/medium/en_US-amy-medium.onnx.json"):
28
+ fetch(f"https://huggingface.co/rhasspy/piper-voices/resolve/main/{name}",
29
+ VOICE_DIR / Path(name).name)
30
+
31
+ from piper import PiperVoice # noqa: E402
32
+ import inspect # noqa: E402
33
+
34
+ print("PiperVoice.load signature:", inspect.signature(PiperVoice.load), flush=True)
35
+ try:
36
+ src = inspect.getsource(PiperVoice.load)
37
+ print("--- PiperVoice.load source ---")
38
+ print(src[:2000], flush=True)
39
+ except Exception as e:
40
+ print("no source:", e, flush=True)
41
+
42
+ voice = PiperVoice.load(str(VOICE_DIR / "en_US-amy-medium.onnx"))
43
+ SENT = "Hi everyone, I'm Streamy, an AI assistant."
44
+ for run in range(3):
45
+ t0 = time.perf_counter()
46
+ chunks = list(voice.synthesize(SENT))
47
+ n = sum(len(c.audio_int16_bytes) for c in chunks)
48
+ dt = time.perf_counter() - t0
49
+ print(f"piper synth {run}: {dt*1000:.0f}ms for {n/2/22050:.2f}s audio "
50
+ f"(RTF {dt / (n/2/22050):.2f})", flush=True)
51
+
52
+ from piper.voice import AudioChunk # noqa: E402
53
+ import numpy as np # noqa: E402
54
+
55
+ pcm = np.concatenate([np.frombuffer(c.audio_int16_bytes, dtype=np.int16)
56
+ for c in chunks]).astype(np.float32) / 32768.0
57
+ np.save("/tmp/dbg_perf_speech.npy", pcm.astype(np.float32))
58
+
59
+ from faster_whisper import WhisperModel # noqa: E402
60
+
61
+ for threads in (1, 2, 4, 8):
62
+ model = WhisperModel("tiny", device="cpu", compute_type="int8", cpu_threads=threads)
63
+ for rep in range(2):
64
+ t0 = time.perf_counter()
65
+ segs, _ = model.transcribe(pcm, language="en", beam_size=1,
66
+ condition_on_previous_text=False)
67
+ text = " ".join(s.text.strip() for s in segs).strip()
68
+ dt = time.perf_counter() - t0
69
+ print(f"whisper tiny threads={threads} rep={rep}: {dt*1000:.0f}ms "
70
+ f"(RTF {dt / (len(pcm)/16000):.2f}) '{text[:40]}'", flush=True)
71
+
72
+ print("[dbg] done", flush=True)