Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion dpnp/tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ def get_float_complex_dtypes(no_float16=True, device=None):
def get_abs_array(data, dtype=None):
if numpy.issubdtype(dtype, numpy.unsignedinteger):
data = numpy.abs(data)
return numpy.array(data, dtype=dtype)
# it is better to use astype with the default casting=unsafe
# otherwise, we need to skip test for cases where overflow occurs
return numpy.array(data).astype(dtype)


def get_all_dtypes(
Expand Down
44 changes: 21 additions & 23 deletions dpnp/tests/test_amin_amax.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@pytest.mark.parametrize("func", ["amax", "amin"])
@pytest.mark.parametrize("keepdims", [True, False])
@pytest.mark.parametrize("dtype", get_all_dtypes())
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
def test_amax_amin(func, keepdims, dtype):
a = [
[[-2.0, 3.0], [9.1, 0.2]],
Expand All @@ -22,52 +22,50 @@ def test_amax_amin(func, keepdims, dtype):
for axis in range(len(a)):
result = getattr(dpnp, func)(ia, axis=axis, keepdims=keepdims)
expected = getattr(numpy, func)(a, axis=axis, keepdims=keepdims)
assert_allclose(expected, result)
assert_allclose(result, expected)


def _get_min_max_input(type, shape):
def _get_min_max_input(dtype, shape):
size = numpy.prod(shape)
a = numpy.arange(size, dtype=type)
a = numpy.arange(size, dtype=dtype)
a[int(size / 2)] = size + 5
if numpy.issubdtype(type, numpy.unsignedinteger):
if numpy.issubdtype(dtype, numpy.unsignedinteger):
a[int(size / 3)] = size
else:
a[int(size / 3)] = -(size + 5)

return a.reshape(shape)


@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True, no_bool=True))
@pytest.mark.parametrize(
"shape", [(4,), (2, 3), (4, 5, 6)], ids=["(4,)", "(2, 3)", "(4, 5, 6)"]
"shape", [(4,), (2, 3), (4, 5, 6)], ids=["1D", "2D", "3D"]
)
def test_amax_diff_shape(dtype, shape):
a = _get_min_max_input(dtype, shape)

ia = dpnp.array(a)

np_res = numpy.amax(a)
dpnp_res = dpnp.amax(ia)
assert_array_equal(dpnp_res, np_res)
expected = numpy.amax(a)
result = dpnp.amax(ia)
assert_array_equal(result, expected)

np_res = a.max()
dpnp_res = ia.max()
numpy.testing.assert_array_equal(dpnp_res, np_res)
expected = a.max()
result = ia.max()
assert_array_equal(result, expected)


@pytest.mark.parametrize("dtype", get_all_dtypes(no_bool=True))
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True, no_bool=True))
@pytest.mark.parametrize(
"shape", [(4,), (2, 3), (4, 5, 6)], ids=["(4,)", "(2, 3)", "(4, 5, 6)"]
"shape", [(4,), (2, 3), (4, 5, 6)], ids=["1D", "2D", "3D"]
)
def test_amin_diff_shape(dtype, shape):
a = _get_min_max_input(dtype, shape)

ia = dpnp.array(a)

np_res = numpy.amin(a)
dpnp_res = dpnp.amin(ia)
assert_array_equal(dpnp_res, np_res)
expected = numpy.amin(a)
result = dpnp.amin(ia)
assert_array_equal(result, expected)

np_res = a.min()
dpnp_res = ia.min()
assert_array_equal(dpnp_res, np_res)
expected = a.min()
result = ia.min()
assert_array_equal(result, expected)
44 changes: 15 additions & 29 deletions dpnp/tests/test_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def test_arange(start, stop, step, dtype):
func = lambda xp: xp.arange(start, stop=stop, step=step, dtype=dtype)

exp_array = func(numpy)
res_array = func(dpnp).asnumpy()
res_array = func(dpnp)

if dtype is None:
_device = dpctl.SyclQueue().sycl_device
Expand All @@ -234,7 +234,7 @@ def test_arange(start, stop, step, dtype):
_dtype, dpnp.complexfloating
):
assert_allclose(
exp_array, res_array, rtol=rtol_mult * numpy.finfo(_dtype).eps
res_array, exp_array, rtol=rtol_mult * numpy.finfo(_dtype).eps
)
else:
assert_array_equal(exp_array, res_array)
Expand Down Expand Up @@ -540,7 +540,7 @@ def test_vander(array, dtype, n, increase):
a_np = numpy.array(array, dtype=dtype)
a_dpnp = dpnp.array(array, dtype=dtype)

assert_allclose(vander_func(numpy, a_np), vander_func(dpnp, a_dpnp))
assert_allclose(vander_func(dpnp, a_dpnp), vander_func(numpy, a_np))


def test_vander_raise_error():
Expand All @@ -560,7 +560,7 @@ def test_vander_raise_error():
)
def test_vander_seq(sequence):
vander_func = lambda xp, x: xp.vander(x)
assert_allclose(vander_func(numpy, sequence), vander_func(dpnp, sequence))
assert_allclose(vander_func(dpnp, sequence), vander_func(numpy, sequence))


@pytest.mark.usefixtures("suppress_complex_warning")
Expand Down Expand Up @@ -607,19 +607,19 @@ def test_full_order(order1, order2):

assert ia.flags.c_contiguous == a.flags.c_contiguous
assert ia.flags.f_contiguous == a.flags.f_contiguous
assert numpy.array_equal(dpnp.asnumpy(ia), a)
assert_equal(ia, a)


def test_full_strides():
a = numpy.full((3, 3), numpy.arange(3, dtype="i4"))
ia = dpnp.full((3, 3), dpnp.arange(3, dtype="i4"))
assert ia.strides == tuple(el // a.itemsize for el in a.strides)
assert_array_equal(dpnp.asnumpy(ia), a)
assert_array_equal(ia, a)

a = numpy.full((3, 3), numpy.arange(6, dtype="i4")[::2])
ia = dpnp.full((3, 3), dpnp.arange(6, dtype="i4")[::2])
assert ia.strides == tuple(el // a.itemsize for el in a.strides)
assert_array_equal(dpnp.asnumpy(ia), a)
assert_array_equal(ia, a)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -762,20 +762,12 @@ def test_linspace(start, stop, num, dtype, retstep):
assert_dtype_allclose(res_dp, res_np)


@pytest.mark.parametrize("func", ["geomspace", "linspace", "logspace"])
@pytest.mark.parametrize(
"func",
["geomspace", "linspace", "logspace"],
ids=["geomspace", "linspace", "logspace"],
"start_dtype", [numpy.float64, numpy.float32, numpy.int64, numpy.int32]
)
@pytest.mark.parametrize(
"start_dtype",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=["float64", "float32", "int64", "int32"],
)
@pytest.mark.parametrize(
"stop_dtype",
[numpy.float64, numpy.float32, numpy.int64, numpy.int32],
ids=["float64", "float32", "int64", "int32"],
"stop_dtype", [numpy.float64, numpy.float32, numpy.int64, numpy.int32]
)
def test_space_numpy_dtype(func, start_dtype, stop_dtype):
start = numpy.array([1, 2, 3], dtype=start_dtype)
Expand Down Expand Up @@ -890,10 +882,7 @@ def test_geomspace(sign, dtype, num, endpoint):
np_res = func(numpy)
dpnp_res = func(dpnp)

if dtype in [numpy.int64, numpy.int32]:
assert_allclose(dpnp_res, np_res, rtol=1)
else:
assert_allclose(dpnp_res, np_res, rtol=1e-04)
assert_allclose(dpnp_res, np_res, rtol=1e-06)


@pytest.mark.parametrize("start", [1j, 1 + 1j])
Expand All @@ -902,22 +891,22 @@ def test_geomspace_complex(start, stop):
func = lambda xp: xp.geomspace(start, stop, num=10)
np_res = func(numpy)
dpnp_res = func(dpnp)
assert_allclose(dpnp_res, np_res, rtol=1e-04)
assert_allclose(dpnp_res, np_res, rtol=1e-06)


@pytest.mark.parametrize("axis", [0, 1])
def test_geomspace_axis(axis):
func = lambda xp: xp.geomspace([2, 3], [20, 15], num=10, axis=axis)
np_res = func(numpy)
dpnp_res = func(dpnp)
assert_allclose(dpnp_res, np_res, rtol=1e-04)
assert_allclose(dpnp_res, np_res, rtol=1e-06)


def test_geomspace_num0():
func = lambda xp: xp.geomspace(1, 10, num=0, endpoint=False)
np_res = func(numpy)
dpnp_res = func(dpnp)
assert_allclose(dpnp_res, np_res, rtol=1e-04)
assert_allclose(dpnp_res, np_res)


@pytest.mark.parametrize("dtype", get_all_dtypes())
Expand All @@ -935,10 +924,7 @@ def test_logspace(dtype, num, endpoint):
np_res = func(numpy)
dpnp_res = func(dpnp)

if dtype in [numpy.int64, numpy.int32]:
assert_allclose(dpnp_res, np_res, rtol=1)
else:
assert_allclose(dpnp_res, np_res, rtol=1e-04)
assert_allclose(dpnp_res, np_res, rtol=1e-06)


@pytest.mark.parametrize("axis", [0, 1])
Expand Down
Loading
Loading