top_pool_op.cu 4.6 KB
Newer Older
W
wangguanzhong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

GUnless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

W
wangguanzhong 已提交
15
#include <vector>
W
wangguanzhong 已提交
16 17
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/memory/memory.h"
W
wangguanzhong 已提交
18
#include "paddle/fluid/platform/cuda_primitives.h"
W
wangguanzhong 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include "util.cu.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

static constexpr int kNumCUDAThreads = 512;
static constexpr int kNumMaximumNumBlocks = 4096;

static inline int NumBlocks(const int N) {
  return std::min((N + kNumCUDAThreads - 1) / kNumCUDAThreads,
                  kNumMaximumNumBlocks);
}

template <typename T>
class TopPoolOpCUDAKernel : public framework::OpKernel<T> {
W
wangguanzhong 已提交
36 37
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
W
wangguanzhong 已提交
38 39
    PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()),
                   "This kernel only runs on GPU device.");
W
wangguanzhong 已提交
40 41 42 43
    auto* x = ctx.Input<Tensor>("X");
    auto* max_map = ctx.Output<Tensor>("MaxMap");
    auto* output = ctx.Output<Tensor>("Output");
    auto* x_data = x->data<T>();
W
wangguanzhong 已提交
44 45 46 47 48 49 50
    auto x_dims = x->dims();
    int NC_num = x_dims[0] * x_dims[1];
    int height = x_dims[2];
    int width = x_dims[3];
    int num = x->numel();
    auto& dev_ctx = ctx.cuda_device_context();

W
wangguanzhong 已提交
51 52
    int* max_map_data = max_map->mutable_data<int>(x_dims, dev_ctx.GetPlace());
    T* output_data = output->mutable_data<T>(x_dims, dev_ctx.GetPlace());
W
wangguanzhong 已提交
53
    auto gpu_place = boost::get<platform::CUDAPlace>(dev_ctx.GetPlace());
W
wangguanzhong 已提交
54

W
wangguanzhong 已提交
55 56
    int threads = kNumCUDAThreads;
    int blocks = NumBlocks(num / height);
W
wangguanzhong 已提交
57

W
wangguanzhong 已提交
58 59 60 61 62
    auto max_val_ptr = memory::Alloc(gpu_place, num / height * sizeof(T));
    T* max_val_data = reinterpret_cast<T*>(max_val_ptr->ptr());
    auto max_ind_ptr = memory::Alloc(gpu_place, num / height * sizeof(int));
    int* max_ind_data = reinterpret_cast<int*>(max_ind_ptr->ptr());

W
wangguanzhong 已提交
63 64 65 66 67 68 69 70 71
    GetMaxInfo<T><<<blocks, threads, 0, dev_ctx.stream()>>>(x->data<T>(),
                                                            NC_num,
                                                            height,
                                                            width,
                                                            2,
                                                            true,
                                                            max_val_data,
                                                            max_ind_data,
                                                            max_map_data);
W
wangguanzhong 已提交
72 73

    blocks = NumBlocks(num);
W
wangguanzhong 已提交
74 75
    ScatterAddFw<T><<<blocks, threads, 0, dev_ctx.stream()>>>(
        x->data<T>(), max_map_data, NC_num, height, width, 2, output_data);
W
wangguanzhong 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  }
};

template <typename T>
class TopPoolGradOpCUDAKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto* x = ctx.Input<Tensor>("X");
    auto* max_map = ctx.Input<Tensor>("MaxMap");
    auto* out_grad = ctx.Input<Tensor>(framework::GradVarName("Output"));
    auto* in_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto x_dims = x->dims();
    auto& dev_ctx = ctx.cuda_device_context();
    T* in_grad_data = in_grad->mutable_data<T>(x_dims, dev_ctx.GetPlace());
    auto gpu_place = boost::get<platform::CUDAPlace>(dev_ctx.GetPlace());
W
wangguanzhong 已提交
91

W
wangguanzhong 已提交
92 93 94 95 96 97
    int threads = kNumCUDAThreads;
    int NC_num = x_dims[0] * x_dims[1];
    int height = x_dims[2];
    int width = x_dims[3];
    int grad_num = in_grad->numel();
    int blocks = NumBlocks(grad_num);
W
wangguanzhong 已提交
98 99 100 101 102 103 104 105 106 107 108
    FillConstant<T><<<blocks, threads, 0, dev_ctx.stream()>>>(
        in_grad_data, 0, grad_num);

    ScatterAddBw<T><<<blocks, threads, 0, dev_ctx.stream()>>>(
        out_grad->data<T>(),
        max_map->data<int>(),
        NC_num,
        height,
        width,
        2,
        in_grad_data);
W
wangguanzhong 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_CUDA_KERNEL(top_pool,
                        ops::TopPoolOpCUDAKernel<float>,
                        ops::TopPoolOpCUDAKernel<double>);
REGISTER_OP_CUDA_KERNEL(top_pool_grad,
                        ops::TopPoolGradOpCUDAKernel<float>,
                        ops::TopPoolGradOpCUDAKernel<double>);