Skip to content

update tests for dpnp.fft.irfft and dpnp.fft.hfft #1958

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

Merged
merged 4 commits into from
Aug 5, 2024
Merged
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
72 changes: 53 additions & 19 deletions tests/test_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@
get_all_dtypes,
get_complex_dtypes,
get_float_dtypes,
is_cpu_device,
)

# aspects of default device:
_def_device = dpctl.SyclQueue().sycl_device
_def_dev_has_fp64 = _def_device.has_aspect_fp64
is_gpu_with_fp64 = not is_cpu_device() and _def_dev_has_fp64

def _make_array_Hermitian(a, n):
"""
This function makes necessary changes of the input array of
`dpnp.fft.irfft` and `dpnp.fft.hfft` functions to make sure the
given array is Hermitian.

"""

a[0].imag = 0
if n in [None, 18]:
# f_ny is Nyquist mode (n//2+1 mode) which is n//2 element
f_ny = -1 if n is None else n // 2
a[f_ny].imag = 0
a[f_ny:] = 0 # no data needed after Nyquist mode

return a


# TODO: `assert_dtype_allclose` calls in this file have `check_only_type_kind=True`
Expand Down Expand Up @@ -391,11 +403,12 @@ def test_hfft_1D(self, dtype, n, norm):
assert_dtype_allclose(result, expected, check_only_type_kind=True)

@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 20])
@pytest.mark.parametrize("n", [None, 5, 18])
@pytest.mark.parametrize("norm", ["forward", "backward", "ortho"])
def test_hfft_1D_complex(self, dtype, n, norm):
x = dpnp.linspace(-1, 1, 11)
a = dpnp.sin(x) + 1j * dpnp.cos(x)
a = _make_array_Hermitian(a, n)
a = dpnp.asarray(a, dtype=dtype)
a_np = dpnp.asnumpy(a)

Expand Down Expand Up @@ -446,13 +459,13 @@ def test_fft_1D(self, dtype, n, norm):
# but dpnp return float32 if input is float32
assert_dtype_allclose(result, expected, check_only_type_kind=True)

@pytest.mark.skipif(is_gpu_with_fp64, reason="MKLD17702")
@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 20])
@pytest.mark.parametrize("n", [None, 5, 18])
@pytest.mark.parametrize("norm", ["forward", "backward", "ortho"])
def test_fft_1D_complex(self, dtype, n, norm):
x = dpnp.linspace(-1, 1, 11)
a = dpnp.sin(x) + 1j * dpnp.cos(x)
a = _make_array_Hermitian(a, n)
a = dpnp.asarray(a, dtype=dtype)
a_np = dpnp.asnumpy(a)

Expand All @@ -473,29 +486,50 @@ def test_fft_1D_on_2D_array(self, dtype, n, axis, norm, order):
expected = numpy.fft.irfft(a_np, n=n, axis=axis, norm=norm)
assert_dtype_allclose(result, expected, check_only_type_kind=True)

@pytest.mark.skipif(is_gpu_with_fp64, reason="MKLD17702")
@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 8])
@pytest.mark.parametrize("axis", [0, 1, 2])
@pytest.mark.parametrize("norm", ["forward", "backward", "ortho"])
@pytest.mark.parametrize("order", ["C", "F"])
def test_fft_1D_on_3D_array(self, dtype, n, axis, norm, order):
x1 = numpy.random.uniform(-10, 10, 24)
x2 = numpy.random.uniform(-10, 10, 24)
x1 = numpy.random.uniform(-10, 10, 120)
x2 = numpy.random.uniform(-10, 10, 120)
a_np = numpy.array(x1 + 1j * x2, dtype=dtype).reshape(
2, 3, 4, order=order
4, 5, 6, order=order
)
# each 1-D array of input should be Hermitian
if axis == 0:
a_np[0].imag = 0
if n is None:
# for axis=0 and n=8, Nyquist mode is not present
f_ny = -1 # Nyquist mode
a_np[-1].imag = 0
elif axis == 1:
a_np[:, 0, :].imag = 0
if n in [None, 8]:
f_ny = -1 # Nyquist mode
a_np[:, f_ny, :].imag = 0
a_np[:, f_ny:, :] = 0 # no data needed after Nyquist mode
elif axis == 2:
a_np[..., 0].imag = 0
if n in [None, 8]:
f_ny = -1 if n is None else n // 2 # Nyquist mode
a_np[..., f_ny].imag = 0
a_np[..., f_ny:] = 0 # no data needed after Nyquist mode

a = dpnp.asarray(a_np)

result = dpnp.fft.irfft(a, n=n, axis=axis, norm=norm)
expected = numpy.fft.irfft(a_np, n=n, axis=axis, norm=norm)
assert_dtype_allclose(result, expected, check_only_type_kind=True)
assert_dtype_allclose(
result, expected, check_only_type_kind=True, factor=16
)

@pytest.mark.skipif(is_gpu_with_fp64, reason="MKLD17702")
@pytest.mark.parametrize("n", [None, 5, 20])
@pytest.mark.parametrize("n", [None, 5, 18])
def test_fft_usm_ndarray(self, n):
x = dpt.linspace(-1, 1, 11)
a = dpt.sin(x) + 1j * dpt.cos(x)
x = dpnp.linspace(-1, 1, 11)
a = dpnp.sin(x) + 1j * dpnp.cos(x)
a = _make_array_Hermitian(a, n)
a_usm = dpt.asarray(a, dtype=dpt.complex64)
a_np = dpt.asnumpy(a_usm)
out_shape = n if n is not None else 2 * (a_usm.shape[0] - 1)
Expand All @@ -506,13 +540,13 @@ def test_fft_usm_ndarray(self, n):
expected = numpy.fft.irfft(a_np, n=n)
assert_dtype_allclose(result, expected, check_only_type_kind=True)

@pytest.mark.skipif(is_gpu_with_fp64, reason="MKLD17702")
@pytest.mark.parametrize("dtype", get_complex_dtypes())
@pytest.mark.parametrize("n", [None, 5, 20])
@pytest.mark.parametrize("n", [None, 5, 18])
@pytest.mark.parametrize("norm", ["forward", "backward", "ortho"])
def test_fft_1D_out(self, dtype, n, norm):
x = dpnp.linspace(-1, 1, 11)
a = dpnp.sin(x) + 1j * dpnp.cos(x)
a = _make_array_Hermitian(a, n)
a = dpnp.asarray(a, dtype=dtype)
a_np = dpnp.asnumpy(a)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ def test_fft(func, device):

expected = getattr(numpy.fft, func)(data)
result = getattr(dpnp.fft, func)(dpnp_data)
assert_dtype_allclose(result, expected)
assert_dtype_allclose(result, expected, factor=16)

expected_queue = dpnp_data.get_array().sycl_queue
result_queue = result.get_array().sycl_queue
Expand Down
2 changes: 1 addition & 1 deletion tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def test_large_values(self, dtype):
assert_dtype_allclose(result, expected)


class TestLogaddexp:
class TestLogAddExp:
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True))
def test_logaddexp(self, dtype):
np_array1, np_array2, expected = _get_numpy_arrays_2in_1out(
Expand Down
Loading