fix: load weights only in torch.load

This commit is contained in:
shavit 2024-08-31 06:15:24 -04:00 committed by Enno Hermann
parent 233dfb54ae
commit 17ca24c3d6
15 changed files with 27 additions and 22 deletions

View File

@ -118,7 +118,7 @@ def load_model(ckpt_path, device, config, model_type="text"):
logger.info(f"{model_type} model not found, downloading...") logger.info(f"{model_type} model not found, downloading...")
_download(config.REMOTE_MODEL_PATHS[model_type]["path"], ckpt_path, config.CACHE_DIR) _download(config.REMOTE_MODEL_PATHS[model_type]["path"], ckpt_path, config.CACHE_DIR)
checkpoint = torch.load(ckpt_path, map_location=device) checkpoint = torch.load(ckpt_path, map_location=device, weights_only=True)
# this is a hack # this is a hack
model_args = checkpoint["model_args"] model_args = checkpoint["model_args"]
if "input_vocab_size" not in model_args: if "input_vocab_size" not in model_args:

View File

@ -332,7 +332,7 @@ class TorchMelSpectrogram(nn.Module):
self.mel_norm_file = mel_norm_file self.mel_norm_file = mel_norm_file
if self.mel_norm_file is not None: if self.mel_norm_file is not None:
with fsspec.open(self.mel_norm_file) as f: with fsspec.open(self.mel_norm_file) as f:
self.mel_norms = torch.load(f) self.mel_norms = torch.load(f, weights_only=True)
else: else:
self.mel_norms = None self.mel_norms = None

View File

@ -124,7 +124,7 @@ def load_voice(voice: str, extra_voice_dirs: List[str] = []):
voices = get_voices(extra_voice_dirs) voices = get_voices(extra_voice_dirs)
paths = voices[voice] paths = voices[voice]
if len(paths) == 1 and paths[0].endswith(".pth"): if len(paths) == 1 and paths[0].endswith(".pth"):
return None, torch.load(paths[0]) return None, torch.load(paths[0], weights_only=True)
else: else:
conds = [] conds = []
for cond_path in paths: for cond_path in paths:

View File

@ -46,7 +46,7 @@ def dvae_wav_to_mel(
mel = mel_stft(wav) mel = mel_stft(wav)
mel = torch.log(torch.clamp(mel, min=1e-5)) mel = torch.log(torch.clamp(mel, min=1e-5))
if mel_norms is None: if mel_norms is None:
mel_norms = torch.load(mel_norms_file, map_location=device) mel_norms = torch.load(mel_norms_file, map_location=device, weights_only=True)
mel = mel / mel_norms.unsqueeze(0).unsqueeze(-1) mel = mel / mel_norms.unsqueeze(0).unsqueeze(-1)
return mel return mel

View File

@ -328,7 +328,7 @@ class HifiganGenerator(torch.nn.Module):
def load_checkpoint( def load_checkpoint(
self, config, checkpoint_path, eval=False, cache=False self, config, checkpoint_path, eval=False, cache=False
): # pylint: disable=unused-argument, redefined-builtin ): # pylint: disable=unused-argument, redefined-builtin
state = torch.load(checkpoint_path, map_location=torch.device("cpu")) state = torch.load(checkpoint_path, map_location=torch.device("cpu"), weights_only=True)
self.load_state_dict(state["model"]) self.load_state_dict(state["model"])
if eval: if eval:
self.eval() self.eval()

View File

@ -91,7 +91,7 @@ class GPTTrainer(BaseTTS):
# load GPT if available # load GPT if available
if self.args.gpt_checkpoint: if self.args.gpt_checkpoint:
gpt_checkpoint = torch.load(self.args.gpt_checkpoint, map_location=torch.device("cpu")) gpt_checkpoint = torch.load(self.args.gpt_checkpoint, map_location=torch.device("cpu"), weights_only=True)
# deal with coqui Trainer exported model # deal with coqui Trainer exported model
if "model" in gpt_checkpoint.keys() and "config" in gpt_checkpoint.keys(): if "model" in gpt_checkpoint.keys() and "config" in gpt_checkpoint.keys():
logger.info("Coqui Trainer checkpoint detected! Converting it!") logger.info("Coqui Trainer checkpoint detected! Converting it!")
@ -184,7 +184,7 @@ class GPTTrainer(BaseTTS):
self.dvae.eval() self.dvae.eval()
if self.args.dvae_checkpoint: if self.args.dvae_checkpoint:
dvae_checkpoint = torch.load(self.args.dvae_checkpoint, map_location=torch.device("cpu")) dvae_checkpoint = torch.load(self.args.dvae_checkpoint, map_location=torch.device("cpu"), weights_only=True)
self.dvae.load_state_dict(dvae_checkpoint, strict=False) self.dvae.load_state_dict(dvae_checkpoint, strict=False)
logger.info("DVAE weights restored from: %s", self.args.dvae_checkpoint) logger.info("DVAE weights restored from: %s", self.args.dvae_checkpoint)
else: else:

View File

@ -3,7 +3,7 @@ import torch
class SpeakerManager: class SpeakerManager:
def __init__(self, speaker_file_path=None): def __init__(self, speaker_file_path=None):
self.speakers = torch.load(speaker_file_path) self.speakers = torch.load(speaker_file_path, weights_only=True)
@property @property
def name_to_id(self): def name_to_id(self):

View File

@ -107,7 +107,7 @@ class NeuralhmmTTS(BaseTTS):
def preprocess_batch(self, text, text_len, mels, mel_len): def preprocess_batch(self, text, text_len, mels, mel_len):
if self.mean.item() == 0 or self.std.item() == 1: if self.mean.item() == 0 or self.std.item() == 1:
statistics_dict = torch.load(self.mel_statistics_parameter_path) statistics_dict = torch.load(self.mel_statistics_parameter_path, weights_only=True)
self.update_mean_std(statistics_dict) self.update_mean_std(statistics_dict)
mels = self.normalize(mels) mels = self.normalize(mels)
@ -292,7 +292,7 @@ class NeuralhmmTTS(BaseTTS):
"Data parameters found for: %s. Loading mel normalization parameters...", "Data parameters found for: %s. Loading mel normalization parameters...",
trainer.config.mel_statistics_parameter_path, trainer.config.mel_statistics_parameter_path,
) )
statistics = torch.load(trainer.config.mel_statistics_parameter_path) statistics = torch.load(trainer.config.mel_statistics_parameter_path, weights_only=True)
data_mean, data_std, init_transition_prob = ( data_mean, data_std, init_transition_prob = (
statistics["mean"], statistics["mean"],
statistics["std"], statistics["std"],

View File

@ -120,7 +120,7 @@ class Overflow(BaseTTS):
def preprocess_batch(self, text, text_len, mels, mel_len): def preprocess_batch(self, text, text_len, mels, mel_len):
if self.mean.item() == 0 or self.std.item() == 1: if self.mean.item() == 0 or self.std.item() == 1:
statistics_dict = torch.load(self.mel_statistics_parameter_path) statistics_dict = torch.load(self.mel_statistics_parameter_path, weights_only=True)
self.update_mean_std(statistics_dict) self.update_mean_std(statistics_dict)
mels = self.normalize(mels) mels = self.normalize(mels)
@ -308,7 +308,7 @@ class Overflow(BaseTTS):
"Data parameters found for: %s. Loading mel normalization parameters...", "Data parameters found for: %s. Loading mel normalization parameters...",
trainer.config.mel_statistics_parameter_path, trainer.config.mel_statistics_parameter_path,
) )
statistics = torch.load(trainer.config.mel_statistics_parameter_path) statistics = torch.load(trainer.config.mel_statistics_parameter_path, weights_only=True)
data_mean, data_std, init_transition_prob = ( data_mean, data_std, init_transition_prob = (
statistics["mean"], statistics["mean"],
statistics["std"], statistics["std"],

View File

@ -170,7 +170,9 @@ def classify_audio_clip(clip, model_dir):
kernel_size=5, kernel_size=5,
distribute_zero_label=False, distribute_zero_label=False,
) )
classifier.load_state_dict(torch.load(os.path.join(model_dir, "classifier.pth"), map_location=torch.device("cpu"))) classifier.load_state_dict(
torch.load(os.path.join(model_dir, "classifier.pth"), map_location=torch.device("cpu"), weights_only=True)
)
clip = clip.cpu().unsqueeze(0) clip = clip.cpu().unsqueeze(0)
results = F.softmax(classifier(clip), dim=-1) results = F.softmax(classifier(clip), dim=-1)
return results[0][0] return results[0][0]
@ -488,6 +490,7 @@ class Tortoise(BaseTTS):
torch.load( torch.load(
os.path.join(self.models_dir, "rlg_auto.pth"), os.path.join(self.models_dir, "rlg_auto.pth"),
map_location=torch.device("cpu"), map_location=torch.device("cpu"),
weights_only=True,
) )
) )
self.rlg_diffusion = RandomLatentConverter(2048).eval() self.rlg_diffusion = RandomLatentConverter(2048).eval()
@ -495,6 +498,7 @@ class Tortoise(BaseTTS):
torch.load( torch.load(
os.path.join(self.models_dir, "rlg_diffuser.pth"), os.path.join(self.models_dir, "rlg_diffuser.pth"),
map_location=torch.device("cpu"), map_location=torch.device("cpu"),
weights_only=True,
) )
) )
with torch.no_grad(): with torch.no_grad():
@ -881,17 +885,17 @@ class Tortoise(BaseTTS):
if os.path.exists(ar_path): if os.path.exists(ar_path):
# remove keys from the checkpoint that are not in the model # remove keys from the checkpoint that are not in the model
checkpoint = torch.load(ar_path, map_location=torch.device("cpu")) checkpoint = torch.load(ar_path, map_location=torch.device("cpu"), weights_only=True)
# strict set False # strict set False
# due to removed `bias` and `masked_bias` changes in Transformers # due to removed `bias` and `masked_bias` changes in Transformers
self.autoregressive.load_state_dict(checkpoint, strict=False) self.autoregressive.load_state_dict(checkpoint, strict=False)
if os.path.exists(diff_path): if os.path.exists(diff_path):
self.diffusion.load_state_dict(torch.load(diff_path), strict=strict) self.diffusion.load_state_dict(torch.load(diff_path, weights_only=True), strict=strict)
if os.path.exists(clvp_path): if os.path.exists(clvp_path):
self.clvp.load_state_dict(torch.load(clvp_path), strict=strict) self.clvp.load_state_dict(torch.load(clvp_path, weights_only=True), strict=strict)
if os.path.exists(vocoder_checkpoint_path): if os.path.exists(vocoder_checkpoint_path):
self.vocoder.load_state_dict( self.vocoder.load_state_dict(
@ -899,6 +903,7 @@ class Tortoise(BaseTTS):
torch.load( torch.load(
vocoder_checkpoint_path, vocoder_checkpoint_path,
map_location=torch.device("cpu"), map_location=torch.device("cpu"),
weights_only=True,
) )
) )
) )

View File

@ -65,7 +65,7 @@ def wav_to_mel_cloning(
mel = mel_stft(wav) mel = mel_stft(wav)
mel = torch.log(torch.clamp(mel, min=1e-5)) mel = torch.log(torch.clamp(mel, min=1e-5))
if mel_norms is None: if mel_norms is None:
mel_norms = torch.load(mel_norms_file, map_location=device) mel_norms = torch.load(mel_norms_file, map_location=device, weights_only=True)
mel = mel / mel_norms.unsqueeze(0).unsqueeze(-1) mel = mel / mel_norms.unsqueeze(0).unsqueeze(-1)
return mel return mel

View File

@ -2,7 +2,7 @@ import torch
def rehash_fairseq_vits_checkpoint(checkpoint_file): def rehash_fairseq_vits_checkpoint(checkpoint_file):
chk = torch.load(checkpoint_file, map_location=torch.device("cpu"))["model"] chk = torch.load(checkpoint_file, map_location=torch.device("cpu"), weights_only=True)["model"]
new_chk = {} new_chk = {}
for k, v in chk.items(): for k, v in chk.items():
if "enc_p." in k: if "enc_p." in k:

View File

@ -17,7 +17,7 @@ def load_file(path: str):
return json.load(f) return json.load(f)
elif path.endswith(".pth"): elif path.endswith(".pth"):
with fsspec.open(path, "rb") as f: with fsspec.open(path, "rb") as f:
return torch.load(f, map_location="cpu") return torch.load(f, map_location="cpu", weights_only=True)
else: else:
raise ValueError("Unsupported file type") raise ValueError("Unsupported file type")

View File

@ -26,7 +26,7 @@ def get_wavlm(device="cpu"):
logger.info("Downloading WavLM model to %s ...", output_path) logger.info("Downloading WavLM model to %s ...", output_path)
urllib.request.urlretrieve(model_uri, output_path) urllib.request.urlretrieve(model_uri, output_path)
checkpoint = torch.load(output_path, map_location=torch.device(device)) checkpoint = torch.load(output_path, map_location=torch.device(device), weights_only=True)
cfg = WavLMConfig(checkpoint["cfg"]) cfg = WavLMConfig(checkpoint["cfg"])
wavlm = WavLM(cfg).to(device) wavlm = WavLM(cfg).to(device)
wavlm.load_state_dict(checkpoint["model"]) wavlm.load_state_dict(checkpoint["model"])

View File

@ -119,9 +119,9 @@
"\n", "\n",
"# load model state\n", "# load model state\n",
"if use_cuda:\n", "if use_cuda:\n",
" cp = torch.load(MODEL_PATH)\n", " cp = torch.load(MODEL_PATH, weights_only=True)\n",
"else:\n", "else:\n",
" cp = torch.load(MODEL_PATH, map_location=lambda storage, loc: storage)\n", " cp = torch.load(MODEL_PATH, map_location=lambda storage, loc: storage, weights_only=True)\n",
"\n", "\n",
"# load the model\n", "# load the model\n",
"model.load_state_dict(cp['model'])\n", "model.load_state_dict(cp['model'])\n",