Support real-valued input in torch.fft.irfft converter#2721
Open
adityasingh2400 wants to merge 1 commit into
Open
Support real-valued input in torch.fft.irfft converter#2721adityasingh2400 wants to merge 1 commit into
adityasingh2400 wants to merge 1 commit into
Conversation
torch.fft.irfft accepts a real-valued input and treats it as a complex tensor with a zero imaginary part. The converter passed the input straight to the complex_irfft dialect op, which only accepts complex64, so converting a model that calls irfft on a real tensor failed with: Op "complex_irfft_0" (op_type: complex_irfft) Input data=... expects tensor or scalar of dtype from type domain ['complex64'] but got tensor[3,fp32] Promote a real input to complex with a zero imaginary part before lowering, matching PyTorch semantics. Complex inputs are unchanged. Added a regression test exercising real-input irfft across n, dim and norm. Fixes apple#2130.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Converting a model that calls
torch.fft.irffton a real-valued tensor fails:torch.fft.irfftaccepts a real-valued input and treats it as a complex tensor with a zero imaginary part (a one-sided Hermitian spectrum is real at DC). Thefft_irffthandler passed the traced input straight to thecomplex_irfftdialect op, which only acceptscomplex64, so any real input is rejected at conversion time. The underlying IRFFT lowering already exists and works; only the real-to-complex promotion was missing.Fixes #2130.
Changes
coremltools/converters/mil/frontend/torch/ops.pyfft_irfft, if the input is not already complex, build a zero imaginary part withmb.filland wrap it into a complex value viamb.complexbefore callingcomplex_irfft. This mirrors what thecomplex_fft/complex_fftnlowering already does for real inputs and matches PyTorch semantics. Complex inputs are passed through unchanged.coremltools/converters/mil/frontend/torch/test/test_torch_ops.pytest_fft_irfft_real_inputtoTestFft, parametrized overn,dim, andnorm. Unlike the existingtest_fft_basic, the input is not wrapped withtorch.complex, so it covers the real-input path from the issue.Testing
Red/green verified end to end (
ct.convertthenpredictonComputeUnit.CPU_ONLY, compared againsttorch.fft.irfft):complex_irfftdtype error above.test_fft_irfft_real_inputpasses (36/36 cases), and the existingTestFfttests are unaffected.Numeric agreement against PyTorch across default args, explicit
n(pad and trim), everynormmode, and 1D/2D/3D inputs:The original issue snippet now converts and produces
[0.15625, 0.78125, 0.78125, 0.78125], matching PyTorch exactly.Implemented with the help of a coding agent.