model_manager.py 3.1 KB

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