|
| 1 | +# Data Parallel Control (dpctl) |
| 2 | +# |
| 3 | +# Copyright 2020-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 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 dpctl |
| 18 | +import dpctl.tensor as dpt |
| 19 | +import dpctl.tensor._tensor_impl as ti |
| 20 | +from dpctl.tensor._manipulation_functions import _broadcast_shapes |
| 21 | + |
| 22 | +from ._type_utils import _all_data_types, _can_cast |
| 23 | + |
| 24 | + |
| 25 | +def _where_result_type(dt1, dt2, dev): |
| 26 | + res_dtype = dpt.result_type(dt1, dt2) |
| 27 | + fp16 = dev.has_aspect_fp16 |
| 28 | + fp64 = dev.has_aspect_fp64 |
| 29 | + |
| 30 | + all_dts = _all_data_types(fp16, fp64) |
| 31 | + if res_dtype in all_dts: |
| 32 | + return res_dtype |
| 33 | + else: |
| 34 | + for res_dtype_ in all_dts: |
| 35 | + if _can_cast(dt1, res_dtype_, fp16, fp64) and _can_cast( |
| 36 | + dt2, res_dtype_, fp16, fp64 |
| 37 | + ): |
| 38 | + return res_dtype_ |
| 39 | + return None |
| 40 | + |
| 41 | + |
| 42 | +def where(condition, x1, x2): |
| 43 | + """where(condition, x1, x2) |
| 44 | +
|
| 45 | + Returns :class:`dpctl.tensor.usm_ndarray` with elements chosen |
| 46 | + from `x1` or `x2` depending on `condition`. |
| 47 | +
|
| 48 | + Args: |
| 49 | + condition (usm_ndarray): When True yields from `x1`, |
| 50 | + and otherwise yields from `x2`. |
| 51 | + Must be compatible with `x1` and `x2` according |
| 52 | + to broadcasting rules. |
| 53 | + x1 (usm_ndarray): Array from which values are chosen when |
| 54 | + `condition` is True. |
| 55 | + Must be compatible with `condition` and `x2` according |
| 56 | + to broadcasting rules. |
| 57 | + x2 (usm_ndarray): Array from which values are chosen when |
| 58 | + `condition` is not True. |
| 59 | + Must be compatible with `condition` and `x2` according |
| 60 | + to broadcasting rules. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + usm_ndarray: |
| 64 | + An array with elements from `x1` where `condition` is True, |
| 65 | + and elements from `x2` elsewhere. |
| 66 | +
|
| 67 | + The data type of the returned array is determined by applying |
| 68 | + the Type Promotion Rules to `x1` and `x2`. |
| 69 | +
|
| 70 | + The memory layout of the returned array is |
| 71 | + F-contiguous (column-major) when all inputs are F-contiguous, |
| 72 | + and C-contiguous (row-major) otherwise. |
| 73 | + """ |
| 74 | + if not isinstance(condition, dpt.usm_ndarray): |
| 75 | + raise TypeError( |
| 76 | + "Expecting dpctl.tensor.usm_ndarray type, " f"got {type(condition)}" |
| 77 | + ) |
| 78 | + if not isinstance(x1, dpt.usm_ndarray): |
| 79 | + raise TypeError( |
| 80 | + "Expecting dpctl.tensor.usm_ndarray type, " f"got {type(x1)}" |
| 81 | + ) |
| 82 | + if not isinstance(x2, dpt.usm_ndarray): |
| 83 | + raise TypeError( |
| 84 | + "Expecting dpctl.tensor.usm_ndarray type, " f"got {type(x2)}" |
| 85 | + ) |
| 86 | + exec_q = dpctl.utils.get_execution_queue( |
| 87 | + ( |
| 88 | + condition.sycl_queue, |
| 89 | + x1.sycl_queue, |
| 90 | + x2.sycl_queue, |
| 91 | + ) |
| 92 | + ) |
| 93 | + if exec_q is None: |
| 94 | + raise dpctl.utils.ExecutionPlacementError |
| 95 | + dst_usm_type = dpctl.utils.get_coerced_usm_type( |
| 96 | + ( |
| 97 | + condition.usm_type, |
| 98 | + x1.usm_type, |
| 99 | + x2.usm_type, |
| 100 | + ) |
| 101 | + ) |
| 102 | + |
| 103 | + x1_dtype = x1.dtype |
| 104 | + x2_dtype = x2.dtype |
| 105 | + dst_dtype = _where_result_type(x1_dtype, x2_dtype, exec_q.sycl_device) |
| 106 | + if dst_dtype is None: |
| 107 | + raise TypeError( |
| 108 | + "function 'where' does not support input " |
| 109 | + f"types ({x1_dtype}, {x2_dtype}), " |
| 110 | + "and the inputs could not be safely coerced " |
| 111 | + "to any supported types according to the casting rule ''safe''." |
| 112 | + ) |
| 113 | + |
| 114 | + res_shape = _broadcast_shapes(condition, x1, x2) |
| 115 | + |
| 116 | + if condition.size == 0: |
| 117 | + return dpt.empty( |
| 118 | + res_shape, dtype=dst_dtype, usm_type=dst_usm_type, sycl_queue=exec_q |
| 119 | + ) |
| 120 | + |
| 121 | + deps = [] |
| 122 | + wait_list = [] |
| 123 | + if x1_dtype != dst_dtype: |
| 124 | + _x1 = dpt.empty_like(x1, dtype=dst_dtype) |
| 125 | + ht_copy1_ev, copy1_ev = ti._copy_usm_ndarray_into_usm_ndarray( |
| 126 | + src=x1, dst=_x1, sycl_queue=exec_q |
| 127 | + ) |
| 128 | + x1 = _x1 |
| 129 | + deps.append(copy1_ev) |
| 130 | + wait_list.append(ht_copy1_ev) |
| 131 | + |
| 132 | + if x2_dtype != dst_dtype: |
| 133 | + _x2 = dpt.empty_like(x2, dtype=dst_dtype) |
| 134 | + ht_copy2_ev, copy2_ev = ti._copy_usm_ndarray_into_usm_ndarray( |
| 135 | + src=x2, dst=_x2, sycl_queue=exec_q |
| 136 | + ) |
| 137 | + x2 = _x2 |
| 138 | + deps.append(copy2_ev) |
| 139 | + wait_list.append(ht_copy2_ev) |
| 140 | + |
| 141 | + condition = dpt.broadcast_to(condition, res_shape) |
| 142 | + x1 = dpt.broadcast_to(x1, res_shape) |
| 143 | + x2 = dpt.broadcast_to(x2, res_shape) |
| 144 | + |
| 145 | + # dst is F-contiguous when all inputs are F contiguous |
| 146 | + # otherwise, defaults to C-contiguous |
| 147 | + if all( |
| 148 | + ( |
| 149 | + condition.flags.fnc, |
| 150 | + x1.flags.fnc, |
| 151 | + x2.flags.fnc, |
| 152 | + ) |
| 153 | + ): |
| 154 | + order = "F" |
| 155 | + else: |
| 156 | + order = "C" |
| 157 | + |
| 158 | + dst = dpt.empty( |
| 159 | + res_shape, |
| 160 | + dtype=dst_dtype, |
| 161 | + order=order, |
| 162 | + usm_type=dst_usm_type, |
| 163 | + sycl_queue=exec_q, |
| 164 | + ) |
| 165 | + |
| 166 | + hev, _ = ti._where( |
| 167 | + condition=condition, |
| 168 | + x1=x1, |
| 169 | + x2=x2, |
| 170 | + dst=dst, |
| 171 | + sycl_queue=exec_q, |
| 172 | + depends=deps, |
| 173 | + ) |
| 174 | + dpctl.SyclEvent.wait_for(wait_list) |
| 175 | + hev.wait() |
| 176 | + |
| 177 | + return dst |
0 commit comments