From 9cbf764d171d3b88256681d7875f6a5d953ea38c Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Mon, 20 Jan 2025 04:44:43 -0800 Subject: [PATCH 01/13] Add test_logspace_list_input --- dpnp/tests/test_arraycreation.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dpnp/tests/test_arraycreation.py b/dpnp/tests/test_arraycreation.py index 909d4d464b06..d23a627f070a 100644 --- a/dpnp/tests/test_arraycreation.py +++ b/dpnp/tests/test_arraycreation.py @@ -917,6 +917,12 @@ def test_logspace_axis(axis): assert_dtype_allclose(func(dpnp), func(numpy)) +def test_logspace_list_input(): + res_np = numpy.logspace([0], [2], base=[5]) + res_dp = dpnp.logspace([0], [2], base=[5]) + assert_allclose(res_dp, res_np) + + @pytest.mark.parametrize( "data", [(), 1, (2, 3), [4], numpy.array(5), numpy.array([6, 7])] ) From ca5ba5be02a7bb52f2fbeeee1a041ba8db65f79d Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Mon, 20 Jan 2025 05:31:28 -0800 Subject: [PATCH 02/13] Add TestMgrid --- dpnp/tests/test_arraycreation.py | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/dpnp/tests/test_arraycreation.py b/dpnp/tests/test_arraycreation.py index d23a627f070a..dde5447bc590 100644 --- a/dpnp/tests/test_arraycreation.py +++ b/dpnp/tests/test_arraycreation.py @@ -968,6 +968,41 @@ def test_meshgrid_raise_error(): dpnp.meshgrid(b, indexing="ab") +class TestMgrid: + def check_results(self, result, expected): + if isinstance(result, (list, tuple)): + assert len(result) == len(expected) + for dp_arr, np_arr in zip(result, expected): + assert_allclose(dp_arr, np_arr) + else: + assert_allclose(result, expected) + + @pytest.mark.parametrize( + "slice", + [ + slice(0, 5, 0.5), # float step + slice(0, 5, 5j), # complex step + ], + ) + def test_single_slice(self, slice): + dpnp_result = dpnp.mgrid[slice] + numpy_result = numpy.mgrid[slice] + self.check_results(dpnp_result, numpy_result) + + @pytest.mark.parametrize( + "slices", + [ + (slice(None, 5, 1), slice(None, 10, 2)), # no start + (slice(0, 5), slice(0, 10)), # no step + (slice(0, 5.5, 1), slice(0, 10, 3j)), # float stop and complex step + ], + ) + def test_md_slice(self, slices): + dpnp_result = dpnp.mgrid[slices] + numpy_result = numpy.mgrid[slices] + self.check_results(dpnp_result, numpy_result) + + def test_exception_tri(): x = dpnp.ones((2, 2)) with pytest.raises(TypeError): From 25a2c6a33e4a842ea1f27d3cb18c0330f4b1c33b Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Mon, 20 Jan 2025 06:53:32 -0800 Subject: [PATCH 03/13] Add test_create_from_usm_ndarray_error --- dpnp/tests/test_arraycreation.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/dpnp/tests/test_arraycreation.py b/dpnp/tests/test_arraycreation.py index dde5447bc590..61b470555eea 100644 --- a/dpnp/tests/test_arraycreation.py +++ b/dpnp/tests/test_arraycreation.py @@ -214,6 +214,20 @@ def test_arange(start, stop, step, dtype): assert_array_equal(exp_array, res_array) +@pytest.mark.parametrize( + "arr", + [ + numpy.array([1]), + dpnp.array([1]), + [1], + ], + ids=["numpy", "dpnp", "list"], +) +def test_create_from_usm_ndarray_error(arr): + with pytest.raises(TypeError): + dpnp.dpnp_array.dpnp_array._create_from_usm_ndarray(arr) + + @pytest.mark.parametrize("func", ["diag", "diagflat"]) @pytest.mark.parametrize("k", [-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6]) @pytest.mark.parametrize( From 2027b2794c0ad02736a96b6e2397780a655a692f Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Mon, 20 Jan 2025 07:18:37 -0800 Subject: [PATCH 04/13] Add TestAttributes to test_arraycreation.py --- dpnp/tests/test_arraycreation.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/dpnp/tests/test_arraycreation.py b/dpnp/tests/test_arraycreation.py index 61b470555eea..432f225d9ead 100644 --- a/dpnp/tests/test_arraycreation.py +++ b/dpnp/tests/test_arraycreation.py @@ -56,6 +56,33 @@ def test_error(self): assert_raises(TypeError, dpnp.array, x, ndmin=3.0) +class TestAttributes: + def setup_method(self): + self.one = dpnp.arange(10) + self.two = dpnp.arange(20).reshape(4, 5) + self.three = dpnp.arange(60).reshape(2, 5, 6) + + def test_attributes(self): + assert_equal(self.one.shape, (10,)) + assert_equal(self.two.shape, (4, 5)) + assert_equal(self.three.shape, (2, 5, 6)) + self.three.shape = (10, 3, 2) + assert_equal(self.three.shape, (10, 3, 2)) + self.three.shape = (2, 5, 6) + assert_equal(self.one.strides, (self.one.itemsize / self.one.itemsize,)) + num = self.two.itemsize / self.two.itemsize + assert_equal(self.two.strides, (5 * num, num)) + num = self.three.itemsize / self.three.itemsize + assert_equal(self.three.strides, (30 * num, 6 * num, num)) + assert_equal(self.one.ndim, 1) + assert_equal(self.two.ndim, 2) + assert_equal(self.three.ndim, 3) + num = self.two.itemsize + assert_equal(self.two.size, 20) + assert_equal(self.two.nbytes, 20 * num) + assert_equal(self.two.itemsize, self.two.dtype.itemsize) + + class TestTrace: @pytest.mark.parametrize("a_sh", [(3, 4), (2, 2, 2)]) @pytest.mark.parametrize( From feb1ca86aaca6bccdc47e1d60be59120750e048d Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Mon, 20 Jan 2025 09:07:48 -0800 Subject: [PATCH 05/13] Update test_flat.py --- dpnp/tests/test_flat.py | 85 +++++++++++++++++++++++++---------------- 1 file changed, 52 insertions(+), 33 deletions(-) diff --git a/dpnp/tests/test_flat.py b/dpnp/tests/test_flat.py index e061520f61c5..755240df886b 100644 --- a/dpnp/tests/test_flat.py +++ b/dpnp/tests/test_flat.py @@ -1,34 +1,53 @@ -import numpy +import numpy as np import pytest - -import dpnp as inp - - -@pytest.mark.parametrize("type", [numpy.int64], ids=["int64"]) -def test_flat(type): - a = numpy.array([1, 0, 2, -3, -1, 2, 21, -9]) - ia = inp.array(a) - - result = ia.flat[0] - expected = a.flat[0] - numpy.testing.assert_array_equal(expected, result) - - -@pytest.mark.parametrize("type", [numpy.int64], ids=["int64"]) -def test_flat2(type): - a = numpy.arange(1, 7).reshape(2, 3) - ia = inp.array(a) - - result = ia.flat[3] - expected = a.flat[3] - numpy.testing.assert_array_equal(expected, result) - - -@pytest.mark.parametrize("type", [numpy.int64], ids=["int64"]) -def test_flat3(type): - a = numpy.arange(1, 7).reshape(2, 3).T - ia = inp.array(a) - - result = ia.flat[3] - expected = a.flat[3] - numpy.testing.assert_array_equal(expected, result) +from numpy.testing import assert_array_equal + +import dpnp + + +class TestFlatiter: + @pytest.mark.parametrize( + "a, index", + [ + (np.array([1, 0, 2, -3, -1, 2, 21, -9]), 0), + (np.arange(1, 7).reshape(2, 3), 3), + (np.arange(1, 7).reshape(2, 3).T, 3), + ], + ids=["1D array", "2D array", "2D.T array"], + ) + def test_flat_getitem(self, a, index): + a_dp = dpnp.array(a) + result = a_dp.flat[index] + expected = a.flat[index] + assert_array_equal(expected, result) + + def test_flat_iteration(self): + a = np.array([[1, 2], [3, 4]]) + a_dp = dpnp.array(a) + for dp_val, np_val in zip(a_dp.flat, a.flat): + assert dp_val == np_val + + def test_init_error(self): + with pytest.raises(TypeError): + _ = dpnp.flatiter([1, 2, 3]) + + def test_flat_key_error(self): + a_dp = dpnp.array(42) + with pytest.raises(KeyError): + _ = a_dp.flat[1] + + def test_flat_invalid_key(self): + a_dp = dpnp.array([1, 2, 3]) + flat = dpnp.flatiter(a_dp) + # check __getitem__ + with pytest.raises(TypeError): + _ = flat["invalid"] + # check __setitem__ + with pytest.raises(TypeError): + flat["invalid"] = 42 + + def test_flat_out_of_bounds(self): + a_dp = dpnp.array([1, 2, 3]) + flat = dpnp.flatiter(a_dp) + with pytest.raises(IndexError): + _ = flat[10] From 60607bd2d20c9416b7da3448737f1303e7c2f086 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Mon, 20 Jan 2025 09:18:16 -0800 Subject: [PATCH 06/13] Add test_identity_error --- dpnp/tests/test_arraycreation.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dpnp/tests/test_arraycreation.py b/dpnp/tests/test_arraycreation.py index 432f225d9ead..77bedc7c6d7f 100644 --- a/dpnp/tests/test_arraycreation.py +++ b/dpnp/tests/test_arraycreation.py @@ -382,6 +382,12 @@ def test_identity(n, dtype): assert_array_equal(func(numpy), func(dpnp)) +def test_identity_error(): + # negative dimensions + with pytest.raises(ValueError): + _ = dpnp.identity(-5) + + @pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) def test_loadtxt(dtype): func = lambda xp: xp.loadtxt(fh, dtype=dtype) From 5f3d90b9028a383a9a13ca49faae80f3e938c775 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Mon, 20 Jan 2025 10:00:50 -0800 Subject: [PATCH 07/13] Add test_ihfft_error to TestHfft --- dpnp/tests/test_fft.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dpnp/tests/test_fft.py b/dpnp/tests/test_fft.py index c54401fd8bfe..8aa0b31053fa 100644 --- a/dpnp/tests/test_fft.py +++ b/dpnp/tests/test_fft.py @@ -670,6 +670,12 @@ def test_ihfft_bool(self, n, norm): expected = numpy.fft.ihfft(a_np, n=n, norm=norm) assert_dtype_allclose(result, expected, check_only_type_kind=True) + def test_ihfft_error(self): + a = dpnp.ones(11) + # incorrect norm + with pytest.raises(ValueError): + _ = dpnp.fft.ihfft(a, norm="backwards") + class TestIrfft: def setup_method(self): From 11b8f95a2c37714e161050c87670a5878d9e409c Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Tue, 21 Jan 2025 02:04:31 -0800 Subject: [PATCH 08/13] Test single slice without start in TestMgrid --- dpnp/tests/test_arraycreation.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dpnp/tests/test_arraycreation.py b/dpnp/tests/test_arraycreation.py index 77bedc7c6d7f..be67dc8396d2 100644 --- a/dpnp/tests/test_arraycreation.py +++ b/dpnp/tests/test_arraycreation.py @@ -1029,6 +1029,7 @@ def check_results(self, result, expected): [ slice(0, 5, 0.5), # float step slice(0, 5, 5j), # complex step + slice(None, 5, 1), # no start ], ) def test_single_slice(self, slice): From 1092d777d48d507bf15d0ae5ab35702a00fc9045 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Tue, 21 Jan 2025 03:31:27 -0800 Subject: [PATCH 09/13] Add test logspace with list base to test_array_creation --- dpnp/tests/test_sycl_queue.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dpnp/tests/test_sycl_queue.py b/dpnp/tests/test_sycl_queue.py index dff8618cca69..7e3218b2b413 100644 --- a/dpnp/tests/test_sycl_queue.py +++ b/dpnp/tests/test_sycl_queue.py @@ -112,6 +112,7 @@ def vvsort(val, vec, size, xp): pytest.param("identity", [4], {}), pytest.param("linspace", [0, 4, 8], {}), pytest.param("logspace", [0, 4, 8], {}), + pytest.param("logspace", [0, 4, 8], {"base": [10]}), pytest.param("ones", [(2, 2)], {}), pytest.param("tri", [3, 5, 2], {}), pytest.param("zeros", [(2, 2)], {}), From c30be6b25376579653244d31ca0c83b7459c953c Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Tue, 21 Jan 2025 03:55:47 -0800 Subject: [PATCH 10/13] Expand TestMgrid --- dpnp/tests/test_arraycreation.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dpnp/tests/test_arraycreation.py b/dpnp/tests/test_arraycreation.py index be67dc8396d2..c1e5ad3c23b4 100644 --- a/dpnp/tests/test_arraycreation.py +++ b/dpnp/tests/test_arraycreation.py @@ -1028,8 +1028,10 @@ def check_results(self, result, expected): "slice", [ slice(0, 5, 0.5), # float step + slice(0, 5, 1j), # complex step slice(0, 5, 5j), # complex step slice(None, 5, 1), # no start + slice(0, 5, None), # no step ], ) def test_single_slice(self, slice): @@ -1043,6 +1045,10 @@ def test_single_slice(self, slice): (slice(None, 5, 1), slice(None, 10, 2)), # no start (slice(0, 5), slice(0, 10)), # no step (slice(0, 5.5, 1), slice(0, 10, 3j)), # float stop and complex step + ( + slice(0.0, 5, 1), + slice(0, 10, 1j), + ), # float start and complex step ], ) def test_md_slice(self, slices): From 6675ca82e3038794fc39affa2516e67247b15cb5 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Tue, 21 Jan 2025 04:22:39 -0800 Subject: [PATCH 11/13] Add test_inplace_scalar to TestFloorDivideRemainder --- dpnp/tests/test_binary_ufuncs.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/dpnp/tests/test_binary_ufuncs.py b/dpnp/tests/test_binary_ufuncs.py index 95a8d09852ad..6fae555194e8 100644 --- a/dpnp/tests/test_binary_ufuncs.py +++ b/dpnp/tests/test_binary_ufuncs.py @@ -320,6 +320,17 @@ def test_inplace_strides(self, func, dtype): assert_dtype_allclose(ia, a) + @pytest.mark.parametrize("dtype", ALL_DTYPES) + def test_inplace_scalar(self, func, dtype): + + a = numpy.array(10, dtype=dtype) + self.do_inplace_op(10, a, func) + + ia = dpnp.array(10, dtype=dtype) + self.do_inplace_op(10, ia, func) + + assert_dtype_allclose(ia, a) + @pytest.mark.parametrize("dtype1", [dpnp.bool] + ALL_DTYPES) @pytest.mark.parametrize("dtype2", get_float_dtypes()) def test_inplace_dtype(self, func, dtype1, dtype2): From 84a52e4477977a9123ac8164341479c758a193f3 Mon Sep 17 00:00:00 2001 From: Vladislav Perevezentsev Date: Tue, 21 Jan 2025 05:22:31 -0800 Subject: [PATCH 12/13] Apply remarks --- dpnp/tests/test_arraycreation.py | 44 +----------------------------- dpnp/tests/test_fft.py | 3 +-- dpnp/tests/test_flat.py | 5 ++-- dpnp/tests/test_ndarray.py | 46 ++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 48 deletions(-) diff --git a/dpnp/tests/test_arraycreation.py b/dpnp/tests/test_arraycreation.py index c1e5ad3c23b4..313f4663bb89 100644 --- a/dpnp/tests/test_arraycreation.py +++ b/dpnp/tests/test_arraycreation.py @@ -56,33 +56,6 @@ def test_error(self): assert_raises(TypeError, dpnp.array, x, ndmin=3.0) -class TestAttributes: - def setup_method(self): - self.one = dpnp.arange(10) - self.two = dpnp.arange(20).reshape(4, 5) - self.three = dpnp.arange(60).reshape(2, 5, 6) - - def test_attributes(self): - assert_equal(self.one.shape, (10,)) - assert_equal(self.two.shape, (4, 5)) - assert_equal(self.three.shape, (2, 5, 6)) - self.three.shape = (10, 3, 2) - assert_equal(self.three.shape, (10, 3, 2)) - self.three.shape = (2, 5, 6) - assert_equal(self.one.strides, (self.one.itemsize / self.one.itemsize,)) - num = self.two.itemsize / self.two.itemsize - assert_equal(self.two.strides, (5 * num, num)) - num = self.three.itemsize / self.three.itemsize - assert_equal(self.three.strides, (30 * num, 6 * num, num)) - assert_equal(self.one.ndim, 1) - assert_equal(self.two.ndim, 2) - assert_equal(self.three.ndim, 3) - num = self.two.itemsize - assert_equal(self.two.size, 20) - assert_equal(self.two.nbytes, 20 * num) - assert_equal(self.two.itemsize, self.two.dtype.itemsize) - - class TestTrace: @pytest.mark.parametrize("a_sh", [(3, 4), (2, 2, 2)]) @pytest.mark.parametrize( @@ -241,20 +214,6 @@ def test_arange(start, stop, step, dtype): assert_array_equal(exp_array, res_array) -@pytest.mark.parametrize( - "arr", - [ - numpy.array([1]), - dpnp.array([1]), - [1], - ], - ids=["numpy", "dpnp", "list"], -) -def test_create_from_usm_ndarray_error(arr): - with pytest.raises(TypeError): - dpnp.dpnp_array.dpnp_array._create_from_usm_ndarray(arr) - - @pytest.mark.parametrize("func", ["diag", "diagflat"]) @pytest.mark.parametrize("k", [-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6]) @pytest.mark.parametrize( @@ -384,8 +343,7 @@ def test_identity(n, dtype): def test_identity_error(): # negative dimensions - with pytest.raises(ValueError): - _ = dpnp.identity(-5) + assert_raises(ValueError, dpnp.identity, -5) @pytest.mark.parametrize("dtype", get_all_dtypes(no_float16=False)) diff --git a/dpnp/tests/test_fft.py b/dpnp/tests/test_fft.py index 8aa0b31053fa..418f05f47c64 100644 --- a/dpnp/tests/test_fft.py +++ b/dpnp/tests/test_fft.py @@ -673,8 +673,7 @@ def test_ihfft_bool(self, n, norm): def test_ihfft_error(self): a = dpnp.ones(11) # incorrect norm - with pytest.raises(ValueError): - _ = dpnp.fft.ihfft(a, norm="backwards") + assert_raises(ValueError, dpnp.fft.ihfft, a, norm="backwards") class TestIrfft: diff --git a/dpnp/tests/test_flat.py b/dpnp/tests/test_flat.py index 755240df886b..c40e95d3ee84 100644 --- a/dpnp/tests/test_flat.py +++ b/dpnp/tests/test_flat.py @@ -1,6 +1,6 @@ import numpy as np import pytest -from numpy.testing import assert_array_equal +from numpy.testing import assert_array_equal, assert_raises import dpnp @@ -28,8 +28,7 @@ def test_flat_iteration(self): assert dp_val == np_val def test_init_error(self): - with pytest.raises(TypeError): - _ = dpnp.flatiter([1, 2, 3]) + assert_raises(TypeError, dpnp.flatiter, [1, 2, 3]) def test_flat_key_error(self): a_dp = dpnp.array(42) diff --git a/dpnp/tests/test_ndarray.py b/dpnp/tests/test_ndarray.py index 0dfbf6c1e1c7..ade8b5387197 100644 --- a/dpnp/tests/test_ndarray.py +++ b/dpnp/tests/test_ndarray.py @@ -4,6 +4,7 @@ from numpy.testing import ( assert_allclose, assert_array_equal, + assert_equal, assert_raises_regex, ) @@ -40,6 +41,51 @@ def test_astype_subok_error(): x.astype("i4", subok=False) +class TestAttributes: + def setup_method(self): + self.one = dpnp.arange(10) + self.two = dpnp.arange(20).reshape(4, 5) + self.three = dpnp.arange(60).reshape(2, 5, 6) + + def test_attributes(self): + assert_equal(self.one.shape, (10,)) + assert_equal(self.two.shape, (4, 5)) + assert_equal(self.three.shape, (2, 5, 6)) + + self.three.shape = (10, 3, 2) + assert_equal(self.three.shape, (10, 3, 2)) + self.three.shape = (2, 5, 6) + + assert_equal(self.one.strides, (self.one.itemsize / self.one.itemsize,)) + num = self.two.itemsize / self.two.itemsize + assert_equal(self.two.strides, (5 * num, num)) + num = self.three.itemsize / self.three.itemsize + assert_equal(self.three.strides, (30 * num, 6 * num, num)) + + assert_equal(self.one.ndim, 1) + assert_equal(self.two.ndim, 2) + assert_equal(self.three.ndim, 3) + + num = self.two.itemsize + assert_equal(self.two.size, 20) + assert_equal(self.two.nbytes, 20 * num) + assert_equal(self.two.itemsize, self.two.dtype.itemsize) + + +@pytest.mark.parametrize( + "arr", + [ + numpy.array([1]), + dpnp.array([1]), + [1], + ], + ids=["numpy", "dpnp", "list"], +) +def test_create_from_usm_ndarray_error(arr): + with pytest.raises(TypeError): + dpnp.dpnp_array.dpnp_array._create_from_usm_ndarray(arr) + + @pytest.mark.parametrize("arr_dtype", get_all_dtypes()) @pytest.mark.parametrize( "arr", From 2fbef535566b491b143b567aa5d8540b5d8e5262 Mon Sep 17 00:00:00 2001 From: Anton <100830759+antonwolfy@users.noreply.github.com> Date: Tue, 21 Jan 2025 17:37:21 +0100 Subject: [PATCH 13/13] Update dpnp/tests/test_ndarray.py --- dpnp/tests/test_ndarray.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpnp/tests/test_ndarray.py b/dpnp/tests/test_ndarray.py index 50686d47bdb7..0a394c291585 100644 --- a/dpnp/tests/test_ndarray.py +++ b/dpnp/tests/test_ndarray.py @@ -83,7 +83,7 @@ def test_attributes(self): ) def test_create_from_usm_ndarray_error(arr): with pytest.raises(TypeError): - dpnp.dpnp_array.dpnp_array._create_from_usm_ndarray(arr) + dpnp.ndarray._create_from_usm_ndarray(arr) @pytest.mark.parametrize("arr_dtype", get_all_dtypes())