Skip to content

Commit d353299

Browse files
vtavanaantonwolfy
andauthored
update tests for dpnp.fft.irfft and dpnp.fft.hfft (#1958)
* unmute tests * reverse unintended changes * address comment --------- Co-authored-by: Anton <100830759+antonwolfy@users.noreply.github.com>
1 parent 543605c commit d353299

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

tests/test_fft.py

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@
1313
get_all_dtypes,
1414
get_complex_dtypes,
1515
get_float_dtypes,
16-
is_cpu_device,
1716
)
1817

19-
# aspects of default device:
20-
_def_device = dpctl.SyclQueue().sycl_device
21-
_def_dev_has_fp64 = _def_device.has_aspect_fp64
22-
is_gpu_with_fp64 = not is_cpu_device() and _def_dev_has_fp64
18+
19+
def _make_array_Hermitian(a, n):
20+
"""
21+
This function makes necessary changes of the input array of
22+
`dpnp.fft.irfft` and `dpnp.fft.hfft` functions to make sure the
23+
given array is Hermitian.
24+
25+
"""
26+
27+
a[0].imag = 0
28+
if n in [None, 18]:
29+
# f_ny is Nyquist mode (n//2+1 mode) which is n//2 element
30+
f_ny = -1 if n is None else n // 2
31+
a[f_ny].imag = 0
32+
a[f_ny:] = 0 # no data needed after Nyquist mode
33+
34+
return a
2335

2436

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

393405
@pytest.mark.parametrize("dtype", get_complex_dtypes())
394-
@pytest.mark.parametrize("n", [None, 5, 20])
406+
@pytest.mark.parametrize("n", [None, 5, 18])
395407
@pytest.mark.parametrize("norm", ["forward", "backward", "ortho"])
396408
def test_hfft_1D_complex(self, dtype, n, norm):
397409
x = dpnp.linspace(-1, 1, 11)
398410
a = dpnp.sin(x) + 1j * dpnp.cos(x)
411+
a = _make_array_Hermitian(a, n)
399412
a = dpnp.asarray(a, dtype=dtype)
400413
a_np = dpnp.asnumpy(a)
401414

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

449-
@pytest.mark.skipif(is_gpu_with_fp64, reason="MKLD17702")
450462
@pytest.mark.parametrize("dtype", get_complex_dtypes())
451-
@pytest.mark.parametrize("n", [None, 5, 20])
463+
@pytest.mark.parametrize("n", [None, 5, 18])
452464
@pytest.mark.parametrize("norm", ["forward", "backward", "ortho"])
453465
def test_fft_1D_complex(self, dtype, n, norm):
454466
x = dpnp.linspace(-1, 1, 11)
455467
a = dpnp.sin(x) + 1j * dpnp.cos(x)
468+
a = _make_array_Hermitian(a, n)
456469
a = dpnp.asarray(a, dtype=dtype)
457470
a_np = dpnp.asnumpy(a)
458471

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

476-
@pytest.mark.skipif(is_gpu_with_fp64, reason="MKLD17702")
477489
@pytest.mark.parametrize("dtype", get_complex_dtypes())
478490
@pytest.mark.parametrize("n", [None, 5, 8])
479491
@pytest.mark.parametrize("axis", [0, 1, 2])
480492
@pytest.mark.parametrize("norm", ["forward", "backward", "ortho"])
481493
@pytest.mark.parametrize("order", ["C", "F"])
482494
def test_fft_1D_on_3D_array(self, dtype, n, axis, norm, order):
483-
x1 = numpy.random.uniform(-10, 10, 24)
484-
x2 = numpy.random.uniform(-10, 10, 24)
495+
x1 = numpy.random.uniform(-10, 10, 120)
496+
x2 = numpy.random.uniform(-10, 10, 120)
485497
a_np = numpy.array(x1 + 1j * x2, dtype=dtype).reshape(
486-
2, 3, 4, order=order
498+
4, 5, 6, order=order
487499
)
500+
# each 1-D array of input should be Hermitian
501+
if axis == 0:
502+
a_np[0].imag = 0
503+
if n is None:
504+
# for axis=0 and n=8, Nyquist mode is not present
505+
f_ny = -1 # Nyquist mode
506+
a_np[-1].imag = 0
507+
elif axis == 1:
508+
a_np[:, 0, :].imag = 0
509+
if n in [None, 8]:
510+
f_ny = -1 # Nyquist mode
511+
a_np[:, f_ny, :].imag = 0
512+
a_np[:, f_ny:, :] = 0 # no data needed after Nyquist mode
513+
elif axis == 2:
514+
a_np[..., 0].imag = 0
515+
if n in [None, 8]:
516+
f_ny = -1 if n is None else n // 2 # Nyquist mode
517+
a_np[..., f_ny].imag = 0
518+
a_np[..., f_ny:] = 0 # no data needed after Nyquist mode
519+
488520
a = dpnp.asarray(a_np)
489521

490522
result = dpnp.fft.irfft(a, n=n, axis=axis, norm=norm)
491523
expected = numpy.fft.irfft(a_np, n=n, axis=axis, norm=norm)
492-
assert_dtype_allclose(result, expected, check_only_type_kind=True)
524+
assert_dtype_allclose(
525+
result, expected, check_only_type_kind=True, factor=16
526+
)
493527

494-
@pytest.mark.skipif(is_gpu_with_fp64, reason="MKLD17702")
495-
@pytest.mark.parametrize("n", [None, 5, 20])
528+
@pytest.mark.parametrize("n", [None, 5, 18])
496529
def test_fft_usm_ndarray(self, n):
497-
x = dpt.linspace(-1, 1, 11)
498-
a = dpt.sin(x) + 1j * dpt.cos(x)
530+
x = dpnp.linspace(-1, 1, 11)
531+
a = dpnp.sin(x) + 1j * dpnp.cos(x)
532+
a = _make_array_Hermitian(a, n)
499533
a_usm = dpt.asarray(a, dtype=dpt.complex64)
500534
a_np = dpt.asnumpy(a_usm)
501535
out_shape = n if n is not None else 2 * (a_usm.shape[0] - 1)
@@ -506,13 +540,13 @@ def test_fft_usm_ndarray(self, n):
506540
expected = numpy.fft.irfft(a_np, n=n)
507541
assert_dtype_allclose(result, expected, check_only_type_kind=True)
508542

509-
@pytest.mark.skipif(is_gpu_with_fp64, reason="MKLD17702")
510543
@pytest.mark.parametrize("dtype", get_complex_dtypes())
511-
@pytest.mark.parametrize("n", [None, 5, 20])
544+
@pytest.mark.parametrize("n", [None, 5, 18])
512545
@pytest.mark.parametrize("norm", ["forward", "backward", "ortho"])
513546
def test_fft_1D_out(self, dtype, n, norm):
514547
x = dpnp.linspace(-1, 1, 11)
515548
a = dpnp.sin(x) + 1j * dpnp.cos(x)
549+
a = _make_array_Hermitian(a, n)
516550
a = dpnp.asarray(a, dtype=dtype)
517551
a_np = dpnp.asnumpy(a)
518552

tests/test_sycl_queue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ def test_fft(func, device):
12391239

12401240
expected = getattr(numpy.fft, func)(data)
12411241
result = getattr(dpnp.fft, func)(dpnp_data)
1242-
assert_dtype_allclose(result, expected)
1242+
assert_dtype_allclose(result, expected, factor=16)
12431243

12441244
expected_queue = dpnp_data.get_array().sycl_queue
12451245
result_queue = result.get_array().sycl_queue

tests/test_umath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def test_large_values(self, dtype):
313313
assert_dtype_allclose(result, expected)
314314

315315

316-
class TestLogaddexp:
316+
class TestLogAddExp:
317317
@pytest.mark.parametrize("dtype", get_all_dtypes(no_complex=True))
318318
def test_logaddexp(self, dtype):
319319
np_array1, np_array2, expected = _get_numpy_arrays_2in_1out(

0 commit comments

Comments
 (0)