model_manager.py 3.7 KB

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