Fix FFT export truncation by preserving dft_length in rFFT lowering - #2991
Fix FFT export truncation by preserving dft_length in rFFT lowering#2991Justin Chu (justinchuby) with Copilot wants to merge 2 commits into
Conversation
Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes an ONNX export correctness issue in the Torch FFT lowering path by explicitly preserving and forwarding the effective transform length (dft_length) into ONNX DFT during real-to-complex (rFFT) lowering, preventing silent spectrum truncation and incorrect exported output shapes.
Changes:
- Compute the active transform-axis length from the input tensor shape during
aten::_fft_r2clowering and pass it explicitly asdft_lengthto ONNXDFT. - Feed the same computed
dft_lengthinto the normalization helper to keep scaling consistent with the transformed signal length. - Add an end-to-end regression test asserting the exported output shape for
torch.fft.rfft(x, n=512, dim=-1)is[4, 257].
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| onnxscript/function_libs/torch_lib/ops/fft.py | Preserves the effective signal length by computing and passing dft_length into ONNX DFT during rFFT lowering. |
| tests/function_libs/torch_lib/e2e_ops_tests.py | Adds a regression test to ensure exported rFFT output shapes are not truncated. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| onnx_program = torch.onnx.export( | ||
| RFFTModel(), | ||
| (x,), | ||
| opset_version=20, | ||
| dynamo=True, | ||
| optimize=False, | ||
| ) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2991 +/- ##
=======================================
Coverage 72.63% 72.63%
=======================================
Files 265 265
Lines 32208 32209 +1
Branches 3042 3042
=======================================
+ Hits 23395 23396 +1
Misses 7779 7779
Partials 1034 1034 ☔ View full report in Codecov by Harness. |
The FFT lowering path was exporting real-to-complex transforms without carrying the active signal length into the ONNX
DFTnode. In some conversions, that alloweddft_lengthto default to1, silently truncating the spectrum and producing incorrect output shapes fortorch.fft.rfft.Summary
aten::_fft_r2cexport so the generated ONNX graph matches PyTorch FFT semantics.Changes
onnxscript/function_libs/torch_lib/ops/fft.py, compute the current axis length from the tensor shape and pass it explicitly asdft_lengthwhen calling ONNXDFT.tests/function_libs/torch_lib/e2e_ops_tests.pycoveringtorch.fft.rfft(x, n=512, dim=-1)and asserting the exported output shape remains[4, 257].dft_length = 1inDFT, breaking shape-inference fortorch.fft.rfftpytorch/pytorch#155997