CudnnPoolLayer.cpp 4.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjinchao01 已提交
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. */

Y
Yu Yang 已提交
15
#include "CudnnPoolLayer.h"
X
Xin Pan 已提交
16
#include "paddle/legacy/math/Matrix.h"
X
Xin Pan 已提交
17 18
#include "paddle/legacy/utils/Logging.h"
#include "paddle/legacy/utils/Stat.h"
Z
zhangjinchao01 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31

namespace paddle {

bool CudnnPoolLayer::typeCheck(const std::string &poolType,
                               hl_pooling_mode_t *mode) {
  if (poolType == "cudnn-max-pool") {
    if (mode) {
      *mode = HL_POOLING_MAX;
    }
  } else if (poolType == "cudnn-avg-pool") {
    if (mode) {
      *mode = HL_POOLING_AVERAGE;
    }
32
  } else if (poolType == "cudnn-avg-incl-pad-pool") {
Z
zhangjinchao01 已提交
33
    if (mode) {
34
      *mode = HL_POOLING_AVERAGE_INCLUDE_PADDING;
Z
zhangjinchao01 已提交
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
    }
  } else {
    return false;
  }

  return true;
}

CudnnPoolLayer::CudnnPoolLayer(const LayerConfig &config) : PoolLayer(config) {
  const std::string &pool_type = config.inputs(0).pool_conf().pool_type();
  CHECK_EQ(CudnnPoolLayer::typeCheck(pool_type, &mode_), true);
}

bool CudnnPoolLayer::init(const LayerMap &layerMap,
                          const ParameterMap &parameterMap) {
  PoolLayer::init(layerMap, parameterMap);

  CHECK(useGpu_) << "CudnnPoolLayer only support gpu";

  hl_create_tensor_descriptor(&inputDesc_);
  hl_create_tensor_descriptor(&outputDesc_);

  windowHeight = sizeY_;
  windowWidth = sizeX_;
  heightPadding = confPaddingY_;
  widthPadding = confPadding_;
  strideHeight = strideY_;
  strideWidth = stride_;

64 65 66 67 68 69 70
  hl_create_pooling_descriptor(&poolingDesc_,
                               mode_,
                               windowHeight,
                               windowWidth,
                               heightPadding,
                               widthPadding,
                               strideHeight,
71
                               strideWidth);
Z
zhangjinchao01 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

  return true;
}

void CudnnPoolLayer::reshape(int batchSize) {
  imageH_ = inputLayers_[0]->getOutput().getFrameHeight();
  imageW_ = inputLayers_[0]->getOutput().getFrameWidth();
  if (imageH_ == 0) {
    imageH_ = imgSizeY_;
  }
  if (imageW_ == 0) {
    imageW_ = imgSize_;
  }
  CHECK_EQ(inputLayers_[0]->getOutput().value->getWidth(),
           channels_ * imageH_ * imageW_);
87 88 89 90
  outputH_ = outputSize(imageH_,
                        sizeY_,
                        confPaddingY_,
                        strideY_,
91 92 93
                        /* caffeMode */ false);
  outputW_ =
      outputSize(imageW_, sizeX_, confPadding_, stride_, /* caffeMode */ false);
Z
zhangjinchao01 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  getOutput().setFrameHeight(outputH_);
  getOutput().setFrameWidth(outputW_);

  hl_tensor_reshape(inputDesc_, batchSize, channels_, imageH_, imageW_);
  hl_tensor_reshape(outputDesc_, batchSize, channels_, outputH_, outputW_);
}

void CudnnPoolLayer::forward(PassType passType) {
  Layer::forward(passType);

  CHECK(inputLayers_[0]->getOutputValue()->useGpu());
  int batchSize = inputLayers_[0]->getOutputValue()->getHeight();
  reshape(batchSize);
  resetOutput(batchSize, outputH_ * outputW_ * channels_);

  real *inputData = getInputValue(0)->getData();
  real *outData = getOutputValue()->getData();
111
  hl_pooling_forward(inputDesc_, inputData, outputDesc_, outData, poolingDesc_);
Z
zhangjinchao01 已提交
112 113 114 115 116 117 118 119 120 121 122 123
}

void CudnnPoolLayer::backward(const UpdateCallback &callback) {
  (void)callback;
  if (NULL == getInputGrad(0)) {
    return;
  }

  real *inputData = getInputValue(0)->getData();
  real *inputGrad = getInputGrad(0)->getData();
  real *outData = getOutputValue()->getData();
  real *outGrad = getOutputGrad()->getData();
124 125 126 127 128 129 130
  hl_pooling_backward(inputDesc_,
                      inputData,
                      inputGrad,
                      outputDesc_,
                      outData,
                      outGrad,
                      poolingDesc_);
Z
zhangjinchao01 已提交
131 132 133 134 135 136 137 138 139
}

CudnnPoolLayer::~CudnnPoolLayer() {
  hl_destroy_tensor_descriptor(inputDesc_);
  hl_destroy_tensor_descriptor(outputDesc_);
  hl_destroy_pooling_descriptor(poolingDesc_);
}

}  // namespace paddle