|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + |
| 6 | +"""MagpieTTS backend using MagpieInferenceRunner with RTF metrics.""" |
| 7 | + |
| 8 | +import io |
| 9 | +import json |
| 10 | +import os |
| 11 | +import shutil |
| 12 | +import tempfile |
| 13 | +import time |
| 14 | +from dataclasses import dataclass |
| 15 | +from typing import Any, Dict, List, Optional, Set |
| 16 | + |
| 17 | +import soundfile as sf |
| 18 | + |
| 19 | +from .base import BackendConfig, GenerationRequest, GenerationResult, InferenceBackend, Modality |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class MagpieTTSConfig(BackendConfig): |
| 24 | + codec_model_path: Optional[str] = None |
| 25 | + top_k: int = 80 |
| 26 | + temperature: float = 0.6 |
| 27 | + use_cfg: bool = True |
| 28 | + cfg_scale: float = 2.5 |
| 29 | + max_decoder_steps: int = 440 |
| 30 | + use_local_transformer: bool = False |
| 31 | + output_sample_rate: int = 22050 |
| 32 | + # Checkpoint loading options (alternative to model_path .nemo file) |
| 33 | + hparams_file: Optional[str] = None |
| 34 | + checkpoint_file: Optional[str] = None |
| 35 | + legacy_codebooks: bool = False |
| 36 | + legacy_text_conditioning: bool = False |
| 37 | + hparams_from_wandb: bool = False |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def from_dict(cls, d: Dict[str, Any]) -> "MagpieTTSConfig": |
| 41 | + known = { |
| 42 | + "model_path", |
| 43 | + "device", |
| 44 | + "dtype", |
| 45 | + "max_new_tokens", |
| 46 | + "temperature", |
| 47 | + "top_p", |
| 48 | + "top_k", |
| 49 | + "codec_model_path", |
| 50 | + "use_cfg", |
| 51 | + "cfg_scale", |
| 52 | + "max_decoder_steps", |
| 53 | + "use_local_transformer", |
| 54 | + "output_sample_rate", |
| 55 | + "hparams_file", |
| 56 | + "checkpoint_file", |
| 57 | + "legacy_codebooks", |
| 58 | + "legacy_text_conditioning", |
| 59 | + "hparams_from_wandb", |
| 60 | + } |
| 61 | + return cls( |
| 62 | + **{k: v for k, v in d.items() if k in known}, extra_config={k: v for k, v in d.items() if k not in known} |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +class MagpieTTSBackend(InferenceBackend): |
| 67 | + """MagpieTTS backend. Input: JSON with 'text' and 'context_audio_filepath'.""" |
| 68 | + |
| 69 | + @classmethod |
| 70 | + def get_config_class(cls) -> type: |
| 71 | + return MagpieTTSConfig |
| 72 | + |
| 73 | + @property |
| 74 | + def name(self) -> str: |
| 75 | + return "magpie_tts" |
| 76 | + |
| 77 | + @property |
| 78 | + def supported_modalities(self) -> Set[Modality]: |
| 79 | + return {Modality.TEXT, Modality.AUDIO_OUT} |
| 80 | + |
| 81 | + def __init__(self, config: BackendConfig): |
| 82 | + self.tts_config = ( |
| 83 | + config |
| 84 | + if isinstance(config, MagpieTTSConfig) |
| 85 | + else MagpieTTSConfig.from_dict( |
| 86 | + { |
| 87 | + **{ |
| 88 | + k: getattr(config, k) |
| 89 | + for k in ["model_path", "device", "dtype", "max_new_tokens", "temperature", "top_p", "top_k"] |
| 90 | + if hasattr(config, k) |
| 91 | + }, |
| 92 | + **config.extra_config, |
| 93 | + } |
| 94 | + ) |
| 95 | + ) |
| 96 | + super().__init__(self.tts_config) |
| 97 | + self._model = self._runner = self._temp_dir = self._checkpoint_name = None |
| 98 | + |
| 99 | + def load_model(self) -> None: |
| 100 | + # Patch NeMo's load_fsspec() to route HuggingFace resolve URLs through |
| 101 | + # huggingface_hub.hf_hub_download() (uses file locks and local caching), |
| 102 | + # avoiding 429s when many ranks start concurrently. |
| 103 | + try: |
| 104 | + import os |
| 105 | + import re |
| 106 | + |
| 107 | + import nemo.collections.tts.modules.audio_codec_modules as _acm |
| 108 | + |
| 109 | + _orig_load_fsspec = getattr(_acm, "load_fsspec", None) |
| 110 | + if callable(_orig_load_fsspec) and not getattr(_acm, "_hf_load_fsspec_patched", False): |
| 111 | + try: |
| 112 | + from huggingface_hub import hf_hub_download |
| 113 | + |
| 114 | + def _hf_resolve_to_local(url: str) -> str | None: |
| 115 | + if not isinstance(url, str): |
| 116 | + return None |
| 117 | + url_no_q = url.split("?", 1)[0] |
| 118 | + m = re.match(r"^https?://huggingface\.co/([^/]+)/([^/]+)/resolve/([^/]+)/(.+)$", url_no_q) |
| 119 | + if not m: |
| 120 | + return None |
| 121 | + repo_id = f"{m.group(1)}/{m.group(2)}" |
| 122 | + revision = m.group(3) |
| 123 | + filename = m.group(4) |
| 124 | + token = os.environ.get("HF_TOKEN") or None |
| 125 | + return hf_hub_download(repo_id=repo_id, filename=filename, revision=revision, token=token) |
| 126 | + |
| 127 | + def _load_fsspec_patched(path: str, map_location: str = None, **kwargs): |
| 128 | + if isinstance(path, str) and path.startswith("http"): |
| 129 | + local = _hf_resolve_to_local(path) |
| 130 | + if local: |
| 131 | + return _orig_load_fsspec(local, map_location=map_location, **kwargs) |
| 132 | + return _orig_load_fsspec(path, map_location=map_location, **kwargs) |
| 133 | + |
| 134 | + _acm.load_fsspec = _load_fsspec_patched |
| 135 | + _acm._hf_load_fsspec_patched = True |
| 136 | + except Exception: |
| 137 | + pass |
| 138 | + except Exception: |
| 139 | + pass |
| 140 | + |
| 141 | + from nemo.collections.tts.modules.magpietts_inference.inference import InferenceConfig, MagpieInferenceRunner |
| 142 | + from nemo.collections.tts.modules.magpietts_inference.utils import ModelLoadConfig, load_magpie_model |
| 143 | + |
| 144 | + if not self.tts_config.codec_model_path: |
| 145 | + raise ValueError("codec_model_path required") |
| 146 | + |
| 147 | + # Support both checkpoint mode (hparams + ckpt) and nemo mode |
| 148 | + has_ckpt_mode = self.tts_config.hparams_file and self.tts_config.checkpoint_file |
| 149 | + if has_ckpt_mode: |
| 150 | + cfg = ModelLoadConfig( |
| 151 | + hparams_file=self.tts_config.hparams_file, |
| 152 | + checkpoint_file=self.tts_config.checkpoint_file, |
| 153 | + codecmodel_path=self.tts_config.codec_model_path, |
| 154 | + legacy_codebooks=self.tts_config.legacy_codebooks, |
| 155 | + legacy_text_conditioning=self.tts_config.legacy_text_conditioning, |
| 156 | + hparams_from_wandb=self.tts_config.hparams_from_wandb, |
| 157 | + ) |
| 158 | + else: |
| 159 | + cfg = ModelLoadConfig( |
| 160 | + nemo_file=self.config.model_path, |
| 161 | + codecmodel_path=self.tts_config.codec_model_path, |
| 162 | + legacy_codebooks=self.tts_config.legacy_codebooks, |
| 163 | + legacy_text_conditioning=self.tts_config.legacy_text_conditioning, |
| 164 | + ) |
| 165 | + self._model, self._checkpoint_name = load_magpie_model(cfg, device=self.config.device) |
| 166 | + |
| 167 | + self._runner = MagpieInferenceRunner( |
| 168 | + self._model, |
| 169 | + InferenceConfig( |
| 170 | + temperature=self.tts_config.temperature, |
| 171 | + topk=self.tts_config.top_k, |
| 172 | + max_decoder_steps=self.tts_config.max_decoder_steps, |
| 173 | + use_cfg=self.tts_config.use_cfg, |
| 174 | + cfg_scale=self.tts_config.cfg_scale, |
| 175 | + use_local_transformer=self.tts_config.use_local_transformer, |
| 176 | + batch_size=16, |
| 177 | + ), |
| 178 | + ) |
| 179 | + |
| 180 | + self._temp_dir = tempfile.mkdtemp(prefix="magpie_tts_") |
| 181 | + self.tts_config.output_sample_rate = self._model.sample_rate |
| 182 | + self._is_loaded = True |
| 183 | + print( |
| 184 | + f"[MagpieTTSBackend] Loaded: {self._checkpoint_name}, sr={self._model.sample_rate}, cfg={self.tts_config.use_cfg}" |
| 185 | + ) |
| 186 | + |
| 187 | + def _extract_json(self, text: str) -> dict: |
| 188 | + """Extract JSON object from text, skipping non-JSON parts.""" |
| 189 | + if not text: |
| 190 | + return {"text": ""} |
| 191 | + idx = text.find("{") |
| 192 | + if idx >= 0: |
| 193 | + try: |
| 194 | + return json.loads(text[idx:]) |
| 195 | + except json.JSONDecodeError: |
| 196 | + pass |
| 197 | + return {"text": text} |
| 198 | + |
| 199 | + def generate(self, requests: List[GenerationRequest]) -> List[GenerationResult]: |
| 200 | + if not self._is_loaded: |
| 201 | + return [GenerationResult(error="Model not loaded", request_id=r.request_id) for r in requests] |
| 202 | + if not requests: |
| 203 | + return [] |
| 204 | + |
| 205 | + start_time = time.time() |
| 206 | + batch_dir = os.path.join(self._temp_dir, f"batch_{int(time.time() * 1000)}") |
| 207 | + output_dir = os.path.join(batch_dir, "output") |
| 208 | + os.makedirs(output_dir, exist_ok=True) |
| 209 | + |
| 210 | + try: |
| 211 | + # Reset KV caches to avoid cross-request shape mismatches |
| 212 | + try: |
| 213 | + if self._model is not None: |
| 214 | + decoder = getattr(self._model, "decoder", None) |
| 215 | + if decoder is not None and hasattr(decoder, "reset_cache"): |
| 216 | + decoder.reset_cache(use_cache=False) |
| 217 | + except Exception: |
| 218 | + pass |
| 219 | + |
| 220 | + # Parse requests, extracting JSON from text |
| 221 | + parsed = [self._extract_json(r.text) for r in requests] |
| 222 | + |
| 223 | + # Create audio_dir with symlinks to all context audio files |
| 224 | + audio_dir = os.path.join(batch_dir, "audio") |
| 225 | + os.makedirs(audio_dir, exist_ok=True) |
| 226 | + |
| 227 | + manifest_path = os.path.join(batch_dir, "manifest.json") |
| 228 | + with open(manifest_path, "w") as f: |
| 229 | + for i, p in enumerate(parsed): |
| 230 | + ctx = p.get("context_audio_filepath", "") |
| 231 | + if ctx and os.path.exists(ctx): |
| 232 | + link_name = f"ctx_{i}_{os.path.basename(ctx)}" |
| 233 | + link_path = os.path.join(audio_dir, link_name) |
| 234 | + if not os.path.exists(link_path): |
| 235 | + os.symlink(ctx, link_path) |
| 236 | + else: |
| 237 | + link_name = f"d{i}.wav" |
| 238 | + link_path = os.path.join(audio_dir, link_name) |
| 239 | + if not os.path.exists(link_path): |
| 240 | + sr = int(getattr(self.tts_config, "output_sample_rate", 22050) or 22050) |
| 241 | + dur_s = 0.1 |
| 242 | + n = max(1, int(sr * dur_s)) |
| 243 | + sf.write(link_path, [0.0] * n, sr) |
| 244 | + f.write( |
| 245 | + json.dumps( |
| 246 | + { |
| 247 | + "text": p.get("text", ""), |
| 248 | + "audio_filepath": link_name, |
| 249 | + "context_audio_filepath": link_name, |
| 250 | + "duration": p.get("duration", 5.0), |
| 251 | + "context_audio_duration": p.get("context_audio_duration", 5.0), |
| 252 | + } |
| 253 | + ) |
| 254 | + + "\n" |
| 255 | + ) |
| 256 | + |
| 257 | + config_path = os.path.join(batch_dir, "config.json") |
| 258 | + with open(config_path, "w") as f: |
| 259 | + json.dump({"batch": {"manifest_path": manifest_path, "audio_dir": audio_dir}}, f) |
| 260 | + |
| 261 | + # Run inference |
| 262 | + from nemo.collections.tts.modules.magpietts_inference.evaluate_generated_audio import load_evalset_config |
| 263 | + |
| 264 | + dataset = self._runner.create_dataset(load_evalset_config(config_path)) |
| 265 | + rtf_list, _ = self._runner.run_inference_on_dataset( |
| 266 | + dataset, output_dir, save_cross_attention_maps=False, save_context_audio=False |
| 267 | + ) |
| 268 | + |
| 269 | + gen_time = time.time() - start_time |
| 270 | + batch_metrics = { |
| 271 | + "total_time_sec": gen_time, |
| 272 | + "num_samples": len(requests), |
| 273 | + **self._runner.compute_mean_rtf_metrics(rtf_list), |
| 274 | + } |
| 275 | + |
| 276 | + # Build results |
| 277 | + results = [] |
| 278 | + for i, req in enumerate(requests): |
| 279 | + path = os.path.join(output_dir, f"predicted_audio_{i}.wav") |
| 280 | + if os.path.exists(path): |
| 281 | + audio, sr = sf.read(path) |
| 282 | + buf = io.BytesIO() |
| 283 | + sf.write(buf, audio, sr, format="WAV") |
| 284 | + buf.seek(0) |
| 285 | + dur = len(audio) / sr |
| 286 | + results.append( |
| 287 | + GenerationResult( |
| 288 | + text=parsed[i].get("text", ""), |
| 289 | + audio_bytes=buf.read(), |
| 290 | + audio_sample_rate=self.tts_config.output_sample_rate, |
| 291 | + audio_format="wav", |
| 292 | + request_id=req.request_id, |
| 293 | + generation_time_ms=gen_time * 1000 / len(requests), |
| 294 | + debug_info={ |
| 295 | + "checkpoint": self._checkpoint_name, |
| 296 | + "audio_duration_sec": dur, |
| 297 | + "rtf": gen_time / len(requests) / dur if dur else 0, |
| 298 | + "config": { |
| 299 | + "temp": self.tts_config.temperature, |
| 300 | + "top_k": self.tts_config.top_k, |
| 301 | + "cfg": self.tts_config.use_cfg, |
| 302 | + "cfg_scale": self.tts_config.cfg_scale, |
| 303 | + }, |
| 304 | + "batch_metrics": batch_metrics, |
| 305 | + }, |
| 306 | + ) |
| 307 | + ) |
| 308 | + else: |
| 309 | + results.append(GenerationResult(error=f"Audio not found: {path}", request_id=req.request_id)) |
| 310 | + return results |
| 311 | + except Exception as e: |
| 312 | + import traceback |
| 313 | + |
| 314 | + traceback.print_exc() |
| 315 | + return [GenerationResult(error=str(e), request_id=r.request_id) for r in requests] |
| 316 | + finally: |
| 317 | + shutil.rmtree(batch_dir, ignore_errors=True) |
| 318 | + |
| 319 | + def validate_request(self, request: GenerationRequest) -> Optional[str]: |
| 320 | + return "Text required" if not request.text else None |
| 321 | + |
| 322 | + def health_check(self) -> Dict[str, Any]: |
| 323 | + h = super().health_check() |
| 324 | + if self._is_loaded: |
| 325 | + h.update( |
| 326 | + { |
| 327 | + "checkpoint": self._checkpoint_name, |
| 328 | + "codec": self.tts_config.codec_model_path, |
| 329 | + "cfg": self.tts_config.use_cfg, |
| 330 | + "cfg_scale": self.tts_config.cfg_scale, |
| 331 | + "sample_rate": self.tts_config.output_sample_rate, |
| 332 | + } |
| 333 | + ) |
| 334 | + return h |
| 335 | + |
| 336 | + def __del__(self): |
| 337 | + if getattr(self, "_temp_dir", None) and os.path.exists(self._temp_dir): |
| 338 | + shutil.rmtree(self._temp_dir, ignore_errors=True) |
0 commit comments