Faster replies: TTS on the GPU, LLM prompt cache kept warm

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 20:11:04 +09:00
co-authored by Claude Opus 5.5
parent fe8319bc95
commit 334b5f5b2b
16 changed files with 1180 additions and 83 deletions
+15 -6
View File
@@ -1,4 +1,4 @@
"""Speaking: text -> speech (Supertonic-3, English and Korean, on the robot) -> USB speaker.
"""Speaking: text -> speech (Supertonic-3, English and Korean, on the robot's GPU: audio/tts_gpu.py) -> USB speaker.
python audio/mouth.py "Hello, I am Lumi."
python audio/mouth.py "안녕하세요, 저는 루미예요."
@@ -27,8 +27,9 @@ VOICE = "F1" # F1-F5 female, M1-M5 male
STEPS = 4 # quality/speed: 4 fast ... 8 best
SPEED = 0.92 # < 1 = slower; 1.05 sounded rushed
SPEED_MIN, SPEED_MAX = 0.72, 1.2 # what "speak slower / faster" may set (Supertonic takes 0.7 to 2.0)
SPLIT_WORDS = 8 # a first sentence this long is said in two parts, cut after its first
# comma: the first part is synthesised in ~0.7 s instead of ~1.8 s
SPLIT_WORDS = 8 # TTS on the CPU only: a first sentence this long is said in two parts,
# cut after its first comma (first part ~0.7 s instead of ~1.8 s); on
# the GPU a whole sentence takes 0.07-0.2 s and is said in one piece
VOLUME = 55 # % of the USB speaker mixer (100 % disturbed people in the office)
CACHED = ["Hi, I'm Lumi, the assistant robot. How can I help you?", "안녕하세요, 저는 루미예요. 무엇을 도와드릴까요?",
"Yes?", "네?", "Okay, I'm coming.", "네, 갈게요.", "I'm right here.",
@@ -41,6 +42,7 @@ CACHED = ["Hi, I'm Lumi, the assistant robot. How can I help you?", "안녕하
_tts = None
_style = None
_gpu = False # synthesis on the GPU (audio/tts_gpu.py engines loaded)
_cache = {}
_volume_set = False
_recent = [] # (time, sentence) Lumi said lately: to tell its own echo from a visitor
@@ -95,13 +97,19 @@ def _set_volume():
def _load():
global _tts, _style
global _tts, _style, _gpu
_set_volume()
if _tts is None:
from supertonic import TTS
from audio import tts_gpu
_tts = TTS(model="supertonic-3", model_dir=ROOT / "models/supertonic3", auto_download=False,
intra_op_num_threads=8)
_style = _tts.get_voice_style(VOICE)
try:
tts_gpu.use(_tts.model)
_gpu = True
except Exception as e: # no engine file, no GPU, no TensorRT: the CPU, 5-10x slower
print(f"mouth: TTS on the CPU ({e})", flush=True)
return _tts
@@ -115,9 +123,10 @@ def sentences(text):
def first_parts(sentence):
"""A long first sentence cut after its first comma (3 words in or later), so the voice starts sooner."""
"""TTS on the CPU: a long first sentence cut after its first comma (3 words in or later), so the voice starts
sooner."""
m = re.search(r"[,;]\s", sentence)
if len(sentence.split()) < SPLIT_WORDS or not m or len(sentence[:m.start()].split()) < 3:
if _gpu or len(sentence.split()) < SPLIT_WORDS or not m or len(sentence[:m.start()].split()) < 3:
return [sentence]
return [sentence[:m.start() + 1], sentence[m.end():]]
+157
View File
@@ -0,0 +1,157 @@
"""Supertonic's vector estimator and vocoder (nearly all of the TTS time) on the GPU through TensorRT.
.venv/bin/python audio/tts_gpu.py build # once on this Thor, after a JetPack / TensorRT change (~90 s)
.venv/bin/python audio/tts_gpu.py check # GPU against CPU: time and difference on a few sentences
On the CPU the vector estimator takes 0.1-0.27 s per step (4 steps a sentence) and the vocoder 0.06-0.17 s. mouth.py
puts Engine in place of the onnxruntime sessions when the engine files load, else the CPU stays (it says why).
The engines are bound to this Thor's TensorRT and GPU, like models/yolov8s.engine.
"""
import pathlib
import sys
import threading
import time
import numpy as np
ROOT = pathlib.Path(__file__).resolve().parents[1] # the project folder
MODELS = {"vector_estimator": "vector_est_ort", "vocoder": "vocoder_ort"} # file -> attribute in supertonic's model;
# the text encoder and durations take ~20 ms, they stay on the CPU
MAX_LEN = 640 # text / latent frames the engine takes (latent ~14.5 frames per s of speech:
# 44 s; a TTS chunk is at most 300 characters). Longer: the CPU session
def onnx(name):
return ROOT / f"models/supertonic3/onnx/{name}.onnx"
def engine_file(name):
return ROOT / f"models/supertonic3_{name}.engine"
def build(name):
import tensorrt as trt
logger = trt.Logger(trt.Logger.WARNING)
builder = trt.Builder(logger)
net = builder.create_network(0)
parser = trt.OnnxParser(net, logger)
if not parser.parse_from_file(str(onnx(name))):
raise SystemExit("\n".join(str(parser.get_error(i)) for i in range(parser.num_errors)))
cfg = builder.create_builder_config()
cfg.set_flag(trt.BuilderFlag.TF32)
prof = builder.create_optimization_profile()
for i in range(net.num_inputs): # batch 1; text_length / latent_length 1 ... MAX_LEN
t = net.get_input(i)
dims = [list(t.shape) for _ in range(3)]
for j, d in enumerate(t.shape):
if d == -1:
dims[0][j], dims[1][j], dims[2][j] = (1, 1, 1) if j == 0 else (1, 80, MAX_LEN)
prof.set_shape(t.name, *dims)
cfg.add_optimization_profile(prof)
engine = builder.build_serialized_network(net, cfg)
if engine is None:
raise SystemExit("engine not built")
engine_file(name).write_bytes(engine)
print("wrote", engine_file(name))
class Engine:
"""Drop-in for the onnxruntime session: run(None, feed) -> [output]. Inputs longer than MAX_LEN go to `cpu`."""
def __init__(self, name, cpu):
import tensorrt as trt
from cuda.bindings import runtime as cr
self.cr, self.cpu = cr, cpu
self.engine = trt.Runtime(trt.Logger(trt.Logger.ERROR)).deserialize_cuda_engine(engine_file(name).read_bytes())
if self.engine is None:
raise RuntimeError(f"cannot load {engine_file(name)}")
self.ctx = self.engine.create_execution_context()
self.names = [self.engine.get_tensor_name(i) for i in range(self.engine.num_io_tensors)]
self.out = next(n for n in self.names if self.engine.get_tensor_mode(n) == trt.TensorIOMode.OUTPUT)
self.stream = self._ok(cr.cudaStreamCreate())
self.dev = {} # name -> (device pointer, bytes), grown when needed
self.lock = threading.Lock() # one run at a time (a skill's result is spoken from another thread)
def _ok(self, r):
err, *val = r if isinstance(r, tuple) else (r,)
if err != self.cr.cudaError_t.cudaSuccess:
raise RuntimeError(f"CUDA error {err}")
return val[0] if val else None
def _buf(self, name, nbytes):
ptr, size = self.dev.get(name, (None, 0))
if size < nbytes:
if ptr is not None:
self._ok(self.cr.cudaFree(ptr))
ptr, size = self._ok(self.cr.cudaMalloc(nbytes)), nbytes
self.dev[name] = (ptr, size)
self.ctx.set_tensor_address(name, int(ptr))
return ptr
def run(self, _outputs, feed):
if any(x.shape[0] != 1 or x.shape[-1] > MAX_LEN for x in feed.values()): # the lengths are the last axis
return self.cpu.run(_outputs, feed)
with self.lock:
return self._run(feed)
def _run(self, feed):
cr = self.cr
h2d, d2h = cr.cudaMemcpyKind.cudaMemcpyHostToDevice, cr.cudaMemcpyKind.cudaMemcpyDeviceToHost
keep = [] # host arrays stay alive until the copies are done
for name, x in feed.items():
x = np.ascontiguousarray(x, np.float32)
keep.append(x)
self.ctx.set_input_shape(name, x.shape)
self._ok(cr.cudaMemcpyAsync(self._buf(name, x.nbytes), x.ctypes.data, x.nbytes, h2d, self.stream))
out = np.empty(tuple(self.ctx.get_tensor_shape(self.out)), np.float32)
ptr = self._buf(self.out, out.nbytes)
if not self.ctx.execute_async_v3(int(self.stream)):
raise RuntimeError("TensorRT run failed")
self._ok(cr.cudaMemcpyAsync(out.ctypes.data, ptr, out.nbytes, d2h, self.stream))
self._ok(cr.cudaStreamSynchronize(self.stream))
return [out]
def use(model):
"""Put the GPU engines into a loaded supertonic model (TTS().model). Raises when an engine does not load."""
for name, attr in MODELS.items():
cpu = getattr(model, attr)
if not isinstance(cpu, Engine):
setattr(model, attr, Engine(name, cpu))
def check():
"""Each sentence on the CPU and on the GPU (same noise): synthesis time and the difference in the sound."""
from audio import mouth
mouth._set_volume = lambda: None # nothing is played or changed
tts = mouth._load()
m = tts.model
use(m) # mouth.py has done it when the engines load
gpu = {a: getattr(m, a) for a in MODELS.values()}
cpu = {a: e.cpu for a, e in gpu.items()}
for s in ["Lumi.", "I was developed by Cutshion Company.", "고마워요! 그렇게 말씀해 주시니 기쁘네요.",
"I can talk with you in English and Korean, answer questions about Cutshion, and guide you around."]:
wavs = []
for sessions in (cpu, gpu):
for a, sess in sessions.items():
setattr(m, a, sess)
np.random.seed(0)
t = time.time()
wav, _ = tts.synthesize(s, voice_style=mouth._style, lang=mouth.lang_of(s), total_steps=mouth.STEPS,
speed=mouth.speed())
wavs.append((time.time() - t, wav.reshape(-1)))
(tc, a), (tg, b) = wavs
n = min(len(a), len(b))
print(f"cpu {tc:4.2f} s gpu {tg:4.2f} s max diff {np.abs(a[:n] - b[:n]).max():.4f} "
f"rms diff / rms {np.sqrt(np.mean((a[:n] - b[:n]) ** 2)) / np.sqrt(np.mean(a[:n] ** 2)):.4f} {s[:40]}")
if __name__ == "__main__":
if sys.argv[1:] == ["build"]:
for n in MODELS:
build(n)
elif sys.argv[1:] == ["check"]:
from audio import tts_gpu # the module mouth.py loads, not __main__ (another Engine class)
tts_gpu.check()
else:
sys.exit(__doc__)