CudnnConvLayer.cpp 3.9 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. */

Y
Yu Yang 已提交
15
#include "CudnnConvLayer.h"
Z
zhangjinchao01 已提交
16 17 18 19 20 21 22 23 24
#include "paddle/utils/Logging.h"
#include "paddle/utils/Stat.h"

namespace paddle {

REGISTER_LAYER(cudnn_conv, CudnnConvLayer);

bool CudnnConvLayer::init(const LayerMap &layerMap,
                          const ParameterMap &parameterMap) {
25
  if (!ConvBaseLayer::init(layerMap, parameterMap)) return false;
Z
zhangjinchao01 已提交
26 27
  CHECK(useGpu_) << "CudnnConvLayer only support gpu";

28 29 30
  CHECK_EQ(inputLayers_.size(), parameters_.size());
  projections_.reserve(inputLayers_.size());
  projConf_.reserve(inputLayers_.size());
Z
zhangjinchao01 已提交
31

32 33 34
  numFilters_ = config_.num_filters();
  CHECK(config_.shared_biases());
  for (size_t i = 0; i < inputLayers_.size(); i++) {
35
    ProjectionConfig *conf = new ProjectionConfig();
36 37
    conf->set_type("conv");
    conf->set_num_filters(numFilters_);
38
    ConvConfig *convConf = conf->mutable_conv_conf();
39
    *convConf = *(config_.mutable_inputs(i)->mutable_conv_conf());
40 41 42
    conf->set_input_size(getPrev(i)->getSize());
    conf->set_output_size(getSize());
    projConf_.emplace_back(conf);
43 44
    projections_.emplace_back(
        Projection::create(*projConf_[i], parameters_[i], useGpu_));
Z
zhangjinchao01 已提交
45 46 47 48
  }

  if (biases_.get() && sharedBiases_) {
    hl_create_tensor_descriptor(&biasDesc_);
49
    hl_create_tensor_descriptor(&outputDesc_);
Z
zhangjinchao01 已提交
50 51 52 53 54 55 56 57 58
    hl_tensor_reshape(biasDesc_, 1, numFilters_ / groups_[0], 1, 1);
    biasOffset_ = numFilters_ / groups_[0];
  }

  return true;
}

void CudnnConvLayer::forward(PassType passType) {
  Layer::forward(passType);
59 60 61

  int batchSize = getInput(0).getBatchSize();
  resetOutput(batchSize, calOutputSize());
Z
zhangjinchao01 已提交
62 63

  for (size_t i = 0; i != inputLayers_.size(); ++i) {
64
    projections_[i]->forward(&getInput(i), &getOutput(), passType);
Z
zhangjinchao01 已提交
65 66 67 68
  }

  if (biases_) {
    REGISTER_TIMER_INFO("CudnnConvBiasTimer", getName().c_str());
69
    int batchSize = inputLayers_[0]->getOutputValue()->getHeight();
70 71 72 73 74 75 76 77 78
    hl_tensor_reshape(outputDesc_,
                      batchSize,
                      numFilters_ / groups_[0],
                      outputH_[0],
                      outputW_[0],
                      numFilters_ * outputH_[0] * outputW_[0],
                      outputH_[0] * outputW_[0],
                      outputW_[0],
                      1);
79
    outputOffset_ = getOutputValue()->getWidth() / groups_[0];
Z
zhangjinchao01 已提交
80 81
    for (int g = 0; g < groups_[0]; ++g) {
      real *biasData = biases_->getW()->getData() + biasOffset_ * g;
82
      real *outData = getOutputValue()->getData() + outputOffset_ * g;
83 84
      hl_convolution_forward_add_bias(
          biasDesc_, biasData, outputDesc_, outData);
Z
zhangjinchao01 已提交
85 86 87
    }
  }

88
  forwardActivation();
Z
zhangjinchao01 已提交
89 90 91 92 93 94 95
}

void CudnnConvLayer::backward(const UpdateCallback &callback) {
  backwardActivation();

  if (biases_ && biases_->getWGrad()) {
    REGISTER_TIMER_INFO("CudnnConvBpBiasTimer", getName().c_str());
96 97 98 99 100
    for (int g = 0; g < groups_[0]; ++g) {
      real *biasGrad = biases_->getWGrad()->getData() + biasOffset_ * g;
      real *outGrad = getOutputGrad()->getData() + outputOffset_ * g;
      hl_convolution_backward_bias(biasDesc_, biasGrad, outputDesc_, outGrad);
    }
Z
zhangjinchao01 已提交
101 102 103 104
    biases_->getParameterPtr()->incUpdate(callback);
  }

  for (size_t i = 0; i != inputLayers_.size(); ++i) {
105
    projections_[i]->backward(callback);
Z
zhangjinchao01 已提交
106 107 108 109
  }
}

CudnnConvLayer::~CudnnConvLayer() {
110
  if (biases_) {
Z
zhangjinchao01 已提交
111
    hl_destroy_tensor_descriptor(biasDesc_);
112
    hl_destroy_tensor_descriptor(outputDesc_);
Z
zhangjinchao01 已提交
113 114 115 116
  }
}

}  // namespace paddle