LoveLogicAI commited on
Commit
5962671
·
verified ·
1 Parent(s): ecf2001

FREESTREAM_ORT_THREADS knob (ORT intra-op cap)

Browse files
Files changed (1) hide show
  1. freestream/persona.py +32 -0
freestream/persona.py CHANGED
@@ -53,6 +53,37 @@ def _voice_cache() -> dict:
53
  return _VOICE_CACHE
54
 
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  class VoiceBackend:
57
  """Voice driver: Piper (CPU, real-time) default; OpenVoice/XTTS for cloning.
58
 
@@ -149,6 +180,7 @@ class VoiceBackend:
149
 
150
  cache = _voice_cache()
151
  if str(model_path) not in cache:
 
152
  cache[str(model_path)] = PiperVoice.load(str(model_path))
153
  voice = cache[str(model_path)]
154
 
 
53
  return _VOICE_CACHE
54
 
55
 
56
+ def _tune_ort():
57
+ """Cap ONNX Runtime's intra-op threads (experiment knob, docs/LATENCY.md).
58
+
59
+ ORT defaults its pool to the *visible* core count (64 here) while the
60
+ cgroup quota is 8 — during synthesis those bursts can exhaust the quota.
61
+ Set FREESTREAM_ORT_THREADS=<n> to pin the pool; unset leaves ORT default.
62
+ """
63
+ import os
64
+ n = os.environ.get("FREESTREAM_ORT_THREADS")
65
+ if not n:
66
+ return
67
+ try:
68
+ threads = max(1, int(n))
69
+ import onnxruntime as ort
70
+ if getattr(ort, "_freestream_patched", False):
71
+ return
72
+ orig = ort.InferenceSession
73
+
74
+ def patched(path, **kw):
75
+ so = ort.SessionOptions()
76
+ so.intra_op_num_threads = threads
77
+ so.inter_op_num_threads = 1
78
+ kw["sess_options"] = kw.get("sess_options", so)
79
+ return orig(path, **kw)
80
+
81
+ ort.InferenceSession = patched
82
+ ort._freestream_patched = True
83
+ except Exception:
84
+ pass
85
+
86
+
87
  class VoiceBackend:
88
  """Voice driver: Piper (CPU, real-time) default; OpenVoice/XTTS for cloning.
89
 
 
180
 
181
  cache = _voice_cache()
182
  if str(model_path) not in cache:
183
+ _tune_ort() # cap ORT threads to the cgroup quota (experiment knob)
184
  cache[str(model_path)] = PiperVoice.load(str(model_path))
185
  voice = cache[str(model_path)]
186