[pre-commit.ci] pre-commit autoupdate (#243)

* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.1.7 → v0.1.8](https://github.com/astral-sh/ruff-pre-commit/compare/v0.1.7...v0.1.8)
- [github.com/psf/black: 23.11.0 → 23.12.0](https://github.com/psf/black/compare/23.11.0...23.12.0)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
pre-commit-ci[bot]
2023-12-19 18:32:07 +08:00
committed by GitHub
parent b8fcf2f66e
commit 5479e9039d
4 changed files with 67 additions and 39 deletions

View File

@@ -7,13 +7,13 @@ repos:
- id: trailing-whitespace - id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7 rev: v0.1.8
hooks: hooks:
- id: ruff - id: ruff
args: [ --fix ] args: [ --fix ]
- repo: https://github.com/psf/black - repo: https://github.com/psf/black
rev: 23.11.0 rev: 23.12.0
hooks: hooks:
- id: black - id: black

View File

@@ -1,17 +1,20 @@
import numpy as np import numpy as np
import onnxruntime as ort import onnxruntime as ort
def convert_pad_shape(pad_shape): def convert_pad_shape(pad_shape):
layer = pad_shape[::-1] layer = pad_shape[::-1]
pad_shape = [item for sublist in layer for item in sublist] pad_shape = [item for sublist in layer for item in sublist]
return pad_shape return pad_shape
def sequence_mask(length, max_length=None): def sequence_mask(length, max_length=None):
if max_length is None: if max_length is None:
max_length = length.max() max_length = length.max()
x = np.arange(max_length, dtype=length.dtype) x = np.arange(max_length, dtype=length.dtype)
return np.expand_dims(x, 0) < np.expand_dims(length, 1) return np.expand_dims(x, 0) < np.expand_dims(length, 1)
def generate_path(duration, mask): def generate_path(duration, mask):
""" """
duration: [b, 1, t_x] duration: [b, 1, t_x]
@@ -28,8 +31,9 @@ def generate_path(duration, mask):
path = np.expand_dims(path, 1).transpose(0, 1, 3, 2) path = np.expand_dims(path, 1).transpose(0, 1, 3, 2)
return path return path
class OnnxInferenceSession():
def __init__(self, path, Providers = ["CPUExecutionProvider"]): class OnnxInferenceSession:
def __init__(self, path, Providers=["CPUExecutionProvider"]):
self.enc = ort.InferenceSession(path["enc"], providers=Providers) self.enc = ort.InferenceSession(path["enc"], providers=Providers)
self.emb_g = ort.InferenceSession(path["emb_g"], providers=Providers) self.emb_g = ort.InferenceSession(path["emb_g"], providers=Providers)
self.dp = ort.InferenceSession(path["dp"], providers=Providers) self.dp = ort.InferenceSession(path["dp"], providers=Providers)
@@ -47,34 +51,47 @@ class OnnxInferenceSession():
bert_en, bert_en,
emo, emo,
sid, sid,
seed = 114514, seed=114514,
seq_noise_scale = 0.8, seq_noise_scale=0.8,
sdp_noise_scale = 0.6, sdp_noise_scale=0.6,
length_scale = 1., length_scale=1.0,
sdp_ratio = 0. sdp_ratio=0.0,
): ):
g = self.emb_g.run(None, {'sid': sid.astype(np.int64),})[0] g = self.emb_g.run(
None,
{
"sid": sid.astype(np.int64),
},
)[0]
g = np.expand_dims(g, -1) g = np.expand_dims(g, -1)
enc_rtn = self.enc.run( enc_rtn = self.enc.run(
None, None,
{ {
"x" : seq.astype(np.int64), "x": seq.astype(np.int64),
"t" : tone.astype(np.int64), "t": tone.astype(np.int64),
"language" : language.astype(np.int64), "language": language.astype(np.int64),
"bert_0" : bert_zh.astype(np.float32), "bert_0": bert_zh.astype(np.float32),
"bert_1" : bert_jp.astype(np.float32), "bert_1": bert_jp.astype(np.float32),
"bert_2" : bert_en.astype(np.float32), "bert_2": bert_en.astype(np.float32),
"emo" : emo.astype(np.float32), "emo": emo.astype(np.float32),
"g" : g.astype(np.float32) "g": g.astype(np.float32),
}) },
)
x, m_p, logs_p, x_mask = enc_rtn[0], enc_rtn[1], enc_rtn[2], enc_rtn[3] x, m_p, logs_p, x_mask = enc_rtn[0], enc_rtn[1], enc_rtn[2], enc_rtn[3]
np.random.seed(seed) np.random.seed(seed)
zinput = np.random.randn(x.shape[0], 2, x.shape[2]) * sdp_noise_scale zinput = np.random.randn(x.shape[0], 2, x.shape[2]) * sdp_noise_scale
logw = self.sdp.run(None, {"x" : x, "x_mask" : x_mask, "zin" : zinput.astype(np.float32), "g" : g})[0] * (sdp_ratio) + \ logw = self.sdp.run(
self.dp.run(None, {"x" : x, "x_mask" : x_mask, "g" : g})[0] * (1 - sdp_ratio) None, {"x": x, "x_mask": x_mask, "zin": zinput.astype(np.float32), "g": g}
)[0] * (sdp_ratio) + self.dp.run(None, {"x": x, "x_mask": x_mask, "g": g})[
0
] * (
1 - sdp_ratio
)
w = np.exp(logw) * x_mask * length_scale w = np.exp(logw) * x_mask * length_scale
w_ceil = np.ceil(w) w_ceil = np.ceil(w)
y_lengths = np.clip(np.sum(w_ceil, (1, 2)), a_min=1., a_max=100000).astype(np.int64) y_lengths = np.clip(np.sum(w_ceil, (1, 2)), a_min=1.0, a_max=100000).astype(
np.int64
)
y_mask = np.expand_dims(sequence_mask(y_lengths, None), 1) y_mask = np.expand_dims(sequence_mask(y_lengths, None), 1)
attn_mask = np.expand_dims(x_mask, 2) * np.expand_dims(y_mask, -1) attn_mask = np.expand_dims(x_mask, 2) * np.expand_dims(y_mask, -1)
attn = generate_path(w_ceil, attn_mask) attn = generate_path(w_ceil, attn_mask)
@@ -85,8 +102,20 @@ class OnnxInferenceSession():
0, 2, 1 0, 2, 1
) # [b, t', t], [b, t, d] -> [b, d, t'] ) # [b, t', t], [b, t, d] -> [b, d, t']
z_p = m_p + np.random.randn(m_p.shape[0], m_p.shape[1], m_p.shape[2]) * np.exp(logs_p) * seq_noise_scale z_p = (
m_p
+ np.random.randn(m_p.shape[0], m_p.shape[1], m_p.shape[2])
* np.exp(logs_p)
* seq_noise_scale
)
z = self.flow.run(None, {"z_p" : z_p.astype(np.float32), "y_mask" : y_mask.astype(np.float32), "g": g})[0] z = self.flow.run(
None,
{
"z_p": z_p.astype(np.float32),
"y_mask": y_mask.astype(np.float32),
"g": g,
},
)[0]
return self.dec.run(None, {"z_in" : z.astype(np.float32), "g": g})[0] return self.dec.run(None, {"z_in": z.astype(np.float32), "g": g})[0]

View File

@@ -6,7 +6,6 @@ from torch.nn import functional as F
import commons import commons
import modules import modules
from . import attentions_onnx from . import attentions_onnx
from vector_quantize_pytorch import VectorQuantize
from torch.nn import Conv1d, ConvTranspose1d, Conv2d from torch.nn import Conv1d, ConvTranspose1d, Conv2d
from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm