Skip to content

[DONE]Optimizing fs2 with OnnxSlim #262

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

Open
wants to merge 1 commit into
base: v2-backport
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 6 additions & 3 deletions deployment/exporters/acoustic_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import onnx
import onnxsim
import onnxslim
import torch
import yaml

Expand Down Expand Up @@ -344,9 +345,11 @@ def _perform_spk_mix(self, spk_mix: Dict[str, float]):
return spk_mix_embed

def _optimize_fs2_aux_graph(self, fs2: onnx.ModelProto) -> onnx.ModelProto:
print(f'Running ONNX Simplifier on {self.fs2_aux_class_name}...')
fs2, check = onnxsim.simplify(fs2, include_subgraph=True)
assert check, 'Simplified ONNX model could not be validated'
# print(f'Running ONNX Simplifier on {self.fs2_aux_class_name}...')
# fs2, check = onnxsim.simplify(fs2, include_subgraph=True)
# assert check, 'Simplified ONNX model could not be validated'
print(f'Running OnnxSlim on {self.fs2_aux_class_name}...')
fs2 = onnxslim.slim(fs2)
onnx_helper.model_reorder_io_list(
fs2, 'input',
target_name='languages', insert_after_name='tokens'
Expand Down
18 changes: 13 additions & 5 deletions deployment/exporters/variance_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import onnx
import onnxsim
import onnxslim
import torch
import yaml

Expand Down Expand Up @@ -82,6 +83,8 @@ def __init__(
if self.freeze_spk is not None:
self.model.register_buffer('frozen_spk_embed', self._perform_spk_mix(self.freeze_spk[1]))

self.use_melody_encoder = hparams['use_melody_encoder']

def build_model(self) -> DiffSingerVarianceONNX:
model = DiffSingerVarianceONNX(
vocab_size=len(self.phoneme_dictionary),
Expand Down Expand Up @@ -649,9 +652,11 @@ def _optimize_linguistic_graph(self, linguistic: onnx.ModelProto) -> onnx.ModelP
'encoder_out': (1, 'n_tokens', hparams['hidden_size'])
}
)
print(f'Running ONNX Simplifier on {self.fs2_class_name}...')
linguistic, check = onnxsim.simplify(linguistic, include_subgraph=True)
assert check, 'Simplified ONNX model could not be validated'
# print(f'Running ONNX Simplifier on {self.fs2_class_name}...')
# linguistic, check = onnxsim.simplify(linguistic, include_subgraph=True)
# assert check, 'Simplified ONNX model could not be validated'
print(f'Running OnnxSlim on {self.fs2_class_name}...')
linguistic = onnxslim.slim(linguistic)
onnx_helper.model_reorder_io_list(
linguistic, 'input',
target_name='languages', insert_after_name='tokens'
Expand All @@ -678,8 +683,11 @@ def _optimize_merge_pitch_predictor_graph(
onnx_helper.model_override_io_shapes(
pitch_pre, output_shapes={'pitch_cond': (1, 'n_frames', hparams['hidden_size'])}
)
pitch_pre, check = onnxsim.simplify(pitch_pre, include_subgraph=True)
assert check, 'Simplified ONNX model could not be validated'
if self.use_melody_encoder:
pitch_pre = onnxslim.slim(pitch_pre)
else:
pitch_pre, check = onnxsim.simplify(pitch_pre, include_subgraph=True)
assert check, 'Simplified ONNX model could not be validated'

onnx_helper.model_override_io_shapes(
pitch_predictor, output_shapes={'pitch_pred': (1, 'n_frames')}
Expand Down
1 change: 1 addition & 0 deletions requirements-onnx.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ MonkeyType==23.3.0
numpy<2.0.0
onnx~=1.16.0
onnxsim~=0.4.36
onnxslim
praat-parselmouth==0.4.3
pyworld==0.3.4
PyYAML
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ MonkeyType==23.3.0
numpy<2.0.0
onnx~=1.16.0
onnxsim~=0.4.36
onnxslim
praat-parselmouth==0.4.3
pyworld==0.3.4
PyYAML
Expand Down