Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs shape quantize, outs #175

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions models/codec/ns3_codec/facodec.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,17 +412,28 @@ def quantize(self, x, n_quantizers=None):
f0_input = x # (B, d, T)
f0_quantizer = self.quantizer[0]
out, q, commit, quantized = f0_quantizer(f0_input, n_quantizers=n_quantizers)
outs += out
qs.append(q)
quantized_buf.append(quantized.sum(0))
commit_loss.append(commit)
if outs == 0:
outs += out
qs.append(q)
quantized_buf.append(quantized.sum(0))
commit_loss.append(commit)
else:
out = out[:, :, : outs.size(2)]
q = q[:, :, : outs.size(2)]
quantized = quantized[:, :, :, : outs.size(2)]
qs.append(q)
quantized_buf.append(quantized.sum(0))
commit_loss.append(commit)

# phone
phone_input = x
phone_quantizer = self.quantizer[1]
out, q, commit, quantized = phone_quantizer(
phone_input, n_quantizers=n_quantizers
)
out = out[:, :, : outs.size(2)]
q = q[:, :, : outs.size(2)]
quantized = quantized[:, :, :, : outs.size(2)]
outs += out
qs.append(q)
quantized_buf.append(quantized.sum(0))
Expand All @@ -431,10 +442,15 @@ def quantize(self, x, n_quantizers=None):
# residual
if self.vq_num_q_r > 0:
residual_quantizer = self.quantizer[2]
if x.shape != quantized_buf[0].shape:
x = x[:, :, : quantized_buf[0].size(2)]
residual_input = x - (quantized_buf[0] + quantized_buf[1]).detach()
out, q, commit, quantized = residual_quantizer(
residual_input, n_quantizers=n_quantizers
)
out = out[:, :, : outs.size(2)]
q = q[:, :, : outs.size(2)]
quantized = quantized[:, :, :, : outs.size(2)]
outs += out
qs.append(q)
quantized_buf.append(quantized.sum(0)) # [L, B, C, T] -> [B, C, T]
Expand Down Expand Up @@ -694,7 +710,6 @@ def forward(
speaker_embedding,
use_residual_code=False,
):

x = 0

x_p = 0
Expand All @@ -713,7 +728,6 @@ def forward(
x = x + x_c

if use_residual_code:

x_r = 0
for i in range(self.vq_num_q_r):
x_r = x_r + self.residual_embs[i](
Expand All @@ -732,7 +746,6 @@ def forward(
return x

def vq2emb(self, vq, speaker_embedding, use_residual=True):

out = 0

x_t = 0
Expand Down Expand Up @@ -1034,17 +1047,28 @@ def quantize(self, x, prosody_feature, n_quantizers=None):
f0_input = f0_input.transpose(1, 2)
f0_quantizer = self.quantizer[0]
out, q, commit, quantized = f0_quantizer(f0_input, n_quantizers=n_quantizers)
outs += out
qs.append(q)
quantized_buf.append(quantized.sum(0))
commit_loss.append(commit)
if outs == 0:
outs += out
qs.append(q)
quantized_buf.append(quantized.sum(0))
commit_loss.append(commit)
else:
out = out[:, :, : outs.size(2)]
q = q[:, :, : outs.size(2)]
quantized = quantized[:, :, :, : outs.size(2)]
qs.append(q)
quantized_buf.append(quantized.sum(0))
commit_loss.append(commit)

# phone
phone_input = x
phone_quantizer = self.quantizer[1]
out, q, commit, quantized = phone_quantizer(
phone_input, n_quantizers=n_quantizers
)
out = out[:, :, : outs.size(2)]
q = q[:, :, : outs.size(2)]
quantized = quantized[:, :, :, : outs.size(2)]
outs += out
qs.append(q)
quantized_buf.append(quantized.sum(0))
Expand All @@ -1053,10 +1077,15 @@ def quantize(self, x, prosody_feature, n_quantizers=None):
# residual
if self.vq_num_q_r > 0:
residual_quantizer = self.quantizer[2]
if x.shape != quantized_buf[0].shape:
x = x[:, :, : quantized_buf[0].size(2)]
residual_input = x - (quantized_buf[0] + quantized_buf[1]).detach()
out, q, commit, quantized = residual_quantizer(
residual_input, n_quantizers=n_quantizers
)
out = out[:, :, : outs.size(2)]
q = q[:, :, : outs.size(2)]
quantized = quantized[:, :, :, : outs.size(2)]
outs += out
qs.append(q)
quantized_buf.append(quantized.sum(0)) # [L, B, C, T] -> [B, C, T]
Expand Down
Loading