-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathupsample_wsl_op.cu
258 lines (230 loc) · 7.29 KB
/
upsample_wsl_op.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
#include "caffe2/core/context_gpu.h"
#include "upsample_wsl_op.h"
#include "caffe2/utils/math.h"
namespace caffe2 {
namespace {
inline __device__ 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
__global__ void UpsampleBilinearKernel(
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 float height_scale,
const float width_scale,
const float* X,
float* Y) {
CUDA_1D_KERNEL_LOOP(index, output_size) {
int indexTemp = index;
const int out_x = indexTemp % output_width;
indexTemp /= output_width;
const int out_y = indexTemp % output_height;
indexTemp /= output_height;
const int c = indexTemp % num_channels;
indexTemp /= num_channels;
const int n = indexTemp;
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;
// Compute Y axis lambdas
const float h1r = rheight * out_y;
const int h1 = (int)h1r;
const int h1p = (h1 < input_height - 1) ? 1 : 0;
const float h1lambda = h1r - h1;
const float h0lambda = 1.f - h1lambda;
// Compute X axis lambdas
const float w1r = rwidth * out_x;
const int w1 = (int)w1r;
const int w1p = (w1 < input_width - 1) ? 1 : 0;
const float w1lambda = w1r - w1;
const float w0lambda = 1.f - w1lambda;
Y[index] =
(h0lambda *
(w0lambda *
X[idx(
n, num_channels, c, input_height, input_width, h1, w1)] +
w1lambda *
X[idx(
n,
num_channels,
c,
input_height,
input_width,
h1,
w1 + w1p)]) +
h1lambda *
(w0lambda *
X[idx(
n,
num_channels,
c,
input_height,
input_width,
h1 + h1p,
w1)] +
w1lambda *
X[idx(
n,
num_channels,
c,
input_height,
input_width,
h1 + h1p,
w1 + w1p)]));
}
}
// input is dY, output is dX
__global__ void UpsampleBilinearGradientKernel(
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 float height_scale,
const float width_scale,
const float* dY,
float* dX) {
CUDA_1D_KERNEL_LOOP(index, input_size) {
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 = (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 = (int)w1r;
const int w1p = (w1 < output_width - 1) ? 1 : 0;
const float w1lambda = w1r - w1;
const float w0lambda = 1.f - w1lambda;
#if __CUDA_ARCH__ >= 350
const float dYi = __ldg(&dY[index]);
#else
const float dYi = dY[index];
#endif
atomicAdd(
&dX[idx(n, num_channels, c, output_height, output_width, h1, w1)],
h0lambda * w0lambda * dYi);
atomicAdd(
&dX[idx(n, num_channels, c, output_height, output_width, h1, w1 + w1p)],
h0lambda * w1lambda * dYi);
atomicAdd(
&dX[idx(n, num_channels, c, output_height, output_width, h1 + h1p, w1)],
h1lambda * w0lambda * dYi);
atomicAdd(
&dX[idx(
n,
num_channels,
c,
output_height,
output_width,
h1 + h1p,
w1 + w1p)],
h1lambda * w1lambda * dYi);
}
}
} // namespace
template <>
bool UpsampleBilinearWSLOp<float, CUDAContext>::RunOnDevice() {
const auto& X = Input(0);
const auto& Z = Input(1);
auto* Y = Output(0);
const int batch_size = X.dim32(0), num_channels = X.dim32(1),
input_height = X.dim32(2), input_width = X.dim32(3);
//int output_width = input_width * width_scale_;
//int output_height = input_height * height_scale_;
int output_width = Z.dim32(3);
int output_height = Z.dim32(2);
width_scale_ = output_width / input_width;
height_scale_ = output_height / input_height;
Y->Resize(batch_size, num_channels, output_height, output_width);
const auto size = Y->numel();
UpsampleBilinearKernel<<<
CAFFE_GET_BLOCKS(size),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(
size,
num_channels,
input_height,
input_width,
output_height,
output_width,
height_scale_,
width_scale_,
X.data<float>(),
Y->template mutable_data<float>());
return true;
}
template <>
bool UpsampleBilinearWSLGradientOp<float, CUDAContext>::RunOnDevice() {
const auto& dY = Input(0);
const auto& X = Input(1);
auto* dX = Output(0);
const auto inputDims = dY.dims();
CAFFE_ENFORCE_EQ(4, inputDims.size());
const int batch_size = dY.dim32(0);
const int num_channels = dY.dim32(1);
const int input_height = dY.dim32(2);
const int input_width = dY.dim32(3);
const int output_height = X.dim32(2);
const int output_width = X.dim32(3);
width_scale_ = input_width / output_width;
height_scale_ = input_height / output_height;
dX->Resize(batch_size, num_channels, output_height, output_width);
math::Set<float, CUDAContext>(
dX->numel(), 0.0f, dX->mutable_data<float>(), &context_);
const auto size = dY.numel();
UpsampleBilinearGradientKernel<<<
CAFFE_GET_BLOCKS(size),
CAFFE_CUDA_NUM_THREADS,
0,
context_.cuda_stream()>>>(
size,
num_channels,
input_height,
input_width,
output_height,
output_width,
height_scale_,
width_scale_,
dY.data<float>(),
dX->template mutable_data<float>());
return true;
}
REGISTER_CUDA_OPERATOR(
UpsampleBilinearWSL,
UpsampleBilinearWSLOp<float, CUDAContext>);
REGISTER_CUDA_OPERATOR(
UpsampleBilinearWSLGradient,
UpsampleBilinearWSLGradientOp<float, CUDAContext>);
} // namespace caffe2