Skip to content

Commit a17eb07

Browse files
committed
Allow scipy_fft module to be used as a scipy fft backend
1 parent a9324ad commit a17eb07

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ and similar inverse c2r FFT (`irfft*`) functions.
7272

7373
The package also provides `mkl_fft.interfaces.numpy_fft` and `mkl_fft.interfaces.scipy_fft` interfaces which provide drop-in replacements for equivalent functions in NumPy and SciPy, respectively.
7474

75+
`mkl_fft.interfaces.scipy_fft` can also be used as a backend for `scipy.fft.set_backend()`
76+
77+
```python
78+
>>> import numpy as np, mkl_fft, mkl_fft.interfaces.scipy_fft as mkl_be, scipy, scipy.fft, mkl
79+
80+
>>> mkl.verbose(1)
81+
# True
82+
83+
>>> x = np.random.randn(8*7).reshape((7, 8))
84+
>>> with scipy.fft.set_backend(mkl_be, only=True):
85+
>>> ff = scipy.fft.fft2(x, workers=4)
86+
>>> ff2 = scipy.fft.fft2(x)
87+
# MKL_VERBOSE oneMKL 2025 Update 1 Product build 20250306 for Intel(R) 64 architecture Intel(R) Advanced Vector Extensions 2 (Intel(R) AVX2) enabled processors, Lnx 2.70GHz intel_thread
88+
# MKL_VERBOSE FFT(drfo7:8:8x8:1:1,input_strides:{0,8,1},output_strides:{0,8,1},bScale:0.0178571,tLim:1,unaligned_input,unaligned_output,desc:0x561750094440) 15.56us CNR:OFF Dyn:1 FastMM:1 TID:0 NThr:4
89+
90+
>>> np.allclose(ff, ff2)
91+
```
7592
---
7693

7794
To build `mkl_fft` from sources on Linux with Intel® OneMKL:

mkl_fft/_scipy_fft.py

+3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ def set_workers(n_workers):
139139
"get_workers",
140140
"set_workers",
141141
"DftiBackend",
142+
# Following needed for module to be used as scipy fft backend
143+
"__ua_domain__",
144+
"__ua_function__",
142145
]
143146

144147
__ua_domain__ = "numpy.scipy.fft"

mkl_fft/tests/test_interfaces.py

+6
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,9 @@ def test_axes(func):
166166
exp = np.fft.rfft2(x, axes=(1, 2))
167167
tol = 64 * np.finfo(np.float64).eps
168168
assert np.allclose(res, exp, atol=tol, rtol=tol)
169+
170+
171+
def test_scipy_fft_backend():
172+
"""scipy_fft exposes properties necessary for use as a scipy fft backend"""
173+
assert hasattr(mfi.scipy_fft, "__ua_domain__")
174+
assert hasattr(mfi.scipy_fft, "__ua_function__")

0 commit comments

Comments
 (0)