Skip to content

Commit

Permalink
Test cases added
Browse files Browse the repository at this point in the history
  • Loading branch information
chudur-budur committed Feb 27, 2023
1 parent 4486cf5 commit 481eafb
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 5 deletions.
24 changes: 24 additions & 0 deletions numba_dpex/dpnp_iface/arrayobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"
Expand All @@ -489,6 +493,8 @@ def impl(
x,
dtype=None,
order="C",
subok=False,
shape=None,
device=None,
usm_type=None,
sycl_queue=None,
Expand All @@ -497,6 +503,8 @@ def impl(
x,
_dtype,
_order,
_subok,
_shape,
_device,
_usm_type,
sycl_queue,
Expand All @@ -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,
Expand Down Expand Up @@ -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"
Expand All @@ -573,6 +585,8 @@ def impl(
x,
dtype=None,
order="C",
subok=False,
shape=None,
device=None,
usm_type=None,
sycl_queue=None,
Expand All @@ -581,6 +595,8 @@ def impl(
x,
_dtype,
_order,
_subok,
_shape,
_device,
_usm_type,
sycl_queue,
Expand All @@ -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,
Expand Down Expand Up @@ -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"
Expand All @@ -657,6 +677,8 @@ def impl(
x,
dtype=None,
order="C",
subok=False,
shape=None,
device=None,
usm_type=None,
sycl_queue=None,
Expand All @@ -665,6 +687,8 @@ def impl(
x,
_dtype,
_order,
_subok,
_shape,
_device,
_usm_type,
sycl_queue,
Expand Down
22 changes: 17 additions & 5 deletions numba_dpex/dpnp_iface/intrinsic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
53 changes: 53 additions & 0 deletions numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_empty_like.py
Original file line number Diff line number Diff line change
@@ -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
58 changes: 58 additions & 0 deletions numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_ones_like.py
Original file line number Diff line number Diff line change
@@ -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)
)
58 changes: 58 additions & 0 deletions numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_zeros_like.py
Original file line number Diff line number Diff line change
@@ -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)
)

0 comments on commit 481eafb

Please sign in to comment.