-
Notifications
You must be signed in to change notification settings - Fork 24.2k
[feature request] Support native ONNX export of FFT-related ops in opset17 (with inverse=True
, it also includes inverse DFT)
#107588
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
Comments
inverse=True
, it also includes inverse DFT)
Or maybe could you please transfer this issue to the core repo issues? |
FFT and STFT will be supported by the onnx.dynamo_export exporter |
see #107922 |
@justinchuby Would it be possible to "backport" support for DFT ops into torch.onnx.export e.g. support enable DFT-17? Or maybe somehow have an opt-in only module enabling these operators for opset17 (via torch.onnx.register_custom_op_symbolic) or introduce some rudimentary support of opset18/opset20 into torch.onnx.export? Currently, dynamo_export has some questionable limitations for recurrent models (or even recurrent models single steps): which prevent its successful use in some scenarios requiring some recurrent models / cells export cc @grazder |
That is possible, but requires refactoring that the team may or may not be able to prioritize. Specifically, we need to use the new Reduce* ops that changed the It is possible to reuse the onnxscript-torchlib implementations.
The issue with DFT in torch.onnx.export is that the values are complex numbers, which are not handled. However it is possible to case on the value type and create a real representation in the ONNX graph similar to how the dynamo exporter handles them. We welcome contribution for either case if it is a priority for you. Thanks! |
@justinchuby I wonder if it's possible to somehow allow in torch.onnx.export of subgraph .rfft(x).view_as_real() only as first workaround? this should at least unblock the typical scenarios where is some fft + magnitude or angle calculations or some masking (the calculations would have to be in (*, 2) real tensors for this, but sometimes it's okay for a workaround) |
Seems that If supported as a dtype, what could be done is probably:
Or maybe alternatively what could be introduced is a dtype Maybe these complex interleaved dtypes are not the most efficient representation for all ops, but probably it would allow to easier backport complex numbers support to the older export mechanism, while dynamo_export is getting more stable, and maybe make semantics more similar to PyTorch which should alleviate dim recalculations... |
Even though the complex types exist, there are no operators defined to do computation on them; so only supporting the types themselves does not mean much. |
The alternative may be possible. One issue with the old exporter I think is actually knowing the types of the inputs. If we know that the types are complex, it is easy to treat them so. But for ops like abs(), when the input type is missing we don’t know which logic we should use (complex or real) |
I think adding such support to the operators would simplify all export (old an new) wrt shape/dim adjustment calulation and in general make it simpler because semantics is closer to PyTorch... Or maybe making some separate named versions of existing operators working with these complex dtypes (don't know which versioning/evolution strategy is viewed better by the onnx community)... is ONNX relying on propagating the dtypes in static way? cause otherwise, maybe it could dynamically change its behavior inside the Abs operator impl... e.g. Abs is maybe not so difficult as it should produce a real tensor in any case, so if it's receiving a complex-dtyped tensor as input at runtime, it should always produce a real tensor still... It could also be that in the future some other complex representations are experimented with in PyTorch (e.g. keeping the real and imag tensors separate as it makes complex gemm reuse existing efficient fp8 kernels)... So IMO it's normal that representations tend to evolve and several can exist to enable efficiency in different memory layout situations... This is to say that maybe the new exporter is also getting some mixed feedback at:
|
I agree. This has been moving very slowly (or at all). Some suggestions are welcomed!
Could you explain? If you are talking about runtime behavior, I expect, for example, if/when Abs supports complex inputs, the runtime will just do the right thing.
We are very aware of this. It will be further improved in torch 2.5. |
I guess I was just misunderstanding your point that not availability of dtype info for a given operator at export time is problematic... |
I guess first - basic ops related to FFT's / abs / pointwise ops. And then gemms and maybe some matrix ops - like mm/mv/eig/svd/linalg.solve... |
Right now, the strategy for handling complex op in the new exporter is that based on the tensor type (real, complex), we dispatch that to a different ONNX decomposition logic. For example, a real valued abs is def aten_abs(self: TRealOrUInt8) -> TRealOrUInt8:
"""abs(Tensor self) -> Tensor"""
return op.Abs(self) whereas a complex one is def aten_abs_complex(self: TRealOrUInt8) -> TRealOrUInt8:
"""abs(Tensor self) -> Tensor"""
# self_real = self[..., 0]
self_real = op.Slice(self, [0], [1], axes=[-1])
# self_imag = self[..., 1]
self_imag = op.Slice(self, [1], [2], axes=[-1])
real_pow = op.Pow(self_real, 2)
imag_pow = op.Pow(self_imag, 2)
real_plus_imag = op.Add(real_pow, imag_pow)
return op.Squeeze(op.Sqrt(real_plus_imag), axes=[-1]) Note that we are still taking a real tensor in ONNX, but we treat it as a special tensor where the last axis represents the real/imag parts. We are able to do this dispatching only by having the tensor type for each of the inputs of the aten ops available. Now, if ONNX |
I wonder if then As for current impl, probably eltwise |
|
@gramalingam I also wonder if 2 slices + add is going to be faster then reduce sum. |
A quick response: this means that two values are being added (in parallel across all tensor indices)? Using ReduceSum for adding 2 elements does not seem efficient. Usually ReduceSum is used for adding many elements together. This may just mean I haven't understood the context or the question correctly. I should probably read the whole thread. |
Ok, I went through the thread. I don't understand what approach using ReduceSum you are referring to. If you are referring to the implementation shown above for The implementation itself seems reasonable, don't see how it can be improved at this level. |
Sounds good, thanks! |
Yes. Philosophically, ONNX very much assumes that types can be determined statically and that type-based dispatch can be done statically (or ahead of execution time). (Eg., onnxruntime has no support for dynamic, type-based, dispatch inside the operator kernel.) |
Sorry, I understand your suggestion now: you mean adding the square of the real part and the imaginary part. I guess you could take it one step more, and use |
I guess you are saying this is a limitation of the old exporter, but not the new one? |
Edit: I got it working by changing The new dynamo exporter worked for me in Pytorch 2.6 in exporting to a LaMa Image Inpainting model (this one) with this code:
Then when I want to use the resulted model:
this loads it albeit with lots of but won't run with an error like: and where if optimizations aren't turned off or if I try to convert it into ort format, it will get an error of: the inference code is:
|
@justinchuby hi ,i'm sorry to boring u, but i don't export torch.fft.fft/ifft to onnx , and i see all u anwers ( update torch-nightly & onnx.dynamo_export), but don't work . Could you provide an official statement that the translation is complete? |
Can you update onnxscript? What is the version? |
🚀 The feature
Seems that more FFT-related ops are now supported by ONNX opset17 and onnxruntime:
It would be good to have torch.fft.rfft and friends to be natively exported without having to construct the basis manually.
I think this would also be import to torchaudio community
Also related: #107446
Motivation, pitch
N/A
Alternatives
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: