|
@@ -57,15 +57,15 @@ class CosyVoiceModel:
|
|
|
self.hift_cache_dict = {}
|
|
|
|
|
|
def load(self, llm_model, flow_model, hift_model):
|
|
|
- self.llm.load_state_dict(torch.load(llm_model, map_location=self.device), strict=False)
|
|
|
+ self.llm.load_state_dict(torch.load(llm_model, map_location=self.device), strict=True)
|
|
|
self.llm.to(self.device).eval()
|
|
|
if self.fp16 is True:
|
|
|
self.llm.half()
|
|
|
- self.flow.load_state_dict(torch.load(flow_model, map_location=self.device), strict=False)
|
|
|
+ self.flow.load_state_dict(torch.load(flow_model, map_location=self.device), strict=True)
|
|
|
self.flow.to(self.device).eval()
|
|
|
# in case hift_model is a hifigan model
|
|
|
hift_state_dict = {k.replace('generator.', ''): v for k, v in torch.load(hift_model, map_location=self.device).items()}
|
|
|
- self.hift.load_state_dict(hift_state_dict, strict=False)
|
|
|
+ self.hift.load_state_dict(hift_state_dict, strict=True)
|
|
|
self.hift.to(self.device).eval()
|
|
|
|
|
|
def load_jit(self, llm_text_encoder_model, llm_llm_model, flow_encoder_model):
|
|
@@ -254,3 +254,175 @@ class CosyVoiceModel:
|
|
|
self.llm_end_dict.pop(this_uuid)
|
|
|
self.mel_overlap_dict.pop(this_uuid)
|
|
|
self.hift_cache_dict.pop(this_uuid)
|
|
|
+
|
|
|
+
|
|
|
+class CosyVoice2Model:
|
|
|
+
|
|
|
+ def __init__(self,
|
|
|
+ llm: torch.nn.Module,
|
|
|
+ flow: torch.nn.Module,
|
|
|
+ hift: torch.nn.Module,
|
|
|
+ fp16: bool):
|
|
|
+ self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
+ self.llm = llm
|
|
|
+ self.flow = flow
|
|
|
+ self.hift = hift
|
|
|
+ self.fp16 = fp16
|
|
|
+ self.token_min_hop_len = 1 * self.flow.input_frame_rate
|
|
|
+ self.token_max_hop_len = 2 * self.flow.input_frame_rate
|
|
|
+ self.token_right_context = self.flow.encoder.pre_lookahead_layer.pre_lookahead_len
|
|
|
+ # hift cache
|
|
|
+ self.mel_cache_len = 8
|
|
|
+ self.source_cache_len = int(self.mel_cache_len * 480)
|
|
|
+ # speech fade in out
|
|
|
+ self.speech_window = np.hamming(2 * self.source_cache_len)
|
|
|
+ # rtf and decoding related
|
|
|
+ self.stream_scale_factor = 1
|
|
|
+ assert self.stream_scale_factor == 1, 'fix stream_scale_factor to 1 as we haven\'t implement cache in flow matching yet, this constraint will be loosen in the future'
|
|
|
+ self.llm_context = torch.cuda.stream(torch.cuda.Stream(self.device)) if torch.cuda.is_available() else nullcontext()
|
|
|
+ self.lock = threading.Lock()
|
|
|
+ # dict used to store session related variable
|
|
|
+ self.tts_speech_token_dict = {}
|
|
|
+ self.llm_end_dict = {}
|
|
|
+ self.hift_cache_dict = {}
|
|
|
+
|
|
|
+ def load(self, llm_model, flow_model, hift_model):
|
|
|
+ self.llm.load_state_dict(torch.load(llm_model, map_location=self.device), strict=True)
|
|
|
+ self.llm.to(self.device).eval()
|
|
|
+ if self.fp16 is True:
|
|
|
+ self.llm.half()
|
|
|
+ self.flow.load_state_dict(torch.load(flow_model, map_location=self.device), strict=True)
|
|
|
+ self.flow.to(self.device).eval()
|
|
|
+ # in case hift_model is a hifigan model
|
|
|
+ hift_state_dict = {k.replace('generator.', ''): v for k, v in torch.load(hift_model, map_location=self.device).items()}
|
|
|
+ self.hift.load_state_dict(hift_state_dict, strict=True)
|
|
|
+ self.hift.to(self.device).eval()
|
|
|
+
|
|
|
+ def load_jit(self, llm_text_encoder_model, llm_llm_model, flow_encoder_model):
|
|
|
+ assert self.fp16 is True, "we only provide fp16 jit model, set fp16=True if you want to use jit model"
|
|
|
+ llm_text_encoder = torch.jit.load(llm_text_encoder_model, map_location=self.device)
|
|
|
+ self.llm.text_encoder = llm_text_encoder
|
|
|
+ llm_llm = torch.jit.load(llm_llm_model, map_location=self.device)
|
|
|
+ self.llm.llm = llm_llm
|
|
|
+ flow_encoder = torch.jit.load(flow_encoder_model, map_location=self.device)
|
|
|
+ self.flow.encoder = flow_encoder
|
|
|
+
|
|
|
+ def load_onnx(self, flow_decoder_estimator_model):
|
|
|
+ import onnxruntime
|
|
|
+ option = onnxruntime.SessionOptions()
|
|
|
+ option.graph_optimization_level = onnxruntime.GraphOptimizationLevel.ORT_ENABLE_ALL
|
|
|
+ option.intra_op_num_threads = 1
|
|
|
+ providers = ['CUDAExecutionProvider' if torch.cuda.is_available() else 'CPUExecutionProvider']
|
|
|
+ del self.flow.decoder.estimator
|
|
|
+ self.flow.decoder.estimator = onnxruntime.InferenceSession(flow_decoder_estimator_model, sess_options=option, providers=providers)
|
|
|
+
|
|
|
+ def llm_job(self, text, prompt_text, llm_prompt_speech_token, llm_embedding, uuid):
|
|
|
+ if self.fp16 is True:
|
|
|
+ llm_embedding = llm_embedding.half()
|
|
|
+ with self.llm_context:
|
|
|
+ for i in self.llm.inference(text=text.to(self.device),
|
|
|
+ text_len=torch.tensor([text.shape[1]], dtype=torch.int32).to(self.device),
|
|
|
+ prompt_text=prompt_text.to(self.device),
|
|
|
+ prompt_text_len=torch.tensor([prompt_text.shape[1]], dtype=torch.int32).to(self.device),
|
|
|
+ prompt_speech_token=llm_prompt_speech_token.to(self.device),
|
|
|
+ prompt_speech_token_len=torch.tensor([llm_prompt_speech_token.shape[1]], dtype=torch.int32).to(self.device),
|
|
|
+ embedding=llm_embedding.to(self.device)):
|
|
|
+ self.tts_speech_token_dict[uuid].append(i)
|
|
|
+ self.llm_end_dict[uuid] = True
|
|
|
+
|
|
|
+ def token2wav(self, token, prompt_token, prompt_feat, embedding, uuid, token_offset, finalize=False, speed=1.0):
|
|
|
+ tts_mel, _ = self.flow.inference(token=token.to(self.device),
|
|
|
+ token_len=torch.tensor([token.shape[1]], dtype=torch.int32).to(self.device),
|
|
|
+ prompt_token=prompt_token.to(self.device),
|
|
|
+ prompt_token_len=torch.tensor([prompt_token.shape[1]], dtype=torch.int32).to(self.device),
|
|
|
+ prompt_feat=prompt_feat.to(self.device),
|
|
|
+ prompt_feat_len=torch.tensor([prompt_feat.shape[1]], dtype=torch.int32).to(self.device),
|
|
|
+ embedding=embedding.to(self.device),
|
|
|
+ finalize=finalize)
|
|
|
+ tts_mel = tts_mel[:, :, token_offset * self.flow.encoder.up_layer.stride:]
|
|
|
+ # append hift cache
|
|
|
+ if self.hift_cache_dict[uuid] is not None:
|
|
|
+ hift_cache_mel, hift_cache_source = self.hift_cache_dict[uuid]['mel'], self.hift_cache_dict[uuid]['source']
|
|
|
+ tts_mel = torch.concat([hift_cache_mel, tts_mel], dim=2)
|
|
|
+ else:
|
|
|
+ hift_cache_source = torch.zeros(1, 1, 0)
|
|
|
+ # keep overlap mel and hift cache
|
|
|
+ if finalize is False:
|
|
|
+ tts_speech, tts_source = self.hift.inference(speech_feat=tts_mel, cache_source=hift_cache_source)
|
|
|
+ if self.hift_cache_dict[uuid] is not None:
|
|
|
+ tts_speech = fade_in_out(tts_speech, self.hift_cache_dict[uuid]['speech'], self.speech_window)
|
|
|
+ self.hift_cache_dict[uuid] = {'mel': tts_mel[:, :, -self.mel_cache_len:],
|
|
|
+ 'source': tts_source[:, :, -self.source_cache_len:],
|
|
|
+ 'speech': tts_speech[:, -self.source_cache_len:]}
|
|
|
+ tts_speech = tts_speech[:, :-self.source_cache_len]
|
|
|
+ else:
|
|
|
+ if speed != 1.0:
|
|
|
+ assert self.hift_cache_dict[uuid] is None, 'speed change only support non-stream inference mode'
|
|
|
+ tts_mel = F.interpolate(tts_mel, size=int(tts_mel.shape[2] / speed), mode='linear')
|
|
|
+ tts_speech, tts_source = self.hift.inference(speech_feat=tts_mel, cache_source=hift_cache_source)
|
|
|
+ if self.hift_cache_dict[uuid] is not None:
|
|
|
+ tts_speech = fade_in_out(tts_speech, self.hift_cache_dict[uuid]['speech'], self.speech_window)
|
|
|
+ return tts_speech
|
|
|
+
|
|
|
+ def tts(self, text, flow_embedding, llm_embedding=torch.zeros(0, 192),
|
|
|
+ prompt_text=torch.zeros(1, 0, dtype=torch.int32),
|
|
|
+ llm_prompt_speech_token=torch.zeros(1, 0, dtype=torch.int32),
|
|
|
+ flow_prompt_speech_token=torch.zeros(1, 0, dtype=torch.int32),
|
|
|
+ prompt_speech_feat=torch.zeros(1, 0, 80), stream=False, speed=1.0, **kwargs):
|
|
|
+ # this_uuid is used to track variables related to this inference thread
|
|
|
+ this_uuid = str(uuid.uuid1())
|
|
|
+ with self.lock:
|
|
|
+ self.tts_speech_token_dict[this_uuid], self.llm_end_dict[this_uuid] = [], False
|
|
|
+ self.hift_cache_dict[this_uuid] = None
|
|
|
+ p = threading.Thread(target=self.llm_job, args=(text, prompt_text, llm_prompt_speech_token, llm_embedding, this_uuid))
|
|
|
+ p.start()
|
|
|
+ if stream is True:
|
|
|
+ token_hop_len, token_offset = self.token_min_hop_len, 0
|
|
|
+ self.flow.encoder.static_chunk_size = self.token_min_hop_len
|
|
|
+ self.flow.decoder.estimator.static_chunk_size = self.token_min_hop_len * self.flow.encoder.up_layer.stride
|
|
|
+ while True:
|
|
|
+ time.sleep(0.1)
|
|
|
+ if len(self.tts_speech_token_dict[this_uuid]) - token_offset >= token_hop_len + self.token_right_context:
|
|
|
+ this_tts_speech_token = torch.tensor(self.tts_speech_token_dict[this_uuid][:token_offset + token_hop_len + self.token_right_context]) \
|
|
|
+ .unsqueeze(dim=0)
|
|
|
+ this_tts_speech = self.token2wav(token=this_tts_speech_token,
|
|
|
+ prompt_token=flow_prompt_speech_token,
|
|
|
+ prompt_feat=prompt_speech_feat,
|
|
|
+ embedding=flow_embedding,
|
|
|
+ uuid=this_uuid,
|
|
|
+ token_offset=token_offset,
|
|
|
+ finalize=False)
|
|
|
+ token_offset += token_hop_len
|
|
|
+ yield {'tts_speech': this_tts_speech.cpu()}
|
|
|
+ # increase token_hop_len for better speech quality
|
|
|
+ token_hop_len = min(self.token_max_hop_len, int(token_hop_len * self.stream_scale_factor))
|
|
|
+ if self.llm_end_dict[this_uuid] is True and len(self.tts_speech_token_dict[this_uuid]) - token_offset < token_hop_len + self.token_right_context:
|
|
|
+ break
|
|
|
+ p.join()
|
|
|
+ # deal with remain tokens, make sure inference remain token len equals token_hop_len when cache_speech is not None
|
|
|
+ this_tts_speech_token = torch.tensor(self.tts_speech_token_dict[this_uuid]).unsqueeze(dim=0)
|
|
|
+ this_tts_speech = self.token2wav(token=this_tts_speech_token,
|
|
|
+ prompt_token=flow_prompt_speech_token,
|
|
|
+ prompt_feat=prompt_speech_feat,
|
|
|
+ embedding=flow_embedding,
|
|
|
+ uuid=this_uuid,
|
|
|
+ token_offset=token_offset,
|
|
|
+ finalize=True)
|
|
|
+ yield {'tts_speech': this_tts_speech.cpu()}
|
|
|
+ else:
|
|
|
+ # deal with all tokens
|
|
|
+ p.join()
|
|
|
+ self.flow.encoder.static_chunk_size = 0
|
|
|
+ self.flow.decoder.estimator.static_chunk_size = 0
|
|
|
+ this_tts_speech_token = torch.tensor(self.tts_speech_token_dict[this_uuid]).unsqueeze(dim=0)
|
|
|
+ this_tts_speech = self.token2wav(token=this_tts_speech_token,
|
|
|
+ prompt_token=flow_prompt_speech_token,
|
|
|
+ prompt_feat=prompt_speech_feat,
|
|
|
+ embedding=flow_embedding,
|
|
|
+ uuid=this_uuid,
|
|
|
+ finalize=True,
|
|
|
+ speed=speed)
|
|
|
+ yield {'tts_speech': this_tts_speech.cpu()}
|
|
|
+ with self.lock:
|
|
|
+ self.tts_speech_token_dict.pop(this_uuid)
|
|
|
+ self.llm_end_dict.pop(this_uuid)
|