Skip to content

fix: match SciPy rfft2 positional argument order - #419

Open
vera-rubin wants to merge 1 commit into
IntelPython:masterfrom
vera-rubin:fix/scipy-rfft2-norm
Open

vera-rubin wants to merge 1 commit into
IntelPython:masterfrom
vera-rubin:fix/scipy-rfft2-norm

Conversation

@vera-rubin

Copy link
Copy Markdown

Description

mkl_fft.interfaces.scipy_fft.rfft2 declares overwrite_x before norm, while scipy.fft.rfft2 is rfft2(x, s=None, axes=(-2, -1), norm=None, overwrite_x=False, workers=None, *, plan=None). A call that passes norm positionally, as SciPy allows, binds the string to overwrite_x and the normalization is silently ignored. For a 2x2 array of ones this returns (4+0j) instead of SciPy's (2+0j):

import numpy as np
from mkl_fft.interfaces import scipy_fft
x = np.ones((2, 2), dtype=np.float64)
print(scipy_fft.rfft2(x, None, (-2, -1), "ortho")[0, 0])

The fix puts norm before overwrite_x, as in SciPy and as the other functions in this module already do (rfft2 was the only one with the two swapped). A regression test compares the positional call with scipy.fft.rfft2 for all four norm values and both real precisions; it fails for "forward" and "ortho" without the fix and passes with it.

Verification

Linux x86_64, Python 3.12.3, NumPy 2.5.5, SciPy 1.18.1, MKL 2026.2, built with pip install -e . --no-build-isolation on master 84ce3f4.

  • Tests: python -m pytest mkl_fft/tests -q: 2817 passed, 2 skipped (without the fix: the 4 new forward/ortho cases fail, everything else passes).
  • Lint: not run locally.

Not verified

Pre-commit hooks, Windows and macOS: left to CI.

Checklist

  • NumPy/SciPy FFT API compatibility preserved, or the break is intentional and called out above.
  • Behavior changes have tests in mkl_fft/tests/; bug fixes have a regression test.
  • CHANGELOG.md updated under ## [dev] with a [gh-NNN] link, or the change isn't user-visible. (Entry added under ### Fixed; the link will be added once this PR has a number.)

ChipLab's AI agents
Nathan Chandrasekar

Copilot AI balanced review requested due to automatic review settings December 4, 2026 01:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@antonwolfy antonwolfy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is right: rfft2 now has the same signature as scipy.fft.rfft2, and it was the only function in scipy_fft or numpy_fft whose signature differed from SciPy 1.18.1 / NumPy 2.5.5. But the new test fails CI (inline).

CI. Test Linux Wheels and Test Windows Wheels for Python 3.15 and 3.15t fail, each only on the 8 cases of test_scipy_rfft2_positional_norm, with AttributeError: module 'mkl_fft.interfaces' has no attribute 'scipy_fft'. Those jobs install SciPy but not mkl-service (python_version < "3.15" marker), so mkl_fft.interfaces has no scipy_fft there. All other jobs pass.

scipy.fft.set_backend was affected too. SciPy forwards positional arguments to the backend unchanged, so on master scipy.fft.rfft2(x, None, (-2, -1), "ortho") inside scipy.fft.set_backend(mkl_fft.interfaces.scipy_fft, only=True) also ignored norm; with this PR it matches SciPy. Worth a line in the description, since the bug was not limited to direct calls.

Also checked, no issues: SciPy has had norm before overwrite_x in rfft2 since 1.4, so every supported SciPy was affected; nothing in the repo calls rfft2 positionally; overwrite_x passed as the 5th positional argument now behaves like SciPy (on master it raised ValueError: Invalid norm value False).

@pytest.mark.parametrize("norm", [None, "forward", "backward", "ortho"])
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_scipy_rfft2_positional_norm(norm, dtype):
sp_fft = pytest.importorskip("scipy.fft", reason="requires scipy")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails the four Python 3.15 wheel test jobs: mkl-service is not installed there, so mfi.scipy_fft does not exist and all 8 cases raise AttributeError instead of skipping. importorskip("scipy.fft") only covers a missing SciPy. The other scipy_fft tests in this file call require_scipy_fft() first:

Suggested change
sp_fft = pytest.importorskip("scipy.fft", reason="requires scipy")
require_scipy_fft()
sp_fft = pytest.importorskip("scipy.fft", reason="requires scipy")

Comment thread CHANGELOG.md
* `irfft` with `out=` now writes directly into `out` when the cast or zero-padded copy of a non-contiguous input has the same memory order as `out`, avoiding a temporary array and an extra copy [gh-396](https://github.com/IntelPython/mkl_fft/pull/396)

### Fixed
* Fixed `mkl_fft.interfaces.scipy_fft.rfft2` ignoring a positional `norm` argument by matching SciPy's parameter order

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CONTRIBUTING.md asks for a [gh-NNN] link, and the PR has its number now:

Suggested change
* Fixed `mkl_fft.interfaces.scipy_fft.rfft2` ignoring a positional `norm` argument by matching SciPy's parameter order
* Fixed `mkl_fft.interfaces.scipy_fft.rfft2` ignoring a positional `norm` argument by matching SciPy's parameter order [gh-419](https://github.com/IntelPython/mkl_fft/pull/419)

assert np.allclose(x, xx, atol=tol, rtol=tol)


@pytest.mark.parametrize("norm", [None, "forward", "backward", "ortho"])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: only "forward" and "ortho" can fail on master. None keeps norm at its default, and "backward" lands in overwrite_x while norm stays at its default, which is backward anyway.

A signature check would also guard the other scipy_fft functions against the same kind of mismatch, not only rfft2. This one fails on master (AssertionError: rfft2) and passes with this PR; it needs import inspect:

def test_scipy_fft_signatures_match_scipy():
    require_scipy_fft()
    sp_fft = pytest.importorskip("scipy.fft", reason="requires scipy")
    for name in mfi.scipy_fft.__all__:
        if not hasattr(sp_fft, name):
            continue
        expected = inspect.signature(getattr(sp_fft, name))
        assert inspect.signature(getattr(mfi.scipy_fft, name)) == expected, name

@antonwolfy antonwolfy added this to the 2.5.0 release milestone Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants