Skip to content
71 changes: 43 additions & 28 deletions dpnp/tests/tensor/elementwise/test_abs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
# *****************************************************************************

import itertools
import re
import warnings

import dpctl
import numpy as np
import pytest

Expand All @@ -42,7 +44,6 @@
_all_dtypes,
_complex_fp_dtypes,
_real_fp_dtypes,
_usm_types,
)


Expand Down Expand Up @@ -71,33 +72,6 @@ def test_abs_out_type(dtype):
assert np.allclose(dpt.asnumpy(r), dpt.asnumpy(dpt.abs(X)))


@pytest.mark.parametrize("usm_type", _usm_types)
def test_abs_usm_type(usm_type):
q = get_queue_or_skip()

arg_dt = np.dtype("i4")
input_shape = (10, 10, 10, 10)
X = dpt.empty(input_shape, dtype=arg_dt, usm_type=usm_type, sycl_queue=q)
X[..., 0::2] = 1
X[..., 1::2] = 0

Y = dpt.abs(X)
assert Y.usm_type == X.usm_type
assert Y.sycl_queue == X.sycl_queue
assert Y.flags.c_contiguous

expected_Y = dpt.asnumpy(X)
assert np.allclose(dpt.asnumpy(Y), expected_Y)


def test_abs_types_property():
get_queue_or_skip()
types = dpt.abs.types
assert isinstance(types, list)
assert len(types) > 0
assert types == dpt.abs.types_


@pytest.mark.parametrize("dtype", _all_dtypes[1:])
def test_abs_order(dtype):
q = get_queue_or_skip()
Expand Down Expand Up @@ -222,3 +196,44 @@ def test_abs_alignment(dtype):

dpt.abs(x[:-1], out=r[1:])
assert np.allclose(dpt.asnumpy(r[1:]), dpt.asnumpy(r2))


def test_abs_errors():
q1 = get_queue_or_skip()
q2 = dpctl.SyclQueue()

x = dpt.ones(2, dtype="float32", sycl_queue=q1)
y = dpt.empty_like(x, sycl_queue=q2)
with pytest.raises(dpt.ExecutionPlacementError) as excinfo:
dpt.abs(x, out=y)
assert "Input and output allocation queues are not compatible" in str(
excinfo.value
)

x = dpt.ones(2, dtype="float32")
y = dpt.empty(3, dtype=x.dtype)
with pytest.raises(ValueError) as excinfo:
dpt.abs(x, out=y)
assert "The shape of input and output arrays are inconsistent" in str(
excinfo.value
)

x = np.ones(2, dtype="float32")
with pytest.raises(TypeError) as excinfo:
dpt.abs(x)
assert re.match(
"Expected dpnp.tensor.usm_ndarray, got.*",
str(excinfo.value),
)

x = dpt.ones(2, dtype="float32")
y = np.empty(x.shape, dtype=x.dtype)
with pytest.raises(TypeError) as excinfo:
dpt.abs(x, out=y)
assert "output array must be of usm_ndarray type" in str(excinfo.value)

x = dpt.ones(5, dtype="f4")
y = dpt.zeros_like(x, dtype="int8")
with pytest.raises(ValueError) as excinfo:
dpt.abs(x, out=y)
assert re.match("Output array of type.*is needed", str(excinfo.value))
88 changes: 15 additions & 73 deletions dpnp/tests/tensor/elementwise/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
from .utils import (
_all_dtypes,
_compare_dtypes,
_usm_types,
)


Expand Down Expand Up @@ -89,21 +88,6 @@ def test_add_dtype_matrix(op1_dtype, op2_dtype):
assert (dpt.asnumpy(r2) == np.full(r2.shape, 2, dtype=r2.dtype)).all()


@pytest.mark.parametrize("op1_usm_type", _usm_types)
@pytest.mark.parametrize("op2_usm_type", _usm_types)
def test_add_usm_type_matrix(op1_usm_type, op2_usm_type):
get_queue_or_skip()

sz = 128
ar1 = dpt.ones(sz, dtype="i4", usm_type=op1_usm_type)
ar2 = dpt.ones_like(ar1, dtype="i4", usm_type=op2_usm_type)

r = dpt.add(ar1, ar2)
assert isinstance(r, dpt.usm_ndarray)
expected_usm_type = dpt.get_coerced_usm_type((op1_usm_type, op2_usm_type))
assert r.usm_type == expected_usm_type


def test_add_order():
get_queue_or_skip()

Expand Down Expand Up @@ -283,23 +267,17 @@ def test_add_types_property():


def test_add_errors():
get_queue_or_skip()
try:
gpu_queue = dpctl.SyclQueue("gpu")
except dpctl.SyclQueueCreationError:
pytest.skip("SyclQueue('gpu') failed, skipping")
try:
cpu_queue = dpctl.SyclQueue("cpu")
except dpctl.SyclQueueCreationError:
pytest.skip("SyclQueue('cpu') failed, skipping")

ar1 = dpt.ones(2, dtype="float32", sycl_queue=gpu_queue)
ar2 = dpt.ones_like(ar1, sycl_queue=gpu_queue)
y = dpt.empty_like(ar1, sycl_queue=cpu_queue)
q1 = get_queue_or_skip()
q2 = dpctl.SyclQueue()

ar1 = dpt.ones(2, dtype="float32", sycl_queue=q1)
ar2 = dpt.ones_like(ar1, dtype="float32", sycl_queue=q2)
y = dpt.empty_like(ar1, sycl_queue=q2)
with pytest.raises(dpt.ExecutionPlacementError) as excinfo:
dpt.add(ar1, ar2, out=y)
assert "Input and output allocation queues are not compatible" in str(
excinfo.value
assert re.match(
"Execution placement can not be unambiguously inferred.*",
str(excinfo.value),
)

ar1 = dpt.ones(2, dtype="float32")
Expand Down Expand Up @@ -327,17 +305,8 @@ def test_add_errors():
dpt.add(ar1, ar2, out=y)
assert "output array must be of usm_ndarray type" in str(excinfo.value)


@pytest.mark.parametrize("dtype", _all_dtypes)
def test_add_dtype_error(
dtype,
):
q = get_queue_or_skip()
skip_if_dtype_not_supported(dtype, q)

ar1 = dpt.ones(5, dtype=dtype)
ar1 = dpt.ones(5, dtype="f4")
ar2 = dpt.ones_like(ar1, dtype="f4")

y = dpt.zeros_like(ar1, dtype="int8")
with pytest.raises(ValueError) as excinfo:
dpt.add(ar1, ar2, out=y)
Expand Down Expand Up @@ -469,39 +438,12 @@ def test_add_inplace_operator_mutual_broadcast():


def test_add_inplace_errors():
get_queue_or_skip()
try:
gpu_queue = dpctl.SyclQueue("gpu")
except dpctl.SyclQueueCreationError:
pytest.skip("SyclQueue('gpu') failed, skipping")
try:
cpu_queue = dpctl.SyclQueue("cpu")
except dpctl.SyclQueueCreationError:
pytest.skip("SyclQueue('cpu') failed, skipping")

ar1 = dpt.ones(2, dtype="float32", sycl_queue=gpu_queue)
ar2 = dpt.ones_like(ar1, sycl_queue=cpu_queue)
with pytest.raises(dpt.ExecutionPlacementError):
dpt.add(ar1, ar2, out=ar1)

ar1 = dpt.ones(2, dtype="float32")
ar2 = dpt.ones(3, dtype="float32")
with pytest.raises(ValueError):
dpt.add(ar1, ar2, out=ar1)

ar1 = np.ones(2, dtype="float32")
ar2 = dpt.ones(2, dtype="float32")
with pytest.raises(TypeError):
dpt.add(ar1, ar2, out=ar1)

ar1 = dpt.ones(2, dtype="float32")
ar2 = {}
with pytest.raises(ValueError):
dpt.add(ar1, ar2, out=ar1)
q1 = get_queue_or_skip()
q2 = dpctl.SyclQueue()

ar1 = dpt.ones((2, 1), dtype="float32")
ar2 = dpt.ones((1, 2), dtype="float32")
with pytest.raises(ValueError):
ar1 = dpt.ones(2, dtype="float32", sycl_queue=q1)
ar2 = dpt.ones_like(ar1, sycl_queue=q2)
with pytest.raises(dpt.ExecutionPlacementError):
dpt.add(ar1, ar2, out=ar1)


Expand Down
24 changes: 1 addition & 23 deletions dpnp/tests/tensor/elementwise/test_atan2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
# THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************

import ctypes

import numpy as np
import pytest
from numpy.testing import assert_allclose
Expand Down Expand Up @@ -86,26 +84,6 @@ def test_atan2_dtype_matrix(op1_dtype, op2_dtype):
assert_allclose(dpt.asnumpy(r), expected, atol=tol, rtol=tol)


@pytest.mark.parametrize("arr_dt", _no_complex_dtypes[1:])
def test_atan2_python_scalar(arr_dt):
q = get_queue_or_skip()
skip_if_dtype_not_supported(arr_dt, q)

X = dpt.ones((10, 10), dtype=arr_dt, sycl_queue=q)
py_ones = (
bool(1),
int(1),
float(1),
np.float32(1),
ctypes.c_int(1),
)
for sc in py_ones:
R = dpt.atan2(X, sc)
assert isinstance(R, dpt.usm_ndarray)
R = dpt.atan2(sc, X)
assert isinstance(R, dpt.usm_ndarray)


@pytest.mark.parametrize("dt", ["f2", "f4", "f8"])
def test_atan2_special_case_one_nan(dt):
"""If either x1_i or x2_i is NaN, the result is NaN."""
Expand Down Expand Up @@ -212,7 +190,7 @@ def test_atan2_special_case_pzero_and_nzero(dt):


@pytest.mark.parametrize("dt", ["f2", "f4", "f8"])
def test_atan2_special_case_pzero_and_negatvie(dt):
def test_atan2_special_case_pzero_and_negative(dt):
"""
If x1_i is +0 and x2_i is less than 0, the result
is an approximation to +pi.
Expand Down
12 changes: 0 additions & 12 deletions dpnp/tests/tensor/elementwise/test_bitwise_and.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,6 @@ def test_bitwise_and_bool():
assert dpt.all(dpt.equal(r_bw, r_lo))


@pytest.mark.parametrize("dtype", ["?"] + _integral_dtypes)
def test_bitwise_and_inplace_python_scalar(dtype):
q = get_queue_or_skip()
skip_if_dtype_not_supported(dtype, q)
X = dpt.zeros((10, 10), dtype=dtype, sycl_queue=q)
dt_kind = X.dtype.kind
if dt_kind == "b":
X &= False
else:
X &= int(0)


@pytest.mark.parametrize("op1_dtype", ["?"] + _integral_dtypes)
@pytest.mark.parametrize("op2_dtype", ["?"] + _integral_dtypes)
def test_bitwise_and_inplace_dtype_matrix(op1_dtype, op2_dtype):
Expand Down
47 changes: 0 additions & 47 deletions dpnp/tests/tensor/elementwise/test_bitwise_invert.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from .utils import (
_compare_dtypes,
_integral_dtypes,
_usm_types,
)


Expand Down Expand Up @@ -92,52 +91,6 @@ def test_bitwise_invert_dtype_matrix(op_dtype):
assert dpt.all(dpt.equal(r, r3))


@pytest.mark.parametrize("op_usm_type", _usm_types)
def test_bitwise_invert_usm_type_matrix(op_usm_type):
get_queue_or_skip()

sz = 128
ar1 = dpt.asarray(
np.random.randint(0, 2, sz), dtype="i4", usm_type=op_usm_type
)

r = dpt.bitwise_invert(ar1)
assert isinstance(r, dpt.usm_ndarray)
assert r.usm_type == op_usm_type


def test_bitwise_invert_order():
get_queue_or_skip()

ar1 = dpt.ones((20, 20), dtype="i4", order="C")
r1 = dpt.bitwise_invert(ar1, order="C")
assert r1.flags.c_contiguous
r2 = dpt.bitwise_invert(ar1, order="F")
assert r2.flags.f_contiguous
r3 = dpt.bitwise_invert(ar1, order="A")
assert r3.flags.c_contiguous
r4 = dpt.bitwise_invert(ar1, order="K")
assert r4.flags.c_contiguous

ar1 = dpt.zeros((20, 20), dtype="i4", order="F")
r1 = dpt.bitwise_invert(ar1, order="C")
assert r1.flags.c_contiguous
r2 = dpt.bitwise_invert(ar1, order="F")
assert r2.flags.f_contiguous
r3 = dpt.bitwise_invert(ar1, order="A")
assert r3.flags.f_contiguous
r4 = dpt.bitwise_invert(ar1, order="K")
assert r4.flags.f_contiguous

ar1 = dpt.ones((40, 40), dtype="i4", order="C")[:20, ::-2]
r4 = dpt.bitwise_invert(ar1, order="K")
assert r4.strides == (20, -1)

ar1 = dpt.zeros((40, 40), dtype="i4", order="C")[:20, ::-2].mT
r4 = dpt.bitwise_invert(ar1, order="K")
assert r4.strides == (-1, 20)


def test_bitwise_invert_large_boolean():
get_queue_or_skip()

Expand Down
8 changes: 0 additions & 8 deletions dpnp/tests/tensor/elementwise/test_bitwise_left_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ def test_bitwise_left_shift_range(op_dtype):
assert dpt.all(dpt.equal(z, 0))


@pytest.mark.parametrize("dtype", _integral_dtypes)
def test_bitwise_left_shift_inplace_python_scalar(dtype):
q = get_queue_or_skip()
skip_if_dtype_not_supported(dtype, q)
X = dpt.zeros((10, 10), dtype=dtype, sycl_queue=q)
X <<= int(0)


@pytest.mark.parametrize("op1_dtype", _integral_dtypes)
@pytest.mark.parametrize("op2_dtype", _integral_dtypes)
def test_bitwise_left_shift_inplace_dtype_matrix(op1_dtype, op2_dtype):
Expand Down
12 changes: 0 additions & 12 deletions dpnp/tests/tensor/elementwise/test_bitwise_or.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,6 @@ def test_bitwise_or_bool():
assert dpt.all(dpt.equal(r_bw, r_lo))


@pytest.mark.parametrize("dtype", ["?"] + _integral_dtypes)
def test_bitwise_or_inplace_python_scalar(dtype):
q = get_queue_or_skip()
skip_if_dtype_not_supported(dtype, q)
X = dpt.zeros((10, 10), dtype=dtype, sycl_queue=q)
dt_kind = X.dtype.kind
if dt_kind == "b":
X |= False
else:
X |= int(0)


@pytest.mark.parametrize("op1_dtype", ["?"] + _integral_dtypes)
@pytest.mark.parametrize("op2_dtype", ["?"] + _integral_dtypes)
def test_bitwise_or_inplace_dtype_matrix(op1_dtype, op2_dtype):
Expand Down
8 changes: 0 additions & 8 deletions dpnp/tests/tensor/elementwise/test_bitwise_right_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,6 @@ def test_bitwise_right_shift_range(op_dtype):
assert dpt.all(dpt.equal(z, 0))


@pytest.mark.parametrize("dtype", _integral_dtypes)
def test_bitwise_right_shift_inplace_python_scalar(dtype):
q = get_queue_or_skip()
skip_if_dtype_not_supported(dtype, q)
X = dpt.zeros((10, 10), dtype=dtype, sycl_queue=q)
X >>= int(0)


@pytest.mark.parametrize("op1_dtype", _integral_dtypes)
@pytest.mark.parametrize("op2_dtype", _integral_dtypes)
def test_bitwise_right_shift_inplace_dtype_matrix(op1_dtype, op2_dtype):
Expand Down
Loading
Loading