ConvTransProjection.cpp 4.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wangyang59 已提交
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 "ConvTransProjection.h"
X
Xin Pan 已提交
16
#include "paddle/legacy/utils/Stat.h"
W
wangyang59 已提交
17 18 19 20

namespace paddle {

REGISTER_PROJECTION(convt, ConvTransProjection);
W
wangyang59 已提交
21 22 23 24 25 26
size_t ConvTransProjection::calOutputSize() {
  outputH_ = in_->getFrameHeight();
  outputW_ = in_->getFrameWidth();
  if (outputH_ == 0) outputH_ = configOutH_;
  if (outputW_ == 0) outputW_ = configOutW_;
  imageH_ = imageSize(outputH_,
W
wanghaoshuang 已提交
27
                      (filterH_ - 1) * dilationH_ + 1,
W
wangyang59 已提交
28 29 30 31 32
                      paddingH_,
                      strideH_,
                      /* caffeMode */ true);

  imageW_ = imageSize(outputW_,
W
wanghaoshuang 已提交
33
                      (filterW_ - 1) * dilationW_ + 1,
W
wangyang59 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
                      paddingW_,
                      strideW_,
                      /* caffeMode */ true);

  const_cast<Argument *>(out_)->setFrameHeight(imageH_);
  const_cast<Argument *>(out_)->setFrameWidth(imageW_);

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

size_t ConvTransProjection::calInputSize() {
  return static_cast<size_t>(configChannels_ * outputH_ * outputW_);
}
W
wangyang59 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

void ConvTransProjection::forward() {
  int batchSize = in_->value->getHeight();
  reshape(batchSize);

  void *workSpace = NULL;
  if (workSpaceInBytes_ > 0) {
    workSpace = getSpaceBytes(workSpaceInBytes_);
  }

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

    real *inData = in_->value->getData() + g * inputOffset_;
    real *wgtData = weight_->getW()->getData() + g * weightOffset_;
    real *outData = out_->value->getData() + g * outputOffset_;
    hl_convolution_backward_data(imageDesc_,
                                 outData,
                                 outputDesc_,
                                 inData,
                                 filterDesc_,
                                 wgtData,
                                 convDesc_,
                                 workSpace,
                                 bwdDataLimitBytes_,
                                 bwdDataAlgo_);
  }
}

void ConvTransProjection::backward(const UpdateCallback &callback) {
  REGISTER_TIMER_INFO("CudnnConvTransBpTimer", getName().c_str());

  void *workSpace = NULL;
  if (workSpaceInBytes_ > 0) {
    workSpace = getSpaceBytes(workSpaceInBytes_);
  }

  for (int g = 0; g < groups_; ++g) {
    real *outGrad = out_->grad->getData() + g * outputOffset_;
    if (weight_->getWGrad()) {
      real *inData = in_->value->getData() + g * inputOffset_;
      real *weightGrad = weight_->getWGrad()->getData() + g * weightOffset_;
      hl_convolution_backward_filter(imageDesc_,
                                     outGrad,
                                     outputDesc_,
                                     inData,
                                     filterDesc_,
                                     weightGrad,
                                     convDesc_,
                                     workSpace,
                                     bwdFilterLimitBytes_,
                                     bwdFilterAlgo_);
    }

    MatrixPtr preGrad = in_->grad;
    if (NULL != preGrad) {
      real *inGrad = preGrad->getData() + g * inputOffset_;
      real *wgtData = weight_->getW()->getData() + g * weightOffset_;
      hl_convolution_forward(imageDesc_,
                             outGrad,
                             outputDesc_,
                             inGrad,
                             filterDesc_,
                             wgtData,
                             convDesc_,
                             workSpace,
                             fwdLimitBytes_,
                             fwdAlgo_);
    }
  }

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

}  // namespace paddle