-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnv_upsampling_cuda_kernel.cu
333 lines (264 loc) · 11.4 KB
/
nv_upsampling_cuda_kernel.cu
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
#include <type_traits>
#include <ATen/ATen.h>
#include "bilinear.h"
#include <cuda.h>
#include <cuda_runtime.h>
#include <THC/THCAtomics.cuh>
#if __CUDA_ARCH__ >= 350
// Device has __ldg
template <typename T>
__device__ __forceinline__ T __ldg(const T *ptr) {
typedef typename detail::working_array<T>::type aliased;
aliased storage = detail::load_storage<T>::impl(ptr);
return detail::fuse<T>(storage);
}
#else
template <typename T>
__device__ __forceinline__ T __ldg(const T *ptr) {
return *ptr;
}
#endif
template <typename scalar_t, typename std::enable_if<std::is_same<
c10::Half, scalar_t>::value>::type * = nullptr>
__device__ __forceinline__ void fastSpecializedAtomicAdd(scalar_t *tensor,
int index, int numel,
scalar_t value) {
#if ((CUDA_VERSION < 10000) || \
(defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 700)))
atomicAdd(reinterpret_cast<at::Half *>(tensor) + index,
static_cast<at::Half>(value));
#else
if (index % 2 == 0 && index < (numel - 1)) {
__half2 value2;
value2.x = value;
value2.y = __int2half_rz(0);
atomicAdd(reinterpret_cast<__half2 *>(tensor) + index / 2, value2);
} else if (index % 2 == 1) {
__half2 value2;
value2.x = __int2half_rz(0);
value2.y = value;
atomicAdd(reinterpret_cast<__half2 *>(tensor) + index / 2, value2);
} else {
atomicAdd(reinterpret_cast<__half *>(tensor) + index,
static_cast<__half>(value));
}
#endif
}
template <typename scalar_t, typename std::enable_if<!std::is_same<
c10::Half, scalar_t>::value>::type * = nullptr>
__device__ __forceinline__ void fastSpecializedAtomicAdd(scalar_t *tensor,
int index, int numel,
scalar_t value) {
atomicAdd(tensor + index, value);
}
template <class scalar_t>
__device__ __forceinline__ void fastAtomicAdd(scalar_t *__restrict__ tensor,
int index, int numel,
scalar_t value) {
fastSpecializedAtomicAdd(tensor, index, numel, value);
}
__device__ __forceinline__ int idx(const int n, const int num_channels,
const int c, const int height,
const int width, const int y, const int x) {
return ((n * num_channels + c) * height + y) * width + x;
}
// input is X, output is Y
template <typename scalar_t>
__global__ void bilinearForwardKernel(
const int output_size, const int num_channels, const int input_height,
const int input_width, const int output_height, const int output_width,
const scalar_t *const __restrict__ X, scalar_t *const __restrict__ Y) {
const float height_scale = 1.0f * output_height / input_height;
const float width_scale = 1.0f * output_width / input_width;
const int batch_size =
output_size / num_channels / output_height / output_width;
const int index = blockDim.x * blockIdx.x + threadIdx.x;
int indexTemp = index;
const int out_x = indexTemp % output_width;
indexTemp /= output_width;
const int out_y = indexTemp % output_height;
const int in_y = fminf(out_y / height_scale, input_height - 1);
const int in_x = fminf(out_x / width_scale, input_width - 1);
const float rheight =
output_height > 1 ? (input_height - 1.f) / (output_height - 1.f) : 0.f;
const float rwidth =
output_width > 1 ? (input_width - 1.f) / (output_width - 1.f) : 0.f;
const float h1r = rheight * out_y;
const int h1 = static_cast<int>(h1r);
const int h1p = (h1 < input_height - 1) ? 1 : 0;
const float h1lambda = h1r - h1;
const float h0lambda = 1.f - h1lambda;
const float w1r = rwidth * out_x;
const int w1 = static_cast<int>(w1r);
const int w1p = (w1 < input_width - 1) ? 1 : 0;
const float w1lambda = w1r - w1;
const float w0lambda = 1.f - w1lambda;
for (int n = 0; n < batch_size; n++) {
for (int c = 0; c < num_channels; c++) {
Y[idx(n, num_channels, c, output_height, output_width, out_y, out_x)] =
static_cast<scalar_t>(
h0lambda *
(w0lambda * __ldg(&X[idx(n, num_channels, c, input_height,
input_width, h1, w1)]) +
w1lambda * __ldg(&X[idx(n, num_channels, c, input_height,
input_width, h1, w1 + w1p)])) +
h1lambda *
(w0lambda * __ldg(&X[idx(n, num_channels, c, input_height,
input_width, h1 + h1p, w1)]) +
w1lambda * __ldg(&X[idx(n, num_channels, c, input_height,
input_width, h1 + h1p, w1 + w1p)])));
}
}
}
// TODO: Launch this with thread per gradInput instead of gradOutput
// input is dY, output is dX
template <typename scalar_t>
__global__ void bilinearBackwardKernel2(
const int input_size, const int num_channels, const int input_height,
const int input_width, const int output_height, const int output_width,
const scalar_t *const __restrict__ dY, scalar_t *const __restrict__ dX) {
const float height_scale = 1.0f * output_height / input_height;
const float width_scale = 1.0f * output_width / input_width;
const int index = blockDim.x * blockIdx.x + threadIdx.x;
int indexTemp = index;
const int in_x = indexTemp % input_width;
indexTemp /= input_width;
const int in_y = indexTemp % input_height;
indexTemp /= input_height;
const int c = indexTemp % num_channels;
indexTemp /= num_channels;
// const int n = indexTemp;
const int n = 0;
if (index > input_size - 1) {
return;
}
const int dst_idx =
idx(n, num_channels, c, input_height, input_width, in_y, in_x);
// accumulator
float acc = 0.0f;
// TODO: figure out which Ys to loop over
// TODO: figure out what lambdas to use for which Y
const int y_window = ceilf(height_scale);
const int x_window = ceilf(width_scale);
// Not expecting overflow here, static_cast<int>(roundf(x)) is ugly
const int y_base_idx = lroundf(in_y * height_scale);
const int x_base_idx = lroundf(in_x * width_scale);
int ctr = 0;
for (int out_y = y_base_idx;
out_y <= min(y_base_idx + y_window, output_height - 1); out_y++) {
for (int out_x = x_base_idx;
out_x <= min(x_base_idx + x_window, output_width - 1); out_x++) {
ctr += 1;
const int src_idx =
idx(n, num_channels, c, output_height, output_width, out_y, out_x);
// TODO: calculate lambdas for y and x !!
acc += dY[src_idx];
}
}
dX[dst_idx] = static_cast<scalar_t>(acc);
}
// input is dY, output is dX
template <typename scalar_t>
__global__ void bilinearBackwardKernel(
const int input_size, const int num_channels, const int input_height,
const int input_width, const int output_height, const int output_width,
const scalar_t *const __restrict__ dY, scalar_t *const __restrict__ dX) {
const float height_scale = 1.0f * output_height / input_height;
const float width_scale = 1.0f * output_width / input_width;
for (size_t index = blockDim.x * blockIdx.x + threadIdx.x; index < input_size;
index += blockDim.x * gridDim.x) {
int indexTemp = index;
const int in_x = indexTemp % input_width;
indexTemp /= input_width;
const int in_y = indexTemp % input_height;
indexTemp /= input_height;
const int c = indexTemp % num_channels;
indexTemp /= num_channels;
const int n = indexTemp;
const int out_y = fminf(in_y / height_scale, output_height - 1);
const int out_x = fminf(in_x / width_scale, output_width - 1);
const float rheight =
output_height > 1 ? (output_height - 1.f) / (input_height - 1.f) : 0.f;
const float rwidth =
output_width > 1 ? (output_width - 1.f) / (input_width - 1.f) : 0.f;
// Compute Y axis lambdas
const float h1r = rheight * in_y;
const int h1 = static_cast<int>(h1r);
const int h1p = (h1 < output_height - 1) ? 1 : 0;
const float h1lambda = h1r - h1;
const float h0lambda = 1.f - h1lambda;
// Compute X axis lambdas
const float w1r = rwidth * in_x;
const int w1 = static_cast<int>(w1r);
const int w1p = (w1 < output_width - 1) ? 1 : 0;
const float w1lambda = w1r - w1;
const float w0lambda = 1.f - w1lambda;
const scalar_t dYi = __ldg(&dY[index]);
const int out_numel = input_size / (input_height * input_width) *
output_height * output_width;
if (n == 2 && c == 1 && h1 == 12 && w1 == 14) {
int idx0 = idx(n, num_channels, c, output_height, output_width, h1, w1);
printf("n = %d, c = %d, h1 = %d, w1 = %d, dX idx = %d, dY idx = %d\n", n,
c, h1, w1, idx0, (int)index);
}
fastAtomicAdd<scalar_t>(
dX, idx(n, num_channels, c, output_height, output_width, h1, w1),
out_numel, static_cast<scalar_t>(h0lambda * w0lambda * dYi));
fastAtomicAdd<scalar_t>(
dX, idx(n, num_channels, c, output_height, output_width, h1, w1 + w1p),
out_numel, static_cast<scalar_t>(h0lambda * w1lambda * dYi));
fastAtomicAdd<scalar_t>(
dX, idx(n, num_channels, c, output_height, output_width, h1 + h1p, w1),
out_numel, static_cast<scalar_t>(h1lambda * w0lambda * dYi));
fastAtomicAdd<scalar_t>(dX, idx(n, num_channels, c, output_height,
output_width, h1 + h1p, w1 + w1p),
out_numel,
static_cast<scalar_t>(h1lambda * w1lambda * dYi));
}
}
at::Tensor bilinear_cuda_forward(at::Tensor &in, const int new_h,
const int new_w) {
const int nIn = in.size(0);
const int cIn = in.size(1);
const int hIn = in.size(2);
const int wIn = in.size(3);
at::Tensor out = at::empty({nIn, cIn, new_h, new_w}, in.options());
const int outSize = nIn * cIn * new_h * new_w;
const dim3 block(256);
const dim3 grid(((outSize / cIn / nIn) + block.x - 1) / block.x);
AT_DISPATCH_FLOATING_TYPES_AND_HALF(
in.type(), "bilinearForwardKernel", ([&] {
bilinearForwardKernel<scalar_t><<<grid, block>>>(
out.numel(), cIn, hIn, wIn, new_h, new_w, in.data<scalar_t>(),
out.data<scalar_t>());
}));
AT_CHECK(cudaGetLastError() == cudaSuccess,
"issue with bilinearForwardKernel, CUDA code ", cudaGetLastError());
return out;
}
at::Tensor bilinear_cuda_backward(at::Tensor &in, const int out_h,
const int out_w) {
const int nIn = in.size(0);
const int cIn = in.size(1);
const int hIn = in.size(2);
const int wIn = in.size(3);
at::Tensor out = at::empty({nIn, cIn, out_h, out_w}, in.options());
/*
const int outSize = nIn * cIn * out_h * out_w;
const dim3 block(256);
const dim3 grid((outSize + block.x - 1) / block.x);
*/
const int inSize = nIn * cIn * hIn * wIn;
const dim3 block(256);
const dim3 grid((inSize + block.x - 1) / block.x);
AT_DISPATCH_FLOATING_TYPES_AND_HALF(
in.type(), "bilinearBackwardKernel", ([&] {
// bilinearBackwardKernel2
bilinearBackwardKernel<scalar_t><<<grid, block>>>(
in.numel(), cIn, hIn, wIn, out_h, out_w, in.data<scalar_t>(),
out.data<scalar_t>());
}));
AT_CHECK(cudaGetLastError() == cudaSuccess,
"issue with bilinearForwardKernel, CUDA code ", cudaGetLastError());
return out;
}