-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexport.py
More file actions
255 lines (208 loc) · 7.98 KB
/
export.py
File metadata and controls
255 lines (208 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import argparse
import os
import re
import shutil
from pathlib import Path
import onnx
import onnxscript
import torch
from julius.filters import LowPassFilter
from omegaconf import DictConfig, OmegaConf
from onnxscript.onnx_opset import opset18 as op
from onnxsim import simplify
from torch.onnx._internal import jit_utils
custom_opset = onnxscript.values.Opset(domain="onnx-script", version=1)
@onnxscript.script(custom_opset)
def Sinc(X):
pi = op.CastLike(3.141592653589793, X)
piX = pi * X
sinc = op.Sin(piX) / piX
zero = op.CastLike(0, X)
one = op.CastLike(1, X)
return op.Where(X == zero, one, sinc)
def custom_sinc(g: jit_utils.GraphContext, X):
return g.onnxscript_op(Sinc, X).setType(X.type())
torch.onnx.register_custom_op_symbolic(
symbolic_name="aten::sinc",
symbolic_fn=custom_sinc,
opset_version=18,
)
class ExportableHiFiPLN(torch.nn.Module):
def __init__(self, config: DictConfig, ckpt_path, model):
super().__init__()
self.model = model(config, export=True)
self.lowpass = config.model.get("filter_lowpass")
self.highpass = config.model.get("filter_highpass")
if ckpt_path is not None:
cp_dict = torch.load(ckpt_path, map_location="cpu", weights_only=False)
if "state_dict" not in cp_dict:
self.model.load_state_dict(cp_dict["generator"], strict=False)
else:
self.model.load_state_dict(
{
k.replace("generator.", ""): v
for k, v in cp_dict["state_dict"].items()
if k.startswith("generator.")
},
strict=False,
)
self.model.eval()
self.model.remove_parametrizations()
self.filter_low = LowPassFilter(
config.f_min / config.sample_rate, zeros=8, fft=False
)
self.filter_high = LowPassFilter(
config.f_max / config.sample_rate, zeros=24, fft=False
)
def forward(self, mel: torch.FloatTensor, f0: torch.FloatTensor):
mel = mel.transpose(-1, -2)
f0 = f0.unsqueeze(1)
wav, (_, _) = self.model(mel, f0)
org_wav = wav
if self.lowpass:
wav = self.filter_high(wav)
if self.highpass:
wav = wav - self.filter_low(org_wav)
wav = wav.squeeze(1)
wav = torch.clamp(wav, -1, 1)
return wav
class ExportableDDSP(torch.nn.Module):
def __init__(self, config: DictConfig, ckpt_path, model):
super().__init__()
self.model = model(config)
if ckpt_path is not None:
cp_dict = torch.load(ckpt_path, map_location="cpu")
if "state_dict" not in cp_dict:
self.model.load_state_dict(cp_dict["generator"])
else:
self.model.load_state_dict(
{
k.replace("generator.", ""): v
for k, v in cp_dict["state_dict"].items()
if k.startswith("generator.")
}
)
self.model.eval()
self.model.remove_parametrizations()
def forward(self, mel: torch.FloatTensor, f0: torch.FloatTensor):
mel = mel.transpose(-1, -2)
f0 = f0.unsqueeze(1)
wav, (_, _) = self.model(mel, f0)
wav = wav.squeeze(1)
wav = torch.clamp(wav, -1, 1)
return wav
def main(input_file, output_path, config, best=False, dynamo=False, optimize=False):
output_path = Path(output_path)
if output_path.exists():
print(f"Output path {output_path} already exists, deleting")
shutil.rmtree(output_path)
output_path.mkdir(parents=True, exist_ok=True)
if input_file is not None and os.path.isdir(input_file):
dirs = [
f
for f in os.listdir(input_file)
if os.path.isdir(os.path.join(input_file, f)) and f.startswith("version_")
]
if len(dirs) > 0:
last_version = 0
for d in dirs:
version = int(d.split("_")[1])
if version > last_version:
last_version = version
input_file = os.path.join(
input_file, f"version_{last_version}", "checkpoints"
)
else:
input_file = os.path.join(input_file, "checkpoints")
files = [f for f in os.listdir(input_file) if f.endswith(".ckpt")]
if len(files) > 0:
best_epoch = 100
last_epoch = 0
choice = 0
for i, f in enumerate(files):
if best:
loss = float(re.search(r"(?:loss=)(\d+\.\d+)", f).group(1))
if loss < best_epoch:
best_epoch = loss
choice = i
else:
step = int(re.search(r"(?:step=)(\d+)", f).group(1))
if step > last_epoch:
last_epoch = step
choice = i
input_file = os.path.join(input_file, files[choice])
print(f"Exporting {input_file} to {output_path}")
# Export ONNX
print(f"Exporting ONNX...")
match config.type:
case "HiFiPLNv1":
from model.hifiplnv1.generator import HiFiPLNv1
model = ExportableHiFiPLN(config, input_file, HiFiPLNv1)
case "HiFiPLNv2":
from model.hifiplnv2.generator import HiFiPLNv2
model = ExportableHiFiPLN(config, input_file, HiFiPLNv2)
case "SinSum":
from model.sinsum.generator import SinSum
model = ExportableHiFiPLN(config, input_file, SinSum)
case "DDSP":
from model.ddsp.generator import DDSP
model = ExportableDDSP(config, input_file, DDSP)
case _:
raise ValueError(f"Unknown model type: {config.type}")
print("Model loaded...")
mel = torch.randn(1, 10, 128)
f0 = torch.randn(1, 10)
output_path = output_path / f"{config.type.lower()}.onnx"
if not dynamo:
torch.onnx.export(
model,
(mel, f0),
output_path.with_suffix(".tmp"),
input_names=["mel", "f0"],
output_names=["waveform"],
opset_version=18,
dynamic_axes={
"mel": {1: "n_frames"},
"f0": {1: "n_frames"},
"waveform": {1: "wave_length"},
},
training=torch.onnx.TrainingMode.EVAL,
do_constant_folding=True,
keep_initializers_as_inputs=False,
)
if optimize:
print("Optimising...")
onnx_model = onnx.load(output_path.with_suffix(".tmp"))
onnx_model, check = simplify(onnx_model)
assert check, "Simplified ONNX model could not be validated"
onnx.checker.check_model(onnx_model, full_check=True)
onnx.save(onnx_model, output_path)
output_path.with_suffix(".tmp").unlink()
else:
output_path.with_suffix(".tmp").rename(output_path)
else:
export_options = torch.onnx.ExportOptions(dynamic_shapes=True)
onnx_model = torch.onnx.dynamo_export(
model, export_options=export_options, mel=mel, f0=f0
)
onnx_model.save(output_path)
print(f"ONNX exported.")
print(f"Exported to {output_path}")
if __name__ == "__main__":
argparser = argparse.ArgumentParser()
argparser.add_argument("--model", type=str, default=None)
argparser.add_argument("--config", type=str, required=True)
argparser.add_argument("--output", type=str, required=True)
argparser.add_argument("--dynamo", action="store_true")
argparser.add_argument("--best", action="store_true")
argparser.add_argument("--optimize", action="store_true")
args = argparser.parse_args()
config = OmegaConf.load(args.config)
main(
args.model,
args.output,
config,
best=args.best,
dynamo=args.dynamo,
optimize=args.optimize,
)