Free, open-source real-time voice changer for Windows: 20 live voices, local AI voice conversion (Seed-VC on ONNX Runtime), soundboard, virtual mic, hotkeys.
ONNX backend: one session Run at a time
sky01fbd29d9b2e
diff --git a/tests/test_onnx_backend.py b/tests/test_onnx_backend.pynew file mode 100644index 0000000..5ca9eda--- /dev/null+++ b/tests/test_onnx_backend.py@@ -0,0 +1,43 @@+import threading+import time++import numpy as np++from voxshift.ai.onnx_backend import OnnxSeedVCModel+++class FakeSession:+ """Records how many threads are inside run() at once."""++ def __init__(self):+ self.inside = self.peak = 0+ self.guard = threading.Lock()++ def run(self, outputs, feeds):+ with self.guard:+ self.inside += 1+ self.peak = max(self.peak, self.inside)+ time.sleep(0.01)+ with self.guard:+ self.inside -= 1+ return [feeds["x"]]+++def test_runs_are_serialised_across_threads():+ # several processors share one model (e.g. a voice reloading while the old one still+ # converts); DirectML must never see two threads in Run at once+ sess = FakeSession()+ m = OnnxSeedVCModel.__new__(OnnxSeedVCModel)+ m.sess = {"dit_step": sess}+ m._run_lock = threading.Lock()++ def work():+ for _ in range(5):+ m._run("dit_step", x=np.zeros(4))++ ts = [threading.Thread(target=work) for _ in range(4)]+ for t in ts:+ t.start()+ for t in ts:+ t.join()+ assert sess.peak == 1diff --git a/voxshift/ai/onnx_backend.py b/voxshift/ai/onnx_backend.pyindex 524957c..84fb995 100644--- a/voxshift/ai/onnx_backend.py+++ b/voxshift/ai/onnx_backend.py@@ -8,6 +8,7 @@ standalone build needs no PyTorch.from __future__ import annotationsimport json+import threadingfrom pathlib import Pathimport numpy as np@@ -58,11 +59,16 @@ class OnnxSeedVCModel:self.whisper_filters, self.mel_basis, self.kaldi_banks = (c["whisper_mel_filters"], c["mel_basis"], c["kaldi_mel_banks"])self.rng = np.random.default_rng()+ # The model is shared by every processor built from it, so a voice that is reloading+ # (steps / smoothing changed) warms up while the previous one still converts. DirectML+ # allows only one thread in Run per session; overlapping calls hang the GPU (TDR).+ self._run_lock = threading.Lock()def _run(self, name, **feeds):feeds = {k: np.ascontiguousarray(v, dtype=np.int64 if v.dtype.kind in "iu" else np.float32)for k, v in feeds.items()}- return self.sess[name].run(None, feeds)[0]+ with self._run_lock:+ return self.sess[name].run(None, feeds)[0]# -- building blocks (all numpy) ------------------------------------def content(self, w16: np.ndarray) -> np.ndarray: