pool_cudnn_op.cu.cc 21.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
C
chengduoZH 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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

15
#include <string>
Y
Yi Wang 已提交
16
#include "paddle/fluid/framework/op_registry.h"
17
#include "paddle/fluid/operators/math/math_function.h"
Y
Yi Wang 已提交
18
#include "paddle/fluid/operators/pool_op.h"
19
#ifdef PADDLE_WITH_CUDA
Y
Yi Wang 已提交
20
#include "paddle/fluid/platform/cudnn_helper.h"
21 22
#endif
#ifdef PADDLE_WITH_HIP
23 24
#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/framework/operator.h"
25 26
#include "paddle/fluid/platform/miopen_helper.h"
#endif
C
chengduoZH 已提交
27 28 29 30 31 32 33 34 35

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using ScopedTensorDescriptor = platform::ScopedTensorDescriptor;
using ScopedPoolingDescriptor = platform::ScopedPoolingDescriptor;
using DataLayout = platform::DataLayout;
using PoolingMode = platform::PoolingMode;
K
update  
Kexin Zhao 已提交
36 37
template <typename T>
using ScalingParamType = typename platform::CudnnDataType<T>::ScalingParamType;
C
chengduoZH 已提交
38

39 40 41 42 43 44 45 46 47 48 49 50
DataLayout getLayoutFromStr(std::string data_format) {
  if (data_format == "NHWC") {
    return DataLayout::kNHWC;
  } else if (data_format == "NCHW") {
    return DataLayout::kNCHW;
  } else if (data_format == "NCDHW") {
    return DataLayout::kNCDHW;
  } else {
    return DataLayout::kNCDHW;
  }
}

C
chengduoZH 已提交
51
template <typename T>
52
class PoolCUDNNOpKernel : public framework::OpKernel<T> {
C
chengduoZH 已提交
53 54
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
55 56 57 58
    PADDLE_ENFORCE_EQ(
        platform::is_gpu_place(ctx.GetPlace()), true,
        platform::errors::InvalidArgument("Pool operator CUDA kernel must use "
                                          "CUDAPlace rather than CPUPlace."));
C
chengduoZH 已提交
59 60 61

    const Tensor *input = ctx.Input<Tensor>("X");
    Tensor *output = ctx.Output<Tensor>("Out");
62
    output->mutable_data<T>(ctx.GetPlace());
C
chengduoZH 已提交
63
    std::string pooling_type = ctx.Attr<std::string>("pooling_type");
64
    bool exclusive = ctx.Attr<bool>("exclusive");
65
    bool adaptive = ctx.Attr<bool>("adaptive");
C
chengduoZH 已提交
66 67 68
    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");
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    std::string data_format = ctx.Attr<std::string>("data_format");
    bool global_pooling = ctx.Attr<bool>("global_pooling");
    std::string padding_algorithm = ctx.Attr<std::string>("padding_algorithm");
    const bool channel_last = (data_format == "NHWC" || data_format == "NDHWC");

    // update paddings
    auto in_x_dims = input->dims();
    framework::DDim data_dims;
    if (channel_last) {
      data_dims = framework::slice_ddim(in_x_dims, 1, in_x_dims.size() - 1);
    } else {
      data_dims = framework::slice_ddim(in_x_dims, 2, in_x_dims.size());
    }
    UpdatePadding(&paddings, global_pooling, adaptive, padding_algorithm,
                  data_dims, strides, ksize);
84 85
    if (data_dims.size() * 2 == static_cast<int>(paddings.size())) {
      for (int i = 0; i < data_dims.size(); ++i) {
86
        paddings.erase(paddings.begin() + i + 1);
C
chengduoZH 已提交
87 88 89
      }
    }

90 91 92 93 94 95 96 97 98 99 100
    if (global_pooling) {
      UpdateKsize(&ksize, data_dims);
    }

    const std::string str_NCHW = "NCHW", str_NHWC = "NHWC";
    const std::string str_NCDHW = "NCDHW", str_NDHWC = "NDHWC";

    // -----------------transformed tensor ------------------------

    Tensor transformed_input(input->type());
    Tensor transformed_output(output->type());
C
chengduoZH 已提交
101 102
    DataLayout layout;

103
    if (data_format == str_NDHWC) {
C
chengduoZH 已提交
104
      layout = DataLayout::kNCDHW;
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
      auto &dev_ctx =
          ctx.template device_context<paddle::platform::CUDADeviceContext>();
      std::vector<int> axis{0, 4, 1, 2, 3};

      // input
      transformed_input.Resize(input->dims());

      auto in_dims_vec = framework::vectorize(input->dims());
      in_dims_vec[1] = input->dims()[4];
      in_dims_vec[2] = input->dims()[1];
      in_dims_vec[3] = input->dims()[2];
      in_dims_vec[4] = input->dims()[3];
      transformed_input.Resize(framework::make_ddim(in_dims_vec));
      transformed_input.mutable_data(ctx.GetPlace(), input->type());

      math::Transpose<paddle::platform::CUDADeviceContext, T, 5> trans5;
      trans5(dev_ctx, *input, &transformed_input, axis);

      // output
      transformed_output.Resize(output->dims());

      auto out_dims_vec = framework::vectorize(output->dims());
      out_dims_vec[1] = output->dims()[4];
      out_dims_vec[2] = output->dims()[1];
      out_dims_vec[3] = output->dims()[2];
      out_dims_vec[4] = output->dims()[3];
      transformed_output.Resize(framework::make_ddim(out_dims_vec));
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#ifdef PADDLE_WITH_HIP
      // MIOPEN not support NHWC data layout
    } else if (data_format == str_NHWC) {
      layout = DataLayout::kNCHW;
      auto &dev_ctx =
          ctx.template device_context<paddle::platform::CUDADeviceContext>();
      std::vector<int> axis{0, 3, 1, 2};

      transformed_input.Resize(input->dims());
      auto in_dims_vec = framework::vectorize(input->dims());
      in_dims_vec[1] = input->dims()[3];
      in_dims_vec[2] = input->dims()[1];
      in_dims_vec[3] = input->dims()[2];
      transformed_input.Resize(framework::make_ddim(in_dims_vec));
      transformed_input.mutable_data(ctx.GetPlace(), input->type());
147

148 149 150 151 152 153 154 155 156 157
      math::Transpose<paddle::platform::CUDADeviceContext, T, 4> trans;
      trans(dev_ctx, *input, &transformed_input, axis);

      transformed_output.Resize(output->dims());
      auto out_dims_vec = framework::vectorize(output->dims());
      out_dims_vec[1] = output->dims()[3];
      out_dims_vec[2] = output->dims()[1];
      out_dims_vec[3] = output->dims()[2];
      transformed_output.Resize(framework::make_ddim(out_dims_vec));
#endif
158 159 160 161
    } else {
      layout = getLayoutFromStr(data_format);
      transformed_input = *input;
      transformed_output = *output;
C
chengduoZH 已提交
162
    }
C
chengduoZH 已提交
163

164 165 166 167 168 169 170 171 172
    const T *tranformed_input_data = transformed_input.data<T>();
    T *tranformed_output_data = transformed_output.mutable_data<T>(
        transformed_output.dims(), ctx.GetPlace());

    // ------------------- cudnn descriptors ---------------------
    ScopedTensorDescriptor input_desc;
    ScopedTensorDescriptor output_desc;
    ScopedPoolingDescriptor pool_desc;

173 174 175 176 177 178
#ifdef PADDLE_WITH_HIP
    miopenTensorDescriptor_t cudnn_input_desc = input_desc.descriptor<T>(
        layout, framework::vectorize<int>(transformed_input.dims()));
    miopenTensorDescriptor_t cudnn_output_desc = output_desc.descriptor<T>(
        layout, framework::vectorize<int>(transformed_output.dims()));
#else
C
chengduoZH 已提交
179
    cudnnTensorDescriptor_t cudnn_input_desc = input_desc.descriptor<T>(
180
        layout, framework::vectorize<int>(transformed_input.dims()));
C
chengduoZH 已提交
181
    cudnnTensorDescriptor_t cudnn_output_desc = output_desc.descriptor<T>(
182
        layout, framework::vectorize<int>(transformed_output.dims()));
183
#endif
C
chengduoZH 已提交
184 185 186 187
    PoolingMode pooling_mode;
    if (pooling_type == "max") {
      pooling_mode = PoolingMode::kMaximum;
    } else {
188 189
      pooling_mode = exclusive ? PoolingMode::kAverageExclusive
                               : PoolingMode::kAverageInclusive;
C
chengduoZH 已提交
190 191
    }

192 193 194 195
#ifdef PADDLE_WITH_HIP
    miopenPoolingDescriptor_t cudnn_pool_desc =
        pool_desc.descriptor(pooling_mode, ksize, paddings, strides);
#else
C
chengduoZH 已提交
196 197
    cudnnPoolingDescriptor_t cudnn_pool_desc =
        pool_desc.descriptor(pooling_mode, ksize, paddings, strides);
198
#endif
C
chengduoZH 已提交
199 200 201

    // ------------------- cudnn pool algorithm ---------------------
    auto handle = ctx.cuda_device_context().cudnn_handle();
K
update  
Kexin Zhao 已提交
202
    ScalingParamType<T> alpha = 1.0f, beta = 0.0f;
203

204 205 206 207 208 209 210 211 212 213 214 215 216
#ifdef PADDLE_WITH_HIP
    char *pool_workspace;
    size_t pool_worksize = 0;
    PADDLE_ENFORCE_CUDA_SUCCESS(
        platform::dynload::miopenPoolingGetWorkSpaceSizeV2(
            cudnn_pool_desc, cudnn_output_desc, &pool_worksize));
    PADDLE_ENFORCE_CUDA_SUCCESS(hipMalloc(&pool_workspace, pool_worksize));
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::miopenPoolingForward(
        handle, cudnn_pool_desc, &alpha, cudnn_input_desc,
        tranformed_input_data, &beta, cudnn_output_desc, tranformed_output_data,
        false, pool_workspace, pool_worksize));
    PADDLE_ENFORCE_CUDA_SUCCESS(hipFree(pool_workspace));
#else
217
    PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnPoolingForward(
218 219 220
        handle, cudnn_pool_desc, &alpha, cudnn_input_desc,
        tranformed_input_data, &beta, cudnn_output_desc,
        tranformed_output_data));
221
#endif
222 223 224 225 226 227 228 229
    // add
    if (data_format == str_NDHWC) {
      auto &dev_ctx =
          ctx.template device_context<paddle::platform::CUDADeviceContext>();
      std::vector<int> axis{0, 2, 3, 4, 1};
      math::Transpose<paddle::platform::CUDADeviceContext, T, 5> trans5_v2;
      trans5_v2(dev_ctx, transformed_output, output, axis);
    }
230 231 232 233 234 235 236 237 238 239
#ifdef PADDLE_WITH_HIP
    // MIOPEN not support NHWC data layout
    if (data_format == str_NHWC) {
      auto &dev_ctx =
          ctx.template device_context<paddle::platform::CUDADeviceContext>();
      std::vector<int> axis{0, 2, 3, 1};
      math::Transpose<paddle::platform::CUDADeviceContext, T, 4> trans;
      trans(dev_ctx, transformed_output, output, axis);
    }
#endif
C
chengduoZH 已提交
240 241 242 243
  }
};

template <typename T>
244
class PoolCUDNNGradOpKernel : public framework::OpKernel<T> {
C
chengduoZH 已提交
245 246
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
247 248 249 250
    PADDLE_ENFORCE_EQ(
        platform::is_gpu_place(ctx.GetPlace()), true,
        platform::errors::InvalidArgument("Pool operator CUDA kernel must use "
                                          "CUDAPlace rather than CPUPlace."));
C
chengduoZH 已提交
251 252 253 254 255 256 257

    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
chengduoZH 已提交
258
    std::string pooling_type = ctx.Attr<std::string>("pooling_type");
259
    bool exclusive = ctx.Attr<bool>("exclusive");
260
    bool adaptive = ctx.Attr<bool>("adaptive");
C
chengduoZH 已提交
261 262 263
    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");
264 265 266 267 268
    std::string data_format = ctx.Attr<std::string>("data_format");
    bool global_pooling = ctx.Attr<bool>("global_pooling");
    std::string padding_algorithm = ctx.Attr<std::string>("padding_algorithm");
    const bool channel_last = (data_format == "NHWC" || data_format == "NDHWC");

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
#ifdef PADDLE_WITH_HIP
    if (pooling_type == "max") {
      using OpKernelMap = paddle::framework::OperatorWithKernel::OpKernelMap;
      using OpKernelFunc = paddle::framework::OperatorWithKernel::OpKernelFunc;
      auto &all_op_kernels =
          paddle::framework::OperatorWithKernel::AllOpKernels();
      std::string op_type = "pool2d_grad";
      auto kernels_iter = all_op_kernels.find(op_type);
      PADDLE_ENFORCE_NE(
          kernels_iter, all_op_kernels.end(),
          platform::errors::Unavailable(
              "There are no kernels which are registered in the %s operator.",
              op_type));
      OpKernelMap &kernels = kernels_iter->second;
      paddle::framework::OpKernelType expected_kernel_key(
          paddle::framework::ToDataType(typeid(T)), ctx.GetPlace());
      auto kernel_iter = kernels.find(expected_kernel_key);
      PADDLE_ENFORCE_NE(kernel_iter, kernels.end(),
                        platform::errors::NotFound(
                            "Operator (%s) does not have kernel for %s.",
                            op_type, KernelTypeToString(expected_kernel_key)));
      std::unique_ptr<OpKernelFunc> kernel_func_(
          new OpKernelFunc(kernel_iter->second));
      (*kernel_func_)(ctx);
      return;
    }
#endif

297 298 299 300 301 302 303 304 305 306
    // update paddings
    auto in_x_dims = input->dims();
    framework::DDim data_dims;
    if (channel_last) {
      data_dims = framework::slice_ddim(in_x_dims, 1, in_x_dims.size() - 1);
    } else {
      data_dims = framework::slice_ddim(in_x_dims, 2, in_x_dims.size());
    }
    UpdatePadding(&paddings, global_pooling, adaptive, padding_algorithm,
                  data_dims, strides, ksize);
307 308
    if (data_dims.size() * 2 == static_cast<int>(paddings.size())) {
      for (int i = 0; i < data_dims.size(); ++i) {
309
        paddings.erase(paddings.begin() + i + 1);
C
fix bug  
chengduoZH 已提交
310
      }
C
chengduoZH 已提交
311 312
    }

313 314 315
    if (global_pooling) {
      UpdateKsize(&ksize, data_dims);
    }
C
chengduoZH 已提交
316

317 318 319 320 321 322 323
    // ------- tensor grad --------------
    Tensor transformed_input(input->type());
    Tensor transformed_output(output->type());
    Tensor transformed_output_grad(output_grad->type());

    input_grad->mutable_data<T>(ctx.GetPlace());
    Tensor transformed_input_grad(input_grad->type());
C
chengduoZH 已提交
324
    DataLayout layout;
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
    const std::string str_NCHW = "NCHW", str_NHWC = "NHWC";
    const std::string str_NCDHW = "NCDHW", str_NDHWC = "NDHWC";
    if (data_format == str_NDHWC) {
      layout = DataLayout::kNCDHW;
      auto &dev_ctx =
          ctx.template device_context<paddle::platform::CUDADeviceContext>();
      std::vector<int> axis{0, 4, 1, 2, 3};

      // input
      transformed_input.Resize(input->dims());
      auto in_dims_vec = framework::vectorize(input->dims());
      in_dims_vec[1] = input->dims()[4];
      in_dims_vec[2] = input->dims()[1];
      in_dims_vec[3] = input->dims()[2];
      in_dims_vec[4] = input->dims()[3];
      transformed_input.Resize(framework::make_ddim(in_dims_vec));
      transformed_input.mutable_data(ctx.GetPlace(), input->type());

      math::Transpose<paddle::platform::CUDADeviceContext, T, 5> trans5;
      trans5(dev_ctx, *input, &transformed_input, axis);

      // output
      transformed_output.Resize(output->dims());
      auto out_dims_vec = framework::vectorize(output->dims());
      out_dims_vec[1] = output->dims()[4];
      out_dims_vec[2] = output->dims()[1];
      out_dims_vec[3] = output->dims()[2];
      out_dims_vec[4] = output->dims()[3];
      transformed_output.Resize(framework::make_ddim(out_dims_vec));

      transformed_output.mutable_data(ctx.GetPlace(), output->type());

      math::Transpose<paddle::platform::CUDADeviceContext, T, 5> trans5_v2;
      trans5_v2(dev_ctx, *output, &transformed_output, axis);

      // output grad
      transformed_output_grad.Resize(framework::make_ddim(out_dims_vec));
      transformed_output_grad.mutable_data(ctx.GetPlace(), output_grad->type());

      math::Transpose<paddle::platform::CUDADeviceContext, T, 5> trans5_v3;
      trans5_v3(dev_ctx, *output_grad, &transformed_output_grad, axis);

      // input grad
      transformed_input_grad.Resize(framework::make_ddim(in_dims_vec));
C
chengduoZH 已提交
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
#ifdef PADDLE_WITH_HIP
      // MIOPEN not support NHWC data layout
    } else if (data_format == str_NHWC) {
      layout = DataLayout::kNCHW;
      auto &dev_ctx =
          ctx.template device_context<paddle::platform::CUDADeviceContext>();
      std::vector<int> axis{0, 3, 1, 2};

      // input
      transformed_input.Resize(input->dims());
      auto in_dims_vec = framework::vectorize(input->dims());
      in_dims_vec[1] = input->dims()[3];
      in_dims_vec[2] = input->dims()[1];
      in_dims_vec[3] = input->dims()[2];
      transformed_input.Resize(framework::make_ddim(in_dims_vec));
      transformed_input.mutable_data(ctx.GetPlace(), input->type());

      math::Transpose<paddle::platform::CUDADeviceContext, T, 4> trans4;
      trans4(dev_ctx, *input, &transformed_input, axis);

      // output
      transformed_output.Resize(output->dims());
      auto out_dims_vec = framework::vectorize(output->dims());
      out_dims_vec[1] = output->dims()[3];
      out_dims_vec[2] = output->dims()[1];
      out_dims_vec[3] = output->dims()[2];
      transformed_output.Resize(framework::make_ddim(out_dims_vec));

      transformed_output.mutable_data(ctx.GetPlace(), output->type());

      math::Transpose<paddle::platform::CUDADeviceContext, T, 4> trans4_v2;
      trans4_v2(dev_ctx, *output, &transformed_output, axis);

      // output grad
      transformed_output_grad.Resize(framework::make_ddim(out_dims_vec));
      transformed_output_grad.mutable_data(ctx.GetPlace(), output_grad->type());

      math::Transpose<paddle::platform::CUDADeviceContext, T, 4> trans4_v3;
      trans4_v3(dev_ctx, *output_grad, &transformed_output_grad, axis);

      // input grad
      transformed_input_grad.Resize(framework::make_ddim(in_dims_vec));
#endif
C
chengduoZH 已提交
413
    } else {
414 415 416 417 418
      layout = getLayoutFromStr(data_format);
      transformed_input = *input;
      transformed_output = *output;
      transformed_output_grad = *output_grad;
      transformed_input_grad = *input_grad;
C
chengduoZH 已提交
419
    }
C
chengduoZH 已提交
420

421 422 423 424 425 426 427 428 429
    const T *input_data = transformed_input.data<T>();
    const T *output_data = transformed_output.data<T>();
    const T *output_grad_data = transformed_output_grad.data<T>();

    // ------------------- cudnn descriptors ---------------------
    ScopedTensorDescriptor input_desc;
    ScopedTensorDescriptor output_desc;
    ScopedPoolingDescriptor pool_desc;

430 431 432 433 434 435
#ifdef PADDLE_WITH_HIP
    miopenTensorDescriptor_t cudnn_input_desc = input_desc.descriptor<T>(
        layout, framework::vectorize<int>(transformed_input.dims()));
    miopenTensorDescriptor_t cudnn_output_desc = output_desc.descriptor<T>(
        layout, framework::vectorize<int>(transformed_output.dims()));
#else
C
chengduoZH 已提交
436
    cudnnTensorDescriptor_t cudnn_input_desc = input_desc.descriptor<T>(
437
        layout, framework::vectorize<int>(transformed_input.dims()));
C
chengduoZH 已提交
438
    cudnnTensorDescriptor_t cudnn_output_desc = output_desc.descriptor<T>(
439
        layout, framework::vectorize<int>(transformed_output.dims()));
440
#endif
C
chengduoZH 已提交
441 442
    PoolingMode pooling_mode;
    if (pooling_type == "max") {
D
dzhwinter 已提交
443 444 445 446 447
      if (FLAGS_cudnn_deterministic) {
        pooling_mode = PoolingMode::kMaximumDeterministic;
      } else {
        pooling_mode = PoolingMode::kMaximum;
      }
C
chengduoZH 已提交
448
    } else {
449 450
      pooling_mode = exclusive ? PoolingMode::kAverageExclusive
                               : PoolingMode::kAverageInclusive;
C
chengduoZH 已提交
451 452
    }

453 454 455 456
#ifdef PADDLE_WITH_HIP
    miopenPoolingDescriptor_t cudnn_pool_desc =
        pool_desc.descriptor(pooling_mode, ksize, paddings, strides);
#else
C
chengduoZH 已提交
457 458
    cudnnPoolingDescriptor_t cudnn_pool_desc =
        pool_desc.descriptor(pooling_mode, ksize, paddings, strides);
459
#endif
C
chengduoZH 已提交
460 461 462

    // ------------------- cudnn pool algorithm ---------------------
    auto handle = ctx.cuda_device_context().cudnn_handle();
K
update  
Kexin Zhao 已提交
463
    ScalingParamType<T> alpha = 1.0f, beta = 0.0f;
C
chengduoZH 已提交
464
    if (input_grad) {
465 466
      T *input_grad_data = transformed_input_grad.mutable_data<T>(
          transformed_input_grad.dims(), ctx.GetPlace());
467 468 469 470 471 472 473 474 475 476 477 478 479 480
// Because beta is zero, it is unnecessary to reset input_grad.
#ifdef PADDLE_WITH_HIP
      char *pool_workspace;
      size_t pool_worksize = 0;
      PADDLE_ENFORCE_CUDA_SUCCESS(
          platform::dynload::miopenPoolingGetWorkSpaceSizeV2(
              cudnn_pool_desc, cudnn_output_desc, &pool_worksize));
      PADDLE_ENFORCE_CUDA_SUCCESS(hipMalloc(&pool_workspace, pool_worksize));
      PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::miopenPoolingBackward(
          handle, cudnn_pool_desc, &alpha, cudnn_output_desc, output_data,
          cudnn_output_desc, output_grad_data, cudnn_input_desc, input_data,
          &beta, cudnn_input_desc, input_grad_data, pool_workspace));
      PADDLE_ENFORCE_CUDA_SUCCESS(hipFree(pool_workspace));
#else
481
      PADDLE_ENFORCE_CUDA_SUCCESS(platform::dynload::cudnnPoolingBackward(
C
chengduoZH 已提交
482
          handle, cudnn_pool_desc, &alpha, cudnn_output_desc, output_data,
483 484
          cudnn_output_desc, output_grad_data, cudnn_input_desc, input_data,
          &beta, cudnn_input_desc, input_grad_data));
485
#endif
486 487 488 489 490 491 492 493

      if (data_format == str_NDHWC) {
        auto &dev_ctx =
            ctx.template device_context<paddle::platform::CUDADeviceContext>();
        std::vector<int> axis{0, 2, 3, 4, 1};
        math::Transpose<paddle::platform::CUDADeviceContext, T, 5> trans5_v4;
        trans5_v4(dev_ctx, transformed_input_grad, input_grad, axis);
      }
494 495 496 497 498 499 500 501 502 503
#ifdef PADDLE_WITH_HIP
      // MIOPEN not support NHWC data layout
      if (data_format == str_NHWC) {
        auto &dev_ctx =
            ctx.template device_context<paddle::platform::CUDADeviceContext>();
        std::vector<int> axis{0, 2, 3, 1};
        math::Transpose<paddle::platform::CUDADeviceContext, T, 4> trans4_v4;
        trans4_v4(dev_ctx, transformed_input_grad, input_grad, axis);
      }
#endif
C
chengduoZH 已提交
504 505 506 507 508 509 510 511
    }
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
K
Kexin Zhao 已提交
512
namespace plat = paddle::platform;
C
chengduoZH 已提交
513

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
#ifdef PADDLE_WITH_HIP
// MIOPEN do not support double
REGISTER_OP_KERNEL(pool2d, CUDNN, plat::CUDAPlace,
                   ops::PoolCUDNNOpKernel<float>,
                   ops::PoolCUDNNOpKernel<plat::float16>);
REGISTER_OP_KERNEL(pool2d_grad, CUDNN, plat::CUDAPlace,
                   ops::PoolCUDNNGradOpKernel<float>,
                   ops::PoolCUDNNGradOpKernel<plat::float16>);

REGISTER_OP_KERNEL(pool3d, CUDNN, plat::CUDAPlace,
                   ops::PoolCUDNNOpKernel<float>,
                   ops::PoolCUDNNOpKernel<plat::float16>);
REGISTER_OP_KERNEL(pool3d_grad, CUDNN, plat::CUDAPlace,
                   ops::PoolCUDNNGradOpKernel<float>);
#else
K
Kexin Zhao 已提交
529
REGISTER_OP_KERNEL(pool2d, CUDNN, plat::CUDAPlace,
530
                   ops::PoolCUDNNOpKernel<float>,
K
Kexin Zhao 已提交
531 532 533
                   ops::PoolCUDNNOpKernel<double>,
                   ops::PoolCUDNNOpKernel<plat::float16>);
REGISTER_OP_KERNEL(pool2d_grad, CUDNN, plat::CUDAPlace,
534
                   ops::PoolCUDNNGradOpKernel<float>,
C
chengduo 已提交
535 536
                   ops::PoolCUDNNGradOpKernel<double>,
                   ops::PoolCUDNNGradOpKernel<plat::float16>);
537

K
Kexin Zhao 已提交
538
REGISTER_OP_KERNEL(pool3d, CUDNN, plat::CUDAPlace,
539
                   ops::PoolCUDNNOpKernel<float>,
K
Kexin Zhao 已提交
540 541
                   ops::PoolCUDNNOpKernel<double>,
                   ops::PoolCUDNNOpKernel<plat::float16>);
K
Kexin Zhao 已提交
542
REGISTER_OP_KERNEL(pool3d_grad, CUDNN, plat::CUDAPlace,
543
                   ops::PoolCUDNNGradOpKernel<float>,
544
                   ops::PoolCUDNNGradOpKernel<double>);
545
#endif