diff --git a/numba_dpex/dpnp_iface/arrayobj.py b/numba_dpex/dpnp_iface/arrayobj.py index 3f7fb9c0ae..17b7885a71 100644 --- a/numba_dpex/dpnp_iface/arrayobj.py +++ b/numba_dpex/dpnp_iface/arrayobj.py @@ -431,6 +431,8 @@ def ol_dpnp_empty_like( x, dtype=None, order="C", + subok=False, + shape=None, device=None, usm_type=None, sycl_queue=None, @@ -471,6 +473,8 @@ def ol_dpnp_empty_like( _ndim = x.ndim if hasattr(x, "ndim") and x.ndim is not None else 0 _dtype = _parse_dtype(dtype, data=x) _order = x.layout if order is None else order + _subok = subok + _shape = shape _usm_type = _parse_usm_type(usm_type) if usm_type is not None else "device" _device = ( _parse_device_filter_string(device) if device is not None else "unknown" @@ -489,6 +493,8 @@ def impl( x, dtype=None, order="C", + subok=False, + shape=None, device=None, usm_type=None, sycl_queue=None, @@ -497,6 +503,8 @@ def impl( x, _dtype, _order, + _subok, + _shape, _device, _usm_type, sycl_queue, @@ -516,6 +524,8 @@ def ol_dpnp_zeros_like( x, dtype=None, order="C", + subok=False, + shape=None, device=None, usm_type=None, sycl_queue=None, @@ -555,6 +565,8 @@ def ol_dpnp_zeros_like( _ndim = x.ndim if hasattr(x, "ndim") and x.ndim is not None else 0 _dtype = _parse_dtype(dtype, data=x) _order = x.layout if order is None else order + _subok = subok + _shape = shape _usm_type = _parse_usm_type(usm_type) if usm_type is not None else "device" _device = ( _parse_device_filter_string(device) if device is not None else "unknown" @@ -573,6 +585,8 @@ def impl( x, dtype=None, order="C", + subok=False, + shape=None, device=None, usm_type=None, sycl_queue=None, @@ -581,6 +595,8 @@ def impl( x, _dtype, _order, + _subok, + _shape, _device, _usm_type, sycl_queue, @@ -600,6 +616,8 @@ def ol_dpnp_ones_like( x, dtype=None, order="C", + subok=False, + shape=None, device=None, usm_type=None, sycl_queue=None, @@ -639,6 +657,8 @@ def ol_dpnp_ones_like( _ndim = x.ndim if hasattr(x, "ndim") and x.ndim is not None else 0 _dtype = _parse_dtype(dtype, data=x) _order = x.layout if order is None else order + _subok = subok + _shape = shape _usm_type = _parse_usm_type(usm_type) if usm_type is not None else "device" _device = ( _parse_device_filter_string(device) if device is not None else "unknown" @@ -657,6 +677,8 @@ def impl( x, dtype=None, order="C", + subok=False, + shape=None, device=None, usm_type=None, sycl_queue=None, @@ -665,6 +687,8 @@ def impl( x, _dtype, _order, + _subok, + _shape, _device, _usm_type, sycl_queue, diff --git a/numba_dpex/dpnp_iface/intrinsic.py b/numba_dpex/dpnp_iface/intrinsic.py index 8c9ed5e4a9..705d24a9aa 100644 --- a/numba_dpex/dpnp_iface/intrinsic.py +++ b/numba_dpex/dpnp_iface/intrinsic.py @@ -173,12 +173,12 @@ def alloc_empty_arrayobj(context, builder, sig, llargs, is_like=False): in DpnpNdArray. """ - arrtype = ( - _parse_empty_like_args(context, builder, sig, llargs) - if is_like - else _parse_empty_args(context, builder, sig, llargs) - ) + if is_like: + arrtype = _parse_empty_like_args(context, builder, sig, llargs) + else: + arrtype = _parse_empty_args(context, builder, sig, llargs) ary = _empty_nd_impl(context, builder, *arrtype) + return ary, arrtype @@ -406,6 +406,8 @@ def impl_dpnp_empty_like( ty_x, ty_dtype, ty_order, + ty_subok, + ty_shape, ty_device, ty_usm_type, ty_sycl_queue, @@ -440,6 +442,8 @@ def impl_dpnp_empty_like( ty_x, ty_dtype, ty_order, + ty_subok, + ty_shape, ty_device, ty_usm_type, ty_sycl_queue, @@ -461,6 +465,8 @@ def impl_dpnp_zeros_like( ty_x, ty_dtype, ty_order, + ty_subok, + ty_shape, ty_device, ty_usm_type, ty_sycl_queue, @@ -495,6 +501,8 @@ def impl_dpnp_zeros_like( ty_x, ty_dtype, ty_order, + ty_subok, + ty_shape, ty_device, ty_usm_type, ty_sycl_queue, @@ -516,6 +524,8 @@ def impl_dpnp_ones_like( ty_x, ty_dtype, ty_order, + ty_subok, + ty_shape, ty_device, ty_usm_type, ty_sycl_queue, @@ -550,6 +560,8 @@ def impl_dpnp_ones_like( ty_x, ty_dtype, ty_order, + ty_subok, + ty_shape, ty_device, ty_usm_type, ty_sycl_queue, diff --git a/numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_empty_like.py b/numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_empty_like.py new file mode 100644 index 0000000000..75bfa8c792 --- /dev/null +++ b/numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_empty_like.py @@ -0,0 +1,53 @@ +# SPDX-FileCopyrightText: 2020 - 2023 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 + +"""Tests for dpnp ndarray constructors.""" + +import dpctl +import dpnp +import numpy +import pytest + +from numba_dpex import dpjit + +shapes = [10, (2, 5)] +dtypes = [dpnp.int32, dpnp.int64, dpnp.float32, dpnp.float64] +usm_types = ["device", "shared", "host"] +devices = ["cpu", "unknown"] + + +@pytest.mark.parametrize("shape", shapes) +@pytest.mark.parametrize("dtype", dtypes) +@pytest.mark.parametrize("usm_type", usm_types) +@pytest.mark.parametrize("device", devices) +def test_dpnp_empty_like(shape, dtype, usm_type, device): + @dpjit + def func1(a): + c = dpnp.empty_like(a, dtype=dtype, usm_type=usm_type, device=device) + return c + + if isinstance(shape, int): + NZ = numpy.random.rand(shape) + else: + NZ = numpy.random.rand(*shape) + + try: + c = func1(NZ) + except Exception: + pytest.fail("Calling dpnp.empty_like inside dpjit failed") + + if len(c.shape) == 1: + assert c.shape[0] == NZ.shape[0] + else: + assert c.shape == NZ.shape + + assert c.dtype == dtype + assert c.usm_type == usm_type + if device != "unknown": + assert ( + c.sycl_device.filter_string + == dpctl.SyclDevice(device).filter_string + ) + else: + c.sycl_device.filter_string == dpctl.SyclDevice().filter_string diff --git a/numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_ones_like.py b/numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_ones_like.py new file mode 100644 index 0000000000..1b6683a423 --- /dev/null +++ b/numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_ones_like.py @@ -0,0 +1,58 @@ +# SPDX-FileCopyrightText: 2020 - 2023 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 + +"""Tests for dpnp ndarray constructors.""" + +import dpctl +import dpctl.tensor as dpt +import dpnp +import numpy +import pytest + +from numba_dpex import dpjit + +shapes = [11, (3, 7)] +dtypes = [dpnp.int32, dpnp.int64, dpnp.float32, dpnp.float64] +usm_types = ["device", "shared", "host"] +devices = ["cpu", "unknown"] + + +@pytest.mark.parametrize("shape", shapes) +@pytest.mark.parametrize("dtype", dtypes) +@pytest.mark.parametrize("usm_type", usm_types) +@pytest.mark.parametrize("device", devices) +def test_dpnp_ones(shape, dtype, usm_type, device): + @dpjit + def func1(a): + c = dpnp.ones(a, dtype=dtype, usm_type=usm_type, device=device) + return c + + if isinstance(shape, int): + NZ = numpy.random.rand(shape) + else: + NZ = numpy.random.rand(*shape) + + try: + c = func1(shape) + except Exception: + pytest.fail("Calling dpnp.empty inside dpjit failed") + + if len(c.shape) == 1: + assert c.shape[0] == NZ.shape[0] + else: + assert c.shape == NZ.shape + + assert c.dtype == dtype + assert c.usm_type == usm_type + if device != "unknown": + assert ( + c.sycl_device.filter_string + == dpctl.SyclDevice(device).filter_string + ) + else: + c.sycl_device.filter_string == dpctl.SyclDevice().filter_string + + assert numpy.array_equal( + dpt.asnumpy(c._array_obj), numpy.ones_like(c._array_obj) + ) diff --git a/numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_zeros_like.py b/numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_zeros_like.py new file mode 100644 index 0000000000..3b0b0f30cf --- /dev/null +++ b/numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_zeros_like.py @@ -0,0 +1,58 @@ +# SPDX-FileCopyrightText: 2020 - 2023 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 + +"""Tests for dpnp ndarray constructors.""" + +import dpctl +import dpctl.tensor as dpt +import dpnp +import numpy +import pytest + +from numba_dpex import dpjit + +shapes = [11, (3, 7)] +dtypes = [dpnp.int32, dpnp.int64, dpnp.float32, dpnp.float64] +usm_types = ["device", "shared", "host"] +devices = ["cpu", "unknown"] + + +@pytest.mark.parametrize("shape", shapes) +@pytest.mark.parametrize("dtype", dtypes) +@pytest.mark.parametrize("usm_type", usm_types) +@pytest.mark.parametrize("device", devices) +def test_dpnp_zeros(shape, dtype, usm_type, device): + @dpjit + def func1(a): + c = dpnp.zeros(a, dtype=dtype, usm_type=usm_type, device=device) + return c + + if isinstance(shape, int): + NZ = numpy.random.rand(shape) + else: + NZ = numpy.random.rand(*shape) + + try: + c = func1(shape) + except Exception: + pytest.fail("Calling dpnp.empty inside dpjit failed") + + if len(c.shape) == 1: + assert c.shape[0] == NZ.shape[0] + else: + assert c.shape == NZ.shape + + assert c.dtype == dtype + assert c.usm_type == usm_type + if device != "unknown": + assert ( + c.sycl_device.filter_string + == dpctl.SyclDevice(device).filter_string + ) + else: + c.sycl_device.filter_string == dpctl.SyclDevice().filter_string + + assert numpy.array_equal( + dpt.asnumpy(c._array_obj), numpy.zeros_like(c._array_obj) + )