forked from hexgrad/kokoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_examples.py
More file actions
45 lines (39 loc) · 1.6 KB
/
device_examples.py
File metadata and controls
45 lines (39 loc) · 1.6 KB
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
"""
Quick example to show how device selection can be controlled, and was checked
"""
import time
from kokoro import KPipeline
from loguru import logger
def generate_audio(pipeline, text):
for _, _, audio in pipeline(text, voice='af_bella'):
samples = audio.shape[0] if audio is not None else 0
assert samples > 0, "No audio generated"
return samples
def time_synthesis(device=None):
try:
start = time.perf_counter()
pipeline = KPipeline(lang_code='a', device=device)
samples = generate_audio(pipeline, "The quick brown fox jumps over the lazy dog.")
ms = (time.perf_counter() - start) * 1000
logger.info(f"✓ {device or 'auto':<6} | {ms:>5.1f}ms total | {samples:>6,d} samples")
except RuntimeError as e:
logger.error(f"✗ {'cuda' if 'CUDA' in str(e) else device or 'auto':<6} | {'not available' if 'CUDA' in str(e) else str(e)}")
def compare_shared_model():
try:
start = time.perf_counter()
en_us = KPipeline(lang_code='a')
en_uk = KPipeline(lang_code='a', model=en_us.model)
for pipeline in [en_us, en_uk]:
generate_audio(pipeline, "Testing model reuse.")
ms = (time.perf_counter() - start) * 1000
logger.info(f"✓ reuse | {ms:>5.1f}ms for both models")
except Exception as e:
logger.error(f"✗ reuse | {str(e)}")
if __name__ == '__main__':
logger.info("Device Selection & Performance")
logger.info("-" * 40)
time_synthesis()
time_synthesis('cuda')
time_synthesis('cpu')
logger.info("-" * 40)
compare_shared_model()