-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy path_searchsorted.py
More file actions
189 lines (171 loc) · 7.3 KB
/
_searchsorted.py
File metadata and controls
189 lines (171 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# *****************************************************************************
# Copyright (c) 2026, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# - Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.
# *****************************************************************************
from typing import Literal, Union
import dpctl
import dpctl.utils as du
# TODO: revert to `from ._usmarray import...`
# when dpnp fully migrates dpctl/tensor
from dpctl.tensor._usmarray import usm_ndarray
from ._copy_utils import _empty_like_orderK
from ._ctors import empty
from ._tensor_impl import _copy_usm_ndarray_into_usm_ndarray as ti_copy
from ._tensor_impl import _take as ti_take
from ._tensor_impl import (
default_device_index_type as ti_default_device_index_type,
)
from ._tensor_sorting_impl import _searchsorted_left, _searchsorted_right
from ._type_utils import isdtype, result_type
def searchsorted(
x1: usm_ndarray,
x2: usm_ndarray,
/,
*,
side: Literal["left", "right"] = "left",
sorter: Union[usm_ndarray, None] = None,
) -> usm_ndarray:
"""searchsorted(x1, x2, side='left', sorter=None)
Finds the indices into `x1` such that, if the corresponding elements
in `x2` were inserted before the indices, the order of `x1`, when sorted
in ascending order, would be preserved.
Args:
x1 (usm_ndarray):
input array. Must be a one-dimensional array. If `sorter` is
`None`, must be sorted in ascending order; otherwise, `sorter` must
be an array of indices that sort `x1` in ascending order.
x2 (usm_ndarray):
array containing search values.
side (Literal["left", "right]):
argument controlling which index is returned if a value lands
exactly on an edge. If `x2` is an array of rank `N` where
`v = x2[n, m, ..., j]`, the element `ret[n, m, ..., j]` in the
return array `ret` contains the position `i` such that
if `side="left"`, it is the first index such that
`x1[i-1] < v <= x1[i]`, `0` if `v <= x1[0]`, and `x1.size`
if `v > x1[-1]`;
and if `side="right"`, it is the first position `i` such that
`x1[i-1] <= v < x1[i]`, `0` if `v < x1[0]`, and `x1.size`
if `v >= x1[-1]`. Default: `"left"`.
sorter (Optional[usm_ndarray]):
array of indices that sort `x1` in ascending order. The array must
have the same shape as `x1` and have an integral data type.
Out of bound index values of `sorter` array are treated using
`"wrap"` mode documented in :py:func:`dpctl.tensor.take`.
Default: `None`.
"""
if not isinstance(x1, usm_ndarray):
raise TypeError(f"Expected dpctl.tensor.usm_ndarray, got {type(x1)}")
if not isinstance(x2, usm_ndarray):
raise TypeError(f"Expected dpctl.tensor.usm_ndarray, got {type(x2)}")
if sorter is not None and not isinstance(sorter, usm_ndarray):
raise TypeError(
f"Expected dpctl.tensor.usm_ndarray, got {type(sorter)}"
)
if side not in ["left", "right"]:
raise ValueError(
"Unrecognized value of 'side' keyword argument. "
"Expected either 'left' or 'right'"
)
if sorter is None:
q = du.get_execution_queue([x1.sycl_queue, x2.sycl_queue])
else:
q = du.get_execution_queue(
[x1.sycl_queue, x2.sycl_queue, sorter.sycl_queue]
)
if q is None:
raise du.ExecutionPlacementError(
"Execution placement can not be unambiguously "
"inferred from input arguments."
)
if x1.ndim != 1:
raise ValueError("First argument array must be one-dimensional")
x1_dt = x1.dtype
x2_dt = x2.dtype
_manager = du.SequentialOrderManager[q]
dep_evs = _manager.submitted_events
ev = dpctl.SyclEvent()
if sorter is not None:
if not isdtype(sorter.dtype, "integral"):
raise ValueError(
f"Sorter array must have integral data type, got {sorter.dtype}"
)
if x1.shape != sorter.shape:
raise ValueError(
"Sorter array must be one-dimension with the same "
"shape as the first argument array"
)
res = empty(x1.shape, dtype=x1_dt, usm_type=x1.usm_type, sycl_queue=q)
ind = (sorter,)
axis = 0
wrap_out_of_bound_indices_mode = 0
ht_ev, ev = ti_take(
x1,
ind,
res,
axis,
wrap_out_of_bound_indices_mode,
sycl_queue=q,
depends=dep_evs,
)
x1 = res
_manager.add_event_pair(ht_ev, ev)
if x1_dt != x2_dt:
dt = result_type(x1, x2)
if x1_dt != dt:
x1_buf = _empty_like_orderK(x1, dt)
dep_evs = _manager.submitted_events
ht_ev, ev = ti_copy(
src=x1, dst=x1_buf, sycl_queue=q, depends=dep_evs
)
_manager.add_event_pair(ht_ev, ev)
x1 = x1_buf
if x2_dt != dt:
x2_buf = _empty_like_orderK(x2, dt)
dep_evs = _manager.submitted_events
ht_ev, ev = ti_copy(
src=x2, dst=x2_buf, sycl_queue=q, depends=dep_evs
)
_manager.add_event_pair(ht_ev, ev)
x2 = x2_buf
dst_usm_type = du.get_coerced_usm_type([x1.usm_type, x2.usm_type])
index_dt = ti_default_device_index_type(q)
dst = _empty_like_orderK(x2, index_dt, usm_type=dst_usm_type)
dep_evs = _manager.submitted_events
if side == "left":
ht_ev, s_ev = _searchsorted_left(
hay=x1,
needles=x2,
positions=dst,
sycl_queue=q,
depends=dep_evs,
)
else:
ht_ev, s_ev = _searchsorted_right(
hay=x1, needles=x2, positions=dst, sycl_queue=q, depends=dep_evs
)
_manager.add_event_pair(ht_ev, s_ev)
return dst