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

RuntimeError: Exporting the operator col2im to ONNX opset version 12 is not supported #102

Open
didadida-r opened this issue Aug 19, 2021 · 5 comments
Labels
question Further information is requested

Comments

@didadida-r
Copy link

hi, i convert the stft model to onnx version , but a error is raised.
'''
RuntimeError: Exporting the operator col2im to ONNX opset version 12 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub
'''

@KinWaiCheuk KinWaiCheuk added help wanted Extra attention is needed question Further information is requested labels Aug 19, 2021
@KinWaiCheuk
Copy link
Owner

At first glance, it seems this error is related to PyTorch version, rather than nnAudio. Which PyTorch version are you using?
Could you please share me the code for converting the stft model to onnx?
I will try to locate the exact problem, thanks!

@KinWaiCheuk
Copy link
Owner

I ran a quick test on my computer with the following code, and I can obtain the onnx file successfully.

from nnAudio.Spectrogram import STFT
import torch

dummy_input = torch.randn(4,44100)
stft_layer = STFT()
torch.onnx.export(stft_layer,
                  dummy_input,
                  'stft_layer.onnx',
                  verbose=True,
                  input_names=['waveforms'],
                  output_names=['stft_output'])

I am using pytorch==1.9.0+cu111 and nnAudio==0.2.5. Could you double check if you are able to do ONNX using the same pytorch and nnAudio as me?

@KinWaiCheuk KinWaiCheuk removed the help wanted Extra attention is needed label Aug 22, 2021
@didadida-r
Copy link
Author

hi, i test it again and this problem might locate in the iSTFT module, here is the test code.

    from nnAudio.Spectrogram import STFT, iSTFT
    import torch
    
    class DummpyModel(nn.Module):
        def __init__(self):
            super(DummpyModel, self).__init__()
            self.stft_layer = STFT()
            self.istft_layer = iSTFT()
        
        def forward(self, wav):
            out = self.stft_layer(wav, output_format="Complex")
            out = self.istft_layer(out, length=wav.shape[-1], onesided=True)
            # out_wav = self.decoder(spec, wav.shape)
            return out

    dummy_input = torch.randn(4,44100)
    model = DummpyModel()
    torch.onnx.export(model,
                    dummy_input,
                    'stft_layer.onnx',
                    verbose=True,
                    opset_version=12,
                    input_names=['waveforms'],
                    output_names=['stft_output'])

@KinWaiCheuk
Copy link
Owner

You are right, when trying to export iSTFT to onnx, it will result in an error message:

RuntimeError: Exporting the operator col2im to ONNX opset version 12 is not supported. Please feel free to request support or submit a pull request on PyTorch GitHub.

This is because the fold function that iSTFT uses is not yet supported by onnx.

def torch_window_sumsquare(w, n_frames, stride, n_fft, power=2):
w_stacks = w.unsqueeze(-1).repeat((1,n_frames)).unsqueeze(0)
# Window length + stride*(frames-1)
output_len = w_stacks.shape[1] + stride*(w_stacks.shape[2]-1)
return fold(w_stacks**power, (1,output_len), kernel_size=(1,n_fft), stride=stride)
def overlap_add(X, stride):
n_fft = X.shape[1]
output_len = n_fft + stride*(X.shape[2]-1)
return fold(X, (1,output_len), kernel_size=(1,n_fft), stride=stride).flatten(1)

There is a open issue under the PyTorch repo, I think people are working on it already.
pytorch/pytorch#41423

Unfortunately, there is not much I can do at the moment. One workaround is to implement the two functions, torch_window_sumsquare and overlap_add without using fold. I do have an old version of nnAudio that did not use fold for inverse before commit 14db7a6e94e3f38ccaded7c779a85b99fdaefff1. But I remember the inverse was quite slow without using fold, and the result was slightly incorrect.

@veronicamorelli
Copy link

Operator col2im is supported for opset version 18 but not the previous ones, is they a way to overcome the error?
"torch.onnx.errors.UnsupportedOperatorError: Exporting the operator 'aten::col2im' to ONNX opset version 17 is not supported. Support for this operator was added in version 18, try exporting with this version."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants