-
Notifications
You must be signed in to change notification settings - Fork 3
/
UpSampleKernel.cpp
408 lines (359 loc) · 15.2 KB
/
UpSampleKernel.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <ATen/ATen.h>
#include <ATen/Dispatch.h>
#include <ATen/native/UpSample.h>
#include <ATen/Parallel.h>
#include <ATen/cpu/vec256/vec256.h>
namespace at {
namespace native {
namespace {
template <typename T>
inline T data_index_init(T offset) {
return offset;
}
template <typename T, typename... Args>
inline T data_index_init(T offset, T &x, const T &X, Args &&... args) {
offset = data_index_init(offset, std::forward<Args>(args)...);
x = offset % X;
return offset / X;
}
inline bool data_index_step() {
return true;
}
template <typename T, typename... Args>
inline bool data_index_step(T &x, const T &X, Args &&... args) {
if (data_index_step(std::forward<Args>(args)...)) {
x = ((x + 1) == X) ? 0 : (x + 1);
return x == 0;
}
return false;
}
static inline int64_t nearest_idx(
int64_t output_index,
int64_t input_size,
int64_t output_size,
c10::optional<double> scales) {
if (output_size == input_size) {
// scale_factor = 1, simply copy
return output_index;
} else if (output_size == 2 * input_size) {
// scale_factor = 2, shift input index
return output_index >> 1;
} else {
float scale = compute_scales_value<float>(scales, input_size, output_size);
return nearest_neighbor_compute_source_index(scale, output_index, input_size);
}
}
template <typename scalar_t, typename scale_type>
void cpu_upsample_nearest(
Tensor& output_,
const Tensor& input_,
const scale_type& scales) {
TORCH_CHECK(input_.dtype() == output_.dtype(), "expected dtype ", input_.dtype(),
" for `output` but got dtype ", output_.dtype());
auto input = input_.contiguous();
auto output = output_.contiguous();
auto input_data = input.data_ptr<scalar_t>();
auto output_data = output.data_ptr<scalar_t>();
auto input_sizes = input.sizes().vec();
auto output_sizes = output.sizes().vec();
auto ndim = input_sizes.size();
auto numel = output.numel();
// treat nbatch and channels as one dimension
int64_t channels = input_sizes[0] * input_sizes[1];
int64_t input_depth = (ndim == 5) ? input_sizes[2] : 1;
int64_t output_depth = (ndim == 5) ? output_sizes[2] : 1;
int64_t input_height = (ndim >= 4) ? input_sizes[ndim - 2] : 1;
int64_t output_height = (ndim >= 4) ? output_sizes[ndim - 2] : 1;
int64_t input_width = input_sizes[ndim - 1];
int64_t output_width = output_sizes[ndim - 1];
auto loop1d = [&](int64_t begin, int64_t end) {
int64_t c = 0;
int64_t ow = 0;
data_index_init(begin, c, channels, ow, output_width);
for (int64_t i = begin; i < end; i++) {
int64_t iw = nearest_idx(ow, input_width, output_width, scales[0]);
output_data[i] = input_data[c * input_width + iw];
data_index_step(c, channels, ow, output_width);
}
};
auto loop2d = [&](int64_t begin, int64_t end) {
int64_t c = 0;
int64_t oh = 0;
int64_t ow = 0;
data_index_init(begin, c, channels, oh, output_height, ow, output_width);
for (int64_t i = begin; i < end; i++) {
int64_t ih = nearest_idx(oh, input_height, output_height, scales[0]);
int64_t iw = nearest_idx(ow, input_width, output_width, scales[1]);
output_data[i] = input_data[c * input_height * input_width + ih * input_width + iw];
data_index_step(c, channels, oh, output_height, ow, output_width);
}
};
auto loop3d = [&](int64_t begin, int64_t end) {
int64_t c = 0;
int64_t od = 0;
int64_t oh = 0;
int64_t ow = 0;
data_index_init(begin, c, channels, od, output_depth, oh, output_height, ow, output_width);
for (int64_t i = begin; i < end; i++) {
int64_t id = nearest_idx(od, input_depth, output_depth, scales[0]);
int64_t ih = nearest_idx(oh, input_height, output_height, scales[1]);
int64_t iw = nearest_idx(ow, input_width, output_width, scales[2]);
int64_t j = c * input_depth * input_height * input_width +
id * input_height * input_width + ih * input_width + iw;
output_data[i] = input_data[j];
data_index_step(c, channels, od, output_depth, oh, output_height, ow, output_width);
}
};
if (ndim == 3) {
// upsample nearest 1d
at::parallel_for(0, numel, at::internal::GRAIN_SIZE, loop1d);
} else if (ndim == 4) {
// upsample nearest 2d
at::parallel_for(0, numel, at::internal::GRAIN_SIZE, loop2d);
} else {
// upsample nearest 3d
TORCH_INTERNAL_ASSERT(ndim == 5);
at::parallel_for(0, numel, at::internal::GRAIN_SIZE, loop3d);
}
if (!output_.is_contiguous()) {
output_.copy_(output);
}
}
template <typename scalar_t, typename scale_type>
void cpu_upsample_nearest_channels_last(
Tensor& output_,
const Tensor& input_,
const scale_type& scales) {
TORCH_CHECK(input_.dtype() == output_.dtype(), "expected dtype ", input_.dtype(),
" for `output` but got dtype ", output_.dtype());
auto input_sizes = input_.sizes().vec();
auto output_sizes = output_.sizes().vec();
auto ndim = input_sizes.size();
TORCH_CHECK(ndim >=4 && ndim <= 5, "Upsample with NHWC format supports tensors with 4 or 5 dims.")
auto channels_last_memory_format = ndim == 4 ? at::MemoryFormat::ChannelsLast : at::MemoryFormat::ChannelsLast3d;
auto input = input_.contiguous(channels_last_memory_format);
auto output = output_.contiguous(channels_last_memory_format);
auto input_data = input.data_ptr<scalar_t>();
auto output_data = output.data_ptr<scalar_t>();
int64_t num_batches = input_sizes[0];
int64_t channels = input_sizes[1];
int64_t input_depth = (ndim == 5) ? input_sizes[2] : 1;
int64_t output_depth = (ndim == 5) ? output_sizes[2] : 1;
int64_t input_height = (ndim >= 4) ? input_sizes[ndim - 2] : 1;
int64_t output_height = (ndim >= 4) ? output_sizes[ndim - 2] : 1;
int64_t input_width = input_sizes[ndim - 1];
int64_t output_width = output_sizes[ndim - 1];
int64_t numel = output.numel();
TORCH_CHECK(channels > 0, "expected input and output channels greater than 0 but got ", channels);
using Vec = vec256::Vec256<scalar_t>;
auto copy = [](scalar_t* out, scalar_t* in, int64_t size) {
int64_t d = 0;
for (; d < size - (size % Vec::size()); d += Vec::size()) {
Vec out_vec = Vec::loadu(in + d);
out_vec.store(out + d);
}
for (; d < size; d++) {
out[d] = in[d];
}
};
auto loop2d = [&](int64_t begin, int64_t end) {
int64_t n = 0;
int64_t oh = 0;
int64_t ow = 0;
data_index_init(begin, n, num_batches, oh, output_height, ow, output_width);
for (int64_t i = begin; i < end; i++) {
int64_t ih = nearest_idx(oh, input_height, output_height, scales[0]);
int64_t iw = nearest_idx(ow, input_width, output_width, scales[1]);
scalar_t* output_ptr = output_data + i * channels;
scalar_t* input_ptr = input_data + n * input_height * input_width * channels +
ih * input_width * channels + iw * channels;
copy(output_ptr, input_ptr, channels);
data_index_step(n, num_batches, oh, output_height, ow, output_width);
}
};
auto loop3d = [&](int64_t begin, int64_t end) {
int64_t n = 0;
int64_t od = 0;
int64_t oh = 0;
int64_t ow = 0;
data_index_init(begin, n, num_batches, od, output_depth, oh, output_height, ow, output_width);
for (int64_t i = begin; i < end; i++) {
int64_t id = nearest_idx(od, input_depth, output_depth, scales[0]);
int64_t ih = nearest_idx(oh, input_height, output_height, scales[1]);
int64_t iw = nearest_idx(ow, input_width, output_width, scales[2]);
scalar_t* output_ptr = output_data + i * channels;
scalar_t* input_ptr = input_data + n * input_depth * input_height * input_width * channels +
id * input_height * input_width * channels +
ih * input_width * channels + iw * channels;
copy(output_ptr, input_ptr, channels);
data_index_step(n, num_batches, od, output_depth, oh, output_height, ow, output_width);
}
};
if (ndim == 4) {
// upsample nearest 2d
at::parallel_for(0, numel / channels, at::internal::GRAIN_SIZE / channels, loop2d);
} else {
// upsample nearest 3d
TORCH_INTERNAL_ASSERT(ndim == 5);
at::parallel_for(0, numel / channels, at::internal::GRAIN_SIZE / channels, loop3d);
}
if (!output_.is_contiguous(channels_last_memory_format)) {
output_.copy_(output);
}
}
template <typename scalar_t, typename scale_type>
void cpu_upsample_nearest_backward(
Tensor& grad_input_,
const Tensor& grad_output_,
const scale_type& scales) {
TORCH_CHECK(grad_input_.dtype() == grad_output_.dtype(), "expected dtype ", grad_output_.dtype(),
" for `grad_input` but got dtype ", grad_input_.dtype());
auto grad_output = grad_output_.contiguous();
auto grad_input = grad_input_.contiguous();
auto grad_output_data = grad_output.data_ptr<scalar_t>();
auto grad_input_data = grad_input.data_ptr<scalar_t>();
auto input_sizes = grad_input.sizes().vec();
auto output_sizes = grad_output.sizes().vec();
auto ndim = input_sizes.size();
// treat nbatch and channels as one dimension
int64_t channels = input_sizes[0] * input_sizes[1];
int64_t input_depth = (ndim == 5) ? input_sizes[2] : 1;
int64_t output_depth = (ndim == 5) ? output_sizes[2] : 1;
int64_t input_height = (ndim >= 4) ? input_sizes[ndim - 2] : 1;
int64_t output_height = (ndim >= 4) ? output_sizes[ndim - 2] : 1;
int64_t input_width = input_sizes[ndim - 1];
int64_t output_width = output_sizes[ndim - 1];
int64_t output_slice_size = output_depth * output_height * output_width;
int64_t input_slice_size = input_depth * input_height * input_width;
auto loop1d = [&](int64_t begin, int64_t end) {
for (int64_t c = begin; c < end; c++){
for (int64_t ow = 0; ow < output_width; ow++) {
int64_t iw = nearest_idx(ow, input_width, output_width, scales[0]);
int64_t output_offset = c * output_slice_size + ow;
int64_t input_offset = c * input_slice_size + iw;
grad_input_data[input_offset] += grad_output_data[output_offset];
}
}
};
auto loop2d = [&](int64_t begin, int64_t end) {
for (int64_t c = begin; c < end; c++) {
for (int64_t oh = 0; oh < output_height; oh++) {
int64_t ih = nearest_idx(oh, input_height, output_height, scales[0]);
for (int64_t ow = 0; ow < output_width; ow++) {
int64_t iw = nearest_idx(ow, input_width, output_width, scales[1]);
int64_t output_offset = c * output_slice_size + oh * output_width + ow;
int64_t input_offset = c * input_slice_size + ih * input_width + iw;
grad_input_data[input_offset] += grad_output_data[output_offset];
}
}
}
};
auto loop3d = [&](int64_t begin, int64_t end) {
for (int64_t c = begin; c < end; c++) {
for (int64_t od = 0; od < output_depth; od++) {
int64_t id = nearest_idx(od, input_depth, output_depth, scales[0]);
for (int64_t oh = 0; oh < output_height; oh++) {
int64_t ih = nearest_idx(oh, input_height, output_height, scales[1]);
for (int64_t ow = 0; ow < output_width; ow++) {
int64_t iw = nearest_idx(ow, input_width, output_width, scales[2]);
int64_t output_offset = c * output_slice_size +
od * output_height * output_width + oh * output_width + ow;
int64_t input_offset = c * input_slice_size +
id * input_height * input_width + ih * input_width + iw;
grad_input_data[input_offset] += grad_output_data[output_offset];
}
}
}
}
};
if (ndim == 3) {
// upsample nearest 1d
at::parallel_for(0, channels, at::internal::GRAIN_SIZE / output_slice_size, loop1d);
} else if (ndim == 4) {
// upsample nearest 2d
at::parallel_for(0, channels, at::internal::GRAIN_SIZE / output_slice_size , loop2d);
} else {
// upsample nearest 3d
TORCH_INTERNAL_ASSERT(ndim == 5);
at::parallel_for(0, channels, at::internal::GRAIN_SIZE / output_slice_size, loop3d);
}
if (!grad_input_.is_contiguous()) {
grad_input_.copy_(grad_input);
}
}
using scale_t = std::vector<c10::optional<double>>;
void upsample_nearest1d_kernel_impl(
Tensor& output,
const Tensor& input,
c10::optional<double> scales_w) {
AT_DISPATCH_FLOATING_TYPES_AND(at::ScalarType::Byte, input.scalar_type(), "upsample_nearest1d", [&] {
cpu_upsample_nearest<scalar_t, scale_t>(output, input, {scales_w});
});
}
void upsample_nearest2d_kernel_impl(
Tensor& output,
const Tensor& input,
c10::optional<double> scales_h,
c10::optional<double> scales_w) {
if (input.is_contiguous(at::MemoryFormat::ChannelsLast)) {
AT_DISPATCH_FLOATING_TYPES_AND(at::ScalarType::Byte, input.scalar_type(), "upsample_nearest2d_channels_last", [&] {
cpu_upsample_nearest_channels_last<scalar_t, scale_t>(output, input, {scales_h, scales_w});
});
} else {
AT_DISPATCH_FLOATING_TYPES_AND(at::ScalarType::Byte, input.scalar_type(), "upsample_nearest2d", [&] {
cpu_upsample_nearest<scalar_t, scale_t>(output, input, {scales_h, scales_w});
});
}
}
void upsample_nearest3d_kernel_impl(
Tensor& output,
const Tensor& input,
c10::optional<double> scales_d,
c10::optional<double> scales_h,
c10::optional<double> scales_w) {
if (input.is_contiguous(at::MemoryFormat::ChannelsLast3d)) {
AT_DISPATCH_FLOATING_TYPES_AND(at::ScalarType::Byte, input.scalar_type(), "upsample_nearest3d_channels_last", [&] {
cpu_upsample_nearest_channels_last<scalar_t, scale_t>(output, input, {scales_d, scales_h, scales_w});
});
} else {
AT_DISPATCH_FLOATING_TYPES_AND(at::ScalarType::Byte, input.scalar_type(), "upsample_nearest3d", [&] {
cpu_upsample_nearest<scalar_t, scale_t>(output, input, {scales_d, scales_h, scales_w});
});
}
}
void upsample_nearest1d_backward_kernel_impl(
Tensor& grad_input,
const Tensor& grad_output,
c10::optional<double> scales_w) {
AT_DISPATCH_FLOATING_TYPES(grad_output.scalar_type(), "upsample_nearest1d_backward", [&] {
cpu_upsample_nearest_backward<scalar_t, scale_t>(grad_input, grad_output, {scales_w});
});
}
void upsample_nearest2d_backward_kernel_impl(
Tensor& grad_input,
const Tensor& grad_output,
c10::optional<double> scales_h,
c10::optional<double> scales_w) {
AT_DISPATCH_FLOATING_TYPES(grad_output.scalar_type(), "upsample_nearest2d_backward", [&] {
cpu_upsample_nearest_backward<scalar_t, scale_t>(grad_input, grad_output, {scales_h, scales_w});
});
}
void upsample_nearest3d_backward_kernel_impl(
Tensor& grad_input,
const Tensor& grad_output,
c10::optional<double> scales_d,
c10::optional<double> scales_h,
c10::optional<double> scales_w) {
AT_DISPATCH_FLOATING_TYPES(grad_output.scalar_type(), "upsample_nearest3d_backward", [&] {
cpu_upsample_nearest_backward<scalar_t, scale_t>(grad_input, grad_output, {scales_d, scales_h, scales_w});
});
}
} // anonymous namespace
REGISTER_DISPATCH(upsample_nearest1d_kernel, &upsample_nearest1d_kernel_impl);
REGISTER_DISPATCH(upsample_nearest2d_kernel, &upsample_nearest2d_kernel_impl);
REGISTER_DISPATCH(upsample_nearest3d_kernel, &upsample_nearest3d_kernel_impl);
REGISTER_DISPATCH(upsample_nearest1d_backward_kernel, &upsample_nearest1d_backward_kernel_impl);
REGISTER_DISPATCH(upsample_nearest2d_backward_kernel, &upsample_nearest2d_backward_kernel_impl);
REGISTER_DISPATCH(upsample_nearest3d_backward_kernel, &upsample_nearest3d_backward_kernel_impl);
} // namespace native
} // namespace at