Skip to content
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

Fix a noise shape of StyleMelGANGenerator to export ONNX model #312

Merged
merged 2 commits into from
Dec 13, 2021

Conversation

c-bata
Copy link
Contributor

@c-bata c-bata commented Dec 11, 2021

Hi! Thank you for creating this awesome project. In this PR, I suggest modifying StyleMelGANGenerator to export the ONNX model.

Problem details

I bumped the following issue when I tried to export StyleMelGANGenerator.

/path/to/venv/lib/python3.8/site-packages/parallel_wavegan/models/style_melgan.py:222: TracerWarning: Converting a tensor to a Python float might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
  math.ceil(c.size(2) / self.noise_upsample_factor),

This warning shows that math.ceil() operation cannot be traced by torch.onnx.export() function. So that the shape of noise will be treated as constant values.

145675032-115c38a1-08a0-4e1a-b0cf-c6162a7fbdca

After applying this patch, we can export ONNX model.

ScreenShot 2021-12-12 5 46 11

Comment on lines 222 to 221
math.ceil(c.size(2) / self.noise_upsample_factor),
(c.size(2)-1) // self.noise_upsample_factor + 1,
Copy link
Contributor Author

@c-bata c-bata Dec 11, 2021

Choose a reason for hiding this comment

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

Note that both of c.size(2) and self.noise_upsample_factor are intergers. So that we can replace math.ceil() operation like this. Even though we may use torch.ceil() function, it takes and returns a tensor, not a scalar value.

>>> import math
>>> noise_upsample_factor = 5
>>> for c_size in range(15):
...     before = math.ceil(c_size / noise_upsample_factor)
...     after = (c_size-1) // noise_upsample_factor + 1
...     print(f"before: {before}, after: {after}")
...
before: 0, after: 0
before: 1, after: 1
before: 1, after: 1
before: 1, after: 1
before: 1, after: 1
before: 1, after: 1
before: 2, after: 2
before: 2, after: 2
before: 2, after: 2
before: 2, after: 2
before: 2, after: 2
before: 3, after: 3
before: 3, after: 3
before: 3, after: 3
before: 3, after: 3

@kan-bayashi
Copy link
Owner

Thank you for your modification! It looks fine.
I slightly modified to pass the CI.

@kan-bayashi kan-bayashi added the enhancement New feature or request label Dec 12, 2021
@kan-bayashi kan-bayashi merged commit 6c2248b into kan-bayashi:master Dec 13, 2021
@c-bata c-bata deleted the export-onnx branch December 13, 2021 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants