Skip to content

Commit 84f9109

Browse files
Adding tests for bitwise functions
1 parent e896b63 commit 84f9109

File tree

7 files changed

+563
-19
lines changed

7 files changed

+563
-19
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2023 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless_equal required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import numpy as np
18+
import pytest
19+
20+
import dpctl.tensor as dpt
21+
from dpctl.tests.helper import get_queue_or_skip, skip_if_dtype_not_supported
22+
23+
from .utils import _integral_dtypes
24+
25+
26+
@pytest.mark.parametrize("op_dtype", _integral_dtypes)
27+
def test_bitwise_and_dtype_matrix_contig(op_dtype):
28+
q = get_queue_or_skip()
29+
skip_if_dtype_not_supported(op_dtype, q)
30+
31+
sz = 7
32+
n = 2 * sz
33+
dt1 = dpt.dtype(op_dtype)
34+
dt2 = dpt.dtype(op_dtype)
35+
36+
x1_range_begin = -sz if dpt.iinfo(dt1).min < 0 else 0
37+
x1 = dpt.arange(x1_range_begin, x1_range_begin + n, dtype=dt1)
38+
39+
x2_range_begin = -sz if dpt.iinfo(dt2).min < 0 else 0
40+
x2 = dpt.arange(x2_range_begin, x2_range_begin + n, dtype=dt1)
41+
42+
r = dpt.bitwise_and(x1, x2)
43+
assert isinstance(r, dpt.usm_ndarray)
44+
45+
x1_np = np.arange(x1_range_begin, x1_range_begin + n, dtype=op_dtype)
46+
x2_np = np.arange(x2_range_begin, x2_range_begin + n, dtype=op_dtype)
47+
r_np = np.bitwise_and(x1_np, x2_np)
48+
49+
assert (r_np == dpt.asnumpy(r)).all()
50+
51+
52+
@pytest.mark.parametrize("op_dtype", _integral_dtypes)
53+
def test_bitwise_and_dtype_matrix_strided(op_dtype):
54+
q = get_queue_or_skip()
55+
skip_if_dtype_not_supported(op_dtype, q)
56+
57+
sz = 11
58+
n = 2 * sz
59+
dt1 = dpt.dtype(op_dtype)
60+
dt2 = dpt.dtype(op_dtype)
61+
62+
x1_range_begin = -sz if dpt.iinfo(dt1).min < 0 else 0
63+
x1 = dpt.arange(x1_range_begin, x1_range_begin + n, dtype=dt1)[::2]
64+
65+
x2_range_begin = -(sz // 2) if dpt.iinfo(dt2).min < 0 else 0
66+
x2 = dpt.arange(x2_range_begin, x2_range_begin + n, dtype=dt1)[::-2]
67+
68+
r = dpt.bitwise_and(x1, x2)
69+
assert isinstance(r, dpt.usm_ndarray)
70+
71+
x1_np = np.arange(x1_range_begin, x1_range_begin + n, dtype=op_dtype)[::2]
72+
x2_np = np.arange(x2_range_begin, x2_range_begin + n, dtype=op_dtype)[::-2]
73+
r_np = np.bitwise_and(x1_np, x2_np)
74+
75+
assert (r_np == dpt.asnumpy(r)).all()
76+
77+
78+
def test_bitwise_and_bool():
79+
get_queue_or_skip()
80+
81+
x1 = dpt.asarray([True, False])
82+
x2 = dpt.asarray([False, True])
83+
84+
r_bw = dpt.bitwise_and(x1[:, dpt.newaxis], x2[dpt.newaxis])
85+
r_lo = dpt.logical_and(x1[:, dpt.newaxis], x2[dpt.newaxis])
86+
87+
assert dpt.all(dpt.equal(r_bw, r_lo))
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2023 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless_equal required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import numpy as np
18+
import pytest
19+
20+
import dpctl.tensor as dpt
21+
from dpctl.tests.helper import get_queue_or_skip, skip_if_dtype_not_supported
22+
23+
from .utils import _compare_dtypes, _integral_dtypes, _usm_types
24+
25+
26+
@pytest.mark.parametrize(
27+
"op_dtype",
28+
[
29+
"b1",
30+
]
31+
+ _integral_dtypes,
32+
)
33+
def test_bitwise_invert_dtype_matrix(op_dtype):
34+
q = get_queue_or_skip()
35+
skip_if_dtype_not_supported(op_dtype, q)
36+
37+
sz = 7
38+
ar1 = dpt.asarray(np.random.randint(0, 2, sz), dtype=op_dtype)
39+
40+
r = dpt.bitwise_invert(ar1)
41+
assert isinstance(r, dpt.usm_ndarray)
42+
assert r.dtype == ar1.dtype
43+
44+
expected = np.bitwise_not(dpt.asnumpy(ar1))
45+
assert _compare_dtypes(r.dtype, expected.dtype, sycl_queue=q)
46+
assert r.shape == ar1.shape
47+
assert (dpt.asnumpy(r) == expected).all()
48+
assert r.sycl_queue == ar1.sycl_queue
49+
50+
r2 = dpt.empty_like(r, dtype=r.dtype)
51+
dpt.bitwise_invert(ar1, out=r2)
52+
assert dpt.all(dpt.equal(r, r2))
53+
54+
ar2 = dpt.zeros(sz, dtype=op_dtype)
55+
r = dpt.bitwise_invert(ar2[::-1])
56+
assert isinstance(r, dpt.usm_ndarray)
57+
58+
expected = np.bitwise_not(np.zeros(ar2.shape, dtype=op_dtype))
59+
assert _compare_dtypes(r.dtype, expected.dtype, sycl_queue=q)
60+
assert r.shape == ar2.shape
61+
assert (dpt.asnumpy(r) == expected).all()
62+
63+
ar3 = dpt.ones(sz, dtype=op_dtype)
64+
r2 = dpt.bitwise_invert(ar3[::2])
65+
assert isinstance(r, dpt.usm_ndarray)
66+
67+
expected = np.bitwise_not(np.ones(ar3.shape, dtype=op_dtype)[::2])
68+
assert _compare_dtypes(r.dtype, expected.dtype, sycl_queue=q)
69+
assert (dpt.asnumpy(r2) == expected).all()
70+
71+
r3 = dpt.empty_like(r, dtype=r.dtype)
72+
dpt.bitwise_invert(ar2[::-1], out=r3)
73+
assert dpt.all(dpt.equal(r, r3))
74+
75+
76+
@pytest.mark.parametrize("op_usm_type", _usm_types)
77+
def test_bitwise_invert_usm_type_matrix(op_usm_type):
78+
get_queue_or_skip()
79+
80+
sz = 128
81+
ar1 = dpt.asarray(
82+
np.random.randint(0, 2, sz), dtype="i4", usm_type=op_usm_type
83+
)
84+
85+
r = dpt.bitwise_invert(ar1)
86+
assert isinstance(r, dpt.usm_ndarray)
87+
assert r.usm_type == op_usm_type
88+
89+
90+
def test_bitwise_invert_order():
91+
get_queue_or_skip()
92+
93+
ar1 = dpt.ones((20, 20), dtype="i4", order="C")
94+
r1 = dpt.bitwise_invert(ar1, order="C")
95+
assert r1.flags.c_contiguous
96+
r2 = dpt.bitwise_invert(ar1, order="F")
97+
assert r2.flags.f_contiguous
98+
r3 = dpt.bitwise_invert(ar1, order="A")
99+
assert r3.flags.c_contiguous
100+
r4 = dpt.bitwise_invert(ar1, order="K")
101+
assert r4.flags.c_contiguous
102+
103+
ar1 = dpt.zeros((20, 20), dtype="i4", order="F")
104+
r1 = dpt.bitwise_invert(ar1, order="C")
105+
assert r1.flags.c_contiguous
106+
r2 = dpt.bitwise_invert(ar1, order="F")
107+
assert r2.flags.f_contiguous
108+
r3 = dpt.bitwise_invert(ar1, order="A")
109+
assert r3.flags.f_contiguous
110+
r4 = dpt.bitwise_invert(ar1, order="K")
111+
assert r4.flags.f_contiguous
112+
113+
ar1 = dpt.ones((40, 40), dtype="i4", order="C")[:20, ::-2]
114+
r4 = dpt.bitwise_invert(ar1, order="K")
115+
assert r4.strides == (20, -1)
116+
117+
ar1 = dpt.zeros((40, 40), dtype="i4", order="C")[:20, ::-2].mT
118+
r4 = dpt.bitwise_invert(ar1, order="K")
119+
assert r4.strides == (-1, 20)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2023 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless_equal required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import numpy as np
18+
import pytest
19+
20+
import dpctl.tensor as dpt
21+
from dpctl.tests.helper import get_queue_or_skip, skip_if_dtype_not_supported
22+
23+
from .utils import _integral_dtypes
24+
25+
26+
@pytest.mark.parametrize("op1_dtype", _integral_dtypes)
27+
@pytest.mark.parametrize("op2_dtype", _integral_dtypes)
28+
def test_bitwise_left_shift_dtype_matrix_contig(op1_dtype, op2_dtype):
29+
q = get_queue_or_skip()
30+
skip_if_dtype_not_supported(op1_dtype, q)
31+
skip_if_dtype_not_supported(op2_dtype, q)
32+
33+
if op1_dtype != op2_dtype and "u8" in [op1_dtype, op2_dtype]:
34+
return
35+
36+
sz = 7
37+
n = 2 * sz
38+
dt1 = dpt.dtype(op1_dtype)
39+
dt2 = dpt.dtype(op2_dtype)
40+
41+
x1_range_begin = -sz if dpt.iinfo(dt1).min < 0 else 0
42+
x1 = dpt.arange(x1_range_begin, x1_range_begin + n, dtype=dt1)
43+
x2 = dpt.arange(0, n, dtype=dt2)
44+
45+
r = dpt.bitwise_left_shift(x1, x2)
46+
assert isinstance(r, dpt.usm_ndarray)
47+
assert r.sycl_queue == x1.sycl_queue
48+
assert r.sycl_queue == x2.sycl_queue
49+
50+
x1_np = np.arange(x1_range_begin, x1_range_begin + n, dtype=op1_dtype)
51+
x2_np = np.arange(0, n, dtype=op2_dtype)
52+
r_np = np.left_shift(x1_np, x2_np)
53+
54+
assert r.dtype == r_np.dtype
55+
assert (dpt.asnumpy(r) == r_np).all()
56+
57+
58+
@pytest.mark.parametrize("op1_dtype", _integral_dtypes)
59+
@pytest.mark.parametrize("op2_dtype", _integral_dtypes)
60+
def test_bitwise_left_shift_dtype_matrix_strided(op1_dtype, op2_dtype):
61+
q = get_queue_or_skip()
62+
skip_if_dtype_not_supported(op1_dtype, q)
63+
skip_if_dtype_not_supported(op2_dtype, q)
64+
65+
if op1_dtype != op2_dtype and "u8" in [op1_dtype, op2_dtype]:
66+
return
67+
68+
sz = 11
69+
n = 2 * sz
70+
dt1 = dpt.dtype(op1_dtype)
71+
dt2 = dpt.dtype(op2_dtype)
72+
73+
x1_range_begin = -sz if dpt.iinfo(dt1).min < 0 else 0
74+
x1 = dpt.arange(x1_range_begin, x1_range_begin + n, dtype=dt1)[::-2]
75+
x2 = dpt.arange(0, n, dtype=dt2)[::2]
76+
77+
r = dpt.bitwise_left_shift(x1, x2)
78+
assert isinstance(r, dpt.usm_ndarray)
79+
assert r.sycl_queue == x1.sycl_queue
80+
assert r.sycl_queue == x2.sycl_queue
81+
82+
x1_np = np.arange(x1_range_begin, x1_range_begin + n, dtype=dt1)[::-2]
83+
x2_np = np.arange(0, n, dtype=dt2)[::2]
84+
r_np = np.left_shift(x1_np, x2_np)
85+
86+
assert r.dtype == r_np.dtype
87+
assert (dpt.asnumpy(r) == r_np).all()
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Data Parallel Control (dpctl)
2+
#
3+
# Copyright 2023 Intel Corporation
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless_equal required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import numpy as np
18+
import pytest
19+
20+
import dpctl.tensor as dpt
21+
from dpctl.tests.helper import get_queue_or_skip, skip_if_dtype_not_supported
22+
23+
from .utils import _integral_dtypes
24+
25+
26+
@pytest.mark.parametrize("op_dtype", _integral_dtypes)
27+
def test_bitwise_or_dtype_matrix_contig(op_dtype):
28+
q = get_queue_or_skip()
29+
skip_if_dtype_not_supported(op_dtype, q)
30+
31+
sz = 7
32+
n = 2 * sz
33+
dt1 = dpt.dtype(op_dtype)
34+
dt2 = dpt.dtype(op_dtype)
35+
36+
x1_range_begin = -sz if dpt.iinfo(dt1).min < 0 else 0
37+
x1 = dpt.arange(x1_range_begin, x1_range_begin + n, dtype=dt1)
38+
39+
x2_range_begin = -sz if dpt.iinfo(dt2).min < 0 else 0
40+
x2 = dpt.arange(x2_range_begin, x2_range_begin + n, dtype=dt1)
41+
42+
r = dpt.bitwise_or(x1, x2)
43+
assert isinstance(r, dpt.usm_ndarray)
44+
45+
x1_np = np.arange(x1_range_begin, x1_range_begin + n, dtype=op_dtype)
46+
x2_np = np.arange(x2_range_begin, x2_range_begin + n, dtype=op_dtype)
47+
r_np = np.bitwise_or(x1_np, x2_np)
48+
49+
assert (r_np == dpt.asnumpy(r)).all()
50+
51+
52+
@pytest.mark.parametrize("op_dtype", _integral_dtypes)
53+
def test_bitwise_or_dtype_matrix_strided(op_dtype):
54+
q = get_queue_or_skip()
55+
skip_if_dtype_not_supported(op_dtype, q)
56+
57+
sz = 11
58+
n = 2 * sz
59+
dt1 = dpt.dtype(op_dtype)
60+
dt2 = dpt.dtype(op_dtype)
61+
62+
x1_range_begin = -sz if dpt.iinfo(dt1).min < 0 else 0
63+
x1 = dpt.arange(x1_range_begin, x1_range_begin + n, dtype=dt1)[::2]
64+
65+
x2_range_begin = -(sz // 2) if dpt.iinfo(dt2).min < 0 else 0
66+
x2 = dpt.arange(x2_range_begin, x2_range_begin + n, dtype=dt1)[::-2]
67+
68+
r = dpt.bitwise_or(x1, x2)
69+
assert isinstance(r, dpt.usm_ndarray)
70+
71+
x1_np = np.arange(x1_range_begin, x1_range_begin + n, dtype=op_dtype)[::2]
72+
x2_np = np.arange(x2_range_begin, x2_range_begin + n, dtype=op_dtype)[::-2]
73+
r_np = np.bitwise_or(x1_np, x2_np)
74+
75+
assert (r_np == dpt.asnumpy(r)).all()
76+
77+
78+
def test_bitwise_or_bool():
79+
get_queue_or_skip()
80+
81+
x1 = dpt.asarray([True, False])
82+
x2 = dpt.asarray([False, True])
83+
84+
r_bw = dpt.bitwise_or(x1[:, dpt.newaxis], x2[dpt.newaxis])
85+
r_lo = dpt.logical_or(x1[:, dpt.newaxis], x2[dpt.newaxis])
86+
87+
assert dpt.all(dpt.equal(r_bw, r_lo))

0 commit comments

Comments
 (0)