forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScatterGatherKernel.cpp
More file actions
288 lines (248 loc) · 9.54 KB
/
ScatterGatherKernel.cpp
File metadata and controls
288 lines (248 loc) · 9.54 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <ATen/native/ScatterGatherShapeChecks.h>
#include <ATen/native/ReduceOpsUtils.h>
#include <ATen/native/TensorIterator.h>
#include <ATen/Parallel.h>
namespace at { namespace native {
namespace {
// Used for `gather`-like methods
// Test:
// 1. index.size(d) == self.size(d) for all d != dim
void gather_shape_check(const Tensor& self, int64_t dim, const Tensor& index) {
auto self_dims = ensure_nonempty_dim(self.dim());
TORCH_CHECK(self_dims == ensure_nonempty_dim(index.dim()),
"Index tensor must have the same number of dimensions as input tensor"
);
for (int64_t i = 0; i < self_dims; ++i) {
if (i != dim) {
TORCH_CHECK(
ensure_nonempty_size(index, i) == ensure_nonempty_size(self, i),
"Size does not match at dimension ", i,
" get ", ensure_nonempty_size(self, i),
" vs ", ensure_nonempty_size(index, i)
);
}
}
}
// Used for `scatter` and `scatter_add`
// Tests:
// 1. index.size(d) <= self.size(d) for all d != dim
// 2. index.size(d) <= src.size(d) for all d if src is a Tensor
void scatter_shape_check(
const Tensor& self, int64_t dim, const Tensor& index,
const c10::optional<Tensor>& src_opt
) {
bool is_wrong_shape = false;
int64_t self_dims = ensure_nonempty_dim(self.dim());
// Check: index.size(d) <= self.size(d) for all d != dim
for (int64_t d = 0; d < self_dims; ++d) {
int64_t index_d_size = ensure_nonempty_size(index, d);
if (d == dim) continue;
if (index_d_size > ensure_nonempty_size(self, d)) {
is_wrong_shape = true;
break;
}
}
// Check: index.size(d) <= src.size(d) for all d if src is Tensor
if (!is_wrong_shape && src_opt.has_value()) {
auto src = src_opt.value();
for (int64_t d = 0; d < self_dims; ++d) {
int64_t index_d_size = ensure_nonempty_size(index, d);
if (index_d_size > ensure_nonempty_size(src, d)) {
is_wrong_shape = true;
break;
}
}
}
if (src_opt.has_value()) {
auto src = src_opt.value();
TORCH_CHECK(!is_wrong_shape,
"Expected index ", index.sizes(),
" to be smaller than self ", self.sizes(),
" apart from dimension ", dim,
" and to be smaller size than src ", src.sizes()
);
}
else {
TORCH_CHECK(!is_wrong_shape,
"Expected index ", index.sizes(),
" to be smaller than self ", self.sizes(),
" apart from dimension ", dim
);
}
}
template <typename func_t>
void cpu_scatter_gather_base_kernel(
Tensor& self, int64_t dim,
const Tensor& index, const Tensor& src,
const std::string& method_name,
const func_t& f,
bool serial_exec = true
) {
auto index_sizes = ensure_nonempty_vec(index.sizes().vec());
auto index_strides = ensure_nonempty_vec(index.strides().vec());
// `dim` is traversed in a kernel function `f`,
// that is why index.stride(dim) = 0 and index.size(dim) = 1.
// Also, index.size(dim) = 1 makes sure that TensorIterator.DimCounter
// has the following form : (i_1,..., i_{dim-1}, 0, i_{dim+1},...,i_n).
index_sizes[dim] = 1;
index_strides[dim] = 0;
// set self.shape = src.shape = index.shape,
// this defines the number of elements to iterate over,
// and set self.stride(dim) = src.stride(dim) = 0,
// because `dim` is traversed in a kernel function `f`.
auto self_restrided = restride_dim(self, dim, index_sizes);
auto index_restrided = index.as_strided(index_sizes, index_strides);
auto src_restrided = restride_dim(src, dim, index_sizes);
auto iter = TensorIterator();
iter.dont_compute_common_dtype();
iter.dont_resize_outputs();
iter.add_output(self_restrided);
iter.add_input(src_restrided, src.device(), src.scalar_type());
iter.add_input(index_restrided);
iter.build();
auto self_dim_stride = ensure_nonempty_stride(self, dim);
auto index_dim_stride = ensure_nonempty_stride(index, dim);
auto src_dim_stride = ensure_nonempty_stride(src, 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* self_data_bytes = data[0];
const auto* index_data_bytes = data[2];
const auto* src_data_bytes = data[1];
for (int64_t i = 0; i < n; ++i) {
f(
(scalar_t*)self_data_bytes, self_dim_stride,
(int64_t*)index_data_bytes, index_dim_stride,
(scalar_t*)src_data_bytes, src_dim_stride
);
self_data_bytes += strides[0];
index_data_bytes += strides[2];
src_data_bytes += strides[1];
}
};
if (serial_exec) {
iter.serial_for_each(loop, {0, iter.numel()});
} else {
iter.for_each(loop);
}
}
);
}
void gather_cpu_kernel(Tensor& result, const Tensor& self, int64_t dim, const Tensor& index) {
if (index.numel() == 0) {
return;
}
dim = maybe_wrap_dim(dim, self.dim());
gather_shape_check(self, dim, index);
int64_t index_dim_size = ensure_nonempty_size(index, dim);
int64_t self_dim_size = ensure_nonempty_size(self, dim);
cpu_scatter_gather_base_kernel(
result, dim, index, self,
"gather_out_cpu", [&] (
auto* result_data, auto result_dim_stride,
const auto* index_data, auto index_dim_stride,
const auto* self_data, auto self_dim_stride
) {
for (int64_t i = 0; i < index_dim_size; ++i) {
int64_t idx_dim = index_data[i * index_dim_stride];
// we are not putting idx_dim in the error message because it disables
// loop optimization in clang-7
TORCH_CHECK(idx_dim >= 0 && idx_dim < self_dim_size,
"index ", index_data[i * index_dim_stride], " is out of bounds for dimension ", dim,
" with size ", self_dim_size);
result_data[i * result_dim_stride] = self_data[idx_dim * self_dim_stride];
}
}, /*serial_exec=*/false
);
}
void scatter_cpu_kernel(Tensor& self, int64_t dim, const Tensor& index, const Tensor& src) {
if (index.numel() == 0) {
return;
}
dim = maybe_wrap_dim(dim, self.dim());
scatter_shape_check(self, dim, index, src);
int64_t index_dim_size = ensure_nonempty_size(index, dim);
int64_t self_dim_size = ensure_nonempty_size(self, dim);
cpu_scatter_gather_base_kernel(
self, dim, index, src,
"scatter_cpu_", [&] (
auto* self_data, auto self_dim_stride,
const auto* index_data, auto index_dim_stride,
const auto* src_data, auto src_dim_stride
) {
for (int64_t i = 0; i < index_dim_size; ++i) {
int64_t idx_dim = index_data[i * index_dim_stride];
// we are not putting idx_dim in the error message because it disables
// loop optimization in clang-7
TORCH_CHECK(idx_dim >= 0 && idx_dim < self_dim_size,
"index ", index_data[i * index_dim_stride],
" is out of bounds for dimension ", dim,
" with size ", self_dim_size);
self_data[idx_dim * self_dim_stride] = src_data[i * src_dim_stride];
}
}, /*serial_exec=*/false
);
}
void scatter_fill_cpu_kernel(Tensor& self, int64_t dim, const Tensor& index, Scalar src) {
if (index.numel() == 0) {
return;
}
dim = maybe_wrap_dim(dim, self.dim());
scatter_shape_check(self, dim, index);
int64_t index_dim_size = ensure_nonempty_size(index, dim);
int64_t self_dim_size = ensure_nonempty_size(self, dim);
cpu_scatter_gather_base_kernel(
self, dim, index, self,
"scatter_fill_cpu_", [&] (
auto* self_data, auto self_dim_stride,
const auto* index_data, auto index_dim_stride,
const auto* src_data, auto src_dim_stride
) {
for (int64_t i = 0; i < index_dim_size; ++i) {
int64_t idx_dim = index_data[i * index_dim_stride];
// we are not putting idx_dim in the error message because it disables
// loop optimization in clang-7
TORCH_CHECK(idx_dim >= 0 && idx_dim < self_dim_size,
"index ", index_data[i * index_dim_stride],
" is out of bounds for dimension ", dim,
" with size ", self_dim_size);
using scalar_t = typename std::remove_pointer<decltype(self_data)>::type;
self_data[idx_dim * self_dim_stride] = src.to<scalar_t>();
}
}, /*serial_exec=*/false
);
}
void scatter_add_cpu_kernel(Tensor& self, int64_t dim, const Tensor& index, const Tensor& src) {
if (index.numel() == 0) {
return;
}
dim = maybe_wrap_dim(dim, self.dim());
scatter_shape_check(self, dim, index, src);
int64_t index_dim_size = ensure_nonempty_size(index, dim);
int64_t self_dim_size = ensure_nonempty_size(self, dim);
cpu_scatter_gather_base_kernel(
self, dim, index, src,
"scatter_add_", [&] (
auto* self_data, auto self_dim_stride,
const auto* index_data, auto index_dim_stride,
const auto* src_data, auto src_dim_stride
) {
for (int64_t i = 0; i < index_dim_size; ++i) {
int64_t idx_dim = index_data[i * index_dim_stride];
// we are not putting idx_dim in the error message because it disables
// loop optimizations in clang-7
TORCH_CHECK(idx_dim >= 0 && idx_dim < self_dim_size,
"index ", index_data[i * index_dim_stride], " is out of bounds for dimension ", dim,
" with size ", self_dim_size);
self_data[idx_dim * self_dim_stride] += src_data[i * src_dim_stride];
}
},
/*serial_exec=*/true);
}
} // anonymous namespace
REGISTER_DISPATCH(gather_stub, &gather_cpu_kernel);
REGISTER_DISPATCH(scatter_stub, &scatter_cpu_kernel);
REGISTER_DISPATCH(scatter_fill_stub, &scatter_fill_cpu_kernel);
REGISTER_DISPATCH(scatter_add_stub, &scatter_add_cpu_kernel);
}} // namespace at::native