multimodalart HF Staff commited on
Commit
93b0f2e
Β·
verified Β·
1 Parent(s): 73e48b2

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +3 -25
  2. portable_tts_runtime.py +17 -3
app.py CHANGED
@@ -174,31 +174,9 @@ RUNNER = OptimizedTTSRunner(
174
  SAMPLE_RATE = int(RUNNER.sample_rate)
175
  print(f"Model loaded. sample_rate={SAMPLE_RATE}", flush=True)
176
 
177
- # Prewarm the model with a short dummy generation so the first user request
178
- # does not pay the torch.compile cost.
179
- print("Prewarming model …", flush=True)
180
- _prewarm_start = time.perf_counter()
181
- try:
182
- _pw = RUNNER.synthesize(
183
- text="The optimized demo is ready.",
184
- language="en",
185
- speaker_id=31,
186
- max_new_tokens=64,
187
- text_temperature=0.0,
188
- text_top_p=1.0,
189
- text_top_k=None,
190
- audio_temperature=0.8,
191
- audio_top_p=0.92,
192
- audio_top_k=None,
193
- audio_repetition_penalty=1.05,
194
- n_vq_for_inference=32,
195
- style_text="The optimized demo is ready.",
196
- style_emotion_id=0,
197
- style_energy=0.5,
198
- )
199
- print(f"Prewarm complete in {time.perf_counter() - _prewarm_start:.1f}s", flush=True)
200
- except Exception as exc:
201
- print(f"Prewarm failed ({exc!r}) β€” continuing anyway", flush=True)
202
 
203
 
204
  # ── Helpers ────────────────────────────────────────────────────────────────
 
174
  SAMPLE_RATE = int(RUNNER.sample_rate)
175
  print(f"Model loaded. sample_rate={SAMPLE_RATE}", flush=True)
176
 
177
+ # NOTE: prewarm is skipped at module scope on ZeroGPU β€” no real GPU is
178
+ # attached to the main process. The first @spaces.GPU call will pay the
179
+ # compile/warmup cost.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
 
182
  # ── Helpers ────────────────────────────────────────────────────────────────
portable_tts_runtime.py CHANGED
@@ -192,7 +192,10 @@ class TorchDecoder4FeatureRuntime:
192
  self.inputs = [SimpleNamespace(name="codes"), SimpleNamespace(name="lengths")]
193
  del audio_tokenizer
194
  gc.collect()
195
- torch.cuda.empty_cache()
 
 
 
196
 
197
  def get_inputs(self) -> list[Any]:
198
  return self.inputs
@@ -237,7 +240,13 @@ class TorchScriptVocoderRuntime:
237
  if not artifact.exists():
238
  raise FileNotFoundError(f"Missing TorchScript vocoder artifact: {artifact}")
239
  self.device = device
240
- self.module = torch.jit.load(str(artifact), map_location=device).eval()
 
 
 
 
 
 
241
  self.use_cudagraph = bool(use_cudagraph and device.type == "cuda")
242
  self.bucket_frames = max(0, int(bucket_frames))
243
  self.graphs: dict[tuple[Any, ...], tuple[torch.cuda.CUDAGraph, torch.Tensor, torch.Tensor]] = {}
@@ -503,7 +512,12 @@ class OptimizedTTSRunner:
503
  if isinstance(self.vocoder_session, TorchScriptVocoderRuntime):
504
  prewarm_buckets = parse_int_csv(config.vocoder_prewarm_buckets)
505
  if prewarm_buckets:
506
- self.vocoder_prewarm_result = self.vocoder_session.prewarm_buckets(prewarm_buckets)
 
 
 
 
 
507
  tts_load_kwargs: dict[str, Any] = {
508
  "trust_remote_code": True,
509
  "torch_dtype": self.dtype,
 
192
  self.inputs = [SimpleNamespace(name="codes"), SimpleNamespace(name="lengths")]
193
  del audio_tokenizer
194
  gc.collect()
195
+ try:
196
+ torch.cuda.empty_cache()
197
+ except Exception:
198
+ pass
199
 
200
  def get_inputs(self) -> list[Any]:
201
  return self.inputs
 
240
  if not artifact.exists():
241
  raise FileNotFoundError(f"Missing TorchScript vocoder artifact: {artifact}")
242
  self.device = device
243
+ # Load on CPU first, then move to device via .to() so ZeroGPU's
244
+ # torch.cuda hijack intercepts the move (torch.jit.load with
245
+ # map_location="cuda" bypasses the hijack and fails in the main
246
+ # process where no real GPU is attached).
247
+ self.module = torch.jit.load(str(artifact), map_location="cpu").eval()
248
+ if device.type == "cuda":
249
+ self.module = self.module.to(device)
250
  self.use_cudagraph = bool(use_cudagraph and device.type == "cuda")
251
  self.bucket_frames = max(0, int(bucket_frames))
252
  self.graphs: dict[tuple[Any, ...], tuple[torch.cuda.CUDAGraph, torch.Tensor, torch.Tensor]] = {}
 
512
  if isinstance(self.vocoder_session, TorchScriptVocoderRuntime):
513
  prewarm_buckets = parse_int_csv(config.vocoder_prewarm_buckets)
514
  if prewarm_buckets:
515
+ try:
516
+ self.vocoder_prewarm_result = self.vocoder_session.prewarm_buckets(prewarm_buckets)
517
+ except Exception:
518
+ # Prewarm can fail in ZeroGPU main process (no real GPU
519
+ # attached at startup). It's an optimization only.
520
+ self.vocoder_prewarm_result = {"requested": prewarm_buckets, "warmed": [], "elapsed_sec": 0.0, "skipped": True}
521
  tts_load_kwargs: dict[str, Any] = {
522
  "trust_remote_code": True,
523
  "torch_dtype": self.dtype,