Skip to content

Commit 3cb0fea

Browse files
committed
Enable third party tests for elementwise operations
1 parent 9ec6b8f commit 3cb0fea

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

dpnp/tests/third_party/cupy/core_tests/test_ndarray_elementwise_op.py

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import annotations
2+
3+
import functools
14
import operator
25

36
import numpy
@@ -6,13 +9,40 @@
69
import dpnp as cupy
710
from dpnp.tests.third_party.cupy import testing
811

9-
pytest.skip("operator interface is not supported", allow_module_level=True)
12+
13+
def cast_exception_type():
14+
"""
15+
Decorator for parameterized tests to cast raising exception
16+
ValueError(...does not support input types...) to TypeError(...) matching
17+
NumPy behavior.
18+
19+
The exception raised when a pair of input dtypes is not supported and could
20+
not be safely coerced to any supported one according to the casting rule.
21+
22+
"""
23+
24+
def decorator(impl):
25+
@functools.wraps(impl)
26+
def test_func(self, *args, **kw):
27+
xp = kw["xp"]
28+
29+
try:
30+
return impl(self, *args, **kw)
31+
except ValueError as e:
32+
if xp is cupy and "does not support input types" in str(e):
33+
raise TypeError(e)
34+
raise
35+
36+
return test_func
37+
38+
return decorator
1039

1140

1241
class TestArrayElementwiseOp:
1342

1443
@testing.for_all_dtypes_combination(names=["x_type", "y_type"])
1544
@testing.numpy_cupy_allclose(rtol=1e-6, accept_error=TypeError)
45+
@cast_exception_type()
1646
def check_array_scalar_op(
1747
self,
1848
op,
@@ -106,6 +136,7 @@ def test_rpow_scalar(self):
106136

107137
@testing.for_all_dtypes_combination(names=["x_type", "y_type"])
108138
@testing.numpy_cupy_allclose(atol=1.0, accept_error=TypeError)
139+
@cast_exception_type()
109140
def check_ipow_scalar(self, xp, x_type, y_type):
110141
a = xp.array([[1, 2, 3], [4, 5, 6]], x_type)
111142
return operator.ipow(a, y_type(3))
@@ -157,6 +188,7 @@ def test_ne_scalar(self):
157188

158189
@testing.for_all_dtypes_combination(names=["x_type", "y_type"])
159190
@testing.numpy_cupy_allclose(accept_error=TypeError)
191+
@cast_exception_type()
160192
def check_array_array_op(
161193
self, op, xp, x_type, y_type, no_bool=False, no_complex=False
162194
):
@@ -216,13 +248,14 @@ def check_pow_array(self, xp, x_type, y_type):
216248
def test_pow_array(self):
217249
# There are some precision issues in HIP that prevent
218250
# checking with atol=0
219-
if cupy.cuda.runtime.is_hip:
220-
self.check_pow_array()
221-
else:
222-
self.check_array_array_op(operator.pow)
251+
# if cupy.cuda.runtime.is_hip:
252+
# self.check_pow_array()
253+
# else:
254+
self.check_array_array_op(operator.pow)
223255

224256
@testing.for_all_dtypes_combination(names=["x_type", "y_type"])
225257
@testing.numpy_cupy_allclose(atol=1.0, accept_error=TypeError)
258+
@cast_exception_type()
226259
def check_ipow_array(self, xp, x_type, y_type):
227260
a = xp.array([[1, 2, 3], [4, 5, 6]], x_type)
228261
b = xp.array([[6, 5, 4], [3, 2, 1]], y_type)
@@ -259,6 +292,7 @@ def test_ne_array(self):
259292

260293
@testing.for_all_dtypes_combination(names=["x_type", "y_type"])
261294
@testing.numpy_cupy_allclose(accept_error=TypeError)
295+
@cast_exception_type()
262296
def check_array_broadcasted_op(
263297
self, op, xp, x_type, y_type, no_bool=False, no_complex=False
264298
):
@@ -320,13 +354,14 @@ def check_broadcasted_pow(self, xp, x_type, y_type):
320354
def test_broadcasted_pow(self):
321355
# There are some precision issues in HIP that prevent
322356
# checking with atol=0
323-
if cupy.cuda.runtime.is_hip:
324-
self.check_broadcasted_pow()
325-
else:
326-
self.check_array_broadcasted_op(operator.pow)
357+
# if cupy.cuda.runtime.is_hip:
358+
# self.check_broadcasted_pow()
359+
# else:
360+
self.check_array_broadcasted_op(operator.pow)
327361

328362
@testing.for_all_dtypes_combination(names=["x_type", "y_type"])
329363
@testing.numpy_cupy_allclose(atol=1.0, accept_error=TypeError)
364+
@cast_exception_type()
330365
def check_broadcasted_ipow(self, xp, x_type, y_type):
331366
a = xp.array([[1, 2, 3], [4, 5, 6]], x_type)
332367
b = xp.array([[1], [2]], y_type)
@@ -480,6 +515,7 @@ def test_typecast_(self, xp, op, dtype, val):
480515
a = op(val, (testing.shaped_arange((5,), xp, dtype) - 2))
481516
return a
482517

518+
@pytest.mark.skip("TODO")
483519
@pytest.mark.parametrize(
484520
"val",
485521
[
@@ -513,9 +549,11 @@ def check_array_boolarray_op(self, op, xp, x_type):
513549
b = xp.array([[3, 1, 4], [-1, -5, -9]], numpy.int8).view(bool)
514550
return op(a, b)
515551

552+
@testing.with_requires("dpctl>=0.22.0dev0")
516553
def test_add_array_boolarray(self):
517554
self.check_array_boolarray_op(operator.add)
518555

556+
@testing.with_requires("dpctl>=0.22.0dev0")
519557
def test_iadd_array_boolarray(self):
520558
self.check_array_boolarray_op(operator.iadd)
521559

@@ -524,6 +562,7 @@ class TestArrayIntElementwiseOp:
524562

525563
@testing.for_all_dtypes_combination(names=["x_type", "y_type"])
526564
@testing.numpy_cupy_allclose(accept_error=TypeError)
565+
@cast_exception_type()
527566
def check_array_scalar_op(self, op, xp, x_type, y_type, swap=False):
528567
a = xp.array([[0, 1, 2], [1, 0, 2]], dtype=x_type)
529568
if swap:
@@ -571,6 +610,7 @@ def test_rmod_scalar(self):
571610

572611
@testing.for_all_dtypes_combination(names=["x_type", "y_type"])
573612
@testing.numpy_cupy_allclose(accept_error=TypeError)
613+
@cast_exception_type()
574614
def check_array_scalarzero_op(self, op, xp, x_type, y_type, swap=False):
575615
a = xp.array([[0, 1, 2], [1, 0, 2]], dtype=x_type)
576616
if swap:
@@ -618,6 +658,7 @@ def test_rmod_scalarzero(self):
618658

619659
@testing.for_all_dtypes_combination(names=["x_type", "y_type"])
620660
@testing.numpy_cupy_allclose(accept_error=TypeError)
661+
@cast_exception_type()
621662
def check_array_array_op(self, op, xp, x_type, y_type):
622663
a = xp.array([[0, 1, 2], [1, 0, 2]], dtype=x_type)
623664
b = xp.array([[0, 0, 1], [0, 1, 2]], dtype=y_type)
@@ -663,6 +704,7 @@ def test_imod_array(self):
663704

664705
@testing.for_all_dtypes_combination(names=["x_type", "y_type"])
665706
@testing.numpy_cupy_allclose(accept_error=TypeError)
707+
@cast_exception_type()
666708
def check_array_broadcasted_op(self, op, xp, x_type, y_type):
667709
a = xp.array([[0, 1, 2], [1, 0, 2], [2, 1, 0]], dtype=x_type)
668710
b = xp.array([[0, 0, 1]], dtype=y_type)
@@ -708,6 +750,7 @@ def test_broadcasted_imod(self):
708750

709751
@testing.for_all_dtypes_combination(names=["x_type", "y_type"])
710752
@testing.numpy_cupy_allclose(accept_error=TypeError)
753+
@cast_exception_type()
711754
def check_array_doubly_broadcasted_op(self, op, xp, x_type, y_type):
712755
a = xp.array([[[0, 1, 2]], [[1, 0, 2]]], dtype=x_type)
713756
b = xp.array([[0], [0], [1]], dtype=y_type)
@@ -733,6 +776,7 @@ def test_doubly_broadcasted_mod(self):
733776
self.check_array_doubly_broadcasted_op(operator.mod)
734777

735778

779+
@pytest.mark.skip("objects as input are not supported")
736780
@pytest.mark.parametrize(
737781
"value",
738782
[
@@ -807,7 +851,7 @@ def test_eq_object(self, dtype, value):
807851
except TypeError:
808852
pytest.skip()
809853

810-
cupy.testing.assert_array_equal(res, expected)
854+
testing.assert_array_equal(res, expected)
811855

812856
def test_ne_object(self, dtype, value):
813857
expected = numpy.array([[1, 2, 3], [4, 5, 6]], dtype=dtype) != value
@@ -818,4 +862,4 @@ def test_ne_object(self, dtype, value):
818862
except TypeError:
819863
pytest.skip()
820864

821-
cupy.testing.assert_array_equal(res, expected)
865+
testing.assert_array_equal(res, expected)

dpnp/tests/third_party/cupy/creation_tests/test_ranges.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import functools
22
import math
3-
import sys
43
import unittest
54

65
import numpy

0 commit comments

Comments
 (0)