Repository navigation
fix: keep C order when zero-padding input that is both C- and F-contiguous - #397
Merged
Merged
Conversation
…guous `_pad_array` allocated the zero-padded copy in F order whenever the input had the F-contiguous flag. An input with a single non-unit dimension, such as shape (m, 1), has both flags, so padding it along the unit axis gave an F-contiguous copy of shape (m, n), and so an F-contiguous result of `fft`, `ifft`, `rfft` and `irfft`, where NumPy returns a C-contiguous one. It also made a C-ordered `out` incompatible with the copy passed to MKL, so the result was computed into a temporary array and copied into `out`. Use F order only when the input is F- but not C-contiguous, which is what NumPy's PyArray_ISFORTRAN checks.
antonwolfy
requested review from
jharlow-intel,
ndgrigorian,
vlad-perevezentsev and
xaleryb
as code owners
October 20, 2026 14:10
jharlow-intel
approved these changes
Nov 9, 2026
jharlow-intel
left a comment
Contributor
There was a problem hiding this comment.
Thanks, looks good, the timings make the case clearly.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the zero-padding layout correction.
No open issues 🎉
What changed in this PR
This PR aligns zero-padding of inputs that are both C- and F-contiguous with NumPy’s C-order behavior, complementing #396’s direct-output optimization.
Changes:
- Use F-order padding only for inputs that are F-contiguous but not C-contiguous.
- Add layout and numerical regression tests for four FFT functions, with and without
out. - Document the layout correction and avoided output copy.
Root AGENTS.md was unavailable, so further repository inspection was stopped.
| File | Description |
|---|---|
mkl_fft/tests/test_fft1d.py |
Tests both-contiguous input padding across shapes and FFT functions. |
mkl_fft/_pydfti.pyx |
Uses PyArray_ISFORTRAN to select padding order. |
CHANGELOG.md |
Records the padding-layout fix and output-copy benefit. |
Review effort: Balanced
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.
Follow-up to #396.
_pad_arrayallocated the zero-padded copy of the input in F order whenever the input had the F-contiguous flag. An input with a single non-unit dimension, such as shape(m, 1), has both the C- and F-contiguous flags, so padding it along the unit axis (nlarger than the input length) gave an F-contiguous copy of shape(m, n). That had two effects:fft,ifft,rfftandirfft(and of the N-D functions, which pad through the same helper) was F-contiguous, where NumPy returns a C-contiguous array;outdid not match the layout of the copy passed to MKL, so the result was computed into a temporary array and then copied intoout. With perf: check x_arr, not x, when deciding whether irfft can write into out directly #396 this also applies toirfft, which before only got the direct path by checking the original input.This PR uses F order only when the input is F- but not C-contiguous (
PyArray_ISFORTRAN), so a both-contiguous input is padded into a C-ordered copy, as NumPy does. Input that is only F-contiguous is still padded in F order.Result layout for padding to
n=8, NumPy 2.5.4 vs mkl_fft on master:(5, 1)(1, 5)(4, 1, 1)(1, 1, 4)The same holds for
fft,ifft,rfftandirfft.Timings for input of shape
(4096, 1)padded ton=64along axis 1 with a C-orderedout, on Lunar Lake, median in ms:fft, C-orderedoutrfft, C-orderedoutirfft, C-orderedoutfft, noouttest_pad_both_contiguous_inputchecks the result layout and values with and withoutoutfor these shapes; it fails on master and passes with this change. The full test suite passes with this PR alone and merged with #396, and the two PRs merge without conflicts.