Skip to content

Commit

Permalink
Merge fafce08 into ebe4868
Browse files Browse the repository at this point in the history
  • Loading branch information
vtavana authored Dec 20, 2024
2 parents ebe4868 + fafce08 commit 967e6ad
Show file tree
Hide file tree
Showing 19 changed files with 177 additions and 58 deletions.
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
1 change: 1 addition & 0 deletions dpnp/tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ def test_reduce_hypot(device):
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
[5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0],
),
pytest.param("round", [1.234, 2.567], 2),
pytest.param("searchsorted", [11, 12, 13, 14, 15], [-10, 20, 12, 13]),
pytest.param(
"subtract",
Expand Down
1 change: 1 addition & 0 deletions dpnp/tests/test_usm_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ def test_1in_1out(func, data, usm_type):
pytest.param("maximum", [0.0, 1.0, 2.0], [3.0, 4.0, 5.0]),
pytest.param("minimum", [0.0, 1.0, 2.0], [3.0, 4.0, 5.0]),
pytest.param("nextafter", [1, 2], [2, 1]),
pytest.param("round", [1.234, 2.567], 2),
pytest.param("searchsorted", [11, 12, 13, 14, 15], [-10, 20, 12, 13]),
pytest.param(
"tensordot",
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 @@ -4,7 +4,6 @@
import pytest

import dpnp as cupy
from dpnp.tests.helper import has_support_aspect64
from dpnp.tests.third_party.cupy import testing


Expand Down Expand Up @@ -41,8 +40,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
8 changes: 7 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,13 @@ 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)
# to avoid overflow, limit `val` to be smaller
# than xp.iinfo(dtype).max
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
15 changes: 9 additions & 6 deletions dpnp/tests/third_party/cupy/linalg_tests/test_einsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,7 @@ def test_scalar_float(self, xp, dtype):
)
)
class TestEinSumBinaryOperation:
@testing.for_all_dtypes_combination(
["dtype_a", "dtype_b"], no_bool=False, no_float16=False
)
@testing.for_all_dtypes_combination(["dtype_a", "dtype_b"], no_int8=True)
@testing.numpy_cupy_allclose(
type_check=has_support_aspect64(), contiguous_check=False
)
Expand Down Expand Up @@ -555,13 +553,18 @@ 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.for_all_dtypes_combination(["dtype_a", "dtype_b", "dtype_c"])
@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
18 changes: 9 additions & 9 deletions dpnp/tests/third_party/cupy/linalg_tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
)
class TestDot(unittest.TestCase):

@testing.for_all_dtypes_combination(["dtype_a", "dtype_b"])
@testing.for_all_dtypes_combination(["dtype_a", "dtype_b"], no_int8=True)
@testing.numpy_cupy_allclose(type_check=has_support_aspect64())
def test_dot(self, xp, dtype_a, dtype_b):
shape_a, shape_b = self.shape
Expand Down Expand Up @@ -238,14 +238,14 @@ def test_dot_vec3(self, xp, dtype):
b = testing.shaped_arange((2,), xp, dtype)
return xp.dot(a, b)

@testing.for_all_dtypes()
@testing.for_all_dtypes(no_int8=True)
@testing.numpy_cupy_allclose()
def test_transposed_dot(self, xp, dtype):
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.for_all_dtypes(no_int8=True)
@testing.numpy_cupy_allclose()
def test_transposed_dot_with_out(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype).transpose(1, 0, 2)
Expand Down Expand Up @@ -320,14 +320,14 @@ def test_reversed_inner(self, xp, dtype):
b = testing.shaped_reverse_arange((5,), xp, dtype)[::-1]
return xp.inner(a, b)

@testing.for_all_dtypes()
@testing.for_all_dtypes(no_int8=True)
@testing.numpy_cupy_allclose()
def test_multidim_inner(self, xp, dtype):
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.for_all_dtypes(no_int8=True)
@testing.numpy_cupy_allclose()
def test_transposed_higher_order_inner(self, xp, dtype):
a = testing.shaped_arange((2, 4, 3), xp, dtype).transpose(2, 0, 1)
Expand Down Expand Up @@ -355,14 +355,14 @@ def test_multidim_outer(self, xp, dtype):
b = testing.shaped_arange((4, 5), xp, dtype)
return xp.outer(a, b)

@testing.for_all_dtypes()
@testing.for_all_dtypes(no_int8=True)
@testing.numpy_cupy_allclose()
def test_tensordot(self, xp, dtype):
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.for_all_dtypes(no_int8=True)
@testing.numpy_cupy_allclose()
def test_transposed_tensordot(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype).transpose(1, 0, 2)
Expand Down Expand Up @@ -516,13 +516,13 @@ def test_matrix_power_1(self, xp, dtype):
a = testing.shaped_arange((3, 3), xp, dtype)
return xp.linalg.matrix_power(a, 1)

@testing.for_all_dtypes()
@testing.for_all_dtypes(no_int8=True)
@testing.numpy_cupy_allclose()
def test_matrix_power_2(self, xp, dtype):
a = testing.shaped_arange((3, 3), xp, dtype)
return xp.linalg.matrix_power(a, 2)

@testing.for_all_dtypes()
@testing.for_all_dtypes(no_int8=True)
@testing.numpy_cupy_allclose()
def test_matrix_power_3(self, xp, dtype):
a = testing.shaped_arange((3, 3), xp, dtype)
Expand Down
2 changes: 1 addition & 1 deletion dpnp/tests/third_party/cupy/logic_tests/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def test_allclose_array_scalar(self, xp, dtype):

class TestIsclose(unittest.TestCase):

@testing.for_all_dtypes(no_complex=True)
@testing.for_all_dtypes(no_complex=True, no_int8=True)
@testing.numpy_cupy_array_equal()
def test_is_close_finite(self, xp, dtype):
# In numpy<1.10 this test fails when dtype is bool
Expand Down
15 changes: 8 additions & 7 deletions dpnp/tests/third_party/cupy/math_tests/test_matmul.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
)
class TestMatmul(unittest.TestCase):

@testing.for_all_dtypes(name="dtype1")
@testing.for_all_dtypes(name="dtype2")
@testing.for_all_dtypes(name="dtype1", no_int8=True)
@testing.for_all_dtypes(name="dtype2", no_int8=True)
@testing.numpy_cupy_allclose(
rtol=1e-3, atol=1e-3, type_check=has_support_aspect64()
) # required for uint8
Expand All @@ -70,8 +70,8 @@ def test_operator_matmul(self, xp, dtype1, dtype2):
x2 = testing.shaped_arange(self.shape_pair[1], xp, dtype2)
return operator.matmul(x1, x2)

@testing.for_all_dtypes(name="dtype1")
@testing.for_all_dtypes(name="dtype2")
@testing.for_all_dtypes(name="dtype1", no_int8=True)
@testing.for_all_dtypes(name="dtype2", no_int8=True)
@testing.numpy_cupy_allclose(
rtol=1e-3, atol=1e-3, type_check=has_support_aspect64()
) # required for uint8
Expand All @@ -97,8 +97,8 @@ def test_cupy_matmul(self, xp, dtype1, dtype2):
)
class TestMatmulOut(unittest.TestCase):

@testing.for_all_dtypes(name="dtype1")
@testing.for_all_dtypes(name="dtype2")
@testing.for_all_dtypes(name="dtype1", no_int8=True)
@testing.for_all_dtypes(name="dtype2", no_int8=True)
@testing.numpy_cupy_allclose(
rtol=1e-3, atol=1e-3, accept_error=TypeError # required for uint8
)
Expand Down Expand Up @@ -140,7 +140,7 @@ def test_overlap_both(self, xp, dtype, shape):

class TestMatmulStrides:

@testing.for_all_dtypes()
@testing.for_all_dtypes(no_int8=True)
@testing.numpy_cupy_allclose(rtol=1e-3, atol=1e-3) # required for uint8
def test_relaxed_c_contiguous_input(self, xp, dtype):
x1 = testing.shaped_arange((2, 2, 3), xp, dtype)[:, None, :, :]
Expand Down Expand Up @@ -171,6 +171,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
3 changes: 2 additions & 1 deletion dpnp/tests/third_party/cupy/math_tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ def test_sqrt(self):
self.check_unary("sqrt")

@testing.for_all_dtypes(no_complex=True)
@testing.numpy_cupy_allclose(atol=1e-5, type_check=has_support_aspect64())
# atol=1e-3 is needed for int8
@testing.numpy_cupy_allclose(atol=1e-3, type_check=has_support_aspect64())
def test_cbrt(self, xp, dtype):
a = testing.shaped_arange((2, 3, 4), xp, dtype)
return xp.cbrt(a)
Expand Down
Loading

0 comments on commit 967e6ad

Please sign in to comment.