Spaces:
Running
Running
File size: 17,532 Bytes
6064c9d |
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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
/*
* File : prroi_pooling_gpu_impl.cu
* Author : Tete Xiao, Jiayuan Mao
* Email : jasonhsiao97@gmail.com
*
* Distributed under terms of the MIT license.
* Copyright (c) 2017 Megvii Technology Limited.
*/
#include "prroi_pooling_gpu_impl.cuh"
#include <cstdio>
#include <cfloat>
#define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; \
i < (n); \
i += blockDim.x * gridDim.x)
#define CUDA_POST_KERNEL_CHECK \
do { \
cudaError_t err = cudaGetLastError(); \
if (cudaSuccess != err) { \
fprintf(stderr, "cudaCheckError() failed : %s\n", cudaGetErrorString(err)); \
exit(-1); \
} \
} while(0)
#define CUDA_NUM_THREADS 512
namespace {
static int CUDA_NUM_BLOCKS(const int N) {
return (N + CUDA_NUM_THREADS - 1) / CUDA_NUM_THREADS;
}
__device__ static float PrRoIPoolingGetData(F_DEVPTR_IN data, const int h, const int w, const int height, const int width)
{
bool overflow = (h < 0) || (w < 0) || (h >= height) || (w >= width);
float retVal = overflow ? 0.0f : data[h * width + w];
return retVal;
}
__device__ static float PrRoIPoolingGetCoeff(float dh, float dw){
dw = dw > 0 ? dw : -dw;
dh = dh > 0 ? dh : -dh;
return (1.0f - dh) * (1.0f - dw);
}
__device__ static float PrRoIPoolingSingleCoorIntegral(float s, float t, float c1, float c2) {
return 0.5 * (t * t - s * s) * c2 + (t - 0.5 * t * t - s + 0.5 * s * s) * c1;
}
__device__ static float PrRoIPoolingInterpolation(F_DEVPTR_IN data, const float h, const float w, const int height, const int width){
float retVal = 0.0f;
int h1 = floorf(h);
int w1 = floorf(w);
retVal += PrRoIPoolingGetData(data, h1, w1, height, width) * PrRoIPoolingGetCoeff(h - float(h1), w - float(w1));
h1 = floorf(h)+1;
w1 = floorf(w);
retVal += PrRoIPoolingGetData(data, h1, w1, height, width) * PrRoIPoolingGetCoeff(h - float(h1), w - float(w1));
h1 = floorf(h);
w1 = floorf(w)+1;
retVal += PrRoIPoolingGetData(data, h1, w1, height, width) * PrRoIPoolingGetCoeff(h - float(h1), w - float(w1));
h1 = floorf(h)+1;
w1 = floorf(w)+1;
retVal += PrRoIPoolingGetData(data, h1, w1, height, width) * PrRoIPoolingGetCoeff(h - float(h1), w - float(w1));
return retVal;
}
__device__ static float PrRoIPoolingMatCalculation(F_DEVPTR_IN this_data, const int s_h, const int s_w, const int e_h, const int e_w,
const float y0, const float x0, const float y1, const float x1, const int h0, const int w0)
{
float alpha, beta, lim_alpha, lim_beta, tmp;
float sum_out = 0;
alpha = x0 - float(s_w);
beta = y0 - float(s_h);
lim_alpha = x1 - float(s_w);
lim_beta = y1 - float(s_h);
tmp = (lim_alpha - 0.5f * lim_alpha * lim_alpha - alpha + 0.5f * alpha * alpha)
* (lim_beta - 0.5f * lim_beta * lim_beta - beta + 0.5f * beta * beta);
sum_out += PrRoIPoolingGetData(this_data, s_h, s_w, h0, w0) * tmp;
alpha = float(e_w) - x1;
lim_alpha = float(e_w) - x0;
tmp = (lim_alpha - 0.5f * lim_alpha * lim_alpha - alpha + 0.5f * alpha * alpha)
* (lim_beta - 0.5f * lim_beta * lim_beta - beta + 0.5f * beta * beta);
sum_out += PrRoIPoolingGetData(this_data, s_h, e_w, h0, w0) * tmp;
alpha = x0 - float(s_w);
beta = float(e_h) - y1;
lim_alpha = x1 - float(s_w);
lim_beta = float(e_h) - y0;
tmp = (lim_alpha - 0.5f * lim_alpha * lim_alpha - alpha + 0.5f * alpha * alpha)
* (lim_beta - 0.5f * lim_beta * lim_beta - beta + 0.5f * beta * beta);
sum_out += PrRoIPoolingGetData(this_data, e_h, s_w, h0, w0) * tmp;
alpha = float(e_w) - x1;
lim_alpha = float(e_w) - x0;
tmp = (lim_alpha - 0.5f * lim_alpha * lim_alpha - alpha + 0.5f * alpha * alpha)
* (lim_beta - 0.5f * lim_beta * lim_beta - beta + 0.5f * beta * beta);
sum_out += PrRoIPoolingGetData(this_data, e_h, e_w, h0, w0) * tmp;
return sum_out;
}
__device__ static void PrRoIPoolingDistributeDiff(F_DEVPTR_OUT diff, const float top_diff, const int h, const int w, const int height, const int width, const float coeff)
{
bool overflow = (h < 0) || (w < 0) || (h >= height) || (w >= width);
if (!overflow)
atomicAdd(diff + h * width + w, top_diff * coeff);
}
__device__ static void PrRoIPoolingMatDistributeDiff(F_DEVPTR_OUT diff, const float top_diff, const int s_h, const int s_w, const int e_h, const int e_w,
const float y0, const float x0, const float y1, const float x1, const int h0, const int w0)
{
float alpha, beta, lim_alpha, lim_beta, tmp;
alpha = x0 - float(s_w);
beta = y0 - float(s_h);
lim_alpha = x1 - float(s_w);
lim_beta = y1 - float(s_h);
tmp = (lim_alpha - 0.5f * lim_alpha * lim_alpha - alpha + 0.5f * alpha * alpha)
* (lim_beta - 0.5f * lim_beta * lim_beta - beta + 0.5f * beta * beta);
PrRoIPoolingDistributeDiff(diff, top_diff, s_h, s_w, h0, w0, tmp);
alpha = float(e_w) - x1;
lim_alpha = float(e_w) - x0;
tmp = (lim_alpha - 0.5f * lim_alpha * lim_alpha - alpha + 0.5f * alpha * alpha)
* (lim_beta - 0.5f * lim_beta * lim_beta - beta + 0.5f * beta * beta);
PrRoIPoolingDistributeDiff(diff, top_diff, s_h, e_w, h0, w0, tmp);
alpha = x0 - float(s_w);
beta = float(e_h) - y1;
lim_alpha = x1 - float(s_w);
lim_beta = float(e_h) - y0;
tmp = (lim_alpha - 0.5f * lim_alpha * lim_alpha - alpha + 0.5f * alpha * alpha)
* (lim_beta - 0.5f * lim_beta * lim_beta - beta + 0.5f * beta * beta);
PrRoIPoolingDistributeDiff(diff, top_diff, e_h, s_w, h0, w0, tmp);
alpha = float(e_w) - x1;
lim_alpha = float(e_w) - x0;
tmp = (lim_alpha - 0.5f * lim_alpha * lim_alpha - alpha + 0.5f * alpha * alpha)
* (lim_beta - 0.5f * lim_beta * lim_beta - beta + 0.5f * beta * beta);
PrRoIPoolingDistributeDiff(diff, top_diff, e_h, e_w, h0, w0, tmp);
}
__global__ void PrRoIPoolingForward(
const int nthreads,
F_DEVPTR_IN bottom_data,
F_DEVPTR_IN bottom_rois,
F_DEVPTR_OUT top_data,
const int channels,
const int height,
const int width,
const int pooled_height,
const int pooled_width,
const float spatial_scale) {
CUDA_KERNEL_LOOP(index, nthreads) {
// (n, c, ph, pw) is an element in the pooled output
int pw = index % pooled_width;
int ph = (index / pooled_width) % pooled_height;
int c = (index / pooled_width / pooled_height) % channels;
int n = index / pooled_width / pooled_height / channels;
bottom_rois += n * 5;
int roi_batch_ind = bottom_rois[0];
float roi_start_w = bottom_rois[1] * spatial_scale;
float roi_start_h = bottom_rois[2] * spatial_scale;
float roi_end_w = bottom_rois[3] * spatial_scale;
float roi_end_h = bottom_rois[4] * spatial_scale;
float roi_width = max(roi_end_w - roi_start_w, ((float)0.0));
float roi_height = max(roi_end_h - roi_start_h, ((float)0.0));
float bin_size_h = roi_height / static_cast<float>(pooled_height);
float bin_size_w = roi_width / static_cast<float>(pooled_width);
const float *this_data = bottom_data + (roi_batch_ind * channels + c) * height * width;
float *this_out = top_data + index;
float win_start_w = roi_start_w + bin_size_w * pw;
float win_start_h = roi_start_h + bin_size_h * ph;
float win_end_w = win_start_w + bin_size_w;
float win_end_h = win_start_h + bin_size_h;
float win_size = max(float(0.0), bin_size_w * bin_size_h);
if (win_size == 0) {
*this_out = 0;
return;
}
float sum_out = 0;
int s_w, s_h, e_w, e_h;
s_w = floorf(win_start_w);
e_w = ceilf(win_end_w);
s_h = floorf(win_start_h);
e_h = ceilf(win_end_h);
for (int w_iter = s_w; w_iter < e_w; ++w_iter)
for (int h_iter = s_h; h_iter < e_h; ++h_iter)
sum_out += PrRoIPoolingMatCalculation(this_data, h_iter, w_iter, h_iter + 1, w_iter + 1,
max(win_start_h, float(h_iter)), max(win_start_w, float(w_iter)),
min(win_end_h, float(h_iter) + 1.0), min(win_end_w, float(w_iter + 1.0)),
height, width);
*this_out = sum_out / win_size;
}
}
__global__ void PrRoIPoolingBackward(
const int nthreads,
F_DEVPTR_IN bottom_rois,
F_DEVPTR_IN top_diff,
F_DEVPTR_OUT bottom_diff,
const int channels,
const int height,
const int width,
const int pooled_height,
const int pooled_width,
const float spatial_scale) {
CUDA_KERNEL_LOOP(index, nthreads) {
// (n, c, ph, pw) is an element in the pooled output
int pw = index % pooled_width;
int ph = (index / pooled_width) % pooled_height;
int c = (index / pooled_width / pooled_height) % channels;
int n = index / pooled_width / pooled_height / channels;
bottom_rois += n * 5;
int roi_batch_ind = bottom_rois[0];
float roi_start_w = bottom_rois[1] * spatial_scale;
float roi_start_h = bottom_rois[2] * spatial_scale;
float roi_end_w = bottom_rois[3] * spatial_scale;
float roi_end_h = bottom_rois[4] * spatial_scale;
float roi_width = max(roi_end_w - roi_start_w, (float)0);
float roi_height = max(roi_end_h - roi_start_h, (float)0);
float bin_size_h = roi_height / static_cast<float>(pooled_height);
float bin_size_w = roi_width / static_cast<float>(pooled_width);
const float *this_out_grad = top_diff + index;
float *this_data_grad = bottom_diff + (roi_batch_ind * channels + c) * height * width;
float win_start_w = roi_start_w + bin_size_w * pw;
float win_start_h = roi_start_h + bin_size_h * ph;
float win_end_w = win_start_w + bin_size_w;
float win_end_h = win_start_h + bin_size_h;
float win_size = max(float(0.0), bin_size_w * bin_size_h);
float sum_out = win_size == float(0) ? float(0) : *this_out_grad / win_size;
int s_w, s_h, e_w, e_h;
s_w = floorf(win_start_w);
e_w = ceilf(win_end_w);
s_h = floorf(win_start_h);
e_h = ceilf(win_end_h);
for (int w_iter = s_w; w_iter < e_w; ++w_iter)
for (int h_iter = s_h; h_iter < e_h; ++h_iter)
PrRoIPoolingMatDistributeDiff(this_data_grad, sum_out, h_iter, w_iter, h_iter + 1, w_iter + 1,
max(win_start_h, float(h_iter)), max(win_start_w, float(w_iter)),
min(win_end_h, float(h_iter) + 1.0), min(win_end_w, float(w_iter + 1.0)),
height, width);
}
}
__global__ void PrRoIPoolingCoorBackward(
const int nthreads,
F_DEVPTR_IN bottom_data,
F_DEVPTR_IN bottom_rois,
F_DEVPTR_IN top_data,
F_DEVPTR_IN top_diff,
F_DEVPTR_OUT bottom_diff,
const int channels,
const int height,
const int width,
const int pooled_height,
const int pooled_width,
const float spatial_scale) {
CUDA_KERNEL_LOOP(index, nthreads) {
// (n, c, ph, pw) is an element in the pooled output
int pw = index % pooled_width;
int ph = (index / pooled_width) % pooled_height;
int c = (index / pooled_width / pooled_height) % channels;
int n = index / pooled_width / pooled_height / channels;
bottom_rois += n * 5;
int roi_batch_ind = bottom_rois[0];
float roi_start_w = bottom_rois[1] * spatial_scale;
float roi_start_h = bottom_rois[2] * spatial_scale;
float roi_end_w = bottom_rois[3] * spatial_scale;
float roi_end_h = bottom_rois[4] * spatial_scale;
float roi_width = max(roi_end_w - roi_start_w, (float)0);
float roi_height = max(roi_end_h - roi_start_h, (float)0);
float bin_size_h = roi_height / static_cast<float>(pooled_height);
float bin_size_w = roi_width / static_cast<float>(pooled_width);
const float *this_out_grad = top_diff + index;
const float *this_bottom_data = bottom_data + (roi_batch_ind * channels + c) * height * width;
const float *this_top_data = top_data + index;
float *this_data_grad = bottom_diff + n * 5;
float win_start_w = roi_start_w + bin_size_w * pw;
float win_start_h = roi_start_h + bin_size_h * ph;
float win_end_w = win_start_w + bin_size_w;
float win_end_h = win_start_h + bin_size_h;
float win_size = max(float(0.0), bin_size_w * bin_size_h);
float sum_out = win_size == float(0) ? float(0) : *this_out_grad / win_size;
// WARNING: to be discussed
if (sum_out == 0)
return;
int s_w, s_h, e_w, e_h;
s_w = floorf(win_start_w);
e_w = ceilf(win_end_w);
s_h = floorf(win_start_h);
e_h = ceilf(win_end_h);
float g_x1_y = 0, g_x2_y = 0, g_x_y1 = 0, g_x_y2 = 0;
for (int h_iter = s_h; h_iter < e_h; ++h_iter) {
g_x1_y += PrRoIPoolingSingleCoorIntegral(max(win_start_h, float(h_iter)) - h_iter,
min(win_end_h, float(h_iter + 1)) - h_iter,
PrRoIPoolingInterpolation(this_bottom_data, h_iter, win_start_w, height, width),
PrRoIPoolingInterpolation(this_bottom_data, h_iter + 1, win_start_w, height, width));
g_x2_y += PrRoIPoolingSingleCoorIntegral(max(win_start_h, float(h_iter)) - h_iter,
min(win_end_h, float(h_iter + 1)) - h_iter,
PrRoIPoolingInterpolation(this_bottom_data, h_iter, win_end_w, height, width),
PrRoIPoolingInterpolation(this_bottom_data, h_iter + 1, win_end_w, height, width));
}
for (int w_iter = s_w; w_iter < e_w; ++w_iter) {
g_x_y1 += PrRoIPoolingSingleCoorIntegral(max(win_start_w, float(w_iter)) - w_iter,
min(win_end_w, float(w_iter + 1)) - w_iter,
PrRoIPoolingInterpolation(this_bottom_data, win_start_h, w_iter, height, width),
PrRoIPoolingInterpolation(this_bottom_data, win_start_h, w_iter + 1, height, width));
g_x_y2 += PrRoIPoolingSingleCoorIntegral(max(win_start_w, float(w_iter)) - w_iter,
min(win_end_w, float(w_iter + 1)) - w_iter,
PrRoIPoolingInterpolation(this_bottom_data, win_end_h, w_iter, height, width),
PrRoIPoolingInterpolation(this_bottom_data, win_end_h, w_iter + 1, height, width));
}
float partial_x1 = -g_x1_y + (win_end_h - win_start_h) * (*this_top_data);
float partial_y1 = -g_x_y1 + (win_end_w - win_start_w) * (*this_top_data);
float partial_x2 = g_x2_y - (win_end_h - win_start_h) * (*this_top_data);
float partial_y2 = g_x_y2 - (win_end_w - win_start_w) * (*this_top_data);
partial_x1 = partial_x1 / win_size * spatial_scale;
partial_x2 = partial_x2 / win_size * spatial_scale;
partial_y1 = partial_y1 / win_size * spatial_scale;
partial_y2 = partial_y2 / win_size * spatial_scale;
// (b, x1, y1, x2, y2)
this_data_grad[0] = 0;
atomicAdd(this_data_grad + 1, (partial_x1 * (1.0 - float(pw) / pooled_width) + partial_x2 * (1.0 - float(pw + 1) / pooled_width))
* (*this_out_grad));
atomicAdd(this_data_grad + 2, (partial_y1 * (1.0 - float(ph) / pooled_height) + partial_y2 * (1.0 - float(ph + 1) / pooled_height))
* (*this_out_grad));
atomicAdd(this_data_grad + 3, (partial_x2 * float(pw + 1) / pooled_width + partial_x1 * float(pw) / pooled_width)
* (*this_out_grad));
atomicAdd(this_data_grad + 4, (partial_y2 * float(ph + 1) / pooled_height + partial_y1 * float(ph) / pooled_height)
* (*this_out_grad));
}
}
} /* !anonymous namespace */
#ifdef __cplusplus
extern "C" {
#endif
void PrRoIPoolingForwardGpu(
cudaStream_t stream,
F_DEVPTR_IN bottom_data,
F_DEVPTR_IN bottom_rois,
F_DEVPTR_OUT top_data,
const int channels_, const int height_, const int width_,
const int pooled_height_, const int pooled_width_,
const float spatial_scale_,
const int top_count) {
PrRoIPoolingForward<<<CUDA_NUM_BLOCKS(top_count), CUDA_NUM_THREADS, 0, stream>>>(
top_count, bottom_data, bottom_rois, top_data,
channels_, height_, width_, pooled_height_, pooled_width_, spatial_scale_);
CUDA_POST_KERNEL_CHECK;
}
void PrRoIPoolingBackwardGpu(
cudaStream_t stream,
F_DEVPTR_IN bottom_data,
F_DEVPTR_IN bottom_rois,
F_DEVPTR_IN top_data,
F_DEVPTR_IN top_diff,
F_DEVPTR_OUT bottom_diff,
const int channels_, const int height_, const int width_,
const int pooled_height_, const int pooled_width_,
const float spatial_scale_,
const int top_count, const int bottom_count) {
cudaMemsetAsync(bottom_diff, 0, sizeof(float) * bottom_count, stream);
PrRoIPoolingBackward<<<CUDA_NUM_BLOCKS(top_count), CUDA_NUM_THREADS, 0, stream>>>(
top_count, bottom_rois, top_diff, bottom_diff,
channels_, height_, width_, pooled_height_, pooled_width_, spatial_scale_);
CUDA_POST_KERNEL_CHECK;
}
void PrRoIPoolingCoorBackwardGpu(
cudaStream_t stream,
F_DEVPTR_IN bottom_data,
F_DEVPTR_IN bottom_rois,
F_DEVPTR_IN top_data,
F_DEVPTR_IN top_diff,
F_DEVPTR_OUT bottom_diff,
const int channels_, const int height_, const int width_,
const int pooled_height_, const int pooled_width_,
const float spatial_scale_,
const int top_count, const int bottom_count) {
cudaMemsetAsync(bottom_diff, 0, sizeof(float) * bottom_count, stream);
PrRoIPoolingCoorBackward<<<CUDA_NUM_BLOCKS(top_count), CUDA_NUM_THREADS, 0, stream>>>(
top_count, bottom_data, bottom_rois, top_data, top_diff, bottom_diff,
channels_, height_, width_, pooled_height_, pooled_width_, spatial_scale_);
CUDA_POST_KERNEL_CHECK;
}
} /* !extern "C" */
|