Skip to content

add an interface for dask.fft #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-with-clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ jobs:
- name: Run mkl_fft tests
run: |
source ${{ env.ONEAPI_ROOT }}/setvars.sh
pip install scipy mkl-service pytest
pip install pytest mkl-service scipy dask
pytest -s -v --pyargs mkl_fft
4 changes: 2 additions & 2 deletions .github/workflows/conda-package-cf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ jobs:
- name: Install mkl_fft
run: |
CHANNELS="-c $GITHUB_WORKSPACE/channel ${{ env.CHANNELS }}"
conda create -n ${{ env.TEST_ENV_NAME }} python=${{ matrix.python_ver }} ${{ matrix.numpy }} $PACKAGE_NAME pytest scipy $CHANNELS
conda create -n ${{ env.TEST_ENV_NAME }} python=${{ matrix.python_ver }} ${{ matrix.numpy }} $PACKAGE_NAME pytest $CHANNELS
# Test installed packages
conda list -n ${{ env.TEST_ENV_NAME }}

Expand Down Expand Up @@ -295,7 +295,7 @@ jobs:
FOR /F "tokens=* USEBACKQ" %%F IN (`python -c "%SCRIPT%"`) DO (
SET PACKAGE_VERSION=%%F
)
SET "TEST_DEPENDENCIES=pytest scipy"
SET "TEST_DEPENDENCIES=pytest"
conda install -n ${{ env.TEST_ENV_NAME }} ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% %TEST_DEPENDENCIES% python=${{ matrix.python }} ${{ matrix.numpy }} -c ${{ env.workdir }}/channel ${{ env.CHANNELS }}

- name: Report content of test environment
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/conda-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ jobs:
- name: Install mkl_fft
run: |
CHANNELS="-c $GITHUB_WORKSPACE/channel ${{ env.CHANNELS }}"
conda create -n ${{ env.TEST_ENV_NAME }} python=${{ matrix.python }} "scipy>=1.10" $CHANNELS
conda install -n ${{ env.TEST_ENV_NAME }} $PACKAGE_NAME pytest $CHANNELS
conda create -n ${{ env.TEST_ENV_NAME }} $PACKAGE_NAME python=${{ matrix.python }} pytest $CHANNELS
# Test installed packages
conda list -n ${{ env.TEST_ENV_NAME }}

Expand Down Expand Up @@ -296,7 +295,7 @@ jobs:
FOR /F "tokens=* USEBACKQ" %%F IN (`python -c "%SCRIPT%"`) DO (
SET PACKAGE_VERSION=%%F
)
SET "TEST_DEPENDENCIES=pytest scipy"
SET "TEST_DEPENDENCIES=pytest"
conda install -n ${{ env.TEST_ENV_NAME }} ${{ env.PACKAGE_NAME }}=%PACKAGE_VERSION% %TEST_DEPENDENCIES% python=${{ matrix.python }} -c ${{ env.workdir }}/channel ${{ env.CHANNELS }}

- name: Report content of test environment
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added Hermitian FFT functions to SciPy interface `mkl_fft.interfaces.scipy_fft`: `hfft`, `ihfft`, `hfftn`, `ihfftn`, `hfft2`, and `ihfft2` [gh-161](https://github.com/IntelPython/mkl_fft/pull/161)
* Added support for `out` kwarg to all FFT functions in `mkl_fft` and `mkl_fft.interfaces.numpy_fft` [gh-157](https://github.com/IntelPython/mkl_fft/pull/157)
* Added `fftfreq`, `fftshift`, `ifftshift`, and `rfftfreq` to both NumPy and SciPy interfaces [gh-179](https://github.com/IntelPython/mkl_fft/pull/179)
* Added a new interface for FFT module of Dask accessible through `mkl_fft.interfaces.dask_fft` [gh-184](https://github.com/IntelPython/mkl_fft/pull/184)

### Changed
* NumPy interface `mkl_fft.interfaces.numpy_fft` is aligned with numpy-2.x.x [gh-139](https://github.com/IntelPython/mkl_fft/pull/139), [gh-157](https://github.com/IntelPython/mkl_fft/pull/157)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ More details can be found in [SciPy 2017 conference proceedings](https://github.

---

The `mkl_fft` package offers interfaces that act as drop-in replacements for equivalent functions in NumPy and SciPy. Learn more about these interfaces [here](https://github.com/IntelPython/mkl_fft/blob/master/mkl_fft/interfaces/README.md).
The `mkl_fft` package offers interfaces that act as drop-in replacements for equivalent functions in NumPy, SciPy, and Dask. Learn more about these interfaces [here](https://github.com/IntelPython/mkl_fft/blob/master/mkl_fft/interfaces/README.md).

While using these interfaces is the easiest way to leverage `mk_fft`, one can also use `mkl_fft` directly with the following FFT functions:

Expand Down
4 changes: 3 additions & 1 deletion conda-recipe-cf/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ requirements:
- python
- mkl-service
- numpy
- scipy >=1.10.1
- dask

test:
commands:
- pytest -v --pyargs mkl_fft
requires:
- pytest
- scipy
imports:
- mkl_fft
- mkl_fft.interfaces
- mkl_fft.interfaces.numpy_fft
- mkl_fft.interfaces.scipy_fft
- mkl_fft.interfaces.dask_fft

about:
home: http://github.com/IntelPython/mkl_fft
Expand Down
4 changes: 3 additions & 1 deletion conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ requirements:
- python
- mkl-service
- {{ pin_compatible('numpy') }}
- scipy >=1.10.1
- dask

test:
commands:
- pytest -v --pyargs mkl_fft
requires:
- pytest
- scipy
imports:
- mkl_fft
- mkl_fft.interfaces
- mkl_fft.interfaces.numpy_fft
- mkl_fft.interfaces.scipy_fft
- mkl_fft.interfaces.dask_fft

about:
home: http://github.com/IntelPython/mkl_fft
Expand Down
42 changes: 41 additions & 1 deletion mkl_fft/interfaces/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Interfaces
The `mkl_fft` package provides interfaces that serve as drop-in replacements for equivalent functions in NumPy and SciPy.
The `mkl_fft` package provides interfaces that serve as drop-in replacements for equivalent functions in NumPy, SciPy, and Dask.

---

Expand Down Expand Up @@ -102,3 +102,43 @@ with scipy.fft.set_backend(mkl_backend, only=True):
print(f"Time with OneMKL FFT backend installed: {t2:.1f} seconds")
# Time with MKL FFT backend installed: 9.1 seconds
```

---

## Dask interface - `mkl_fft.interfaces.dask_fft`

This interface is a drop-in replacement for the [`dask.fft`](https://dask.pydata.org/en/latest/array-api.html#fast-fourier-transforms) module and includes **all** the functions available there:

* complex-to-complex FFTs: `fft`, `ifft`, `fft2`, `ifft2`, `fftn`, `ifftn`.

* real-to-complex and complex-to-real FFTs: `rfft`, `irfft`, `rfft2`, `irfft2`, `rfftn`, `irfftn`.

* Hermitian FFTs: `hfft`, `ihfft`.

* Helper routines: `fft_wrap`, `fftfreq`, `rfftfreq`, `fftshift`, `ifftshift`. These routines serve as a fallback to the Dask implementation and are included for completeness.

The following example shows how to use this interface for calculating a 2D FFT.

```python
import numpy, dask
import mkl_fft.interfaces.dask_fft as dask_fft

a = numpy.random.randn(128, 64) + 1j*numpy.random.randn(128, 64)
x = dask.array.from_array(a, chunks=(64, 64))
lazy_res = dask_fft.fft(x)
mkl_res = lazy_res.compute()
np_res = numpy.fft.fft(a)
numpy.allclose(mkl_res, np_res)
# True

# There are two chunks in this example based on the size of input array (128, 64) and chunk size (64, 64)
# to confirm that MKL FFT is called twice, turn on verbosity
import mkl
mkl.verbose(1)
# True

mkl_res = lazy_res.compute() # MKL_VERBOSE FFT is shown twice below which means MKL FFT is called twice
# MKL_VERBOSE oneMKL 2024.0 Update 2 Patch 2 Product build 20240823 for Intel(R) 64 architecture Intel(R) Advanced Vector Extensions 512 (Intel(R) AVX-512) with support for INT8, BF16, FP16 (limited) instructions, and Intel(R) Advanced Matrix Extensions (Intel(R) AMX) with INT8 and BF16, Lnx 3.80GHz intel_thread
# MKL_VERBOSE FFT(dcfo64*64,input_strides:{0,1},output_strides:{0,1},input_distance:64,output_distance:64,bScale:0.015625,tLim:32,unaligned_input,desc:0x7fd000010e40) 432.84us CNR:OFF Dyn:1 FastMM:1 TID:0 NThr:112
# MKL_VERBOSE FFT(dcfo64*64,input_strides:{0,1},output_strides:{0,1},input_distance:64,output_distance:64,bScale:0.015625,tLim:32,unaligned_input,desc:0x7fd480011300) 499.00us CNR:OFF Dyn:1 FastMM:1 TID:0 NThr:112
```
2 changes: 2 additions & 0 deletions mkl_fft/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from . import numpy_fft, scipy_fft

from . import dask_fft # isort: skip
67 changes: 67 additions & 0 deletions mkl_fft/interfaces/dask_fft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python
# Copyright (c) 2025, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Intel Corporation nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from dask.array.fft import fft_wrap, fftfreq, fftshift, ifftshift, rfftfreq

from . import numpy_fft as _numpy_fft

__all__ = [
"fft",
"ifft",
"fft2",
"ifft2",
"fftn",
"ifftn",
"rfft",
"irfft",
"rfft2",
"irfft2",
"rfftn",
"irfftn",
"hfft",
"ihfft",
"fftshift",
"ifftshift",
"fftfreq",
"rfftfreq",
"fft_wrap",
]


fft = fft_wrap(_numpy_fft.fft)
ifft = fft_wrap(_numpy_fft.ifft)
fft2 = fft_wrap(_numpy_fft.fft2)
ifft2 = fft_wrap(_numpy_fft.ifft2)
fftn = fft_wrap(_numpy_fft.fftn)
ifftn = fft_wrap(_numpy_fft.ifftn)
rfft = fft_wrap(_numpy_fft.rfft)
irfft = fft_wrap(_numpy_fft.irfft)
rfft2 = fft_wrap(_numpy_fft.rfft2)
irfft2 = fft_wrap(_numpy_fft.irfft2)
rfftn = fft_wrap(_numpy_fft.rfftn)
irfftn = fft_wrap(_numpy_fft.irfftn)
hfft = fft_wrap(_numpy_fft.hfft)
ihfft = fft_wrap(_numpy_fft.ihfft)
8 changes: 7 additions & 1 deletion mkl_fft/tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,16 @@ def test_axes(func):


@pytest.mark.parametrize(
"interface", [mfi.scipy_fft, mfi.numpy_fft], ids=["scipy", "numpy"]
"interface",
[mfi.scipy_fft, mfi.numpy_fft, mfi.dask_fft],
ids=["scipy", "numpy", "dask"],
)
@pytest.mark.parametrize(
"func", ["fftshift", "ifftshift", "fftfreq", "rfftfreq"]
)
def test_interface_helper_functions(interface, func):
assert hasattr(interface, func)


def test_dask_fftwrap():
assert hasattr(mfi.dask_fft, "fft_wrap")
Loading
Loading