mirror of
https://github.com/RVC-Boss/GPT-SoVITS.git
synced 2026-11-17 18:43:37 +08:00
Sync 20261031 v5 code update and remove unused v5_inference module
This commit is contained in:
parent
b4570b876f
commit
04a44eb977
@ -512,7 +512,6 @@ class TTS:
|
||||
|
||||
self.t2s_model: Text2SemanticLightningModule = None
|
||||
self.t2s_accel = None
|
||||
self.streaming_accel = None
|
||||
self.vits_model: Union[SynthesizerTrn, SynthesizerTrnV3] = None
|
||||
self.bert_tokenizer: AutoTokenizer = None
|
||||
self.bert_model: AutoModelForMaskedLM = None
|
||||
@ -679,9 +678,6 @@ class TTS:
|
||||
|
||||
|
||||
def init_t2s_weights(self, weights_path: str):
|
||||
if self.streaming_accel is not None:
|
||||
self.streaming_accel.close()
|
||||
self.streaming_accel = None
|
||||
if self.t2s_accel is not None:
|
||||
self.t2s_accel.close()
|
||||
self.t2s_accel = None
|
||||
@ -795,9 +791,6 @@ class TTS:
|
||||
print("Half precision is not supported on CPU.")
|
||||
return
|
||||
|
||||
if self.streaming_accel is not None:
|
||||
self.streaming_accel.close()
|
||||
self.streaming_accel = None
|
||||
if self.t2s_accel is not None:
|
||||
self.t2s_accel.close()
|
||||
self.t2s_accel = None
|
||||
@ -834,9 +827,6 @@ class TTS:
|
||||
Args:
|
||||
device: torch.device, the device to use for all models.
|
||||
"""
|
||||
if self.streaming_accel is not None:
|
||||
self.streaming_accel.close()
|
||||
self.streaming_accel = None
|
||||
if self.t2s_accel is not None:
|
||||
self.t2s_accel.close()
|
||||
self.t2s_accel = None
|
||||
@ -1177,31 +1167,7 @@ class TTS:
|
||||
fixed_length_chunk = inputs.get("fixed_length_chunk", False)
|
||||
chunk_split_thershold = 0.0 # 该值代表语义token与mute token的余弦相似度阈值,若大于该阈值,则视为可切分点。
|
||||
|
||||
gpt_streaming = inputs.get("gpt_streaming", False)
|
||||
if gpt_streaming:
|
||||
from .streaming import (validate_streaming_options, PipelineStop, semantic_updates,
|
||||
v2_token_chunks, v5_reference, stream_v5_windows,
|
||||
StreamingWindowSchedule)
|
||||
validate_streaming_options(inputs, self.configs.version)
|
||||
stream_stop = PipelineStop(self)
|
||||
stream_options = dict(inputs, sample_steps=sample_steps, cfg_rate=cfg_rate,
|
||||
speed_factor=speed_factor,
|
||||
streaming_chunk_seconds=inputs.get("streaming_chunk_seconds", 2.0),
|
||||
streaming_lookahead_seconds=inputs.get("streaming_lookahead_seconds", 0.12),
|
||||
streaming_overlap_seconds=inputs.get("streaming_overlap_seconds", 0.04),
|
||||
gpt_streaming_lookahead_seconds=inputs.get("gpt_streaming_lookahead_seconds", 0.5))
|
||||
acoustic_rng = torch.Generator(device=self.configs.device).manual_seed(actual_seed)
|
||||
# Reference semantic codes depend on the active SoVITS model.
|
||||
if getattr(self, "_streaming_ref_model", None) != id(self.vits_model):
|
||||
ref_audio_path = ref_audio_path or self.prompt_cache["ref_audio_path"]
|
||||
if not ref_audio_path:
|
||||
raise ValueError("A reloadable reference audio is required after model changes")
|
||||
self.prompt_cache["ref_audio_path"] = None
|
||||
self._streaming_ref_model = id(self.vits_model)
|
||||
if self.prompt_cache["prompt_lang"] != prompt_lang:
|
||||
self.prompt_cache["prompt_text"] = None
|
||||
|
||||
if streaming_mode and self.configs.use_vocoder and not gpt_streaming:
|
||||
if streaming_mode and self.configs.use_vocoder:
|
||||
print(i18n("SoVits V3/4模型不支持流式推理模式,已自动回退到分段返回模式"))
|
||||
streaming_mode = False
|
||||
return_fragment = True
|
||||
@ -1253,7 +1219,7 @@ class TTS:
|
||||
assert prompt_lang in self.configs.languages
|
||||
|
||||
if no_prompt_text and self.configs.use_vocoder:
|
||||
raise NO_PROMPT_ERROR("prompt_text cannot be empty when using SoVITS_V3")
|
||||
raise NO_PROMPT_ERROR("SoVITS V3/V4/V5 do not support reference-text-free mode; prompt_text cannot be empty")
|
||||
|
||||
if ref_audio_path in [None, ""] and (
|
||||
(self.prompt_cache["prompt_semantic"] is None) or (self.prompt_cache["refer_spec"] in [None, []])
|
||||
@ -1321,15 +1287,7 @@ class TTS:
|
||||
)
|
||||
else:
|
||||
print(f"############ {i18n('切分文本')} ############")
|
||||
# Preserve cut0's single-session behavior; other methods use the
|
||||
# frontend's normal punctuation/newline and long-text handling.
|
||||
texts = self.text_preprocessor.pre_seg_text(
|
||||
text.replace("\n", " ") if gpt_streaming and text_split_method == "cut0" else text,
|
||||
text_lang,
|
||||
text_split_method,
|
||||
)
|
||||
if gpt_streaming and text_split_method == "cut0" and len(texts) > 1:
|
||||
raise ValueError("Text exceeds the single-session frontend limit; shorten the input")
|
||||
texts = self.text_preprocessor.pre_seg_text(text, text_lang, text_split_method)
|
||||
data = []
|
||||
for i in range(len(texts)):
|
||||
if i % batch_size == 0:
|
||||
@ -1365,8 +1323,6 @@ class TTS:
|
||||
return batch[0]
|
||||
|
||||
t2 = time.perf_counter()
|
||||
streaming_tokens = streaming_audio = semantic_token_generator = None
|
||||
streaming_reference = None
|
||||
try:
|
||||
print("############ 推理 ############")
|
||||
###### inference ######
|
||||
@ -1376,8 +1332,6 @@ class TTS:
|
||||
is_first_package = True
|
||||
output_sr = self.configs.sampling_rate if not self.configs.use_vocoder else self.vocoder_configs["sr"]
|
||||
for item in data:
|
||||
if gpt_streaming and stream_stop.is_set():
|
||||
break
|
||||
t3 = time.perf_counter()
|
||||
if return_fragment or streaming_mode:
|
||||
item = make_batch(item)
|
||||
@ -1536,59 +1490,27 @@ class TTS:
|
||||
)
|
||||
batch_audio_fragment.append(audio_fragment)
|
||||
|
||||
elif gpt_streaming and self.configs.version in V5_VERSIONS:
|
||||
if streaming_reference is None:
|
||||
streaming_reference = v5_reference(self)
|
||||
reference = streaming_reference
|
||||
# Validate the acoustic context before starting the AR owner thread.
|
||||
frames = round(stream_options["streaming_chunk_seconds"] * 100)
|
||||
future = round(stream_options["streaming_lookahead_seconds"] * 100)
|
||||
if reference[6].shape[-1] + frames + future > 1000:
|
||||
raise ValueError("Reference plus streaming window exceeds the V5 context")
|
||||
streaming_tokens = semantic_updates(self, all_phoneme_ids[0], prompt,
|
||||
all_bert_features[0], stream_options, stream_stop)
|
||||
streaming_audio = stream_v5_windows(self.vits_model, self.vocoder, denorm_spec,
|
||||
self.configs.device, streaming_tokens, batch_phones[0], reference,
|
||||
stream_options, stream_stop)
|
||||
for pcm in streaming_audio:
|
||||
yield output_sr, pcm
|
||||
streaming_audio.close()
|
||||
streaming_tokens.close()
|
||||
streaming_audio = streaming_tokens = None
|
||||
if not stream_stop.is_set() and fragment_interval:
|
||||
yield output_sr, np.zeros(int(output_sr * fragment_interval), dtype=np.int16)
|
||||
t4 = time.perf_counter()
|
||||
else:
|
||||
# refer_audio_spec: torch.Tensor = [
|
||||
# item.to(dtype=self.precision, device=self.configs.device)
|
||||
# for item in self.prompt_cache["refer_spec"]
|
||||
# ]
|
||||
window_schedule = None
|
||||
if gpt_streaming:
|
||||
if inputs.get("streaming_window_mode", "fixed") == "hybrid":
|
||||
window_schedule = StreamingWindowSchedule(min_chunk_length, min_chunk_length * 4)
|
||||
streaming_tokens = semantic_updates(self, all_phoneme_ids[0], prompt,
|
||||
all_bert_features[0], stream_options, stream_stop)
|
||||
semantic_token_generator = v2_token_chunks(streaming_tokens, min_chunk_length,
|
||||
self.configs.mute_emb_sim_matrix if not fixed_length_chunk else None,
|
||||
chunk_split_thershold, schedule=window_schedule)
|
||||
else:
|
||||
semantic_token_generator =self.t2s_model.model.infer_panel(
|
||||
all_phoneme_ids[0].unsqueeze(0),
|
||||
all_phoneme_lens,
|
||||
prompt,
|
||||
all_bert_features[0].unsqueeze(0),
|
||||
top_k=top_k,
|
||||
top_p=top_p,
|
||||
temperature=temperature,
|
||||
early_stop_num=self.configs.hz * self.configs.max_sec,
|
||||
max_len=max_len,
|
||||
repetition_penalty=repetition_penalty,
|
||||
streaming_mode=True,
|
||||
chunk_length=min_chunk_length,
|
||||
mute_emb_sim_matrix=self.configs.mute_emb_sim_matrix if not fixed_length_chunk else None,
|
||||
chunk_split_thershold=chunk_split_thershold,
|
||||
)
|
||||
semantic_token_generator =self.t2s_model.model.infer_panel(
|
||||
all_phoneme_ids[0].unsqueeze(0),
|
||||
all_phoneme_lens,
|
||||
prompt,
|
||||
all_bert_features[0].unsqueeze(0),
|
||||
top_k=top_k,
|
||||
top_p=top_p,
|
||||
temperature=temperature,
|
||||
early_stop_num=self.configs.hz * self.configs.max_sec,
|
||||
max_len=max_len,
|
||||
repetition_penalty=repetition_penalty,
|
||||
streaming_mode=True,
|
||||
chunk_length=min_chunk_length,
|
||||
mute_emb_sim_matrix=self.configs.mute_emb_sim_matrix if not fixed_length_chunk else None,
|
||||
chunk_split_thershold=chunk_split_thershold,
|
||||
)
|
||||
t4 = time.perf_counter()
|
||||
t_34 += t4 - t3
|
||||
phones = batch_phones[0].unsqueeze(0).to(self.configs.device)
|
||||
@ -1653,8 +1575,7 @@ class TTS:
|
||||
result_length=semantic_tokens.shape[-1]+overlap_len if not is_first_chunk else None,
|
||||
overlap_frames=last_latent[:,:,-overlap_len*(2 if self.vits_model.semantic_frame_rate == "25hz" else 1):] \
|
||||
if last_latent is not None else None,
|
||||
padding_length=token_padding_length,
|
||||
**({"generator": acoustic_rng} if gpt_streaming else {})
|
||||
padding_length=token_padding_length
|
||||
)
|
||||
audio_chunk=audio_chunk.detach()[0, 0, :]
|
||||
else:
|
||||
@ -1669,7 +1590,7 @@ class TTS:
|
||||
audio_chunk_ = audio_chunk_[:-overlap_size]
|
||||
elif is_first_chunk and is_final:
|
||||
is_first_chunk = False
|
||||
elif not is_first_chunk and (not is_final or gpt_streaming):
|
||||
elif not is_first_chunk and not is_final:
|
||||
audio_chunk_ = self.sola_algorithm([last_audio_chunk, audio_chunk_], overlap_size)
|
||||
audio_chunk_ = (
|
||||
audio_chunk_[last_audio_chunk.shape[0]-overlap_size:-overlap_size] if not is_final \
|
||||
@ -1678,7 +1599,7 @@ class TTS:
|
||||
|
||||
last_latent = latent
|
||||
last_audio_chunk = audio_chunk
|
||||
audio_result = self.audio_postprocess(
|
||||
yield self.audio_postprocess(
|
||||
[[audio_chunk_]],
|
||||
output_sr,
|
||||
None,
|
||||
@ -1687,21 +1608,13 @@ class TTS:
|
||||
0.0,
|
||||
super_sampling if self.configs.use_vocoder and self.configs.version == "v3" else False,
|
||||
)
|
||||
if window_schedule is not None:
|
||||
window_schedule.observe(semantic_tokens.numel(), audio_result[1].size, output_sr)
|
||||
yield audio_result
|
||||
|
||||
if is_first_package:
|
||||
print(f"first_package_delay: {time.perf_counter()-t0:.3f}")
|
||||
is_first_package = False
|
||||
|
||||
|
||||
if gpt_streaming:
|
||||
semantic_token_generator.close()
|
||||
streaming_tokens.close()
|
||||
semantic_token_generator = streaming_tokens = None
|
||||
if not gpt_streaming or not stream_stop.is_set():
|
||||
yield output_sr, np.zeros(int(output_sr*fragment_interval), dtype=np.int16)
|
||||
yield output_sr, np.zeros(int(output_sr*fragment_interval), dtype=np.int16)
|
||||
|
||||
t5 = time.perf_counter()
|
||||
t_45 += t5 - t4
|
||||
@ -1740,8 +1653,6 @@ class TTS:
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
if gpt_streaming:
|
||||
raise
|
||||
traceback.print_exc()
|
||||
# 必须返回一个空音频, 否则会导致显存不释放。
|
||||
yield 16000, np.zeros(int(16000), dtype=np.int16)
|
||||
@ -1754,11 +1665,6 @@ class TTS:
|
||||
self.init_vits_weights(self.configs.vits_weights_path)
|
||||
raise e
|
||||
finally:
|
||||
if gpt_streaming:
|
||||
stream_stop.set()
|
||||
for iterator in (streaming_audio, semantic_token_generator, streaming_tokens):
|
||||
if iterator is not None:
|
||||
iterator.close()
|
||||
self.empty_cache()
|
||||
|
||||
def empty_cache(self):
|
||||
|
||||
@ -100,8 +100,8 @@ cnhubert.cnhubert_base_path = cnhubert_base_path
|
||||
import random
|
||||
|
||||
from tools.acceleration import create_acceleration, cuda_graph_available, flash_attention_available
|
||||
from module.models import Generator, SynthesizerTrn, SynthesizerTrnV3
|
||||
from module.models_v5 import synthesize_v5_mel, V5_VERSIONS
|
||||
from module.models import CFM, Generator, SynthesizerTrn, SynthesizerTrnV3
|
||||
from module.models_v5 import V5_VERSIONS
|
||||
|
||||
|
||||
def set_seed(seed):
|
||||
@ -283,14 +283,14 @@ def change_sovits_weights(sovits_path, prompt_language=None, text_language=None)
|
||||
{
|
||||
"__type__": "update",
|
||||
"visible": visible_sample_steps,
|
||||
"value": (4 if model_version == "v5turbo" else 32 if model_version in {"v3", "v5dev"} else 8),
|
||||
"value": (32 if model_version == "v3" else 8),
|
||||
"choices": [4, 8, 16, 32],
|
||||
},
|
||||
{"__type__": "update", "visible": visible_inp_refs},
|
||||
{"__type__": "update", "value": False, "interactive": True if model_version not in v3v4set else False},
|
||||
{"__type__": "update", "visible": True if model_version == "v3" else False},
|
||||
{"__type__": "update", "value": i18n("模型加载中,请等待"), "interactive": False},
|
||||
{"__type__": "update", "visible": model_version in V5_VERSIONS, "value": (1.30 if model_version == "v5dev" else 0.0)},
|
||||
{"__type__": "update", "visible": False, "value": 0.0, "interactive": False},
|
||||
)
|
||||
|
||||
dict_s2 = load_sovits_new(sovits_path)
|
||||
@ -355,6 +355,10 @@ def change_sovits_weights(sovits_path, prompt_language=None, text_language=None)
|
||||
# torch.save(vq_model.state_dict(),"merge_win.pth")
|
||||
vq_model.eval()
|
||||
|
||||
if model_version in V5_VERSIONS:
|
||||
vq_model.cfm.noise_temperature = None
|
||||
vq_model.cfm.use_static_cache = False
|
||||
|
||||
yield (
|
||||
{"__type__": "update", "choices": list(dict_language.keys())},
|
||||
{"__type__": "update", "choices": list(dict_language.keys())},
|
||||
@ -365,14 +369,14 @@ def change_sovits_weights(sovits_path, prompt_language=None, text_language=None)
|
||||
{
|
||||
"__type__": "update",
|
||||
"visible": visible_sample_steps,
|
||||
"value": (4 if model_version == "v5turbo" else 32 if model_version in {"v3", "v5dev"} else 8),
|
||||
"value": (32 if model_version == "v3" else 8),
|
||||
"choices": [4, 8, 16, 32],
|
||||
},
|
||||
{"__type__": "update", "visible": visible_inp_refs},
|
||||
{"__type__": "update", "value": False, "interactive": True if model_version not in v3v4set else False},
|
||||
{"__type__": "update", "visible": True if model_version == "v3" else False},
|
||||
{"__type__": "update", "value": i18n("合成语音"), "interactive": True},
|
||||
{"__type__": "update", "visible": model_version in V5_VERSIONS, "value": (1.30 if model_version == "v5dev" else 0.0)},
|
||||
{"__type__": "update", "visible": False, "value": 0.0, "interactive": False},
|
||||
)
|
||||
with open("./weight.json") as f:
|
||||
data = f.read()
|
||||
@ -492,13 +496,12 @@ def init_hifigan():
|
||||
hifigan_model.eval()
|
||||
hifigan_model.remove_weight_norm()
|
||||
state_dict_g = torch.load(
|
||||
"%s/GPT_SoVITS/pretrained_models/gsv-%s-pretrained/vocoder.pth" %
|
||||
(now_dir, "v5" if model_version in V5_VERSIONS else model_version),
|
||||
"%s/GPT_SoVITS/pretrained_models/gsv-v4-pretrained/vocoder.pth" % (now_dir,),
|
||||
map_location="cpu",
|
||||
weights_only=False,
|
||||
)
|
||||
print("loading vocoder", hifigan_model.load_state_dict(state_dict_g))
|
||||
hifigan_version = model_version
|
||||
hifigan_version = "v4"
|
||||
clean_bigvgan_model()
|
||||
clean_sv_cn_model()
|
||||
if is_half == True:
|
||||
@ -865,9 +868,9 @@ def get_tts_wav(
|
||||
):
|
||||
global cache, t2s_model_cudagraph
|
||||
if sample_steps is None:
|
||||
sample_steps = 4 if model_version == "v5turbo" else 32 if model_version in {"v3", "v5dev"} else 8
|
||||
sample_steps = 32 if model_version == "v3" else 8
|
||||
if cfg_rate is None:
|
||||
cfg_rate = 1.30 if model_version == "v5dev" else 0.0
|
||||
cfg_rate = 0.0
|
||||
sample_steps, cfg_rate = int(sample_steps), float(cfg_rate)
|
||||
if ref_wav_path:
|
||||
pass
|
||||
@ -1081,30 +1084,27 @@ def get_tts_wav(
|
||||
fea_todo, ge = vq_model.decode_encp(pred_semantic, phoneme_ids1, refer, ge, speed)
|
||||
cfm_resss = []
|
||||
idx = 0
|
||||
if model_version in V5_VERSIONS:
|
||||
cfm_res = synthesize_v5_mel(vq_model, fea_ref, fea_todo, mel2, sample_steps, cfg_rate)
|
||||
cfm_res = cfm_res.to(fea_todo.dtype)
|
||||
else:
|
||||
while 1:
|
||||
fea_todo_chunk = fea_todo[:, :, idx : idx + chunk_len]
|
||||
if fea_todo_chunk.shape[-1] == 0:
|
||||
break
|
||||
idx += chunk_len
|
||||
fea = torch.cat([fea_ref, fea_todo_chunk], 2).transpose(2, 1)
|
||||
cfm_res = vq_model.cfm.inference(
|
||||
fea, torch.LongTensor([fea.size(1)]).to(fea.device), mel2, sample_steps, inference_cfg_rate=0
|
||||
)
|
||||
cfm_res = cfm_res[:, :, mel2.shape[2] :]
|
||||
mel2 = cfm_res[:, :, -T_min:]
|
||||
fea_ref = fea_todo_chunk[:, :, -T_min:]
|
||||
cfm_resss.append(cfm_res)
|
||||
cfm_res = torch.cat(cfm_resss, 2)
|
||||
while 1:
|
||||
fea_todo_chunk = fea_todo[:, :, idx : idx + chunk_len]
|
||||
if fea_todo_chunk.shape[-1] == 0:
|
||||
break
|
||||
idx += chunk_len
|
||||
fea = torch.cat([fea_ref, fea_todo_chunk], 2).transpose(2, 1)
|
||||
cfm_res = CFM.inference(
|
||||
vq_model.cfm, fea, torch.LongTensor([fea.size(1)]).to(fea.device), mel2, sample_steps,
|
||||
temperature=1.0, inference_cfg_rate=0,
|
||||
)
|
||||
cfm_res = cfm_res[:, :, mel2.shape[2] :]
|
||||
mel2 = cfm_res[:, :, -T_min:]
|
||||
fea_ref = fea_todo_chunk[:, :, -T_min:]
|
||||
cfm_resss.append(cfm_res)
|
||||
cfm_res = torch.cat(cfm_resss, 2)
|
||||
cfm_res = denorm_spec(cfm_res)
|
||||
if model_version == "v3":
|
||||
if bigvgan_model == None:
|
||||
init_bigvgan()
|
||||
else: # v4
|
||||
if hifigan_model is None or hifigan_version != model_version:
|
||||
if hifigan_model is None or hifigan_version != "v4":
|
||||
init_hifigan()
|
||||
vocoder_model = bigvgan_model if model_version == "v3" else hifigan_model
|
||||
with torch.inference_mode():
|
||||
@ -1299,7 +1299,7 @@ with gr.Blocks(title="GPT-SoVITS WebUI", analytics_enabled=False, js=js, css=css
|
||||
with gr.Column(scale=13):
|
||||
ref_text_free = gr.Checkbox(
|
||||
label=i18n("开启无参考文本模式。不填参考文本亦相当于开启。")
|
||||
+ i18n("v3暂不支持该模式,使用了会报错。"),
|
||||
+ i18n("v3、v4、v5 均不支持无参考文本模式,请填写参考文本!"),
|
||||
value=False,
|
||||
interactive=True if model_version not in v3v4set else False,
|
||||
show_label=True,
|
||||
@ -1322,8 +1322,8 @@ with gr.Blocks(title="GPT-SoVITS WebUI", analytics_enabled=False, js=js, css=css
|
||||
)
|
||||
cfg_rate = gr.Slider(
|
||||
minimum=0, maximum=2, step=0.05,
|
||||
value=(1.30 if model_version == "v5dev" else 0.0), label="CFG",
|
||||
visible=model_version in V5_VERSIONS, interactive=True, scale=1,
|
||||
value=0.0, label="CFG",
|
||||
visible=False, interactive=False, scale=1,
|
||||
)
|
||||
inp_refs = (
|
||||
gr.File(
|
||||
@ -1344,7 +1344,7 @@ with gr.Blocks(title="GPT-SoVITS WebUI", analytics_enabled=False, js=js, css=css
|
||||
sample_steps = (
|
||||
gr.Radio(
|
||||
label=i18n("采样步数,如果觉得电,提高试试,如果觉得慢,降低试试"),
|
||||
value=(4 if model_version == "v5turbo" else 32 if model_version in {"v3", "v5dev"} else 8),
|
||||
value=(32 if model_version == "v3" else 8),
|
||||
choices=[4, 8, 16, 32],
|
||||
visible=True,
|
||||
)
|
||||
@ -1353,7 +1353,7 @@ with gr.Blocks(title="GPT-SoVITS WebUI", analytics_enabled=False, js=js, css=css
|
||||
label=i18n("采样步数,如果觉得电,提高试试,如果觉得慢,降低试试"),
|
||||
choices=[4, 8, 16, 32],
|
||||
visible=False,
|
||||
value=(4 if model_version == "v5turbo" else 32 if model_version in {"v3", "v5dev"} else 8),
|
||||
value=(32 if model_version == "v3" else 8),
|
||||
)
|
||||
)
|
||||
if_sr_Checkbox = gr.Checkbox(
|
||||
|
||||
@ -213,7 +213,7 @@ def inference(
|
||||
for item in tts_pipeline.run(inputs):
|
||||
yield item, actual_seed
|
||||
except NO_PROMPT_ERROR:
|
||||
gr.Warning(i18n("V3不支持无参考文本模式,请填写参考文本!"))
|
||||
gr.Warning(i18n("v3、v4、v5 均不支持无参考文本模式,请填写参考文本!"))
|
||||
|
||||
|
||||
def custom_sort_key(s):
|
||||
@ -373,7 +373,8 @@ with gr.Blocks(title="GPT-SoVITS WebUI", analytics_enabled=False, js=js, css=css
|
||||
)
|
||||
with gr.Column():
|
||||
ref_text_free = gr.Checkbox(
|
||||
label=i18n("开启无参考文本模式。不填参考文本亦相当于开启。"),
|
||||
label=i18n("开启无参考文本模式。不填参考文本亦相当于开启。")
|
||||
+ i18n("v3、v4、v5 均不支持无参考文本模式,请填写参考文本!"),
|
||||
value=False,
|
||||
interactive=True if model_version != "v3" else False,
|
||||
show_label=True,
|
||||
|
||||
@ -1,133 +0,0 @@
|
||||
import math
|
||||
import os
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
|
||||
V5_TEMPERATURE = 0.875
|
||||
V5_REFERENCE_ANCHOR = 1.0
|
||||
V5_ROLLING_TAIL_FRAMES = 32
|
||||
V5_MAX_TARGET_CHUNK_FRAMES = 640
|
||||
V5_REFERENCE_FRAMES = 500
|
||||
V5_TOTAL_CHUNK_FRAMES = 1000
|
||||
V5_DEFAULT_CFG = 1.30
|
||||
V5_REQUEST_DEFAULT_CFG = 0.0
|
||||
V5_VERSIONS = {"v5", "v5dev", "v5turbo"}
|
||||
|
||||
|
||||
def sampling_defaults(version, legacy_steps=32):
|
||||
if version == "v5turbo":
|
||||
return 4, 0.0
|
||||
if version in V5_VERSIONS:
|
||||
return 32, 1.30
|
||||
return legacy_steps, 0.0
|
||||
|
||||
|
||||
def resolve_sampling(version, sample_steps=None, cfg_rate=None, legacy_steps=32):
|
||||
steps, cfg = sampling_defaults(version, legacy_steps)
|
||||
return (steps if sample_steps is None else int(sample_steps),
|
||||
cfg if cfg_rate is None else float(cfg_rate))
|
||||
|
||||
|
||||
def validate_v5_sampling(sample_steps, cfg_rate):
|
||||
steps = int(sample_steps)
|
||||
cfg = float(cfg_rate)
|
||||
if steps not in (4, 8, 16, 32):
|
||||
raise ValueError("Euler steps must be one of 4, 8, 16, 32")
|
||||
if not math.isfinite(cfg) or cfg < 0 or cfg > 2:
|
||||
raise ValueError("CFG must be a finite value between 0 and 2")
|
||||
return steps, cfg
|
||||
|
||||
|
||||
class CFMV5(nn.Module):
|
||||
def __init__(self, in_channels, dit):
|
||||
super().__init__()
|
||||
self.in_channels = in_channels
|
||||
self.estimator = dit
|
||||
self.use_conditioner_cache = True
|
||||
self.use_static_cache = os.environ.get(
|
||||
"GSVV5_CFM_STATIC_CACHE", os.environ.get("GSVV4_CFM_STATIC_CACHE", "1")
|
||||
) != "0"
|
||||
|
||||
@torch.inference_mode()
|
||||
def inference(self, mu, x_lens, prompt, n_timesteps, temperature=V5_TEMPERATURE,
|
||||
inference_cfg_rate=V5_DEFAULT_CFG):
|
||||
steps, cfg = validate_v5_sampling(n_timesteps, inference_cfg_rate)
|
||||
batch, frames = mu.shape[:2]
|
||||
prompt_len = prompt.shape[-1]
|
||||
# Match v4: noise, time and model inputs follow mu/model dtype.
|
||||
state_dtype = mu.dtype
|
||||
model_dtype = mu.dtype
|
||||
x = torch.randn(batch, self.in_channels, frames, device=mu.device, dtype=state_dtype) * V5_TEMPERATURE
|
||||
x[..., :prompt_len] = 0
|
||||
prompt_x = torch.zeros_like(x, dtype=model_dtype)
|
||||
prompt_x[..., :prompt_len] = prompt.to(model_dtype)
|
||||
condition = mu.transpose(2, 1)
|
||||
cache_enabled = bool(self.use_conditioner_cache and self.use_static_cache)
|
||||
cache = (self.estimator.prepare_static_cache(prompt_x, x_lens, condition)
|
||||
if cache_enabled else None)
|
||||
text_cache = None
|
||||
step = 1.0 / steps
|
||||
for index in range(steps):
|
||||
time = torch.full((batch,), index * step, device=mu.device, dtype=state_dtype)
|
||||
velocity, text_embedding, _ = self.estimator(
|
||||
x, prompt_x, x_lens, time, None, condition,
|
||||
drop_audio_cond=False, drop_text=False, static_cache=cache,
|
||||
infer=True, text_cache=text_cache,
|
||||
)
|
||||
if self.use_conditioner_cache and cache is None:
|
||||
text_cache = text_embedding
|
||||
velocity = velocity.transpose(2, 1)
|
||||
if cfg > 1e-5:
|
||||
negative, _, _ = self.estimator(
|
||||
x, prompt_x, x_lens, time, None, condition,
|
||||
drop_audio_cond=True, drop_text=False, static_cache=cache,
|
||||
infer=True, text_cache=text_cache,
|
||||
)
|
||||
negative = negative.transpose(2, 1)
|
||||
velocity = velocity + cfg * (velocity - negative)
|
||||
x = x + step * velocity
|
||||
x[..., :prompt_len] = 0
|
||||
return x
|
||||
|
||||
|
||||
@torch.inference_mode()
|
||||
def synthesize_v5_mel(model, reference_features, target_features, reference_mel,
|
||||
sample_steps=None, cfg_rate=None):
|
||||
sample_steps, cfg_rate = resolve_sampling(model.version, sample_steps, cfg_rate)
|
||||
steps, cfg = validate_v5_sampling(sample_steps, cfg_rate)
|
||||
reference_frames = min(reference_mel.shape[-1], reference_features.shape[-1])
|
||||
if reference_frames < 1:
|
||||
raise ValueError("V5 requires nonempty reference mel and semantic features")
|
||||
reference_mel = reference_mel[..., :reference_frames][..., -V5_REFERENCE_FRAMES:]
|
||||
reference_features = reference_features[..., :reference_frames][..., -V5_REFERENCE_FRAMES:]
|
||||
reference_frames = reference_mel.shape[-1]
|
||||
chunk_frames = min(V5_TOTAL_CHUNK_FRAMES - reference_frames, V5_MAX_TARGET_CHUNK_FRAMES)
|
||||
original_mel = reference_mel
|
||||
original_features = reference_features
|
||||
rolling_mel = reference_mel
|
||||
rolling_features = reference_features
|
||||
results = []
|
||||
for index, start in enumerate(range(0, target_features.shape[-1], chunk_frames)):
|
||||
target = target_features[..., start:start + chunk_frames]
|
||||
if index == 0:
|
||||
prompt = original_mel
|
||||
features = original_features
|
||||
else:
|
||||
tail = min(V5_ROLLING_TAIL_FRAMES, reference_frames,
|
||||
rolling_mel.shape[-1], rolling_features.shape[-1])
|
||||
prefix = reference_frames - tail
|
||||
prompt = torch.cat((original_mel[..., :prefix], rolling_mel[..., -tail:]), dim=-1)
|
||||
features = torch.cat((original_features[..., :prefix], rolling_features[..., -tail:]), dim=-1)
|
||||
mu = torch.cat((features, target), dim=-1).transpose(2, 1)
|
||||
lengths = torch.full((mu.shape[0],), mu.shape[1], dtype=torch.long, device=mu.device)
|
||||
generated = model.cfm.inference(
|
||||
mu, lengths, prompt, steps, inference_cfg_rate=cfg,
|
||||
)[..., prompt.shape[-1]:]
|
||||
results.append(generated)
|
||||
rolling_mel = generated[..., -reference_frames:].to(reference_mel.dtype)
|
||||
rolling_features = target[..., -reference_frames:]
|
||||
if not results:
|
||||
raise ValueError("V5 requires nonempty target semantic features")
|
||||
return torch.cat(results, dim=-1)
|
||||
2
webui.py
2
webui.py
@ -1496,7 +1496,7 @@ with gr.Blocks(title="GPT-SoVITS WebUI", analytics_enabled=False, js=js, css=css
|
||||
version_checkbox = gr.Radio(
|
||||
label=i18n("训练模型的版本"),
|
||||
value=version,
|
||||
choices=["v2", "v2Pro", "v2ProPlus", "v4", "v5dev", "v5turbo"],
|
||||
choices=["v2", "v2Pro", "v2ProPlus", "v4"],
|
||||
scale=5,
|
||||
)
|
||||
with gr.Accordion(label=i18n("预训练模型路径"), open=False):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user