Skip to content

Support variance ONNX export on PyTorch 2.x#293

Open
IDExpensive-One wants to merge 1 commit intoopenvpi:mainfrom
IDExpensive-One:fix/variance-export-pytorch2
Open

Support variance ONNX export on PyTorch 2.x#293
IDExpensive-One wants to merge 1 commit intoopenvpi:mainfrom
IDExpensive-One:fix/variance-export-pytorch2

Conversation

@IDExpensive-One
Copy link

Summary

The variance model ONNX export currently requires PyTorch 1.13.x because torch.jit.script is used on models returned by view_as_*_predictor(). On PyTorch 2.x, this fails with RuntimeError: Unsupported value kind: Tensor because TorchScript attempts to compile all methods on the class, including those that reference attributes removed by deepcopy + del in the view_as_* methods.

This PR:

  • Adds a trace-based fallback for PyTorch 2.x using lightweight wrapper modules passed directly to torch.onnx.export
  • Preserves the original torch.jit.script path for PyTorch 1.13.x
  • Relaxes the hard version check in export.py to a warning

Tested on

  • PyTorch 1.13.1+cpu (Python 3.10) - original script path works
  • PyTorch 2.7.0+cu128 (Python 3.11) - trace-based fallback works
  • Exported ONNX models load and run correctly in OpenUTAU

The variance model export previously required PyTorch 1.13.x because it
used torch.jit.script on models returned by view_as_*_predictor(). This
fails on PyTorch 2.x with "Unsupported value kind: Tensor" as TorchScript
tries to compile all methods on the class, including those referencing
deleted attributes.

This adds a fallback path for PyTorch 2.x that uses lightweight wrapper
modules with trace-based torch.onnx.export instead of torch.jit.script.
The original script-based path is preserved for PyTorch 1.13.x.

The version check in export.py is relaxed from a hard error to a warning.
Copilot AI review requested due to automatic review settings March 9, 2026 11:42
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Enables ONNX export of the variance (and pitch) predictor models on PyTorch 2.x by avoiding TorchScript scripting of view_as_*_predictor() outputs, while keeping the existing TorchScript-based export flow for PyTorch 1.13.x.

Changes:

  • Relax PyTorch 1.13.x hard requirement in the CLI export script to a warning.
  • Add a PyTorch 2.x path in the variance exporter that wraps predictor submodules for trace-based ONNX export instead of torch.jit.script.
  • Preserve the PyTorch 1.13.x path that traces the backbone and scripts the predictor before exporting.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
scripts/export.py Replaces the strict PyTorch 1.13.x requirement with a warning to allow running exports on newer versions.
deployment/exporters/variance_exporter.py Adds a PyTorch-version-dependent export path: TorchScript scripting on 1.13.x, wrapper-based trace export otherwise.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.'
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning text is misleading because check_pytorch_version() runs unconditionally for all export commands (acoustic/variance/vocoder), but the message claims it is "Proceeding with trace-based fallback for variance models." Consider either (1) making the warning generic (e.g., "Export is tested on 1.13.x...") or (2) moving this version warning into the variance export command / exporter so it only appears when the fallback is actually relevant.

Suggested change
f'Proceeding with trace-based fallback for variance models.'
f'Export may not behave as expected with this PyTorch version.'

Copilot uses AI. Check for mistakes.
dummy_time,
condition

if torch.__version__.startswith('1.13.'):
Copy link

Copilot AI Mar 9, 2026

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_model and reusing it to avoid repeating version parsing logic and to make future adjustments less error-prone.

Suggested change
if torch.__version__.startswith('1.13.'):
is_torch_113 = torch.__version__.startswith('1.13.')
if is_torch_113:

Copilot uses AI. Check for mistakes.
step,
condition

if torch.__version__.startswith('1.13.'):
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same version check duplication as above: this block repeats torch.__version__.startswith('1.13.'). To reduce maintenance overhead, reuse a single precomputed flag (e.g., is_torch_113) within _torch_export_model.

Suggested change
if torch.__version__.startswith('1.13.'):
if is_torch_113:

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants