Skip to content

Commit f8f1ff7

Browse files
committed
in place divide and floor_divide
1 parent 76e3f87 commit f8f1ff7

File tree

2 files changed

+123
-22
lines changed

2 files changed

+123
-22
lines changed

dpnp/dpnp_algo/dpnp_elementwise_common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@ def _call_divide(src1, src2, dst, sycl_queue, depends=None):
10721072
ti._divide_result_type,
10731073
_call_divide,
10741074
_divide_docstring_,
1075+
ti._divide_inplace,
10751076
)
10761077

10771078

@@ -1218,6 +1219,7 @@ def dpnp_floor(x, out=None, order="K"):
12181219
ti._floor_divide_result_type,
12191220
ti._floor_divide,
12201221
_floor_divide_docstring_,
1222+
ti._floor_divide_inplace,
12211223
)
12221224

12231225

tests/test_mathematical.py

Lines changed: 121 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -965,9 +965,9 @@ def test_invalid_out(self, out):
965965
assert_raises(TypeError, numpy.add, a.asnumpy(), 2, out)
966966

967967

968-
class TestHypot:
969-
@pytest.mark.parametrize("dtype", get_float_dtypes())
970-
def test_hypot(self, dtype):
968+
class TestDivide:
969+
@pytest.mark.parametrize("dtype", get_float_dtypes() + get_complex_dtypes())
970+
def test_divide(self, dtype):
971971
array1_data = numpy.arange(10)
972972
array2_data = numpy.arange(5, 15)
973973
out = numpy.empty(10, dtype=dtype)
@@ -976,56 +976,71 @@ def test_hypot(self, dtype):
976976
dp_array1 = dpnp.array(array1_data, dtype=dtype)
977977
dp_array2 = dpnp.array(array2_data, dtype=dtype)
978978
dp_out = dpnp.array(out, dtype=dtype)
979-
result = dpnp.hypot(dp_array1, dp_array2, out=dp_out)
979+
result = dpnp.divide(dp_array1, dp_array2, out=dp_out)
980980

981981
# original
982982
np_array1 = numpy.array(array1_data, dtype=dtype)
983983
np_array2 = numpy.array(array2_data, dtype=dtype)
984-
expected = numpy.hypot(np_array1, np_array2, out=out)
984+
expected = numpy.divide(np_array1, np_array2, out=out)
985985

986-
assert_allclose(expected, result)
987-
assert_allclose(out, dp_out)
986+
tol = 1e-07
987+
assert_allclose(expected, result, rtol=tol, atol=tol)
988+
assert_allclose(out, dp_out, rtol=tol, atol=tol)
988989

989-
@pytest.mark.parametrize("dtype", get_float_dtypes())
990+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
991+
@pytest.mark.parametrize("dtype", get_float_dtypes() + get_complex_dtypes())
990992
def test_out_dtypes(self, dtype):
991993
size = 10
992994

993995
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
994996
np_array2 = numpy.arange(size, dtype=dtype)
995-
np_out = numpy.empty(size, dtype=numpy.float32)
996-
expected = numpy.hypot(np_array1, np_array2, out=np_out)
997+
np_out = numpy.empty(size, dtype=numpy.complex64)
998+
expected = numpy.divide(np_array1, np_array2, out=np_out)
997999

9981000
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
9991001
dp_array2 = dpnp.arange(size, dtype=dtype)
10001002

1001-
dp_out = dpnp.empty(size, dtype=dpnp.float32)
1002-
if dtype != dpnp.float32:
1003+
dp_out = dpnp.empty(size, dtype=dpnp.complex64)
1004+
if dtype != dpnp.complex64:
10031005
# dtype of out mismatches types of input arrays
10041006
with pytest.raises(TypeError):
1005-
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1007+
dpnp.divide(dp_array1, dp_array2, out=dp_out)
10061008

10071009
# allocate new out with expected type
10081010
dp_out = dpnp.empty(size, dtype=dtype)
10091011

1010-
result = dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1012+
result = dpnp.divide(dp_array1, dp_array2, out=dp_out)
10111013

1012-
tol = numpy.finfo(numpy.float32).resolution
1014+
tol = 1e-07
10131015
assert_allclose(expected, result, rtol=tol, atol=tol)
10141016

1015-
@pytest.mark.parametrize("dtype", get_float_dtypes())
1017+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1018+
@pytest.mark.parametrize("dtype", get_float_dtypes() + get_complex_dtypes())
10161019
def test_out_overlap(self, dtype):
10171020
size = 15
10181021
# DPNP
10191022
dp_a = dpnp.arange(2 * size, dtype=dtype)
1020-
dpnp.hypot(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1023+
dpnp.divide(dp_a[size::], dp_a[::2], out=dp_a[:size:])
10211024

10221025
# original
10231026
np_a = numpy.arange(2 * size, dtype=dtype)
1024-
numpy.hypot(np_a[size::], np_a[::2], out=np_a[:size:])
1027+
numpy.divide(np_a[size::], np_a[::2], out=np_a[:size:])
10251028

1026-
tol = numpy.finfo(numpy.float32).resolution
1029+
tol = 1e-07
10271030
assert_allclose(np_a, dp_a, rtol=tol, atol=tol)
10281031

1032+
@pytest.mark.parametrize("dtype", get_float_dtypes() + get_complex_dtypes())
1033+
def test_inplace_strided_out(self, dtype):
1034+
size = 21
1035+
1036+
np_a = numpy.arange(size, dtype=dtype)
1037+
np_a[::3] /= 4
1038+
1039+
dp_a = dpnp.arange(size, dtype=dtype)
1040+
dp_a[::3] /= 4
1041+
1042+
assert_allclose(dp_a, np_a)
1043+
10291044
@pytest.mark.parametrize(
10301045
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
10311046
)
@@ -1035,7 +1050,7 @@ def test_invalid_shape(self, shape):
10351050
dp_out = dpnp.empty(shape)
10361051

10371052
with pytest.raises(ValueError):
1038-
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1053+
dpnp.divide(dp_array1, dp_array2, out=dp_out)
10391054

10401055
@pytest.mark.parametrize(
10411056
"out",
@@ -1045,8 +1060,8 @@ def test_invalid_shape(self, shape):
10451060
def test_invalid_out(self, out):
10461061
a = dpnp.arange(10)
10471062

1048-
assert_raises(TypeError, dpnp.hypot, a, 2, out)
1049-
assert_raises(TypeError, numpy.hypot, a.asnumpy(), 2, out)
1063+
assert_raises(TypeError, dpnp.divide, a, 2, out)
1064+
assert_raises(TypeError, numpy.divide, a.asnumpy(), 2, out)
10501065

10511066

10521067
class TestFmax:
@@ -1211,6 +1226,90 @@ def test_invalid_out(self, out):
12111226
assert_raises(TypeError, numpy.fmin, a.asnumpy(), 2, out)
12121227

12131228

1229+
class TestHypot:
1230+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1231+
def test_hypot(self, dtype):
1232+
array1_data = numpy.arange(10)
1233+
array2_data = numpy.arange(5, 15)
1234+
out = numpy.empty(10, dtype=dtype)
1235+
1236+
# DPNP
1237+
dp_array1 = dpnp.array(array1_data, dtype=dtype)
1238+
dp_array2 = dpnp.array(array2_data, dtype=dtype)
1239+
dp_out = dpnp.array(out, dtype=dtype)
1240+
result = dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1241+
1242+
# original
1243+
np_array1 = numpy.array(array1_data, dtype=dtype)
1244+
np_array2 = numpy.array(array2_data, dtype=dtype)
1245+
expected = numpy.hypot(np_array1, np_array2, out=out)
1246+
1247+
assert_allclose(expected, result)
1248+
assert_allclose(out, dp_out)
1249+
1250+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1251+
def test_out_dtypes(self, dtype):
1252+
size = 10
1253+
1254+
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
1255+
np_array2 = numpy.arange(size, dtype=dtype)
1256+
np_out = numpy.empty(size, dtype=numpy.float32)
1257+
expected = numpy.hypot(np_array1, np_array2, out=np_out)
1258+
1259+
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
1260+
dp_array2 = dpnp.arange(size, dtype=dtype)
1261+
1262+
dp_out = dpnp.empty(size, dtype=dpnp.float32)
1263+
if dtype != dpnp.float32:
1264+
# dtype of out mismatches types of input arrays
1265+
with pytest.raises(TypeError):
1266+
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1267+
1268+
# allocate new out with expected type
1269+
dp_out = dpnp.empty(size, dtype=dtype)
1270+
1271+
result = dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1272+
1273+
tol = numpy.finfo(numpy.float32).resolution
1274+
assert_allclose(expected, result, rtol=tol, atol=tol)
1275+
1276+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1277+
def test_out_overlap(self, dtype):
1278+
size = 15
1279+
# DPNP
1280+
dp_a = dpnp.arange(2 * size, dtype=dtype)
1281+
dpnp.hypot(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1282+
1283+
# original
1284+
np_a = numpy.arange(2 * size, dtype=dtype)
1285+
numpy.hypot(np_a[size::], np_a[::2], out=np_a[:size:])
1286+
1287+
tol = numpy.finfo(numpy.float32).resolution
1288+
assert_allclose(np_a, dp_a, rtol=tol, atol=tol)
1289+
1290+
@pytest.mark.parametrize(
1291+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
1292+
)
1293+
def test_invalid_shape(self, shape):
1294+
dp_array1 = dpnp.arange(10)
1295+
dp_array2 = dpnp.arange(5, 15)
1296+
dp_out = dpnp.empty(shape)
1297+
1298+
with pytest.raises(ValueError):
1299+
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1300+
1301+
@pytest.mark.parametrize(
1302+
"out",
1303+
[4, (), [], (3, 7), [2, 4]],
1304+
ids=["4", "()", "[]", "(3, 7)", "[2, 4]"],
1305+
)
1306+
def test_invalid_out(self, out):
1307+
a = dpnp.arange(10)
1308+
1309+
assert_raises(TypeError, dpnp.hypot, a, 2, out)
1310+
assert_raises(TypeError, numpy.hypot, a.asnumpy(), 2, out)
1311+
1312+
12141313
class TestMaximum:
12151314
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
12161315
def test_maximum(self, dtype):

0 commit comments

Comments
 (0)