Skip to content

Commit 9ece0a3

Browse files
JosephGumanJoseph Thomas Gumanmanopapad
authored
logical_and reduction (#1123)
* Adding functionality for logical_and.reduce() with appropriate modifications in tests \n\n Joseph Guman <joeytg@stanford.edu> * Minor style change to test for logical_and.reduce() \n\nSigned-off-by: Joseph Guman <joeytg@stanford.edu> * Style changes and adding logical_or.reduce() functionality with UnaryRedCode.ANY Signed-off-by: Joseph Guman <joeytg@stanford.edu> * Minor fixes --------- Co-authored-by: Joseph Thomas Guman <joeytg@sapling2.stanford.edu> Co-authored-by: Manolis Papadakis <mpapadakis@nvidia.com> Co-authored-by: Manolis Papadakis <manopapad@gmail.com>
1 parent 788fed0 commit 9ece0a3

4 files changed

Lines changed: 47 additions & 2 deletions

File tree

cunumeric/_ufunc/comparison.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,15 @@
7373
"logical_and",
7474
BinaryOpCode.LOGICAL_AND,
7575
relation_types_of(all_dtypes),
76+
red_code=UnaryRedCode.ALL,
7677
)
7778

7879
logical_or = create_binary_ufunc(
7980
"Compute the truth value of x1 OR x2 element-wise.",
8081
"logical_or",
8182
BinaryOpCode.LOGICAL_OR,
8283
relation_types_of(all_dtypes),
84+
red_code=UnaryRedCode.ANY,
8385
)
8486

8587
logical_xor = create_binary_ufunc(

cunumeric/_ufunc/ufunc.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,16 @@ def reduce(
753753
f"reduction for {self} is not yet implemented"
754754
)
755755

756+
if self._op_code in [
757+
BinaryOpCode.LOGICAL_AND,
758+
BinaryOpCode.LOGICAL_OR,
759+
]:
760+
res_dtype = bool
761+
if dtype is not None:
762+
raise TypeError("Cannot set dtype on a logical reduction")
763+
else:
764+
res_dtype = None
765+
756766
# NumPy seems to be using None as the default axis value for scalars
757767
if array.ndim == 0 and axis == 0:
758768
axis = None
@@ -767,6 +777,7 @@ def reduce(
767777
keepdims=keepdims,
768778
initial=initial,
769779
where=where,
780+
res_dtype=res_dtype,
770781
)
771782

772783

tests/integration/test_fallback.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ def test_ufunc():
2828
in_num = num.array([0, 1, 2, 3])
2929
in_np = in_num.__array__()
3030

31-
out_num = np.logical_and.reduce(in_num)
32-
out_np = np.logical_and.reduce(in_np)
31+
# This test uses logical_and.accumulate because it is currently
32+
# unimplemented, and we want to verify a behaviour of unimplemented ufunc
33+
# methods. If logical_and.accumulate becomes implemented in the future,
34+
# this assertion will start to fail, and a new (unimplemented) ufunc method
35+
# should be found to replace it
36+
assert not num.logical_and.accumulate._cunumeric.implemented
37+
38+
out_num = num.logical_and.accumulate(in_num)
39+
out_np = np.logical_and.accumulate(in_np)
3340
assert np.array_equal(out_num, out_np)
3441

3542

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
import pytest
3+
4+
import cunumeric as num
5+
6+
7+
@pytest.mark.parametrize("axis", [None, 0, 1, 2, (0, 1, 2)])
8+
def test_logical_reductions(axis):
9+
input = [[[12, 0, 1, 2], [9, 0, 0, 1]], [[0, 0, 0, 5], [1, 1, 1, 1]]]
10+
in_num = num.array(input)
11+
in_np = np.array(input)
12+
13+
out_num = num.logical_and.reduce(in_num, axis=axis)
14+
out_np = np.logical_and.reduce(in_np, axis=axis)
15+
assert num.array_equal(out_num, out_np)
16+
17+
out_num = num.logical_or.reduce(in_num, axis=axis)
18+
out_np = np.logical_or.reduce(in_np, axis=axis)
19+
assert num.array_equal(out_num, out_np)
20+
21+
22+
if __name__ == "__main__":
23+
import sys
24+
25+
sys.exit(pytest.main(sys.argv))

0 commit comments

Comments
 (0)