fix: device mismatch error in embedding loading on branch dev (#737)

* fix device mismatch bug

* fix additional device mismatch bug when zero-shot

* chore(format): run black on dev

---------

Co-authored-by: dragon <com888yy@gmail.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Iris Sally
2024-08-31 23:04:22 +08:00
committed by GitHub
parent 024f93e36e
commit a82e1df50d
3 changed files with 10 additions and 4 deletions
+3 -2
View File
@@ -272,6 +272,7 @@ class Chat:
vq_config=asdict(self.config.dvae.vq),
dim=self.config.dvae.decoder.idim,
coef=coef,
device=self.device,
)
.to(device)
.eval()
@@ -288,8 +289,8 @@ class Chat:
self.config.embed.num_text_tokens,
self.config.embed.num_vq,
)
embed.from_pretrained(embed_path)
self.embed = embed
embed.from_pretrained(embed_path, device=self.device)
self.embed = embed.to(self.device)
self.logger.log(logging.INFO, "embed loaded.")
gpt = GPT(
+5 -1
View File
@@ -179,8 +179,10 @@ class MelSpectrogramFeatures(torch.nn.Module):
hop_length=256,
n_mels=100,
padding: Literal["center", "same"] = "center",
device: torch.device = torch.device("cuda"),
):
super().__init__()
self.device = device
if padding not in ["center", "same"]:
raise ValueError("Padding must be 'center' or 'same'.")
self.padding = padding
@@ -197,6 +199,7 @@ class MelSpectrogramFeatures(torch.nn.Module):
return super().__call__(audio)
def forward(self, audio: torch.Tensor) -> torch.Tensor:
audio = audio.to(self.device)
mel: torch.Tensor = self.mel_spec(audio)
features = torch.log(torch.clip(mel, min=1e-5))
return features
@@ -210,6 +213,7 @@ class DVAE(nn.Module):
vq_config: Optional[dict] = None,
dim=512,
coef: Optional[str] = None,
device: torch.device = torch.device("cuda"),
):
super().__init__()
if coef is None:
@@ -227,7 +231,7 @@ class DVAE(nn.Module):
nn.Conv1d(dim, dim, 4, 2, 1),
nn.GELU(),
)
self.preprocessor_mel = MelSpectrogramFeatures()
self.preprocessor_mel = MelSpectrogramFeatures(device=device)
self.encoder: Optional[DVAEDecoder] = DVAEDecoder(**encoder_config)
self.decoder = DVAEDecoder(**decoder_config)
+2 -1
View File
@@ -34,12 +34,13 @@ class Embed(nn.Module):
)
@torch.inference_mode()
def from_pretrained(self, filename: str):
def from_pretrained(self, filename: str, device: torch.device):
state_dict_tensors = {}
with safe_open(filename, framework="pt") as f:
for k in f.keys():
state_dict_tensors[k] = f.get_tensor(k)
self.load_state_dict(state_dict_tensors)
self.to(device)
def __call__(
self, input_ids: torch.Tensor, text_mask: torch.Tensor