Skip to content

Commit 634348b

Browse files
authored
Merge pull request #1204 from IntelPython/boolean-reductions
Implements dpctl.tensor.any and dpctl.tensor.all
2 parents b2a66d2 + d1d67b9 commit 634348b

File tree

8 files changed

+1353
-0
lines changed

8 files changed

+1353
-0
lines changed

dpctl/tensor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pybind11_add_module(${python_module_name} MODULE
4444
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/full_ctor.cpp
4545
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/triul_ctor.cpp
4646
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/where.cpp
47+
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/boolean_reductions.cpp
4748
${CMAKE_CURRENT_SOURCE_DIR}/libtensor/source/device_support_queries.cpp
4849
)
4950
target_compile_options(${python_module_name} PRIVATE -fno-sycl-id-queries-fit-in-int)

dpctl/tensor/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
from dpctl.tensor._reshape import reshape
8989
from dpctl.tensor._search_functions import where
9090
from dpctl.tensor._usmarray import usm_ndarray
91+
from dpctl.tensor._utility_functions import all, any
9192

9293
from ._constants import e, inf, nan, newaxis, pi
9394

@@ -130,6 +131,8 @@
130131
"tril",
131132
"triu",
132133
"where",
134+
"all",
135+
"any",
133136
"dtype",
134137
"isdtype",
135138
"bool",

dpctl/tensor/_utility_functions.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
from numpy.core.numeric import normalize_axis_tuple
2+
3+
import dpctl
4+
import dpctl.tensor as dpt
5+
import dpctl.tensor._tensor_impl as ti
6+
7+
8+
def _boolean_reduction(x, axis, keepdims, func):
9+
if not isinstance(x, dpt.usm_ndarray):
10+
raise TypeError(f"Expected dpctl.tensor.usm_ndarray, got {type(x)}")
11+
12+
nd = x.ndim
13+
if axis is None:
14+
red_nd = nd
15+
# case of a scalar
16+
if red_nd == 0:
17+
return dpt.astype(x, dpt.bool)
18+
x_tmp = x
19+
res_shape = tuple()
20+
perm = list(range(nd))
21+
else:
22+
if not isinstance(axis, (tuple, list)):
23+
axis = (axis,)
24+
axis = normalize_axis_tuple(axis, nd, "axis")
25+
26+
red_nd = len(axis)
27+
# check for axis=()
28+
if red_nd == 0:
29+
return dpt.astype(x, dpt.bool)
30+
perm = [i for i in range(nd) if i not in axis] + list(axis)
31+
x_tmp = dpt.permute_dims(x, perm)
32+
res_shape = x_tmp.shape[: nd - red_nd]
33+
34+
exec_q = x.sycl_queue
35+
res_usm_type = x.usm_type
36+
37+
wait_list = []
38+
res_tmp = dpt.empty(
39+
res_shape,
40+
dtype=dpt.int32,
41+
usm_type=res_usm_type,
42+
sycl_queue=exec_q,
43+
)
44+
hev0, ev0 = func(
45+
src=x_tmp,
46+
trailing_dims_to_reduce=red_nd,
47+
dst=res_tmp,
48+
sycl_queue=exec_q,
49+
)
50+
wait_list.append(hev0)
51+
52+
# copy to boolean result array
53+
res = dpt.empty(
54+
res_shape,
55+
dtype=dpt.bool,
56+
usm_type=res_usm_type,
57+
sycl_queue=exec_q,
58+
)
59+
hev1, _ = ti._copy_usm_ndarray_into_usm_ndarray(
60+
src=res_tmp, dst=res, sycl_queue=exec_q, depends=[ev0]
61+
)
62+
wait_list.append(hev1)
63+
64+
if keepdims:
65+
res_shape = res_shape + (1,) * red_nd
66+
inv_perm = sorted(range(nd), key=lambda d: perm[d])
67+
res = dpt.permute_dims(dpt.reshape(res, res_shape), inv_perm)
68+
dpctl.SyclEvent.wait_for(wait_list)
69+
return res
70+
71+
72+
def all(x, axis=None, keepdims=False):
73+
"""all(x, axis=None, keepdims=False)
74+
75+
Tests whether all input array elements evaluate to True along a given axis.
76+
77+
Args:
78+
x (usm_ndarray): Input array.
79+
axis (Optional[Union[int, Tuple[int,...]]]): Axis (or axes)
80+
along which to perform a logical AND reduction.
81+
When `axis` is `None`, a logical AND reduction
82+
is performed over all dimensions of `x`.
83+
If `axis` is negative, the axis is counted from
84+
the last dimension to the first.
85+
Default: `None`.
86+
keepdims (bool, optional): If `True`, the reduced axes are included
87+
in the result as singleton dimensions, and the result is
88+
broadcastable to the input array shape.
89+
If `False`, the reduced axes are not included in the result.
90+
Default: `False`.
91+
92+
Returns:
93+
usm_ndarray:
94+
An array with a data type of `bool`
95+
containing the results of the logical AND reduction.
96+
"""
97+
return _boolean_reduction(x, axis, keepdims, ti._all)
98+
99+
100+
def any(x, axis=None, keepdims=False):
101+
"""any(x, axis=None, keepdims=False)
102+
103+
Tests whether any input array elements evaluate to True along a given axis.
104+
105+
Args:
106+
x (usm_ndarray): Input array.
107+
axis (Optional[Union[int, Tuple[int,...]]]): Axis (or axes)
108+
along which to perform a logical OR reduction.
109+
When `axis` is `None`, a logical OR reduction
110+
is performed over all dimensions of `x`.
111+
If `axis` is negative, the axis is counted from
112+
the last dimension to the first.
113+
Default: `None`.
114+
keepdims (bool, optional): If `True`, the reduced axes are included
115+
in the result as singleton dimensions, and the result is
116+
broadcastable to the input array shape.
117+
If `False`, the reduced axes are not included in the result.
118+
Default: `False`.
119+
120+
Returns:
121+
usm_ndarray:
122+
An array with a data type of `bool`
123+
containing the results of the logical OR reduction.
124+
"""
125+
return _boolean_reduction(x, axis, keepdims, ti._any)

0 commit comments

Comments
 (0)