forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortingKernel.cpp
209 lines (187 loc) · 6.77 KB
/
SortingKernel.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <ATen/ATen.h>
#include <ATen/Dispatch.h>
#include <ATen/Parallel.h>
#include <ATen/NumericUtils.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/native/StridedRandomAccessor.h>
#include <ATen/native/CompositeRandomAccessor.h>
#include <ATen/native/Sorting.h>
#include <ATen/native/SortingUtils.h>
namespace at { namespace native {
namespace {
void _fill_indices(Tensor& indices, int64_t dim) {
auto dim_size = indices.size(dim);
auto idx_dim = at::arange(0, dim_size, indices.options().dtype(at::kLong));
auto idx_dim_sizes = std::vector<int64_t>(indices.dim(), 1);
auto idx_dim_strides = std::vector<int64_t>(indices.dim(), 0);
idx_dim_sizes[dim] = dim_size;
idx_dim_strides[dim] = 1;
auto idx_dim_restrided = idx_dim.as_strided(idx_dim_sizes, idx_dim_strides);
indices.copy_(idx_dim_restrided);
}
template <typename func_t>
void _dim_apply(
Tensor& values,
Tensor& indices,
int64_t dim,
const std::string& method_name,
const func_t& f) {
dim = maybe_wrap_dim(dim, values.dim());
TORCH_CHECK(
dim >= 0 && dim < values.dim(),
method_name, "(): invalid dimension parameter ", dim
);
auto iter = TensorIteratorConfig()
.check_all_same_dtype(false)
.resize_outputs(false)
.declare_static_shape(values.sizes(), /*squash_dim=*/dim)
.add_output(values)
.add_output(indices)
.build();
auto values_dim_stride = values.stride(dim);
auto indices_dim_stride = indices.stride(dim);
auto dim_size = values.size(dim);
AT_DISPATCH_ALL_TYPES_AND2(
ScalarType::Bool, ScalarType::Half, iter.dtype(),
method_name, [&] {
auto loop = [&](char** data, const int64_t* strides, int64_t n) {
auto* values_data_bytes = data[0];
auto* indices_data_bytes = data[1];
for (int64_t i = 0; i < n; ++i) {
f(
reinterpret_cast<scalar_t*>(values_data_bytes),
values_dim_stride,
reinterpret_cast<int64_t*>(indices_data_bytes),
indices_dim_stride,
dim_size
);
values_data_bytes += strides[0];
indices_data_bytes += strides[1];
}
};
iter.for_each(loop);
}
);
}
template <typename scalar_t>
struct KeyValueCompAsc {
template <typename LHS, typename RHS>
constexpr bool operator()(LHS lhs, RHS rhs) const {
return (!_isnan<scalar_t>(get<0>(lhs)) && _isnan<scalar_t>(get<0>(rhs)))
|| (get<0>(lhs) < get<0>(rhs));
}
};
template <typename scalar_t>
struct KeyValueCompDesc {
template <typename LHS, typename RHS>
constexpr bool operator()(LHS lhs, RHS rhs) const {
return (_isnan<scalar_t>(get<0>(lhs)) && !_isnan<scalar_t>(get<0>(rhs)))
|| (get<0>(lhs) > get<0>(rhs));
}
};
static void sort_kernel(
Tensor& values,
Tensor& indices,
int64_t dim,
bool descending) {
dim = maybe_wrap_dim(dim, values.dim());
_fill_indices(indices, dim);
_dim_apply(
values, indices, dim,
"sort_cpu", [&](
auto* values, int64_t values_dim_stride,
auto* indices, int64_t indices_dim_stride,
int64_t dim_size
) {
using scalar_t = typename std::remove_pointer<decltype(values)>::type;
auto values_accessor = StridedRandomAccessor<scalar_t>(
values, values_dim_stride);
auto indices_accessor = StridedRandomAccessor<int64_t>(
indices, indices_dim_stride);
auto composite_accessor = CompositeRandomAccessorCPU<
decltype(values_accessor), decltype(indices_accessor)
>(values_accessor, indices_accessor);
if (descending) {
std::sort(composite_accessor, composite_accessor + dim_size,
KeyValueCompDesc<scalar_t>());
}
else {
std::sort(composite_accessor, composite_accessor + dim_size,
KeyValueCompAsc<scalar_t>());
}
}
);
}
static void topk_kernel(
Tensor& values,
Tensor& indices,
const Tensor& self,
int64_t k,
int64_t dim,
bool largest,
bool sorted) {
AT_DISPATCH_ALL_TYPES(self.scalar_type(), "topk_cpu", [&] {
dim_apply(
{self, values, indices},
dim,
[&](int64_t i, TensorList tl) {
auto tmp_values = tl[0].accessor<scalar_t, 1>();
auto mode_values = tl[1].accessor<scalar_t, 1>();
auto mode_indices = tl[2].accessor<int64_t, 1>();
auto n = tmp_values.size(0);
auto use_partial_sort = k * 64 <= n;
using elem_t = std::pair<scalar_t, int64_t>;
std::vector<elem_t> queue(n);
for (int64_t j = 0; j < n; j++) {
queue[j].first = tmp_values[j];
queue[j].second = j;
}
// we want NaN to be sorted as top for numpy compatibility
if (use_partial_sort) {
if (largest) {
std::partial_sort(queue.begin(), queue.begin() + k, queue.end(),
[](const elem_t& x, const elem_t& y) -> bool {
return ((_isnan<scalar_t>(x.first) && !_isnan<scalar_t>(y.first)) || (x.first > y.first));
});
} else {
std::partial_sort(queue.begin(), queue.begin() + k, queue.end(),
[](const elem_t& x, const elem_t& y) -> bool {
return ((!_isnan<scalar_t>(x.first) && _isnan<scalar_t>(y.first)) || (x.first < y.first));
});
}
} else {
if (largest) {
std::nth_element(queue.begin(), queue.begin() + k - 1, queue.end(),
[](const elem_t& x, const elem_t& y) -> bool {
return ((_isnan<scalar_t>(x.first) && !_isnan<scalar_t>(y.first)) || (x.first > y.first));
});
if (sorted) {
std::sort(queue.begin(), queue.begin() + k - 1,
[](const elem_t& x, const elem_t& y) -> bool {
return ((_isnan<scalar_t>(x.first) && !_isnan<scalar_t>(y.first)) || (x.first > y.first));
});
}
} else {
std::nth_element(queue.begin(), queue.begin() + k -1, queue.end(),
[](const elem_t& x, const elem_t& y) -> bool {
return ((!_isnan<scalar_t>(x.first) && _isnan<scalar_t>(y.first)) || (x.first < y.first));
});
if (sorted) {
std::sort(queue.begin(), queue.begin() + k -1,
[](const elem_t& x, const elem_t& y) -> bool {
return ((!_isnan<scalar_t>(x.first) && _isnan<scalar_t>(y.first)) || (x.first < y.first));
});
}
}
}
for (int64_t j = 0; j < k; j++) {
mode_values[j] = queue[j].first;
mode_indices[j] = queue[j].second;
}
});
});
}
} // anonymous namespace
REGISTER_DISPATCH(sort_stub, &sort_kernel);
REGISTER_DISPATCH(topk_stub, &topk_kernel);
}} //at::native