From 2bfe54761ac10901f83731fc9e8f55489f908df5 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Tue, 28 Nov 2023 09:50:31 -0600 Subject: [PATCH 1/6] Implement dpnp.mgrid and dpnp.ogrid function --- dpnp/dpnp_algo/dpnp_arraycreation.py | 136 +++++++++++++++++++++++++++ dpnp/dpnp_iface_arraycreation.py | 65 +++---------- tests/skipped_tests.tbl | 4 - tests/skipped_tests_gpu.tbl | 5 - tests/test_sycl_queue.py | 10 ++ tests/test_usm_type.py | 6 ++ 6 files changed, 165 insertions(+), 61 deletions(-) diff --git a/dpnp/dpnp_algo/dpnp_arraycreation.py b/dpnp/dpnp_algo/dpnp_arraycreation.py index b6d3c6120683..c651dcbebd84 100644 --- a/dpnp/dpnp_algo/dpnp_arraycreation.py +++ b/dpnp/dpnp_algo/dpnp_arraycreation.py @@ -1,5 +1,7 @@ +import math import operator +import dpctl.utils as dpu import numpy import dpnp @@ -10,6 +12,7 @@ "dpnp_geomspace", "dpnp_linspace", "dpnp_logspace", + "nd_grid", ] @@ -256,3 +259,136 @@ def dpnp_logspace( if dtype is None: return dpnp.power(base, res) return dpnp.power(base, res).astype(dtype, copy=False) + + +class nd_grid: + """ + Construct a multi-dimensional "meshgrid". + + ``grid = nd_grid()`` creates an instance which will return a mesh-grid + when indexed. The dimension and number of the output arrays are equal + to the number of indexing dimensions. If the step length is not a + complex number, then the stop is not inclusive. + + However, if the step length is a complex number (e.g. 5j), then the + integer part of its magnitude is interpreted as specifying the + number of points to create between the start and stop values, where + the stop value is inclusive. + + If instantiated with an argument of ``sparse=True``, the mesh-grid is + open (or not fleshed out) so that only one-dimension of each returned + argument is greater than 1. + + Parameters + ---------- + sparse : bool, optional + Whether the grid is sparse or not. Default is False. + + """ + + def __init__( + self, sparse=False, device=None, usm_type="device", sycl_queue=None + ): + dpu.validate_usm_type(usm_type, allow_none=False) + self.sparse = sparse + self.device = device + self.usm_type = usm_type + self.sycl_queue = sycl_queue + + def __getitem__(self, key): + sycl_queue_normalized = dpnp.get_normalized_queue_device( + sycl_queue=self.sycl_queue, device=self.device + ) + if isinstance(key, slice): + step = key.step + stop = key.stop + start = key.start + if start is None: + start = 0 + if isinstance(step, complex): + step = abs(step) + length = int(step) + if step != 1: + step = (stop - start) / float(step - 1) + stop = stop + step + return ( + dpnp.arange( + 0, + length, + 1, + dtype=dpnp.default_float_type(), + usm_type=self.usm_type, + sycl_queue=sycl_queue_normalized, + ) + * step + + start + ) + else: + return dpnp.arange( + start, + stop, + step, + usm_type=self.usm_type, + sycl_queue=sycl_queue_normalized, + ) + + size = [] + dtype = int + for k in range(len(key)): + step = key[k].step + start = key[k].start + stop = key[k].stop + if start is None: + start = 0 + if step is None: + step = 1 + if isinstance(step, complex): + size.append(int(abs(step))) + dtype = dpnp.default_float_type() + else: + size.append( + int(math.ceil((key[k].stop - start) / (step * 1.0))) + ) + if ( + isinstance(step, float) + or isinstance(start, float) + or isinstance(stop, float) + ): + dtype = dpnp.default_float_type() + if self.sparse: + nn = [ + dpnp.arange( + _x, + dtype=_t, + usm_type=self.usm_type, + sycl_queue=sycl_queue_normalized, + ) + for _x, _t in zip(size, (dtype,) * len(size)) + ] + else: + nn = dpnp.indices( + size, + dtype, + usm_type=self.usm_type, + sycl_queue=sycl_queue_normalized, + ) + for k in range(len(size)): + step = key[k].step + start = key[k].start + stop = key[k].stop + if start is None: + start = 0 + if step is None: + step = 1 + if isinstance(step, complex): + step = int(abs(step)) + if step != 1: + step = (stop - start) / float(step - 1) + nn[k] = nn[k] * step + start + if self.sparse: + slobj = [dpnp.newaxis] * len(size) + for k in range(len(size)): + slobj[k] = slice(None, None) + nn[k] = nn[k][tuple(slobj)] + slobj[k] = dpnp.newaxis + return nn diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 0ed1187cb1d8..5828d5f590bd 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -54,6 +54,7 @@ dpnp_geomspace, dpnp_linspace, dpnp_logspace, + nd_grid, ) __all__ = [ @@ -83,7 +84,9 @@ "logspace", "meshgrid", "mgrid", + "mgrid_device", "ogrid", + "ogrid_device", "ones", "ones_like", "ptp", @@ -1369,64 +1372,22 @@ def meshgrid(*xi, copy=True, sparse=False, indexing="xy"): return call_origin(numpy.meshgrid, xi, copy, sparse, indexing) -class MGridClass: - """ - Construct a dense multi-dimensional "meshgrid". - - For full documentation refer to :obj:`numpy.mgrid`. - - Examples - -------- - >>> import dpnp as np - >>> np.mgrid[0:5,0:5] - array([[[0, 0, 0, 0, 0], - [1, 1, 1, 1, 1], - [2, 2, 2, 2, 2], - [3, 3, 3, 3, 3], - [4, 4, 4, 4, 4]], - [[0, 1, 2, 3, 4], - [0, 1, 2, 3, 4], - [0, 1, 2, 3, 4], - [0, 1, 2, 3, 4], - [0, 1, 2, 3, 4]]]) - >>> np.mgrid[-1:1:5j] - array([-1. , -0.5, 0. , 0.5, 1. ]) - - """ - - def __getitem__(self, key): - return dpnp.array(numpy.mgrid[key]) - - -mgrid = MGridClass() - - -class OGridClass: - """ - Construct an open multi-dimensional "meshgrid". +def mgrid_device(*, device=None, usm_type="device", sycl_queue=None): + return nd_grid( + sparse=False, device=device, usm_type=usm_type, sycl_queue=sycl_queue + ) - For full documentation refer to :obj:`numpy.ogrid`. - Examples - -------- - >>> import dpnp as np - >>> from numpy import ogrid - >>> ogrid[-1:1:5j] - array([-1. , -0.5, 0. , 0.5, 1. ]) - >>> ogrid[0:5,0:5] - [array([[0], - [1], - [2], - [3], - [4]]), array([[0, 1, 2, 3, 4]])] +mgrid = nd_grid(sparse=False) - """ - def __getitem__(self, key): - return dpnp.array(numpy.ogrid[key]) +def ogrid_device(*, device=None, usm_type="device", sycl_queue=None): + return nd_grid( + sparse=True, device=device, usm_type=usm_type, sycl_queue=sycl_queue + ) -ogrid = OGridClass() +ogrid = nd_grid(sparse=True) def ones( diff --git a/tests/skipped_tests.tbl b/tests/skipped_tests.tbl index e61f7497d97d..553493d4eeff 100644 --- a/tests/skipped_tests.tbl +++ b/tests/skipped_tests.tbl @@ -196,10 +196,6 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid1 tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid2 tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid3 -tests/third_party/cupy/creation_tests/test_ranges.py::TestMgrid::test_mgrid3 -tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid3 -tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid4 -tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid5 tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_AxisConcatenator_init1 tests/third_party/cupy/indexing_tests/test_generate.py::TestAxisConcatenator::test_len tests/third_party/cupy/indexing_tests/test_generate.py::TestC_::test_c_1 diff --git a/tests/skipped_tests_gpu.tbl b/tests/skipped_tests_gpu.tbl index a8d198b77334..17b471c3bade 100644 --- a/tests/skipped_tests_gpu.tbl +++ b/tests/skipped_tests_gpu.tbl @@ -271,11 +271,6 @@ tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid1 tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid2 tests/third_party/cupy/creation_tests/test_ranges.py::TestMeshgrid_param_7_{copy=True, indexing='ij', sparse=True}::test_meshgrid3 -tests/third_party/cupy/creation_tests/test_ranges.py::TestMgrid::test_mgrid3 -tests/third_party/cupy/creation_tests/test_ranges.py::TestMgrid::test_mgrid5 -tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid3 -tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid4 -tests/third_party/cupy/creation_tests/test_ranges.py::TestOgrid::test_ogrid5 tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_negative_size tests/third_party/cupy/creation_tests/test_ranges.py::TestRanges::test_arange_no_dtype_int diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index f95bd0a4db59..5efebb4a34b9 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -1245,3 +1245,13 @@ def test_indices(device): assert_allclose(numpy_array, dpnp_array) assert dpnp_array.sycl_device == device + + +@pytest.mark.parametrize( + "device", + valid_devices, + ids=[device.filter_string for device in valid_devices], +) +def test_grid(device): + assert dpnp.mgrid_device(device=device)[0:4].sycl_device == device + assert dpnp.ogrid_device(device=device)[0:4].sycl_device == device diff --git a/tests/test_usm_type.py b/tests/test_usm_type.py index d58963db2674..4d8900dd5ca0 100644 --- a/tests/test_usm_type.py +++ b/tests/test_usm_type.py @@ -486,3 +486,9 @@ def test_take(usm_type_x, usm_type_ind): def test_indices(usm_type): x = dp.indices((2,), usm_type=usm_type) assert x.usm_type == usm_type + + +@pytest.mark.parametrize("usm_type", list_of_usm_types, ids=list_of_usm_types) +def test_grid(usm_type): + assert dp.mgrid_device(usm_type=usm_type)[0:4].usm_type == usm_type + assert dp.ogrid_device(usm_type=usm_type)[0:4].usm_type == usm_type From f10254f9e5697db52adbe9118dc4da69c4d5cb21 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Fri, 15 Dec 2023 04:35:10 -0600 Subject: [PATCH 2/6] address comments --- dpnp/dpnp_algo/dpnp_arraycreation.py | 6 +-- dpnp/dpnp_iface_arraycreation.py | 78 +++++++++++++++++++++++----- tests/test_sycl_queue.py | 6 +-- tests/test_usm_type.py | 6 +-- 4 files changed, 74 insertions(+), 22 deletions(-) diff --git a/dpnp/dpnp_algo/dpnp_arraycreation.py b/dpnp/dpnp_algo/dpnp_arraycreation.py index c651dcbebd84..403ca1e9e523 100644 --- a/dpnp/dpnp_algo/dpnp_arraycreation.py +++ b/dpnp/dpnp_algo/dpnp_arraycreation.py @@ -12,7 +12,7 @@ "dpnp_geomspace", "dpnp_linspace", "dpnp_logspace", - "nd_grid", + "dpnp_nd_grid", ] @@ -261,11 +261,11 @@ def dpnp_logspace( return dpnp.power(base, res).astype(dtype, copy=False) -class nd_grid: +class dpnp_nd_grid: """ Construct a multi-dimensional "meshgrid". - ``grid = nd_grid()`` creates an instance which will return a mesh-grid + ``grid = dpnp_nd_grid()`` creates an instance which will return a mesh-grid when indexed. The dimension and number of the output arrays are equal to the number of indexing dimensions. If the step length is not a complex number, then the stop is not inclusive. diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 5828d5f590bd..67d9f25ef783 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -54,7 +54,7 @@ dpnp_geomspace, dpnp_linspace, dpnp_logspace, - nd_grid, + dpnp_nd_grid, ) __all__ = [ @@ -84,9 +84,7 @@ "logspace", "meshgrid", "mgrid", - "mgrid_device", "ogrid", - "ogrid_device", "ones", "ones_like", "ptp", @@ -1372,22 +1370,76 @@ def meshgrid(*xi, copy=True, sparse=False, indexing="xy"): return call_origin(numpy.meshgrid, xi, copy, sparse, indexing) -def mgrid_device(*, device=None, usm_type="device", sycl_queue=None): - return nd_grid( - sparse=False, device=device, usm_type=usm_type, sycl_queue=sycl_queue - ) +class MGridClass: + """ + Construct a dense multi-dimensional "meshgrid". + + For full documentation refer to :obj:`numpy.mgrid`. + + Examples + -------- + >>> import dpnp as np + >>> np.mgrid[0:5,0:5] + array([[[0, 0, 0, 0, 0], + [1, 1, 1, 1, 1], + [2, 2, 2, 2, 2], + [3, 3, 3, 3, 3], + [4, 4, 4, 4, 4]], + [[0, 1, 2, 3, 4], + [0, 1, 2, 3, 4], + [0, 1, 2, 3, 4], + [0, 1, 2, 3, 4], + [0, 1, 2, 3, 4]]]) + >>> np.mgrid[-1:1:5j] + array([-1. , -0.5, 0. , 0.5, 1. ]) + + """ + + def __getitem__(self, key): + return dpnp_nd_grid(sparse=False)[key] + + def __call__(self, device=None, usm_type="device", sycl_queue=None): + return dpnp_nd_grid( + sparse=False, + device=device, + usm_type=usm_type, + sycl_queue=sycl_queue, + ) -mgrid = nd_grid(sparse=False) +mgrid = MGridClass() -def ogrid_device(*, device=None, usm_type="device", sycl_queue=None): - return nd_grid( - sparse=True, device=device, usm_type=usm_type, sycl_queue=sycl_queue - ) +class OGridClass: + """ + Construct an open multi-dimensional "meshgrid". + + For full documentation refer to :obj:`numpy.ogrid`. + + Examples + -------- + >>> import dpnp as np + >>> np.ogrid[-1:1:5j] + array([-1. , -0.5, 0. , 0.5, 1. ]) + >>> np.ogrid[0:5,0:5] + [array([[0], + [1], + [2], + [3], + [4]]), array([[0, 1, 2, 3, 4]])] + + """ + + def __getitem__(self, key): + return dpnp_nd_grid(sparse=True)[key] + + def __call__(self, device=None, usm_type="device", sycl_queue=None): + return dpnp_nd_grid( + sparse=True, device=device, usm_type=usm_type, sycl_queue=sycl_queue + ) -ogrid = nd_grid(sparse=True) +ogrid = OGridClass() def ones( diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index 5efebb4a34b9..213f19518d48 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -1252,6 +1252,6 @@ def test_indices(device): valid_devices, ids=[device.filter_string for device in valid_devices], ) -def test_grid(device): - assert dpnp.mgrid_device(device=device)[0:4].sycl_device == device - assert dpnp.ogrid_device(device=device)[0:4].sycl_device == device +@pytest.mark.parametrize("func", ["mgrid", "ogrid"]) +def test_grid(device, func): + assert getattr(dpnp, func)(device=device)[0:4].sycl_device == device diff --git a/tests/test_usm_type.py b/tests/test_usm_type.py index 4d8900dd5ca0..194662d0be51 100644 --- a/tests/test_usm_type.py +++ b/tests/test_usm_type.py @@ -489,6 +489,6 @@ def test_indices(usm_type): @pytest.mark.parametrize("usm_type", list_of_usm_types, ids=list_of_usm_types) -def test_grid(usm_type): - assert dp.mgrid_device(usm_type=usm_type)[0:4].usm_type == usm_type - assert dp.ogrid_device(usm_type=usm_type)[0:4].usm_type == usm_type +@pytest.mark.parametrize("func", ["mgrid", "ogrid"]) +def test_grid(usm_type, func): + assert getattr(dp, func)(usm_type=usm_type)[0:4].usm_type == usm_type From 6967c7d42cc39729290a22129b800885e89d3576 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Tue, 9 Jan 2024 09:57:57 -0600 Subject: [PATCH 3/6] address comments --- dpnp/dpnp_algo/dpnp_arraycreation.py | 16 +++++++--------- tests/test_sycl_queue.py | 4 +++- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/dpnp/dpnp_algo/dpnp_arraycreation.py b/dpnp/dpnp_algo/dpnp_arraycreation.py index 403ca1e9e523..0399deea254a 100644 --- a/dpnp/dpnp_algo/dpnp_arraycreation.py +++ b/dpnp/dpnp_algo/dpnp_arraycreation.py @@ -291,14 +291,12 @@ def __init__( ): dpu.validate_usm_type(usm_type, allow_none=False) self.sparse = sparse - self.device = device self.usm_type = usm_type - self.sycl_queue = sycl_queue + self.sycl_queue_normalized = dpnp.get_normalized_queue_device( + sycl_queue=sycl_queue, device=device + ) def __getitem__(self, key): - sycl_queue_normalized = dpnp.get_normalized_queue_device( - sycl_queue=self.sycl_queue, device=self.device - ) if isinstance(key, slice): step = key.step stop = key.stop @@ -318,7 +316,7 @@ def __getitem__(self, key): 1, dtype=dpnp.default_float_type(), usm_type=self.usm_type, - sycl_queue=sycl_queue_normalized, + sycl_queue=self.sycl_queue_normalized, ) * step + start @@ -329,7 +327,7 @@ def __getitem__(self, key): stop, step, usm_type=self.usm_type, - sycl_queue=sycl_queue_normalized, + sycl_queue=self.sycl_queue_normalized, ) size = [] @@ -361,7 +359,7 @@ def __getitem__(self, key): _x, dtype=_t, usm_type=self.usm_type, - sycl_queue=sycl_queue_normalized, + sycl_queue=self.sycl_queue_normalized, ) for _x, _t in zip(size, (dtype,) * len(size)) ] @@ -370,7 +368,7 @@ def __getitem__(self, key): size, dtype, usm_type=self.usm_type, - sycl_queue=sycl_queue_normalized, + sycl_queue=self.sycl_queue_normalized, ) for k in range(len(size)): step = key[k].step diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index 55edabe94b0e..c2173c887c46 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -1366,7 +1366,9 @@ def test_indices(device, sparse): ) @pytest.mark.parametrize("func", ["mgrid", "ogrid"]) def test_grid(device, func): - assert getattr(dpnp, func)(device=device)[0:4].sycl_device == device + sycl_queue = dpctl.SyclQueue(device) + x = getattr(dpnp, func)(sycl_queue=sycl_queue)[0:4] + assert_sycl_queue_equal(x.sycl_queue, sycl_queue) @pytest.mark.parametrize( From 6433489c491038a947ee93978fd1508ded17513e Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Tue, 9 Jan 2024 15:26:11 -0600 Subject: [PATCH 4/6] Skip dtype check for Iris Xe --- tests/third_party/cupy/creation_tests/test_ranges.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/third_party/cupy/creation_tests/test_ranges.py b/tests/third_party/cupy/creation_tests/test_ranges.py index 623adc409b7e..6c76fe509db3 100644 --- a/tests/third_party/cupy/creation_tests/test_ranges.py +++ b/tests/third_party/cupy/creation_tests/test_ranges.py @@ -363,7 +363,7 @@ def test_mgrid1(self, xp): def test_mgrid2(self, xp): return xp.mgrid[-10:10:10j] - @testing.numpy_cupy_array_equal() + @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) def test_mgrid3(self, xp): x = xp.zeros(10)[:, None] y = xp.ones(10)[:, None] @@ -374,7 +374,7 @@ def test_mgrid4(self, xp): # check len(keys) > 1 return xp.mgrid[-10:10:10j, -10:10:10j] - @testing.numpy_cupy_array_equal() + @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) def test_mgrid5(self, xp): # check len(keys) > 1 x = xp.zeros(10)[:, None] @@ -396,18 +396,18 @@ def test_ogrid1(self, xp): def test_ogrid2(self, xp): return xp.ogrid[-10:10:10j] - @testing.numpy_cupy_array_equal() + @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) def test_ogrid3(self, xp): x = xp.zeros(10)[:, None] y = xp.ones(10)[:, None] return xp.ogrid[x:y:10j] - @testing.numpy_cupy_array_equal() + @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) def test_ogrid4(self, xp): # check len(keys) > 1 return xp.ogrid[-10:10:10j, -10:10:10j] - @testing.numpy_cupy_array_equal() + @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) def test_ogrid5(self, xp): # check len(keys) > 1 x = xp.zeros(10)[:, None] From c58787db302d599ca35ac3a56ef41b125f41bc11 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Wed, 10 Jan 2024 09:35:42 -0800 Subject: [PATCH 5/6] Update test_ranges.py --- tests/third_party/cupy/creation_tests/test_ranges.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/third_party/cupy/creation_tests/test_ranges.py b/tests/third_party/cupy/creation_tests/test_ranges.py index 6c76fe509db3..83170cb3e37f 100644 --- a/tests/third_party/cupy/creation_tests/test_ranges.py +++ b/tests/third_party/cupy/creation_tests/test_ranges.py @@ -363,7 +363,7 @@ def test_mgrid1(self, xp): def test_mgrid2(self, xp): return xp.mgrid[-10:10:10j] - @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) + @testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64()) def test_mgrid3(self, xp): x = xp.zeros(10)[:, None] y = xp.ones(10)[:, None] @@ -374,7 +374,7 @@ def test_mgrid4(self, xp): # check len(keys) > 1 return xp.mgrid[-10:10:10j, -10:10:10j] - @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) + @testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64()) def test_mgrid5(self, xp): # check len(keys) > 1 x = xp.zeros(10)[:, None] @@ -396,18 +396,18 @@ def test_ogrid1(self, xp): def test_ogrid2(self, xp): return xp.ogrid[-10:10:10j] - @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) + @testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64()) def test_ogrid3(self, xp): x = xp.zeros(10)[:, None] y = xp.ones(10)[:, None] return xp.ogrid[x:y:10j] - @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) + @testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64()) def test_ogrid4(self, xp): # check len(keys) > 1 return xp.ogrid[-10:10:10j, -10:10:10j] - @testing.numpy_cupy_array_equal(type_check=has_support_aspect64()) + @testing.numpy_cupy_allclose(rtol=1e-4, type_check=has_support_aspect64()) def test_ogrid5(self, xp): # check len(keys) > 1 x = xp.zeros(10)[:, None] From 38b0e82a5f4d199663a3c694312a71506d476a65 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Thu, 11 Jan 2024 10:31:44 -0600 Subject: [PATCH 6/6] Added description of all parameters --- dpnp/dpnp_iface_arraycreation.py | 65 ++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index a8e2874bee31..c006bd2436da 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -1453,6 +1453,24 @@ class MGridClass: For full documentation refer to :obj:`numpy.mgrid`. + Parameters + ---------- + device : {None, string, SyclDevice, SyclQueue}, optional + An array API concept of device where the output array is created. + The `device` can be ``None`` (the default), an OneAPI filter selector string, + an instance of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL device, + an instance of :class:`dpctl.SyclQueue`, or a `Device` object returned by + :obj:`dpnp.dpnp_array.dpnp_array.device` property. + usm_type : {"device", "shared", "host"}, optional + The type of SYCL USM allocation for the output array. + sycl_queue : {None, SyclQueue}, optional + A SYCL queue to use for output array allocation and copying. + + Returns + ------- + out : one dpnp.ndarray or tuple of dpnp.ndarray + Returns one array of grid indices, grid.shape = (len(dimensions),) + tuple(dimensions). + Examples -------- >>> import dpnp as np @@ -1467,8 +1485,18 @@ class MGridClass: [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]]) - >>> np.mgrid[-1:1:5j] + + >>> x = np.mgrid[-1:1:5j] + >>> x + array([-1. , -0.5, 0. , 0.5, 1. ]) + >>> x.usm_type + 'device' + + >>> y = np.mgrid(usm_type="host")[-1:1:5j] + >>> y array([-1. , -0.5, 0. , 0.5, 1. ]) + >>> x.usm_type + 'host' """ @@ -1493,18 +1521,47 @@ class OGridClass: For full documentation refer to :obj:`numpy.ogrid`. + Parameters + ---------- + device : {None, string, SyclDevice, SyclQueue}, optional + An array API concept of device where the output array is created. + The `device` can be ``None`` (the default), an OneAPI filter selector string, + an instance of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL device, + an instance of :class:`dpctl.SyclQueue`, or a `Device` object returned by + :obj:`dpnp.dpnp_array.dpnp_array.device` property. + usm_type : {"device", "shared", "host"}, optional + The type of SYCL USM allocation for the output array. + sycl_queue : {None, SyclQueue}, optional + A SYCL queue to use for output array allocation and copying. + + Returns + ------- + out : one dpnp.ndarray or tuple of dpnp.ndarray + Returns a tuple of arrays, with grid[i].shape = (1, ..., 1, dimensions[i], 1, ..., 1) + with dimensions[i] in the ith place. + Examples -------- >>> import dpnp as np - >>> np.ogrid[-1:1:5j] - array([-1. , -0.5, 0. , 0.5, 1. ]) - >>> np.ogrid[0:5,0:5] + >>> np.ogrid[0:5, 0:5] [array([[0], [1], [2], [3], [4]]), array([[0, 1, 2, 3, 4]])] + >>> x = np.ogrid[-1:1:5j] + >>> x + array([-1. , -0.5, 0. , 0.5, 1. ]) + >>> x.usm_type + 'device' + + >>> y = np.ogrid(usm_type="host")[-1:1:5j] + >>> y + array([-1. , -0.5, 0. , 0.5, 1. ]) + >>> x.usm_type + 'host' + """ def __getitem__(self, key):