|
|
|
@ -30,11 +30,13 @@ static inline int NumBlocks(const int N) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
__global__ void GPUROIPoolForward(
|
|
|
|
|
const int nthreads, const T* input_data, const int64_t* input_rois,
|
|
|
|
|
const float spatial_scale, const int channels, const int height,
|
|
|
|
|
const int width, const int pooled_height, const int pooled_width,
|
|
|
|
|
T* output_data, int64_t* argmax_data) {
|
|
|
|
|
__global__ void GPUROIPoolForward(const int nthreads, const T* input_data,
|
|
|
|
|
const int64_t* input_rois,
|
|
|
|
|
const float spatial_scale, const int channels,
|
|
|
|
|
const int height, const int width,
|
|
|
|
|
const int pooled_height,
|
|
|
|
|
const int pooled_width, T* output_data,
|
|
|
|
|
int64_t* argmax_data) {
|
|
|
|
|
int index = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
|
int offset = blockDim.x * gridDim.x;
|
|
|
|
|
for (size_t i = index; i < nthreads; i += offset) {
|
|
|
|
@ -88,18 +90,10 @@ static inline int NumBlocks(const int N) {
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
__global__ void GPUROIPoolBackward(
|
|
|
|
|
const int nthreads,
|
|
|
|
|
const int64_t* input_rois,
|
|
|
|
|
const T* output_grad,
|
|
|
|
|
const int64_t* argmax_data,
|
|
|
|
|
const int num_rois,
|
|
|
|
|
const float spatial_scale,
|
|
|
|
|
const int channels,
|
|
|
|
|
const int height,
|
|
|
|
|
const int width,
|
|
|
|
|
const int pooled_height,
|
|
|
|
|
const int pooled_width,
|
|
|
|
|
T* input_grad) {
|
|
|
|
|
const int nthreads, const int64_t* input_rois, const T* output_grad,
|
|
|
|
|
const int64_t* argmax_data, const int num_rois, const float spatial_scale,
|
|
|
|
|
const int channels, const int height, const int width,
|
|
|
|
|
const int pooled_height, const int pooled_width, T* input_grad) {
|
|
|
|
|
int index = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
|
int offset = blockDim.x * gridDim.x;
|
|
|
|
|
for (int i = index; i < nthreads; i += offset) {
|
|
|
|
@ -118,13 +112,13 @@ __global__ void GPUROIPoolBackward(
|
|
|
|
|
|
|
|
|
|
int argmax = offset_argmax_data[ph * pooled_width + pw];
|
|
|
|
|
if (argmax != -1) {
|
|
|
|
|
platform::CudaAtomicAdd(offset_input_grad + argmax,
|
|
|
|
|
platform::CudaAtomicAdd(
|
|
|
|
|
offset_input_grad + argmax,
|
|
|
|
|
static_cast<T>(offset_output_grad[ph * pooled_width + pw]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Place, typename T>
|
|
|
|
|
class GPUROIPoolOpKernel : public framework::OpKernel<T> {
|
|
|
|
|
public:
|
|
|
|
@ -151,17 +145,10 @@ class GPUROIPoolOpKernel : public framework::OpKernel<T> {
|
|
|
|
|
int blocks = NumBlocks(output_size);
|
|
|
|
|
int threads = kNumCUDAThreads;
|
|
|
|
|
|
|
|
|
|
GPUROIPoolForward<T>
|
|
|
|
|
<<<blocks, threads, 0, ctx.cuda_device_context().stream()>>>(
|
|
|
|
|
output_size,
|
|
|
|
|
in->data<T>(),
|
|
|
|
|
rois->data<int64_t>(),
|
|
|
|
|
spatial_scale,
|
|
|
|
|
channels,
|
|
|
|
|
height,
|
|
|
|
|
width,
|
|
|
|
|
pooled_height,
|
|
|
|
|
pooled_width,
|
|
|
|
|
GPUROIPoolForward<
|
|
|
|
|
T><<<blocks, threads, 0, ctx.cuda_device_context().stream()>>>(
|
|
|
|
|
output_size, in->data<T>(), rois->data<int64_t>(), spatial_scale,
|
|
|
|
|
channels, height, width, pooled_height, pooled_width,
|
|
|
|
|
out->mutable_data<T>(ctx.GetPlace()),
|
|
|
|
|
argmax->mutable_data<int64_t>(ctx.GetPlace()));
|
|
|
|
|
}
|
|
|
|
@ -175,10 +162,8 @@ class GPUROIPoolGradOpKernel : public framework::OpKernel<T> {
|
|
|
|
|
auto* rois = ctx.Input<Tensor>("ROIs");
|
|
|
|
|
auto* argmax = ctx.Input<Tensor>("Argmax");
|
|
|
|
|
|
|
|
|
|
auto* out_grad =
|
|
|
|
|
ctx.Input<Tensor>(framework::GradVarName("Out"));
|
|
|
|
|
auto* x_grad =
|
|
|
|
|
ctx.Output<Tensor>(framework::GradVarName("X"));
|
|
|
|
|
auto* out_grad = ctx.Input<Tensor>(framework::GradVarName("Out"));
|
|
|
|
|
auto* x_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
|
|
|
|
|
|
|
|
|
|
auto pooled_height = ctx.Attr<int>("pooled_height");
|
|
|
|
|
auto pooled_width = ctx.Attr<int>("pooled_width");
|
|
|
|
@ -199,19 +184,11 @@ class GPUROIPoolGradOpKernel : public framework::OpKernel<T> {
|
|
|
|
|
int threads = kNumCUDAThreads;
|
|
|
|
|
|
|
|
|
|
if (output_grad_size > 0) {
|
|
|
|
|
GPUROIPoolBackward<T>
|
|
|
|
|
<<<blocks, threads, 0, ctx.cuda_device_context().stream()>>>(
|
|
|
|
|
output_grad_size,
|
|
|
|
|
rois->data<int64_t>(),
|
|
|
|
|
out_grad->data<T>(),
|
|
|
|
|
argmax->data<int64_t>(),
|
|
|
|
|
rois_num,
|
|
|
|
|
spatial_scale,
|
|
|
|
|
channels,
|
|
|
|
|
height,
|
|
|
|
|
width,
|
|
|
|
|
pooled_height,
|
|
|
|
|
pooled_width,
|
|
|
|
|
GPUROIPoolBackward<
|
|
|
|
|
T><<<blocks, threads, 0, ctx.cuda_device_context().stream()>>>(
|
|
|
|
|
output_grad_size, rois->data<int64_t>(), out_grad->data<T>(),
|
|
|
|
|
argmax->data<int64_t>(), rois_num, spatial_scale, channels, height,
|
|
|
|
|
width, pooled_height, pooled_width,
|
|
|
|
|
x_grad->mutable_data<T>(ctx.GetPlace()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -223,8 +200,7 @@ class GPUROIPoolGradOpKernel : public framework::OpKernel<T> {
|
|
|
|
|
|
|
|
|
|
namespace ops = paddle::operators;
|
|
|
|
|
REGISTER_OP_GPU_KERNEL(
|
|
|
|
|
roi_pool,
|
|
|
|
|
ops::GPUROIPoolOpKernel<paddle::platform::GPUPlace, float>,
|
|
|
|
|
roi_pool, ops::GPUROIPoolOpKernel<paddle::platform::GPUPlace, float>,
|
|
|
|
|
ops::GPUROIPoolOpKernel<paddle::platform::GPUPlace, double>);
|
|
|
|
|
REGISTER_OP_GPU_KERNEL(
|
|
|
|
|
roi_pool_grad,
|
|
|
|
|