|
| 1 | +from numpy.core.numeric import normalize_axis_index |
| 2 | + |
| 3 | +import dpctl |
| 4 | +import dpctl.tensor as dpt |
| 5 | +import dpctl.tensor._tensor_impl as ti |
| 6 | + |
| 7 | +from ._tensor_sorting_impl import ( |
| 8 | + _argsort_ascending, |
| 9 | + _argsort_descending, |
| 10 | + _sort_ascending, |
| 11 | + _sort_descending, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def sort(x, axis=-1, descending=False, stable=False): |
| 16 | + if not isinstance(x, dpt.usm_ndarray): |
| 17 | + raise TypeError( |
| 18 | + f"Expected type dpctl.tensor.usm_ndarray, got {type(x)}" |
| 19 | + ) |
| 20 | + nd = x.ndim |
| 21 | + axis = normalize_axis_index(axis, ndim=nd, msg_prefix="axis") |
| 22 | + a1 = axis + 1 |
| 23 | + if a1 == nd: |
| 24 | + perm = list(range(nd)) |
| 25 | + arr = x |
| 26 | + else: |
| 27 | + perm = [i for i in range(nd) if i != axis] + [ |
| 28 | + axis, |
| 29 | + ] |
| 30 | + arr = dpt.permute_dims(x, perm) |
| 31 | + exec_q = x.sycl_queue |
| 32 | + host_tasks_list = [] |
| 33 | + impl_fn = _sort_descending if descending else _sort_ascending |
| 34 | + if arr.flags.c_contiguous: |
| 35 | + res = dpt.empty_like(arr, order="C") |
| 36 | + ht_ev, _ = impl_fn( |
| 37 | + src=arr, trailing_dims_to_sort=1, dst=res, sycl_queue=exec_q |
| 38 | + ) |
| 39 | + host_tasks_list.append(ht_ev) |
| 40 | + else: |
| 41 | + tmp = dpt.empty_like(arr, order="C") |
| 42 | + ht_ev, copy_ev = ti._copy_usm_ndarray_into_usm_ndarray( |
| 43 | + src=arr, dst=tmp, sycl_queue=exec_q |
| 44 | + ) |
| 45 | + host_tasks_list.append(ht_ev) |
| 46 | + res = dpt.empty_like(arr, order="C") |
| 47 | + ht_ev, _ = impl_fn( |
| 48 | + src=tmp, |
| 49 | + trailing_dims_to_sort=1, |
| 50 | + dst=res, |
| 51 | + sycl_queue=exec_q, |
| 52 | + depends=[copy_ev], |
| 53 | + ) |
| 54 | + host_tasks_list.append(ht_ev) |
| 55 | + if a1 != nd: |
| 56 | + inv_perm = sorted(range(nd), key=lambda d: perm[d]) |
| 57 | + res = dpt.permute_dims(res, inv_perm) |
| 58 | + dpctl.SyclEvent.wait_for(host_tasks_list) |
| 59 | + return res |
| 60 | + |
| 61 | + |
| 62 | +def argsort(x, axis=-1, descending=False, stable=False): |
| 63 | + if not isinstance(x, dpt.usm_ndarray): |
| 64 | + raise TypeError( |
| 65 | + f"Expected type dpctl.tensor.usm_ndarray, got {type(x)}" |
| 66 | + ) |
| 67 | + nd = x.ndim |
| 68 | + axis = normalize_axis_index(axis, ndim=nd, msg_prefix="axis") |
| 69 | + a1 = axis + 1 |
| 70 | + if a1 == nd: |
| 71 | + perm = list(range(nd)) |
| 72 | + arr = x |
| 73 | + else: |
| 74 | + perm = [i for i in range(nd) if i != axis] + [ |
| 75 | + axis, |
| 76 | + ] |
| 77 | + arr = dpt.permute_dims(x, perm) |
| 78 | + exec_q = x.sycl_queue |
| 79 | + host_tasks_list = [] |
| 80 | + impl_fn = _argsort_descending if descending else _argsort_ascending |
| 81 | + index_dt = ti.default_device_index_type(exec_q) |
| 82 | + if arr.flags.c_contiguous: |
| 83 | + res = dpt.empty_like(arr, dtype=index_dt, order="C") |
| 84 | + ht_ev, _ = impl_fn( |
| 85 | + src=arr, trailing_dims_to_sort=1, dst=res, sycl_queue=exec_q |
| 86 | + ) |
| 87 | + host_tasks_list.append(ht_ev) |
| 88 | + else: |
| 89 | + tmp = dpt.empty_like(arr, order="C") |
| 90 | + ht_ev, copy_ev = ti._copy_usm_ndarray_into_usm_ndarray( |
| 91 | + src=arr, dst=tmp, sycl_queue=exec_q |
| 92 | + ) |
| 93 | + host_tasks_list.append(ht_ev) |
| 94 | + res = dpt.empty_like(arr, dtype=index_dt, order="C") |
| 95 | + ht_ev, _ = impl_fn( |
| 96 | + src=tmp, |
| 97 | + trailing_dims_to_sort=1, |
| 98 | + dst=res, |
| 99 | + sycl_queue=exec_q, |
| 100 | + depends=[copy_ev], |
| 101 | + ) |
| 102 | + host_tasks_list.append(ht_ev) |
| 103 | + if a1 != nd: |
| 104 | + inv_perm = sorted(range(nd), key=lambda d: perm[d]) |
| 105 | + res = dpt.permute_dims(res, inv_perm) |
| 106 | + dpctl.SyclEvent.wait_for(host_tasks_list) |
| 107 | + return res |
0 commit comments