mirror of
https://github.com/RVC-Boss/GPT-SoVITS.git
synced 2025-04-06 03:57:44 +08:00
【fast_inference_】通过简单添加torch.no_grad()修饰器,可能会使速度加快 (#930)
* 添加with torch.no_grad(),速度快一大截 * 恢复先前缩进 * 恢复make batch的位置 * 改用修饰器 * 去除没必要的增加的空行 --------- Co-authored-by: XTer <xxoy1234@outlook.com>
This commit is contained in:
parent
ec7647e08d
commit
3706ad1b8b
4
.gitignore
vendored
4
.gitignore
vendored
@ -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
|
||||||
|
@ -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
|
||||||
@ -437,7 +435,8 @@ class TTS:
|
|||||||
device:torch.device=torch.device("cpu"),
|
device:torch.device=torch.device("cpu"),
|
||||||
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):
|
||||||
@ -485,6 +484,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)\
|
||||||
@ -568,7 +569,8 @@ class TTS:
|
|||||||
'''
|
'''
|
||||||
self.stop_flag = True
|
self.stop_flag = True
|
||||||
|
|
||||||
|
# 使用装饰器
|
||||||
|
@torch.no_grad()
|
||||||
def run(self, inputs:dict):
|
def run(self, inputs:dict):
|
||||||
"""
|
"""
|
||||||
Text to speech inference.
|
Text to speech inference.
|
||||||
@ -643,11 +645,10 @@ 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"]):
|
||||||
self.set_ref_audio(ref_audio_path)
|
self.set_ref_audio(ref_audio_path)
|
||||||
|
|
||||||
if not no_prompt_text:
|
if not no_prompt_text:
|
||||||
prompt_text = prompt_text.strip("\n")
|
prompt_text = prompt_text.strip("\n")
|
||||||
@ -664,7 +665,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
|
||||||
@ -718,6 +718,7 @@ class TTS:
|
|||||||
)
|
)
|
||||||
return batch[0]
|
return batch[0]
|
||||||
|
|
||||||
|
|
||||||
t2 = ttime()
|
t2 = ttime()
|
||||||
try:
|
try:
|
||||||
print("############ 推理 ############")
|
print("############ 推理 ############")
|
||||||
@ -745,18 +746,18 @@ 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,
|
||||||
prompt,
|
prompt,
|
||||||
all_bert_features,
|
all_bert_features,
|
||||||
# prompt_phone_len=ph_offset,
|
# prompt_phone_len=ph_offset,
|
||||||
top_k=top_k,
|
top_k=top_k,
|
||||||
top_p=top_p,
|
top_p=top_p,
|
||||||
temperature=temperature,
|
temperature=temperature,
|
||||||
early_stop_num=self.configs.hz * self.configs.max_sec,
|
early_stop_num=self.configs.hz * self.configs.max_sec,
|
||||||
)
|
)
|
||||||
t4 = ttime()
|
t4 = ttime()
|
||||||
t_34 += t4 - t3
|
t_34 += t4 - t3
|
||||||
|
|
||||||
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user