添加with torch.no_grad(),速度快一大截

This commit is contained in:
XTer 2024-04-06 22:00:30 +08:00
parent ec7647e08d
commit adb7f71b64
2 changed files with 359 additions and 358 deletions

4
.gitignore vendored
View File

@ -10,6 +10,8 @@ reference
GPT_weights GPT_weights
SoVITS_weights SoVITS_weights
TEMP TEMP
PortableGit
ffmpeg.exe ffmpeg.exe
ffprobe.exe ffprobe.exe
tmp_audio
trained

View File

@ -249,8 +249,6 @@ class TTS:
if self.configs.is_half and str(self.configs.device)!="cpu": if self.configs.is_half and str(self.configs.device)!="cpu":
self.bert_model = self.bert_model.half() self.bert_model = self.bert_model.half()
def init_vits_weights(self, weights_path: str): def init_vits_weights(self, weights_path: str):
print(f"Loading VITS weights from {weights_path}") print(f"Loading VITS weights from {weights_path}")
self.configs.vits_weights_path = weights_path self.configs.vits_weights_path = weights_path
@ -282,7 +280,6 @@ class TTS:
if self.configs.is_half and str(self.configs.device)!="cpu": if self.configs.is_half and str(self.configs.device)!="cpu":
self.vits_model = self.vits_model.half() self.vits_model = self.vits_model.half()
def init_t2s_weights(self, weights_path: str): def init_t2s_weights(self, weights_path: str):
print(f"Loading Text2Semantic weights from {weights_path}") print(f"Loading Text2Semantic weights from {weights_path}")
self.configs.t2s_weights_path = weights_path self.configs.t2s_weights_path = weights_path
@ -379,7 +376,6 @@ class TTS:
# self.refer_spec = spec # self.refer_spec = spec
self.prompt_cache["refer_spec"] = spec self.prompt_cache["refer_spec"] = spec
def _set_prompt_semantic(self, ref_wav_path:str): def _set_prompt_semantic(self, ref_wav_path:str):
zero_wav = np.zeros( zero_wav = np.zeros(
int(self.configs.sampling_rate * 0.3), int(self.configs.sampling_rate * 0.3),
@ -420,7 +416,8 @@ class TTS:
max_length = max(seq_lengths) max_length = max(seq_lengths)
else: else:
max_length = max(seq_lengths) if max_length < max(seq_lengths) else max_length max_length = max(seq_lengths) if max_length < max(seq_lengths) else max_length
# 我爱套 torch.no_grad()
# with torch.no_grad():
padded_sequences = [] padded_sequences = []
for seq, length in zip(sequences, seq_lengths): for seq, length in zip(sequences, seq_lengths):
padding = [0] * axis + [0, max_length - length] + [0] * (ndim - axis - 1) padding = [0] * axis + [0, max_length - length] + [0] * (ndim - axis - 1)
@ -438,6 +435,8 @@ class TTS:
precision:torch.dtype=torch.float32, precision:torch.dtype=torch.float32,
): ):
# 但是这里不能套,反而会负优化
# with torch.no_grad():
_data:list = [] _data:list = []
index_and_len_list = [] index_and_len_list = []
for idx, item in enumerate(data): for idx, item in enumerate(data):
@ -473,7 +472,6 @@ class TTS:
batch_index_list.append([]) batch_index_list.append([])
batch_index_list[-1].append(i) batch_index_list[-1].append(i)
for batch_idx, index_list in enumerate(batch_index_list): for batch_idx, index_list in enumerate(batch_index_list):
item_list = [data[idx] for idx in index_list] item_list = [data[idx] for idx in index_list]
phones_list = [] phones_list = []
@ -485,6 +483,8 @@ class TTS:
norm_text_batch = [] norm_text_batch = []
bert_max_len = 0 bert_max_len = 0
phones_max_len = 0 phones_max_len = 0
# 但是这里也不能套,反而会负优化
# with torch.no_grad():
for item in item_list: for item in item_list:
if prompt_data is not None: if prompt_data is not None:
all_bert_features = torch.cat([prompt_data["bert_features"], item["bert_features"]], 1)\ all_bert_features = torch.cat([prompt_data["bert_features"], item["bert_features"]], 1)\
@ -513,7 +513,6 @@ class TTS:
all_phones_batch = all_phones_list all_phones_batch = all_phones_list
all_bert_features_batch = all_bert_features_list all_bert_features_batch = all_bert_features_list
# max_len = max(bert_max_len, phones_max_len) # max_len = max(bert_max_len, phones_max_len)
# phones_batch = self.batch_sequences(phones_list, axis=0, pad_value=0, max_length=max_len) # phones_batch = self.batch_sequences(phones_list, axis=0, pad_value=0, max_length=max_len)
#### 直接对phones和bert_features进行pad会增大复读概率。 #### 直接对phones和bert_features进行pad会增大复读概率。
@ -568,7 +567,6 @@ class TTS:
''' '''
self.stop_flag = True self.stop_flag = True
def run(self, inputs:dict): def run(self, inputs:dict):
""" """
Text to speech inference. Text to speech inference.
@ -596,6 +594,34 @@ class TTS:
returns: returns:
tuple[int, np.ndarray]: sampling rate and audio data. tuple[int, np.ndarray]: sampling rate and audio data.
""" """
def make_batch(batch_texts):
batch_data = []
print(i18n("############ 提取文本Bert特征 ############"))
for text in tqdm(batch_texts):
phones, bert_features, norm_text = self.text_preprocessor.segment_and_extract_feature_for_text(text, text_lang)
if phones is None:
continue
res={
"phones": phones,
"bert_features": bert_features,
"norm_text": norm_text,
}
batch_data.append(res)
if len(batch_data) == 0:
return None
batch, _ = self.to_batch(batch_data,
prompt_data=self.prompt_cache if not no_prompt_text else None,
batch_size=batch_size,
threshold=batch_threshold,
split_bucket=False,
device=self.configs.device,
precision=self.precision
)
return batch[0]
# 直接给全体套一个torch.no_grad()
with torch.no_grad():
########## variables initialization ########### ########## variables initialization ###########
self.stop_flag:bool = False self.stop_flag:bool = False
text:str = inputs.get("text", "") text:str = inputs.get("text", "")
@ -643,7 +669,6 @@ class TTS:
((self.prompt_cache["prompt_semantic"] is None) or (self.prompt_cache["refer_spec"] is None)): ((self.prompt_cache["prompt_semantic"] is None) or (self.prompt_cache["refer_spec"] is None)):
raise ValueError("ref_audio_path cannot be empty, when the reference audio is not set using set_ref_audio()") raise ValueError("ref_audio_path cannot be empty, when the reference audio is not set using set_ref_audio()")
###### setting reference audio and prompt text preprocessing ######## ###### setting reference audio and prompt text preprocessing ########
t0 = ttime() t0 = ttime()
if (ref_audio_path is not None) and (ref_audio_path != self.prompt_cache["ref_audio_path"]): if (ref_audio_path is not None) and (ref_audio_path != self.prompt_cache["ref_audio_path"]):
@ -664,7 +689,6 @@ class TTS:
self.prompt_cache["bert_features"] = bert_features self.prompt_cache["bert_features"] = bert_features
self.prompt_cache["norm_text"] = norm_text self.prompt_cache["norm_text"] = norm_text
###### text preprocessing ######## ###### text preprocessing ########
t1 = ttime() t1 = ttime()
data:list = None data:list = None
@ -693,30 +717,7 @@ class TTS:
data.append([]) data.append([])
data[-1].append(texts[i]) data[-1].append(texts[i])
def make_batch(batch_texts):
batch_data = []
print(i18n("############ 提取文本Bert特征 ############"))
for text in tqdm(batch_texts):
phones, bert_features, norm_text = self.text_preprocessor.segment_and_extract_feature_for_text(text, text_lang)
if phones is None:
continue
res={
"phones": phones,
"bert_features": bert_features,
"norm_text": norm_text,
}
batch_data.append(res)
if len(batch_data) == 0:
return None
batch, _ = self.to_batch(batch_data,
prompt_data=self.prompt_cache if not no_prompt_text else None,
batch_size=batch_size,
threshold=batch_threshold,
split_bucket=False,
device=self.configs.device,
precision=self.precision
)
return batch[0]
t2 = ttime() t2 = ttime()
try: try:
@ -745,7 +746,7 @@ class TTS:
else: else:
prompt = self.prompt_cache["prompt_semantic"].expand(len(all_phoneme_ids), -1).to(self.configs.device) prompt = self.prompt_cache["prompt_semantic"].expand(len(all_phoneme_ids), -1).to(self.configs.device)
with torch.no_grad():
pred_semantic_list, idx_list = self.t2s_model.model.infer_panel( pred_semantic_list, idx_list = self.t2s_model.model.infer_panel(
all_phoneme_ids, all_phoneme_ids,
all_phoneme_lens, all_phoneme_lens,
@ -765,6 +766,9 @@ class TTS:
batch_audio_fragment = [] batch_audio_fragment = []
# 这里要记得加 torch.no_grad() 不然速度慢一大截
# with torch.no_grad():
# ## vits并行推理 method 1 # ## vits并行推理 method 1
# pred_semantic_list = [item[-idx:] for item, idx in zip(pred_semantic_list, idx_list)] # pred_semantic_list = [item[-idx:] for item, idx in zip(pred_semantic_list, idx_list)]
# pred_semantic_len = torch.LongTensor([item.shape[0] for item in pred_semantic_list]).to(self.configs.device) # pred_semantic_len = torch.LongTensor([item.shape[0] for item in pred_semantic_list]).to(self.configs.device)
@ -791,7 +795,6 @@ class TTS:
audio_frag_end_idx.insert(0, 0) audio_frag_end_idx.insert(0, 0)
batch_audio_fragment= [_batch_audio_fragment[audio_frag_end_idx[i-1]:audio_frag_end_idx[i]] for i in range(1, len(audio_frag_end_idx))] batch_audio_fragment= [_batch_audio_fragment[audio_frag_end_idx[i-1]:audio_frag_end_idx[i]] for i in range(1, len(audio_frag_end_idx))]
# ## vits串行推理 # ## vits串行推理
# for i, idx in enumerate(idx_list): # for i, idx in enumerate(idx_list):
# phones = batch_phones[i].unsqueeze(0).to(self.configs.device) # phones = batch_phones[i].unsqueeze(0).to(self.configs.device)
@ -878,14 +881,12 @@ class TTS:
audio_fragment:torch.Tensor = torch.cat([audio_fragment, zero_wav], dim=0) audio_fragment:torch.Tensor = torch.cat([audio_fragment, zero_wav], dim=0)
audio[i][j] = audio_fragment.cpu().numpy() audio[i][j] = audio_fragment.cpu().numpy()
if split_bucket: if split_bucket:
audio = self.recovery_order(audio, batch_index_list) audio = self.recovery_order(audio, batch_index_list)
else: else:
# audio = [item for batch in audio for item in batch] # audio = [item for batch in audio for item in batch]
audio = sum(audio, []) audio = sum(audio, [])
audio = np.concatenate(audio, 0) audio = np.concatenate(audio, 0)
audio = (audio * 32768).astype(np.int16) audio = (audio * 32768).astype(np.int16)
@ -898,8 +899,6 @@ class TTS:
return sr, audio return sr, audio
def speed_change(input_audio:np.ndarray, speed:float, sr:int): def speed_change(input_audio:np.ndarray, speed:float, sr:int):
# 将 NumPy 数组转换为原始 PCM 流 # 将 NumPy 数组转换为原始 PCM 流
raw_audio = input_audio.astype(np.int16).tobytes() raw_audio = input_audio.astype(np.int16).tobytes()