Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .github/workflows/conda-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ jobs:
env:
DPNP_TEST_ALL_INT_TYPES: 1
run: |
pytest -n auto -ra --pyargs ${{ env.PACKAGE_NAME }}.tests
pytest -ra --pyargs ${{ env.PACKAGE_NAME }}.tests

upload:
name: Upload ['${{ matrix.os }}', python='${{ matrix.python }}']
Expand Down
5 changes: 4 additions & 1 deletion dpnp/dpnp_algo/dpnp_elementwise_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,10 @@ def __call__(self, x, decimals=0, out=None, dtype=None):
dtype = x_usm.dtype

out_usm = None if out is None else dpnp.get_usm_ndarray(out)
x_usm = dpt.round(x_usm * 10**decimals, out=out_usm)
# the output of x_usm multiplied by 10^decimals should be
# float to avoid overflow for integer dtypes
x_usm = dpt.multiply(x_usm, float(10**decimals))
x_usm = dpt.round(x_usm, out=out_usm)
res_usm = dpt.divide(x_usm, 10**decimals, out=out_usm)

if dtype is not None:
Expand Down
13 changes: 13 additions & 0 deletions dpnp/dpnp_iface_manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,20 @@ def copyto(dst, src, casting="same_kind", where=True):
f"but got {type(dst)}"
)
if not dpnp.is_supported_array_type(src):
src_orig = src
src = dpnp.array(src, sycl_queue=dst.sycl_queue)
if not hasattr(src_orig, "dtype"):
# This case (scalar, list, etc) needs special handling to
# behave similar to NumPy
if dpnp.issubdtype(src, dpnp.integer) and dpnp.issubdtype(
dst, dpnp.unsignedinteger
):
if dpnp.any(src < 0):
raise OverflowError(
"Cannot copy negative values to an unsigned int array"
)

src = src.astype(dst.dtype)

if not dpnp.can_cast(src.dtype, dst.dtype, casting=casting):
raise TypeError(
Expand Down
46 changes: 33 additions & 13 deletions dpnp/tests/third_party/cupy/core_tests/test_elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import pytest

import dpnp as cupy
from dpnp.tests.helper import has_support_aspect64
from dpnp.tests.helper import (
has_support_aspect64,
is_win_platform,
numpy_version,
)
from dpnp.tests.third_party.cupy import testing


Expand Down Expand Up @@ -94,20 +98,22 @@ class TestElementwiseType(unittest.TestCase):
@testing.for_int_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal(accept_error=OverflowError)
def test_large_int_upper_1(self, xp, dtype):
a = xp.array([0], dtype=numpy.int8)
a = xp.array([0], dtype=xp.int8)
b = xp.iinfo(dtype).max
return a + b

@testing.for_int_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal(accept_error=OverflowError)
def test_large_int_upper_2(self, xp, dtype):
if (
numpy.issubdtype(dtype, numpy.unsignedinteger)
and numpy.lib.NumpyVersion(numpy.__version__) < "2.0.0"
):
pytest.skip("numpy promotes dtype differently")
if numpy_version() < "2.0.0":
flag = dtype in [xp.int16, xp.int32, xp.int64, xp.longlong]
if xp.issubdtype(dtype, xp.unsignedinteger) or flag:
pytest.skip("numpy doesn't raise OverflowError")

if dtype in [xp.int8, xp.intc] and is_win_platform():
pytest.skip("numpy promotes dtype differently")

a = xp.array([1], dtype=numpy.int8)
a = xp.array([1], dtype=xp.int8)
b = xp.iinfo(dtype).max - 1
return a + b

Expand All @@ -116,7 +122,7 @@ def test_large_int_upper_2(self, xp, dtype):
def test_large_int_upper_3(self, xp, dtype):
if (
numpy.issubdtype(dtype, numpy.unsignedinteger)
and numpy.lib.NumpyVersion(numpy.__version__) < "2.0.0"
and numpy_version() < "2.0.0"
):
pytest.skip("numpy promotes dtype differently")
elif (
Expand All @@ -134,7 +140,7 @@ def test_large_int_upper_3(self, xp, dtype):
def test_large_int_upper_4(self, xp, dtype):
if (
numpy.issubdtype(dtype, numpy.unsignedinteger)
and numpy.lib.NumpyVersion(numpy.__version__) < "2.0.0"
and numpy_version() < "2.0.0"
):
pytest.skip("numpy promotes dtype differently")
elif (
Expand All @@ -150,14 +156,28 @@ def test_large_int_upper_4(self, xp, dtype):
@testing.for_int_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal(accept_error=OverflowError)
def test_large_int_lower_1(self, xp, dtype):
a = xp.array([0], dtype=numpy.int8)
if numpy_version() < "2.0.0":
if dtype in [xp.int16, xp.int32, xp.int64, xp.longlong]:
pytest.skip("numpy doesn't raise OverflowError")

if dtype in [xp.int8, xp.intc] and is_win_platform():
pytest.skip("numpy promotes dtype differently")

a = xp.array([0], dtype=xp.int8)
b = xp.iinfo(dtype).min
return a + b

@testing.for_int_dtypes(no_bool=True)
@testing.numpy_cupy_array_equal(accept_error=OverflowError)
def test_large_int_lower_2(self, xp, dtype):
a = xp.array([-1], dtype=numpy.int8)
if numpy_version() < "2.0.0":
if dtype in [xp.int16, xp.int32, xp.int64, xp.longlong]:
pytest.skip("numpy doesn't raise OverflowError")

if dtype in [xp.int8, xp.intc] and is_win_platform():
pytest.skip("numpy promotes dtype differently")

a = xp.array([-1], dtype=xp.int8)
b = xp.iinfo(dtype).min + 1
return a + b

Expand All @@ -166,7 +186,7 @@ def test_large_int_lower_2(self, xp, dtype):
def test_large_int_lower_3(self, xp, dtype):
if (
numpy.issubdtype(dtype, numpy.unsignedinteger)
and numpy.lib.NumpyVersion(numpy.__version__) < "2.0.0"
and numpy_version() < "2.0.0"
):
pytest.skip("numpy promotes dtype differently")
elif (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ def test_conjugate_pass(self, xp, dtype):

class TestAngle(unittest.TestCase):

# For dtype=int8, uint8, NumPy returns float16, but dpnp returns float32
# so type_check=False
@testing.for_all_dtypes()
@testing.numpy_cupy_array_almost_equal(type_check=has_support_aspect64())
@testing.numpy_cupy_array_almost_equal(type_check=False)
def test_angle(self, xp, dtype):
x = testing.shaped_arange((2, 3), xp, dtype)
return xp.angle(x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def test_astype_type(self, src_dtype, dst_dtype, order):
b = astype_without_warning(a, dst_dtype, order=order)
a_cpu = testing.shaped_arange((2, 3, 4), numpy, src_dtype)
b_cpu = astype_without_warning(a_cpu, dst_dtype, order=order)
assert b.dtype.type == b_cpu.dtype.type
assert b.dtype == b_cpu.dtype

@testing.for_orders("CAK")
@testing.for_all_dtypes()
Expand Down
6 changes: 5 additions & 1 deletion dpnp/tests/third_party/cupy/creation_tests/test_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,11 @@ def test_linspace_mixed_start_stop2(self, xp, dtype_range, dtype_out):
# TODO (ev-br): np 2.0: had to bump the default rtol on Windows
# and numpy 1.26+weak promotion from 0 to 5e-6
if xp.dtype(dtype_range).kind in "u":
start = xp.array([160, 120], dtype=dtype_range)
if dtype_range == xp.uint8 or dtype_out == xp.uint8:
val = 125
else:
val = 160
start = xp.array([val, 120], dtype=dtype_range)
else:
start = xp.array([-120, 120], dtype=dtype_range)
stop = 0
Expand Down
4 changes: 4 additions & 0 deletions dpnp/tests/third_party/cupy/indexing_tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ class TestChoose(unittest.TestCase):
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
def test_choose(self, xp, dtype):
# TODO: include additional dtype when dpnp#2201 is merged
dtype_list = [xp.int8, xp.int16]
if dtype in dtype_list or xp.issubdtype(dtype, xp.unsignedinteger):
pytest.skip("dpnp.choose() does not support new integer dtypes.")
a = xp.array([0, 2, 1, 2])
c = testing.shaped_arange((3, 4), xp, dtype)
return a.choose(c)
Expand Down
9 changes: 9 additions & 0 deletions dpnp/tests/third_party/cupy/linalg_tests/test_einsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ class TestEinSumBinaryOperation:
type_check=has_support_aspect64(), contiguous_check=False
)
def test_einsum_binary(self, xp, dtype_a, dtype_b):
if all(dtype in [xp.int8, xp.uint8] for dtype in [dtype_a, dtype_b]):
pytest.skip("avoid overflow")
a = testing.shaped_arange(self.shape_a, xp, dtype_a)
b = testing.shaped_arange(self.shape_b, xp, dtype_b)
# casting should be added for dpnp to allow cast int64 to float32
Expand Down Expand Up @@ -555,13 +557,20 @@ def test_scalar_2(self, xp, dtype):
)
)
class TestEinSumTernaryOperation:

@testing.for_all_dtypes_combination(
["dtype_a", "dtype_b", "dtype_c"], no_bool=False, no_float16=False
)
@testing.numpy_cupy_allclose(
type_check=has_support_aspect64(), contiguous_check=False
)
def test_einsum_ternary(self, xp, dtype_a, dtype_b, dtype_c):
flag = all(
dtype in [xp.int8, xp.uint8]
for dtype in [dtype_a, dtype_b, dtype_c]
)
if self.subscripts == "ij,jk,kl" and flag:
pytest.skip("avoid overflow")
a = testing.shaped_arange(self.shape_a, xp, dtype_a)
b = testing.shaped_arange(self.shape_b, xp, dtype_b)
c = testing.shaped_arange(self.shape_c, xp, dtype_c)
Expand Down
28 changes: 28 additions & 0 deletions dpnp/tests/third_party/cupy/linalg_tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,21 @@
)
class TestDot(unittest.TestCase):

# Avoid overflow
skip_dtypes = {
(numpy.int8, numpy.int8),
(numpy.int8, numpy.uint8),
(numpy.uint8, numpy.uint8),
}

@testing.for_all_dtypes_combination(["dtype_a", "dtype_b"])
@testing.numpy_cupy_allclose(type_check=has_support_aspect64())
def test_dot(self, xp, dtype_a, dtype_b):
if (dtype_a, dtype_b) in self.skip_dtypes or (
dtype_b,
dtype_a,
) in self.skip_dtypes:
pytest.skip("avoid overflow")
shape_a, shape_b = self.shape
if self.trans_a:
a = testing.shaped_arange(shape_a[::-1], xp, dtype_a).T
Expand Down Expand Up @@ -241,13 +253,17 @@ def test_dot_vec3(self, xp, dtype):
@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_transposed_dot(self, xp, dtype):
if dtype in [numpy.int8, numpy.uint8]:
pytest.skip("avoid overflow")
a = testing.shaped_arange((2, 3, 4), xp, dtype).transpose(1, 0, 2)
b = testing.shaped_arange((2, 3, 4), xp, dtype).transpose(0, 2, 1)
return xp.dot(a, b)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_transposed_dot_with_out(self, xp, dtype):
if dtype in [numpy.int8, numpy.uint8]:
pytest.skip("avoid overflow")
a = testing.shaped_arange((2, 3, 4), xp, dtype).transpose(1, 0, 2)
b = testing.shaped_arange((4, 2, 3), xp, dtype).transpose(2, 0, 1)
c = xp.ndarray((3, 2, 3, 2), dtype=dtype)
Expand Down Expand Up @@ -323,13 +339,17 @@ def test_reversed_inner(self, xp, dtype):
@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_multidim_inner(self, xp, dtype):
if dtype in [numpy.int8, numpy.uint8]:
pytest.skip("avoid overflow")
a = testing.shaped_arange((2, 3, 4), xp, dtype)
b = testing.shaped_arange((3, 2, 4), xp, dtype)
return xp.inner(a, b)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_transposed_higher_order_inner(self, xp, dtype):
if dtype in [numpy.int8, numpy.uint8]:
pytest.skip("avoid overflow")
a = testing.shaped_arange((2, 4, 3), xp, dtype).transpose(2, 0, 1)
b = testing.shaped_arange((4, 2, 3), xp, dtype).transpose(1, 2, 0)
return xp.inner(a, b)
Expand Down Expand Up @@ -358,13 +378,17 @@ def test_multidim_outer(self, xp, dtype):
@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_tensordot(self, xp, dtype):
if dtype in [numpy.int8, numpy.uint8]:
pytest.skip("avoid overflow")
a = testing.shaped_arange((2, 3, 4), xp, dtype)
b = testing.shaped_arange((3, 4, 5), xp, dtype)
return xp.tensordot(a, b)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_transposed_tensordot(self, xp, dtype):
if dtype in [numpy.int8, numpy.uint8]:
pytest.skip("avoid overflow")
a = testing.shaped_arange((2, 3, 4), xp, dtype).transpose(1, 0, 2)
b = testing.shaped_arange((4, 3, 2), xp, dtype).transpose(2, 0, 1)
return xp.tensordot(a, b)
Expand Down Expand Up @@ -519,12 +543,16 @@ def test_matrix_power_1(self, xp, dtype):
@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_matrix_power_2(self, xp, dtype):
if dtype in [numpy.int8, numpy.uint8]:
pytest.skip("avoid overflow")
a = testing.shaped_arange((3, 3), xp, dtype)
return xp.linalg.matrix_power(a, 2)

@testing.for_all_dtypes()
@testing.numpy_cupy_allclose()
def test_matrix_power_3(self, xp, dtype):
if dtype in [numpy.int8, numpy.uint8]:
pytest.skip("avoid overflow")
a = testing.shaped_arange((3, 3), xp, dtype)
return xp.linalg.matrix_power(a, 3)

Expand Down
2 changes: 2 additions & 0 deletions dpnp/tests/third_party/cupy/logic_tests/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ class TestIsclose(unittest.TestCase):
@testing.for_all_dtypes(no_complex=True)
@testing.numpy_cupy_array_equal()
def test_is_close_finite(self, xp, dtype):
if dtype in [xp.int8, xp.uint8]:
pytest.skip("avoid overflow")
# In numpy<1.10 this test fails when dtype is bool
a = xp.array([0.9e-5, 1.1e-5, 1000 + 1e-4, 1000 - 1e-4]).astype(dtype)
b = xp.array([0, 0, 1000, 1000]).astype(dtype)
Expand Down
33 changes: 33 additions & 0 deletions dpnp/tests/third_party/cupy/math_tests/test_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,24 @@
)
class TestMatmul(unittest.TestCase):

# Avoid overflow
skip_dtypes = {
(numpy.int8, numpy.int8),
(numpy.int8, numpy.uint8),
(numpy.uint8, numpy.uint8),
}

@testing.for_all_dtypes(name="dtype1")
@testing.for_all_dtypes(name="dtype2")
@testing.numpy_cupy_allclose(
rtol=1e-3, atol=1e-3, type_check=has_support_aspect64()
) # required for uint8
def test_operator_matmul(self, xp, dtype1, dtype2):
if (dtype1, dtype2) in self.skip_dtypes or (
dtype2,
dtype1,
) in self.skip_dtypes:
pytest.skip("avoid overflow")
x1 = testing.shaped_arange(self.shape_pair[0], xp, dtype1)
x2 = testing.shaped_arange(self.shape_pair[1], xp, dtype2)
return operator.matmul(x1, x2)
Expand All @@ -76,6 +88,11 @@ def test_operator_matmul(self, xp, dtype1, dtype2):
rtol=1e-3, atol=1e-3, type_check=has_support_aspect64()
) # required for uint8
def test_cupy_matmul(self, xp, dtype1, dtype2):
if (dtype1, dtype2) in self.skip_dtypes or (
dtype2,
dtype1,
) in self.skip_dtypes:
pytest.skip("avoid overflow")
x1 = testing.shaped_arange(self.shape_pair[0], xp, dtype1)
x2 = testing.shaped_arange(self.shape_pair[1], xp, dtype2)
return xp.matmul(x1, x2)
Expand All @@ -97,12 +114,25 @@ def test_cupy_matmul(self, xp, dtype1, dtype2):
)
class TestMatmulOut(unittest.TestCase):

# Avoid overflow
skip_dtypes = {
(numpy.int8, numpy.int8),
(numpy.int8, numpy.uint8),
(numpy.uint8, numpy.uint8),
}

@testing.for_all_dtypes(name="dtype1")
@testing.for_all_dtypes(name="dtype2")
@testing.numpy_cupy_allclose(
rtol=1e-3, atol=1e-3, accept_error=TypeError # required for uint8
)
def test_cupy_matmul_noncontiguous(self, xp, dtype1, dtype2):
if (dtype1, dtype2) in self.skip_dtypes or (
dtype2,
dtype1,
) in self.skip_dtypes:
pytest.skip("avoid overflow")

x1 = testing.shaped_arange(self.shape_pair[0], xp, dtype1)
x2 = testing.shaped_arange(self.shape_pair[1], xp, dtype2)
out = xp.zeros(self.shape_pair[2], dtype=dtype1)[::-1]
Expand Down Expand Up @@ -143,6 +173,8 @@ class TestMatmulStrides:
@testing.for_all_dtypes()
@testing.numpy_cupy_allclose(rtol=1e-3, atol=1e-3) # required for uint8
def test_relaxed_c_contiguous_input(self, xp, dtype):
if dtype in [numpy.int8, numpy.uint8]:
pytest.skip("avoid overflow")
x1 = testing.shaped_arange((2, 2, 3), xp, dtype)[:, None, :, :]
x2 = testing.shaped_arange((2, 1, 3, 1), xp, dtype)
return x1 @ x2
Expand Down Expand Up @@ -171,6 +203,7 @@ class TestMatmulLarge(unittest.TestCase):

# Avoid overflow
skip_dtypes = {
(numpy.int8, numpy.int8),
(numpy.int8, numpy.uint8),
(numpy.int8, numpy.int16),
(numpy.int8, numpy.float16),
Expand Down
Loading
Loading