ConvProjection.cpp 4.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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 "ConvProjection.h"
X
Xin Pan 已提交
16
#include "paddle/legacy/utils/Stat.h"
17 18 19 20 21

namespace paddle {

REGISTER_PROJECTION(conv, ConvProjection);

W
wangyang59 已提交
22 23 24 25 26 27
size_t ConvProjection::calOutputSize() {
  imageH_ = in_->getFrameHeight();
  imageW_ = in_->getFrameWidth();
  if (imageH_ == 0) imageH_ = configImgH_;
  if (imageW_ == 0) imageW_ = configImgW_;
  outputH_ = outputSize(imageH_,
28
                        (filterH_ - 1) * dilationH_ + 1,
W
wangyang59 已提交
29 30 31 32
                        paddingH_,
                        strideH_,
                        /* caffeMode */ true);
  outputW_ = outputSize(imageW_,
33
                        (filterW_ - 1) * dilationW_ + 1,
W
wangyang59 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
                        paddingW_,
                        strideW_,
                        /* caffeMode */ true);

  const_cast<Argument *>(out_)->setFrameHeight(outputH_);
  const_cast<Argument *>(out_)->setFrameWidth(outputW_);

  inputOffset_ = (configChannels_ / groups_) * imageH_ * imageW_;
  outputOffset_ = (configNumFilters_ / groups_) * outputH_ * outputW_;
  return outputH_ * outputW_ * configNumFilters_;
}

size_t ConvProjection::calInputSize() {
  return static_cast<size_t>(configChannels_ * imageH_ * imageW_);
}

50 51 52 53
void ConvProjection::forward() {
  int batchSize = in_->value->getHeight();
  reshape(batchSize);

54
  void *workSpace = NULL;
55 56 57 58 59 60 61 62 63 64
  if (workSpaceInBytes_ > 0) {
    workSpace = getSpaceBytes(workSpaceInBytes_);
  }

  for (int g = 0; g < groups_; ++g) {
    REGISTER_TIMER_INFO("CudnnConvFwTimer", getName().c_str());

    real *inputData = in_->value->getData() + g * inputOffset_;
    real *wgtData = weight_->getW()->getData() + g * weightOffset_;
    real *outData = out_->value->getData() + g * outputOffset_;
W
wangyang59 已提交
65
    hl_convolution_forward(imageDesc_,
66 67 68 69 70 71 72 73 74
                           inputData,
                           outputDesc_,
                           outData,
                           filterDesc_,
                           wgtData,
                           convDesc_,
                           workSpace,
                           fwdLimitBytes_,
                           fwdAlgo_);
75 76 77
  }
}

78
void ConvProjection::backward(const UpdateCallback &callback) {
79 80
  REGISTER_TIMER_INFO("CudnnConvBpTimer", getName().c_str());

81
  void *workSpace = NULL;
82 83 84 85 86 87 88 89 90
  if (workSpaceInBytes_ > 0) {
    workSpace = getSpaceBytes(workSpaceInBytes_);
  }

  for (int g = 0; g < groups_; ++g) {
    real *outGrad = out_->grad->getData() + g * outputOffset_;
    if (weight_->getWGrad()) {
      real *inputData = in_->value->getData() + g * inputOffset_;
      real *weightGrad = weight_->getWGrad()->getData() + g * weightOffset_;
W
wangyang59 已提交
91
      hl_convolution_backward_filter(imageDesc_,
92 93 94 95 96 97 98 99 100
                                     inputData,
                                     outputDesc_,
                                     outGrad,
                                     filterDesc_,
                                     weightGrad,
                                     convDesc_,
                                     workSpace,
                                     bwdFilterLimitBytes_,
                                     bwdFilterAlgo_);
101 102 103 104 105
    }

    MatrixPtr preGrad = in_->grad;
    if (NULL != preGrad) {
      real *inputGrad = preGrad->getData() + g * inputOffset_;
106
      real *wgtData = weight_->getW()->getData() + g * weightOffset_;
W
wangyang59 已提交
107
      hl_convolution_backward_data(imageDesc_,
108 109 110 111 112 113 114 115 116
                                   inputGrad,
                                   outputDesc_,
                                   outGrad,
                                   filterDesc_,
                                   wgtData,
                                   convDesc_,
                                   workSpace,
                                   bwdDataLimitBytes_,
                                   bwdDataAlgo_);
117 118 119 120 121 122 123
    }
  }

  weight_->getParameterPtr()->incUpdate(callback);
}

}  // namespace paddle