Skip to content

Commit fce404c

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

File tree

2 files changed

+123
-23
lines changed

2 files changed

+123
-23
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 & 23 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,70 @@ 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 = numpy.finfo(dtype).resolution
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)
1011-
1012-
tol = numpy.finfo(numpy.float32).resolution
1012+
result = dpnp.divide(dp_array1, dp_array2, out=dp_out)
1013+
tol = numpy.finfo(dtype).resolution
10131014
assert_allclose(expected, result, rtol=tol, atol=tol)
10141015

1015-
@pytest.mark.parametrize("dtype", get_float_dtypes())
1016+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1017+
@pytest.mark.parametrize("dtype", get_float_dtypes() + get_complex_dtypes())
10161018
def test_out_overlap(self, dtype):
10171019
size = 15
10181020
# DPNP
10191021
dp_a = dpnp.arange(2 * size, dtype=dtype)
1020-
dpnp.hypot(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1022+
dpnp.divide(dp_a[size::], dp_a[::2], out=dp_a[:size:])
10211023

10221024
# original
10231025
np_a = numpy.arange(2 * size, dtype=dtype)
1024-
numpy.hypot(np_a[size::], np_a[::2], out=np_a[:size:])
1026+
numpy.divide(np_a[size::], np_a[::2], out=np_a[:size:])
10251027

1026-
tol = numpy.finfo(numpy.float32).resolution
1028+
tol = numpy.finfo(dtype).resolution
10271029
assert_allclose(np_a, dp_a, rtol=tol, atol=tol)
10281030

1031+
@pytest.mark.parametrize("dtype", get_float_dtypes() + get_complex_dtypes())
1032+
def test_inplace_strided_out(self, dtype):
1033+
size = 21
1034+
1035+
np_a = numpy.arange(size, dtype=dtype)
1036+
np_a[::3] /= 4
1037+
1038+
dp_a = dpnp.arange(size, dtype=dtype)
1039+
dp_a[::3] /= 4
1040+
1041+
assert_allclose(dp_a, np_a)
1042+
10291043
@pytest.mark.parametrize(
10301044
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
10311045
)
@@ -1035,7 +1049,7 @@ def test_invalid_shape(self, shape):
10351049
dp_out = dpnp.empty(shape)
10361050

10371051
with pytest.raises(ValueError):
1038-
dpnp.hypot(dp_array1, dp_array2, out=dp_out)
1052+
dpnp.divide(dp_array1, dp_array2, out=dp_out)
10391053

10401054
@pytest.mark.parametrize(
10411055
"out",
@@ -1045,8 +1059,8 @@ def test_invalid_shape(self, shape):
10451059
def test_invalid_out(self, out):
10461060
a = dpnp.arange(10)
10471061

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

10511065

10521066
class TestFmax:
@@ -1211,6 +1225,90 @@ def test_invalid_out(self, out):
12111225
assert_raises(TypeError, numpy.fmin, a.asnumpy(), 2, out)
12121226

12131227

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

0 commit comments

Comments
 (0)