model_manager.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. quantize: bool = False,
  20. ) -> None:
  21. self.mode = mode
  22. self.device = device
  23. self.half = half
  24. self.compile = compile
  25. self.num_workers = num_workers
  26. self.quantize = quantize
  27. self.precision = torch.half if half else torch.bfloat16
  28. # Check if MPS or CUDA is available
  29. if torch.backends.mps.is_available():
  30. self.device = "mps"
  31. logger.info("mps is available, running on mps.")
  32. elif not torch.cuda.is_available():
  33. self.device = "cpu"
  34. logger.info("CUDA is not available, running on CPU.")
  35. # Load the TTS models
  36. self.load_llama_model(
  37. llama_checkpoint_path, self.device, self.precision, self.compile, self.mode
  38. )
  39. self.load_decoder_model(
  40. decoder_config_name, decoder_checkpoint_path, self.device
  41. )
  42. self.tts_inference_engine = TTSInferenceEngine(
  43. llama_queue=self.llama_queue,
  44. decoder_model=self.decoder_model,
  45. precision=self.precision,
  46. compile=self.compile,
  47. )
  48. # Warm up the models
  49. if self.mode == "tts":
  50. self.warm_up(self.tts_inference_engine)
  51. def load_llama_model(
  52. self, checkpoint_path, device, precision, compile, mode
  53. ) -> None:
  54. if mode == "tts":
  55. self.llama_queue = launch_thread_safe_queue(
  56. checkpoint_path=checkpoint_path,
  57. device=device,
  58. precision=precision,
  59. compile=compile,
  60. num_workers=self.num_workers,
  61. quantize=self.quantize,
  62. )
  63. else:
  64. raise ValueError(f"Invalid mode: {mode}")
  65. logger.info("LLAMA model loaded.")
  66. def load_decoder_model(self, config_name, checkpoint_path, device) -> None:
  67. self.decoder_model = load_decoder_model(
  68. config_name=config_name,
  69. checkpoint_path=checkpoint_path,
  70. device=device,
  71. )
  72. logger.info("Decoder model loaded.")
  73. def warm_up(self, tts_inference_engine) -> None:
  74. request = ServeTTSRequest(
  75. text="Hello world.",
  76. references=[],
  77. reference_id=None,
  78. max_new_tokens=1024,
  79. # chunk_length=200,
  80. chunk_length=150,
  81. top_p=0.7,
  82. repetition_penalty=1.2,
  83. temperature=0.7,
  84. format="wav",
  85. )
  86. list(inference(request, tts_inference_engine))
  87. logger.info("Models warmed up.")