From eda8a0aa39e2f1c22d5cc7779b39648ca60a1ae0 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Thu, 6 Jun 2024 23:24:52 -0500 Subject: [PATCH 1/3] Added device keyword argument to astype function --- dpnp/dpnp_array.py | 21 +++++++++++++++++++-- dpnp/dpnp_iface.py | 11 +++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index fb8e1fcef12d..71a3a175d78b 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -562,7 +562,15 @@ def asnumpy(self): return dpt.asnumpy(self._array_obj) - def astype(self, dtype, order="K", casting="unsafe", subok=True, copy=True): + def astype( + self, + dtype, + order="K", + casting="unsafe", + subok=True, + copy=True, + device=None, + ): """ Copy the array with data type casting. @@ -597,6 +605,13 @@ def astype(self, dtype, order="K", casting="unsafe", subok=True, copy=True): this is set to ``False``, and the `dtype`, `order`, and `subok` requirements are satisfied, the input array is returned instead of a copy. + 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. Default: `None`. Returns ------- @@ -626,7 +641,9 @@ def astype(self, dtype, order="K", casting="unsafe", subok=True, copy=True): f"subok={subok} is currently not supported" ) - return dpnp.astype(self, dtype, order=order, casting=casting, copy=copy) + return dpnp.astype( + self, dtype, order=order, casting=casting, copy=copy, device=device + ) # 'base', # 'byteswap', diff --git a/dpnp/dpnp_iface.py b/dpnp/dpnp_iface.py index 0dfd63dab217..83f94e330b06 100644 --- a/dpnp/dpnp_iface.py +++ b/dpnp/dpnp_iface.py @@ -180,7 +180,7 @@ def asnumpy(a, order="C"): # pylint: disable=redefined-outer-name -def astype(x1, dtype, order="K", casting="unsafe", copy=True): +def astype(x1, dtype, order="K", casting="unsafe", copy=True, device=None): """ Copy the array with data type casting. @@ -213,6 +213,13 @@ def astype(x1, dtype, order="K", casting="unsafe", copy=True): By default, ``astype`` always returns a newly allocated array. If this is set to ``False``, and the `dtype`, `order`, and `subok` requirements are satisfied, the input array is returned instead of a copy. + 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. Default: `None`. Returns ------- @@ -228,7 +235,7 @@ def astype(x1, dtype, order="K", casting="unsafe", copy=True): x1_obj = dpnp.get_usm_ndarray(x1) array_obj = dpt.astype( - x1_obj, dtype, order=order, casting=casting, copy=copy + x1_obj, dtype, order=order, casting=casting, copy=copy, device=device ) # return x1 if dpctl returns a zero copy of x1_obj From fb61d2f0e75c3fd6a802d6a91b4733606710cd27 Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Thu, 6 Jun 2024 23:32:05 -0500 Subject: [PATCH 2/3] Added test for astype function --- tests/test_sycl_queue.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index 8332f26949ba..ba73c363347c 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -2208,3 +2208,21 @@ def test_histogram_bin_edges(weights, device): edges_queue = result_edges.sycl_queue assert_sycl_queue_equal(edges_queue, iv.sycl_queue) + + +@pytest.mark.parametrize( + "device_x", + valid_devices, + ids=[device.filter_string for device in valid_devices], +) +@pytest.mark.parametrize( + "device_y", + valid_devices, + ids=[device.filter_string for device in valid_devices], +) +def test_astype(device_x, device_y): + x = dpnp.array([1, 2, 3], dtype="i4", device=device_x) + y = dpnp.astype(x, dtype="f4") + assert_sycl_queue_equal(y.sycl_queue, x.sycl_queue) + y = dpnp.astype(x, dtype="f4", device=device_y) + assert_sycl_queue_equal(y.sycl_queue, x.to_device(device_y).sycl_queue) From 9b75790428abcc12e4d4c3165b661b88eefb590d Mon Sep 17 00:00:00 2001 From: Natalia Polina Date: Tue, 11 Jun 2024 15:09:56 -0700 Subject: [PATCH 3/3] address comments --- dpnp/dpnp_array.py | 2 +- dpnp/dpnp_iface.py | 2 +- tests/test_sycl_queue.py | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index 71a3a175d78b..fd2d06f74285 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -611,7 +611,7 @@ def astype( 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. Default: `None`. + :obj:`dpnp.dpnp_array.dpnp_array.device` property. Default: ``None``. Returns ------- diff --git a/dpnp/dpnp_iface.py b/dpnp/dpnp_iface.py index 83f94e330b06..49e7b41c01c9 100644 --- a/dpnp/dpnp_iface.py +++ b/dpnp/dpnp_iface.py @@ -219,7 +219,7 @@ def astype(x1, dtype, order="K", casting="unsafe", copy=True, device=None): 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. Default: `None`. + :obj:`dpnp.dpnp_array.dpnp_array.device` property. Default: ``None``. Returns ------- diff --git a/tests/test_sycl_queue.py b/tests/test_sycl_queue.py index ba73c363347c..703721ca22a0 100644 --- a/tests/test_sycl_queue.py +++ b/tests/test_sycl_queue.py @@ -2224,5 +2224,6 @@ def test_astype(device_x, device_y): x = dpnp.array([1, 2, 3], dtype="i4", device=device_x) y = dpnp.astype(x, dtype="f4") assert_sycl_queue_equal(y.sycl_queue, x.sycl_queue) - y = dpnp.astype(x, dtype="f4", device=device_y) - assert_sycl_queue_equal(y.sycl_queue, x.to_device(device_y).sycl_queue) + sycl_queue = dpctl.SyclQueue(device_y) + y = dpnp.astype(x, dtype="f4", device=sycl_queue) + assert_sycl_queue_equal(y.sycl_queue, sycl_queue)