-
Notifications
You must be signed in to change notification settings - Fork 21.5k
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
[ONNX] Support opset 17 operators #81075
Comments
Justin, you listed istft here, but I can't find istft in ONNX's list of operators. Is the idea to implement istft in terms of the available onnx operators? Edit: This PR also lists ISTFT in its description, but there's no mention of ISTFT in the code changes. Now I'm confused. Does ONNX opset 17 support ISTFT or not? |
Hi @iver56, I can confirm that ISTFT is not in opset 17. However onnx is open to including it as a new op (please feel free to open an issue in the onnx/onnx repo if not already existed). We plan to support exporting istft from pytorch in spite of the missing ISTFI op. |
If I may add, even though ISTFT is not available, both forward and inverse operations for DFT are supported by ONNX. Reference This should help users implement ISTFT on their own. |
@stonelazy i think so. Do you have a list of related torch functions you think could be fitting? |
I hope am not wrong, below are the torch's equivalent in ONNX. |
SInce LayerNorm is also part of opset 17, does it make sense to include it as well in this list? |
Will opset17 be available, only if all operators are implemented? |
opset17 is enabled in torch-nightly. support for new ops will be added gradually. |
Thanks :) Hoping for stft and window functions to come soon. |
That's a very important feature for SpeechBrain as well. The feature extraction part relies on fft/stft and we cannot currently fully export/import it in ONNX (@fpaissan). |
Hello, I've installed torch from nightly releases ( |
Hi! Even though we can export to the opset17 format, support for stft is not yet added unfortunately. |
This PR addresses issue [#81075](#81075), making `torch.stft` compatible with ONNX Opset 17's STFT operator. The conversion works for _most_ of `torch.stft` functionality: - Batched or unbatched inputs - Normalization - Pre-computed windows - Rectangular windows - One-sided returns - Window centering (implicitly supported) What is currently _not_ supported is **complex types**, due to the lack of conversion functionality between PyTorch and ONNX (#86746). Regardless, this is easy to bypass by setting `return_complex=False` when using `torch.stft`. Note that there is already a draft PR to address this (#83944), but it is currently closed and it only partially addresses the conversion (i.e., most of `torch.stft` functionality is lacking, and unit tests are missing). Pull Request resolved: #92087 Approved by: https://github.com/justinchuby
This PR addresses issue [#81075](pytorch/pytorch#81075), making `torch.stft` compatible with ONNX Opset 17's STFT operator. The conversion works for _most_ of `torch.stft` functionality: - Batched or unbatched inputs - Normalization - Pre-computed windows - Rectangular windows - One-sided returns - Window centering (implicitly supported) What is currently _not_ supported is **complex types**, due to the lack of conversion functionality between PyTorch and ONNX (pytorch/pytorch#86746). Regardless, this is easy to bypass by setting `return_complex=False` when using `torch.stft`. Note that there is already a draft PR to address this (pytorch/pytorch#83944), but it is currently closed and it only partially addresses the conversion (i.e., most of `torch.stft` functionality is lacking, and unit tests are missing). Pull Request resolved: pytorch/pytorch#92087 Approved by: https://github.com/justinchuby
Are there any plans to implement fft_fft / fft_rfft or would you accept pull requests? |
We will accept pull requests for sure! |
Looks like it is blocked on support for complex types in onnx export 😔 |
If you only need an ONNX support for real-valued FFT ops (e.g. Note that if you plan to use ONNX models with those ops in runtime, you might need to use a custom build of ORT due to the bug in IRFFT. The alternative is to use TRT plugins, that is, do PyTorch -> ONNX -> TRT conversion (TRT plugins do not use ORT, so are not affected by the bug). |
@Alexey-Kamenev this sounds like it could potentially be extremely useful, but I don't really understand what I would need to do. For instance, could I create a PyTorch model that creates spectrograms from audio, and export this model to ONNX? If so, could you describe the steps necessary? |
Yes, you could do that, if in your PyTorch code you use something like
Unfortunately, due to lack of support of complex type in ONNX exporter, the process is not exactly trivial - you would need to use some sort of a wrapper which would use standard PyTorch FFT functions when not in ONNX export mode and custom symbolic functions which emit proper ONNX operators (e.g. We already did all of that in Modulus (see the AFNO example I mentioned above), but if you don't want to take the dependency on Modulus, you can implement a simpler version using the code by checking the tests. I know, it does not sound particularly simple, but that's the consequence of not having complex type support in ONNX exporter. Otherwise, it would have been as simple as something like calling |
More support on the way in our new torch.onnx.dynamo_export exporter. cc @titaiwangms |
There is a |
l1_loss will be supported |
What is the current workaround to export models with mel spectrograms? And is support for stft the only thing that is missing to make it work? |
STFT is supported by the torch.onnx.dynamo_export export API. I have not looked at melspectrogram. Supposedly ONNX has a related op that we have not made use of. |
It should now be supported by |
@justinchuby does this mean that there's now a simpler method for exporting pytorch models containing fft than the approach described by @Alexey-Kamenev above? Or is that approach still required? |
Yes. The new |
Can anyone produce a working example where Here is a simple MWE, with a setup common to audio signal processing models: import torch
class STFTModel(torch.nn.Module):
def __init__(self):
super().__init__()
self._window = torch.hann_window(window_length=320)
def forward(self, signals: torch.Tensor) -> torch.Tensor:
x = signals.stft(
n_fft=512,
hop_length=160,
win_length=320,
return_complex=True, # doesn't affect errors
window=self._window,
pad_mode="constant", # aten.reflection_pad1d unsupported op
)
return x
m = STFTModel()
# Shape [B, T] audio signals
input_signals = torch.randn([2, 16000])
args = (input_signals,)
export_options = torch.onnx.ExportOptions(dynamic_shapes=True)
torch.onnx.dynamo_export(
m,
*args,
export_options=export_options,
) Here are the short versions of error messages: Without dynamic shapes (not useful to anyone using
With dynamic shapes (as the example shows):
Exporting within the context of
Relevant context (should be the latest in everything): $ pip freeze | egrep '(torch|onnx)'
onnx==1.15.0
onnxscript==0.1.0.dev20231106
pytorch-triton==2.1.0+6e4932cda8
torch==2.2.0.dev20231106+cu121
torchaudio==2.2.0.dev20231106+cu121
torchvision==0.17.0.dev20231106+cu121 |
Thanks for sharing the info. I will reproduce this and get back to you. |
We will continue in #113067 @shanecarroll-smarsh |
Tracking issue on the onnx opset 17 support. Contributions welcome.
Closes #80834
The text was updated successfully, but these errors were encountered: