model_manager.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import torch
  2. from loguru import logger
  3. from fish_speech.inference_engine import TTSInferenceEngine
  4. from fish_speech.models.dac.inference import load_model as load_decoder_model
  5. from fish_speech.models.text2semantic.inference import launch_thread_safe_queue
  6. from fish_speech.utils.schema import ServeTTSRequest
  7. from tools.server.inference import inference_wrapper as inference
  8. class ModelManager:
  9. def __init__(
  10. self,
  11. mode: str,
  12. device: str,
  13. half: bool,
  14. compile: bool,
  15. llama_checkpoint_path: str,
  16. decoder_checkpoint_path: str,
  17. decoder_config_name: str,
  18. num_workers: int = 1,
  19. ) -> None:
  20. self.mode = mode
  21. self.device = device
  22. self.half = half
  23. self.compile = compile
  24. self.num_workers = num_workers
  25. self.precision = torch.half if half else torch.bfloat16
  26. # Check if MPS or CUDA is available
  27. if torch.backends.mps.is_available():
  28. self.device = "mps"
  29. logger.info("mps is available, running on mps.")
  30. elif not torch.cuda.is_available():
  31. self.device = "cpu"
  32. logger.info("CUDA is not available, running on CPU.")
  33. # Load the TTS models
  34. self.load_llama_model(
  35. llama_checkpoint_path, self.device, self.precision, self.compile, self.mode
  36. )
  37. self.load_decoder_model(
  38. decoder_config_name, decoder_checkpoint_path, self.device
  39. )
  40. self.tts_inference_engine = TTSInferenceEngine(
  41. llama_queue=self.llama_queue,
  42. decoder_model=self.decoder_model,
  43. precision=self.precision,
  44. compile=self.compile,
  45. )
  46. # Warm up the models
  47. if self.mode == "tts":
  48. self.warm_up(self.tts_inference_engine)
  49. def load_llama_model(
  50. self, checkpoint_path, device, precision, compile, mode
  51. ) -> None:
  52. if mode == "tts":
  53. self.llama_queue = launch_thread_safe_queue(
  54. checkpoint_path=checkpoint_path,
  55. device=device,
  56. precision=precision,
  57. compile=compile,
  58. num_workers=self.num_workers,
  59. )
  60. else:
  61. raise ValueError(f"Invalid mode: {mode}")
  62. logger.info("LLAMA model loaded.")
  63. def load_decoder_model(self, config_name, checkpoint_path, device) -> None:
  64. self.decoder_model = load_decoder_model(
  65. config_name=config_name,
  66. checkpoint_path=checkpoint_path,
  67. device=device,
  68. )
  69. logger.info("Decoder model loaded.")
  70. def warm_up(self, tts_inference_engine) -> None:
  71. request = ServeTTSRequest(
  72. text="Hello world.",
  73. references=[],
  74. reference_id=None,
  75. max_new_tokens=1024,
  76. # chunk_length=200,
  77. chunk_length=150,
  78. top_p=0.7,
  79. repetition_penalty=1.2,
  80. temperature=0.7,
  81. format="wav",
  82. )
  83. list(inference(request, tts_inference_engine))
  84. logger.info("Models warmed up.")