Find indices where elements should be inserted to maintain order. If `sorted_sequence` is N-dimensional, the innermost dimension of `values` are searched in the corresponding dimension of `sorted_sequence`. This implementation is optimized for GPU execution. Parameters
(sorted_sequence, values, right=False, out_dtype="int64")
| 1082 | |
| 1083 | |
| 1084 | def searchsorted(sorted_sequence, values, right=False, out_dtype="int64"): |
| 1085 | """Find indices where elements should be inserted to maintain order. |
| 1086 | If `sorted_sequence` is N-dimensional, the innermost dimension of |
| 1087 | `values` are searched in the corresponding dimension of `sorted_sequence`. |
| 1088 | |
| 1089 | This implementation is optimized for GPU execution. |
| 1090 | |
| 1091 | Parameters |
| 1092 | ---------- |
| 1093 | sorted_sequence : te.Tensor |
| 1094 | N-D or 1-D Tensor, containing monotonically increasing sequence |
| 1095 | on the innermost dimension. |
| 1096 | |
| 1097 | values : te.Tensor |
| 1098 | N-D Tensor containing the search values. When `sorted_sequence` is 1-D, |
| 1099 | the shape of `values` can be arbitrary. Otherwise, ranks of `sorted_sequence` |
| 1100 | and `values` must be the same, and outer N-1 axes must have the same size. |
| 1101 | |
| 1102 | right : bool, optional |
| 1103 | Controls which index is returned if a value lands exactly on one of sorted values. If |
| 1104 | False (side='left'), the index of the first suitable location found is given. If true |
| 1105 | (side='right'), return the last such index. |
| 1106 | |
| 1107 | out_dtype : string, optional |
| 1108 | The data type of the output indices. |
| 1109 | |
| 1110 | Returns |
| 1111 | ------- |
| 1112 | indices : te.Tensor |
| 1113 | Tensor with same shape as values, representing the indices of |
| 1114 | elements of `values` if they are inserted in `sorted_sequence`. |
| 1115 | """ |
| 1116 | if len(sorted_sequence.shape) > 1: |
| 1117 | for i in range(len(values.shape) - 1): |
| 1118 | assert values.shape[i] == sorted_sequence.shape[i], ( |
| 1119 | "Outer dimensions of sorted_sequence and values must match for N-D searchsorted" |
| 1120 | ) |
| 1121 | |
| 1122 | def ir(sorted_sequence_buf, values_buf, indices_buf): |
| 1123 | with IRBuilder() as ib: |
| 1124 | sorted_sequence_shape = sorted_sequence_buf.shape |
| 1125 | values_shape = values_buf.shape |
| 1126 | num_search = prod(values_shape) |
| 1127 | search_range = sorted_sequence_shape[-1] |
| 1128 | |
| 1129 | sorted_sequence_ptr = T.buffer_proxy(sorted_sequence_buf) |
| 1130 | values_ptr = T.buffer_proxy(values_buf) |
| 1131 | indices_ptr = T.buffer_proxy(indices_buf) |
| 1132 | |
| 1133 | max_threads = int(tvm.target.Target.current(allow_none=False).attrs["max_num_threads"]) |
| 1134 | nthread_tx = max_threads |
| 1135 | nthread_bx = ceil_div(num_search, nthread_tx) |
| 1136 | tx = te.thread_axis("threadIdx.x") |
| 1137 | bx = te.thread_axis("blockIdx.x") |
| 1138 | with T.frame_scope( |
| 1139 | [ |
| 1140 | T.attr(tx, "thread_extent", nthread_tx), |
| 1141 | T.attr(bx, "thread_extent", nthread_bx), |
nothing calls this directly
no test coverage detected
searching dependent graphs…