-
Notifications
You must be signed in to change notification settings - Fork 328
Support variance ONNX export on PyTorch 2.x #293
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -388,33 +388,47 @@ def _torch_export_model(self): | |||||
| noise = torch.randn(shape, device=self.device) | ||||||
| condition = torch.rand((1, hparams['hidden_size'], 15), device=self.device) | ||||||
|
|
||||||
| print(f'Tracing {self.pitch_backbone_class_name} backbone...') | ||||||
| pitch_predictor = self.model.view_as_pitch_predictor() | ||||||
| pitch_predictor.pitch_predictor.set_backbone( | ||||||
| torch.jit.trace( | ||||||
| pitch_predictor.pitch_predictor.backbone, | ||||||
| ( | ||||||
| noise, | ||||||
| dummy_time, | ||||||
| condition | ||||||
|
|
||||||
| if torch.__version__.startswith('1.13.'): | ||||||
| print(f'Tracing {self.pitch_backbone_class_name} backbone...') | ||||||
| pitch_predictor.pitch_predictor.set_backbone( | ||||||
| torch.jit.trace( | ||||||
| pitch_predictor.pitch_predictor.backbone, | ||||||
| ( | ||||||
| noise, | ||||||
| dummy_time, | ||||||
| condition | ||||||
| ) | ||||||
| ) | ||||||
| ) | ||||||
| ) | ||||||
|
|
||||||
| print(f'Scripting {self.pitch_predictor_class_name}...') | ||||||
| pitch_predictor = torch.jit.script( | ||||||
| pitch_predictor, | ||||||
| example_inputs=[ | ||||||
| ( | ||||||
| condition.transpose(1, 2), | ||||||
| 1 # p_sample branch | ||||||
| ), | ||||||
| ( | ||||||
| condition.transpose(1, 2), | ||||||
| dummy_steps # p_sample_plms branch | ||||||
| ) | ||||||
| ] | ||||||
| ) | ||||||
| print(f'Scripting {self.pitch_predictor_class_name}...') | ||||||
| pitch_predictor = torch.jit.script( | ||||||
| pitch_predictor, | ||||||
| example_inputs=[ | ||||||
| ( | ||||||
| condition.transpose(1, 2), | ||||||
| 1 # p_sample branch | ||||||
| ), | ||||||
| ( | ||||||
| condition.transpose(1, 2), | ||||||
| dummy_steps # p_sample_plms branch | ||||||
| ) | ||||||
| ] | ||||||
| ) | ||||||
| else: | ||||||
| print(f'Wrapping {self.pitch_predictor_class_name} for trace-based export...') | ||||||
|
|
||||||
| class _PitchPredWrapper(torch.nn.Module): | ||||||
| def __init__(self, model): | ||||||
| super().__init__() | ||||||
| self.pitch_predictor = model.pitch_predictor | ||||||
|
|
||||||
| def forward(self, pitch_cond, steps): | ||||||
| return self.pitch_predictor(pitch_cond, steps=steps) | ||||||
|
|
||||||
| pitch_predictor = _PitchPredWrapper(pitch_predictor) | ||||||
|
|
||||||
| print(f'Exporting {self.pitch_predictor_class_name}...') | ||||||
| torch.onnx.export( | ||||||
|
|
@@ -535,33 +549,47 @@ def _torch_export_model(self): | |||||
| condition = torch.rand((1, hparams['hidden_size'], 15), device=self.device) | ||||||
| step = (torch.rand((1,), device=self.device) * hparams['K_step']).long() | ||||||
|
|
||||||
| print(f'Tracing {self.variance_backbone_class_name} backbone...') | ||||||
| multi_var_predictor = self.model.view_as_variance_predictor() | ||||||
| multi_var_predictor.variance_predictor.set_backbone( | ||||||
| torch.jit.trace( | ||||||
| multi_var_predictor.variance_predictor.backbone, | ||||||
| ( | ||||||
| noise, | ||||||
| step, | ||||||
| condition | ||||||
|
|
||||||
| if torch.__version__.startswith('1.13.'): | ||||||
|
||||||
| if torch.__version__.startswith('1.13.'): | |
| if is_torch_113: |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,10 +15,13 @@ | |||||
|
|
||||||
|
|
||||||
| def check_pytorch_version(): | ||||||
| # Require PyTorch version to be exactly 1.13.x | ||||||
| import warnings | ||||||
| if torch.__version__.startswith('1.13.'): | ||||||
| return | ||||||
| raise RuntimeError('This script requires PyTorch 1.13.x. Please install the correct version.') | ||||||
| warnings.warn( | ||||||
| f'ONNX export is tested on PyTorch 1.13.x, but you have {torch.__version__}. ' | ||||||
| f'Proceeding with trace-based fallback for variance models.' | ||||||
|
||||||
| f'Proceeding with trace-based fallback for variance models.' | |
| f'Export may not behave as expected with this PyTorch version.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PyTorch version gate (
torch.__version__.startswith('1.13.')) is duplicated in multiple blocks in this method. Consider computing a single boolean once (e.g.,is_torch_113) near the top of_torch_export_modeland reusing it to avoid repeating version parsing logic and to make future adjustments less error-prone.