ConvOperator.cpp 4.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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. */

15
#include "ConvOperator.h"
Y
Yu Yang 已提交
16 17
#include "paddle/math/MathUtils.h"
#include "paddle/math/Matrix.h"
Z
zhangjinchao01 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31

namespace paddle {

/**
 * @brief ConvOperator takes two inputs to perform the convolution.
 * The first input is the image, and the second input is the convolution kernel.
 * The height of data for two inputs are the same. Each data of the first input
 * is convolved with each data of the second input indepedently.
 *
 * The config file api is conv_operator.
 */

REGISTER_OPERATOR(conv, ConvOperator);

W
wangyang59 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
void ConvOperator::reshape(int batchSize) {
  imageH_ = ins_[0]->getFrameHeight();
  imageW_ = ins_[0]->getFrameWidth();
  if (imageH_ == 0) imageH_ = imgSizeY_;
  if (imageW_ == 0) imageW_ = imgSize_;
  outputH_ = outputSize(imageH_, filterSizeY_, paddingY_, strideY_, caffeMode_);
  outputW_ = outputSize(imageW_, filterSize_, padding_, stride_, caffeMode_);
  /// Check that the outputSizes are consistent with config
  CHECK_EQ(outputH_, outputY_);
  CHECK_EQ(outputW_, outputX_);
  out_->setFrameHeight(outputH_);
  out_->setFrameWidth(outputW_);

  reshapeImageDescriptors();

  inputOffset_ = channels_ * imageH_ * imageW_;
  outputOffset_ = numFilters_ * outputH_ * outputW_;
  weightOffset_ = numFilters_ * channels_ * filterSize_ * filterSizeY_;

  if (!isSelectAlgo_) {
    allocConvWorkSpace();
  }

  isSelectAlgo_ = true;
}

Z
zhangjinchao01 已提交
58 59 60 61 62
void ConvOperator::forward() {
  size_t batchSize = ins_[0]->value->getHeight();
  reshape(batchSize);
  CHECK_EQ(ins_[1]->value->getHeight(), batchSize);
  checkFilterSize(ins_[1]->value);
63 64 65 66 67
  Matrix::resizeOrCreate(out_->value,
                         batchSize,
                         outputH_ * outputW_ * numFilters_,
                         false,
                         useGpu_);
Z
zhangjinchao01 已提交
68 69 70 71 72 73
  {
    AsyncGpuBlock block;
    for (size_t batchId = 0; batchId < batchSize; ++batchId) {
      real *inputData = ins_[0]->value->getData() + inputOffset_ * batchId;
      real *wgtData = ins_[1]->value->getData() + weightOffset_ * batchId;
      real *outData = out_->value->getData() + outputOffset_ * batchId;
74
      hl_convolution_forward(imageDesc_,
75 76 77 78 79 80 81 82 83
                             inputData,
                             outputDesc_,
                             outData,
                             filterDesc_,
                             wgtData,
                             convDesc_,
                             workSpace_,
                             workSpaceInBytes_,
                             fwdAlgo_);
Z
zhangjinchao01 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96
    }
  }
}

void ConvOperator::backward() {
  size_t batchSize = ins_[0]->value->getHeight();
  {
    AsyncGpuBlock block;
    for (size_t batchId = 0; batchId < batchSize; ++batchId) {
      real *outGrad = out_->grad->getData() + outputOffset_ * batchId;
      if (ins_[1]->grad) {
        real *inputData = ins_[0]->value->getData() + inputOffset_ * batchId;
        real *weightGrad = ins_[1]->grad->getData() + weightOffset_ * batchId;
97
        hl_convolution_backward_filter(imageDesc_,
98 99 100 101 102 103 104 105
                                       inputData,
                                       outputDesc_,
                                       outGrad,
                                       filterDesc_,
                                       weightGrad,
                                       convDesc_,
                                       workSpace_,
                                       workSpaceInBytes_,
106
                                       bwdFilterAlgo_);
Z
zhangjinchao01 已提交
107 108 109 110 111 112
      }

      MatrixPtr preGrad = ins_[0]->grad;
      if (NULL != preGrad) {
        real *inputGrad = preGrad->getData() + inputOffset_ * batchId;
        real *wgtData = ins_[1]->value->getData() + weightOffset_ * batchId;
113
        hl_convolution_backward_data(imageDesc_,
114 115 116 117 118 119 120 121 122
                                     inputGrad,
                                     outputDesc_,
                                     outGrad,
                                     filterDesc_,
                                     wgtData,
                                     convDesc_,
                                     workSpace_,
                                     workSpaceInBytes_,
                                     bwdDataAlgo_);
Z
zhangjinchao01 已提交
123 124 125 126 127 128
      }
    }
  }
}

}  // namespace paddle