pool_cudnn_op.cu 5.9 KB
Newer Older
C
chengduoZH 已提交
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
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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

Unless 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. */

#include "paddle/operators/pool_cudnn_op.h"
#include "paddle/platform/cudnn_helper.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using ScopedTensorDescriptor = platform::ScopedTensorDescriptor;
using ScopedPoolingDescriptor = platform::ScopedPoolingDescriptor;
using DataLayout = platform::DataLayout;
using PoolingMode = platform::PoolingMode;

// NOTE: copy from conv_cudnn
C
chengduoZH 已提交
28
std::vector<int> Dims2VectorPool(const framework::DDim &dims) {
C
chengduoZH 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  std::vector<int> ret;
  for (int i = 0; i < dims.size(); i++) {
    ret.push_back(dims[i]);
  }
  return ret;
}

template <typename T>
class PoolCudnnOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
    PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()),
                   "It must use GPUPlace.");

    const Tensor *input = ctx.Input<Tensor>("X");
    Tensor *output = ctx.Output<Tensor>("Out");

    const T *input_data = input->data<T>();
    T *output_data = output->mutable_data<T>(ctx.GetPlace());

C
fix doc  
chengduoZH 已提交
49
    std::string pooling_type = ctx.Attr<std::string>("poolingType");
C
chengduoZH 已提交
50 51 52
    std::vector<int> ksize = ctx.Attr<std::vector<int>>("ksize");
    std::vector<int> strides = ctx.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = ctx.Attr<std::vector<int>>("paddings");
C
fix doc  
chengduoZH 已提交
53
    if (ctx.Attr<bool>("globalPooling")) {
C
chengduoZH 已提交
54 55 56 57 58 59 60 61 62 63 64 65
      for (size_t i = 0; i < ksize.size(); ++i) {
        ksize[i] = static_cast<int>(input->dims()[i + 2]);
      }
    }

    // ------------------- cudnn descriptors ---------------------
    ScopedTensorDescriptor input_desc;
    ScopedTensorDescriptor output_desc;
    ScopedPoolingDescriptor pool_desc;
    DataLayout layout = DataLayout::kNCHW;

    cudnnTensorDescriptor_t cudnn_input_desc =
C
chengduoZH 已提交
66
        input_desc.descriptor<T>(layout, Dims2VectorPool(input->dims()));
C
chengduoZH 已提交
67
    cudnnTensorDescriptor_t cudnn_output_desc =
C
chengduoZH 已提交
68
        output_desc.descriptor<T>(layout, Dims2VectorPool(output->dims()));
C
chengduoZH 已提交
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

    PoolingMode pooling_mode;
    if (pooling_type == "max") {
      pooling_mode = PoolingMode::kMaximum;
    } else {
      pooling_mode = PoolingMode::kAverage;
    }

    cudnnPoolingDescriptor_t cudnn_pool_desc =
        pool_desc.descriptor(pooling_mode, ksize, paddings, strides);

    // ------------------- cudnn pool algorithm ---------------------
    auto handle = ctx.cuda_device_context().cudnn_handle();
    T alpha = 1.0f, beta = 0.0f;

    PADDLE_ENFORCE(platform::dynload::cudnnPoolingForward(
        handle, cudnn_pool_desc, &alpha, cudnn_input_desc, input_data, &beta,
        cudnn_output_desc, output_data));
  }
};

template <typename T>
class PoolCudnnGradOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
    PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()),
                   "It must use GPUPlace.");

    const Tensor *input = ctx.Input<Tensor>("X");
    const Tensor *output = ctx.Input<Tensor>("Out");
    const Tensor *output_grad =
        ctx.Input<Tensor>(framework::GradVarName("Out"));
    Tensor *input_grad = ctx.Output<Tensor>(framework::GradVarName("X"));

C
fix doc  
chengduoZH 已提交
103
    std::string pooling_type = ctx.Attr<std::string>("poolingType");
C
chengduoZH 已提交
104 105 106 107
    std::vector<int> ksize = ctx.Attr<std::vector<int>>("ksize");
    std::vector<int> strides = ctx.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = ctx.Attr<std::vector<int>>("paddings");

C
fix doc  
chengduoZH 已提交
108
    if (ctx.Attr<bool>("globalPooling")) {
C
chengduoZH 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      for (size_t i = 0; i < ksize.size(); ++i)
        ksize[i] = static_cast<int>(input->dims()[i + 2]);
    }

    const T *input_data = input->data<T>();
    const T *output_data = output->data<T>();
    const T *output_grad_data = output_grad->data<T>();

    // ------------------- cudnn descriptors ---------------------
    ScopedTensorDescriptor input_desc;
    ScopedTensorDescriptor output_desc;
    ScopedPoolingDescriptor pool_desc;
    DataLayout layout = DataLayout::kNCHW;

    cudnnTensorDescriptor_t cudnn_input_desc =
C
chengduoZH 已提交
124
        input_desc.descriptor<T>(layout, Dims2VectorPool(input->dims()));
C
chengduoZH 已提交
125
    cudnnTensorDescriptor_t cudnn_output_desc =
C
chengduoZH 已提交
126
        output_desc.descriptor<T>(layout, Dims2VectorPool(output->dims()));
C
chengduoZH 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

    PoolingMode pooling_mode;
    if (pooling_type == "max") {
      pooling_mode = PoolingMode::kMaximum;
    } else {
      pooling_mode = PoolingMode::kAverage;
    }

    cudnnPoolingDescriptor_t cudnn_pool_desc =
        pool_desc.descriptor(pooling_mode, ksize, paddings, strides);

    // ------------------- cudnn pool algorithm ---------------------
    auto handle = ctx.cuda_device_context().cudnn_handle();
    T alpha = 1.0f, beta = 0.0f;

    if (input_grad) {
      T *input_grad_data = input_grad->mutable_data<T>(ctx.GetPlace());
144 145
      math::SetConstant<paddle::platform::GPUPlace, T> set_zero;
      set_zero(ctx.device_context(), input_grad, static_cast<T>(0));
C
chengduoZH 已提交
146 147 148

      PADDLE_ENFORCE(platform::dynload::cudnnPoolingBackward(
          handle, cudnn_pool_desc, &alpha, cudnn_output_desc, output_data,
149 150
          cudnn_output_desc, output_grad_data, cudnn_input_desc, input_data,
          &beta, cudnn_input_desc, input_grad_data));
C
chengduoZH 已提交
151 152 153 154 155 156 157 158 159 160 161
    }
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

REGISTER_OP_GPU_KERNEL(pool2d_cudnn, ops::PoolCudnnOpKernel<float>);
REGISTER_OP_GPU_KERNEL(pool2d_cudnn_grad, ops::PoolCudnnGradOpKernel<float>);