ConvProjection.cpp 3.3 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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"
Y
Yu Yang 已提交
16
#include "paddle/utils/Stat.h"
17 18 19 20 21 22 23 24 25

namespace paddle {

REGISTER_PROJECTION(conv, ConvProjection);

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

26
  void *workSpace = NULL;
27 28 29 30 31 32 33 34 35 36
  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 已提交
37
    hl_convolution_forward(imageDesc_,
38 39 40 41 42 43 44 45 46
                           inputData,
                           outputDesc_,
                           outData,
                           filterDesc_,
                           wgtData,
                           convDesc_,
                           workSpace,
                           fwdLimitBytes_,
                           fwdAlgo_);
47 48 49
  }
}

50
void ConvProjection::backward(const UpdateCallback &callback) {
51 52
  REGISTER_TIMER_INFO("CudnnConvBpTimer", getName().c_str());

53
  void *workSpace = NULL;
54 55 56 57 58 59 60 61 62
  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 已提交
63
      hl_convolution_backward_filter(imageDesc_,
64 65 66 67 68 69 70 71 72
                                     inputData,
                                     outputDesc_,
                                     outGrad,
                                     filterDesc_,
                                     weightGrad,
                                     convDesc_,
                                     workSpace,
                                     bwdFilterLimitBytes_,
                                     bwdFilterAlgo_);
73 74 75 76 77
    }

    MatrixPtr preGrad = in_->grad;
    if (NULL != preGrad) {
      real *inputGrad = preGrad->getData() + g * inputOffset_;
78
      real *wgtData = weight_->getW()->getData() + g * weightOffset_;
W
wangyang59 已提交
79
      hl_convolution_backward_data(imageDesc_,
80 81 82 83 84 85 86 87 88
                                   inputGrad,
                                   outputDesc_,
                                   outGrad,
                                   filterDesc_,
                                   wgtData,
                                   convDesc_,
                                   workSpace,
                                   bwdDataLimitBytes_,
                                   bwdDataAlgo_);
89 90 91 92 93 94 95
    }
  }

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

}  // namespace paddle