Repository navigation
add an interface for dask.fft
#214
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ test: | |
| requires: | ||
| - pytest | ||
| - scipy >=1.10 | ||
| - dask | ||
| imports: | ||
| - mkl_fft | ||
| - mkl_fft.interfaces | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,8 +27,17 @@ | |
|
|
||
| # find scipy, not scipy.fft, to avoid circular dependency | ||
| try: | ||
| # check to see if scipy is installed | ||
| import scipy | ||
| except ImportError: | ||
| pass | ||
| else: | ||
| from . import scipy_fft | ||
|
|
||
| try: | ||
| # check to see if dask is installed | ||
| import dask | ||
| except ImportError: | ||
| pass | ||
| else: | ||
| from . import dask_fft | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
| 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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| # This file includes tests from dask.fft module: | ||
| # https://github.com/dask/dask/blob/main/dask/array/tests/test_fft.py | ||
|
|
||
| import contextlib | ||
| from itertools import combinations_with_replacement | ||
|
|
||
| import dask | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this use |
||
| import dask.array as da | ||
| import numpy as np | ||
| import pytest | ||
| from dask.array.numpy_compat import NUMPY_GE_200 | ||
| from dask.array.utils import assert_eq, same_keys | ||
|
|
||
| import mkl_fft.interfaces.dask_fft as dask_fft | ||
|
|
||
| requires_dask_2024_8_2 = pytest.mark.skipif( | ||
| dask.__version__ < "2024.8.2", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This compares version strings, so |
||
| reason="norm kwarg requires Dask >= 2024.8.2", | ||
| ) | ||
|
|
||
| all_1d_funcnames = ["fft", "ifft", "rfft", "irfft", "hfft", "ihfft"] | ||
|
|
||
| all_nd_funcnames = [ | ||
| "fft2", | ||
| "ifft2", | ||
| "fftn", | ||
| "ifftn", | ||
| "rfft2", | ||
| "irfft2", | ||
| "rfftn", | ||
| "irfftn", | ||
| ] | ||
|
|
||
| if not da._array_expr_enabled(): | ||
|
|
||
| nparr = np.arange(100).reshape(10, 10) | ||
| darr = da.from_array(nparr, chunks=(1, 10)) | ||
| darr2 = da.from_array(nparr, chunks=(10, 1)) | ||
| darr3 = da.from_array(nparr, chunks=(10, 10)) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("funcname", all_1d_funcnames) | ||
| def test_cant_fft_chunked_axis(funcname): | ||
| da_fft = getattr(dask_fft, funcname) | ||
|
|
||
| bad_darr = da.from_array(nparr, chunks=(5, 5)) | ||
| for i in range(bad_darr.ndim): | ||
| with pytest.raises(ValueError): | ||
| da_fft(bad_darr, axis=i) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("funcname", all_1d_funcnames) | ||
| def test_fft(funcname): | ||
| da_fft = getattr(dask_fft, funcname) | ||
| np_fft = getattr(np.fft, funcname) | ||
|
|
||
| # pylint: disable=possibly-used-before-assignment | ||
| assert_eq(da_fft(darr), np_fft(nparr)) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("funcname", all_nd_funcnames) | ||
| def test_fft2n_shapes(funcname): | ||
| da_fft = getattr(dask_fft, funcname) | ||
| np_fft = getattr(np.fft, funcname) | ||
|
|
||
| # pylint: disable=possibly-used-before-assignment | ||
| assert_eq(da_fft(darr3), np_fft(nparr)) | ||
| assert_eq( | ||
| da_fft(darr3, (8, 9), axes=(1, 0)), np_fft(nparr, (8, 9), axes=(1, 0)) | ||
| ) | ||
| assert_eq( | ||
| da_fft(darr3, (12, 11), axes=(1, 0)), | ||
| np_fft(nparr, (12, 11), axes=(1, 0)), | ||
| ) | ||
|
|
||
| if NUMPY_GE_200 and funcname.endswith("fftn"): | ||
| ctx = pytest.warns( | ||
| DeprecationWarning, | ||
| match="`axes` should not be `None` if `s` is not `None`", | ||
| ) | ||
| else: | ||
| ctx = contextlib.nullcontext() | ||
| with ctx: | ||
| expect = np_fft(nparr, (8, 9)) | ||
| with ctx: | ||
| actual = da_fft(darr3, (8, 9)) | ||
| assert_eq(expect, actual) | ||
|
|
||
|
|
||
| @requires_dask_2024_8_2 | ||
| @pytest.mark.parametrize("funcname", all_1d_funcnames) | ||
| def test_fft_n_kwarg(funcname): | ||
| da_fft = getattr(dask_fft, funcname) | ||
| np_fft = getattr(np.fft, funcname) | ||
|
|
||
| assert_eq(da_fft(darr, 5), np_fft(nparr, 5)) | ||
| assert_eq(da_fft(darr, 13), np_fft(nparr, 13)) | ||
| assert_eq( | ||
| da_fft(darr, 13, norm="backward"), np_fft(nparr, 13, norm="backward") | ||
| ) | ||
| assert_eq(da_fft(darr, 13, norm="ortho"), np_fft(nparr, 13, norm="ortho")) | ||
| assert_eq( | ||
| da_fft(darr, 13, norm="forward"), np_fft(nparr, 13, norm="forward") | ||
| ) | ||
| # pylint: disable=possibly-used-before-assignment | ||
| assert_eq(da_fft(darr2, axis=0), np_fft(nparr, axis=0)) | ||
| assert_eq(da_fft(darr2, 5, axis=0), np_fft(nparr, 5, axis=0)) | ||
| assert_eq( | ||
| da_fft(darr2, 13, axis=0, norm="backward"), | ||
| np_fft(nparr, 13, axis=0, norm="backward"), | ||
| ) | ||
| assert_eq( | ||
| da_fft(darr2, 12, axis=0, norm="ortho"), | ||
| np_fft(nparr, 12, axis=0, norm="ortho"), | ||
| ) | ||
| assert_eq( | ||
| da_fft(darr2, 12, axis=0, norm="forward"), | ||
| np_fft(nparr, 12, axis=0, norm="forward"), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("funcname", all_1d_funcnames) | ||
| def test_fft_consistent_names(funcname): | ||
| da_fft = getattr(dask_fft, funcname) | ||
|
|
||
| assert same_keys(da_fft(darr, 5), da_fft(darr, 5)) | ||
| assert same_keys(da_fft(darr2, 5, axis=0), da_fft(darr2, 5, axis=0)) | ||
| assert not same_keys(da_fft(darr, 5), da_fft(darr, 13)) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("funcname", all_nd_funcnames) | ||
| @pytest.mark.parametrize("dtype", ["float32", "float64"]) | ||
| def test_nd_ffts_axes(funcname, dtype): | ||
| np_fft = getattr(np.fft, funcname) | ||
| da_fft = getattr(dask_fft, funcname) | ||
|
|
||
| shape = (7, 8, 9) | ||
| chunk_size = (3, 3, 3) | ||
| a = np.arange(np.prod(shape), dtype=dtype).reshape(shape) | ||
| d = da.from_array(a, chunks=chunk_size) | ||
|
|
||
| for num_axes in range(1, d.ndim): | ||
| for axes in combinations_with_replacement(range(d.ndim), num_axes): | ||
| cs = list(chunk_size) | ||
| for i in axes: | ||
| cs[i] = shape[i] | ||
| d2 = d.rechunk(cs) | ||
| if len(set(axes)) < len(axes): | ||
| with pytest.raises(ValueError): | ||
| da_fft(d2, axes=axes) | ||
| else: | ||
| r = da_fft(d2, axes=axes) | ||
| er = np_fft(a, axes=axes) | ||
| if np.lib.NumpyVersion(np.__version__) >= "2.0.0": | ||
| check_dtype = True | ||
| assert r.dtype == er.dtype | ||
| else: | ||
| check_dtype = False | ||
| assert r.shape == er.shape | ||
|
|
||
| assert_eq(r, er, check_dtype=check_dtype, rtol=1e-6, atol=1e-4) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Heads-up: this entry ended up in an old released section (next to the Python 3.13 entry, gh-164). Could you move it to the current dev section when you rebase?