|
| 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