ConvBaseProjection.cpp 6.5 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 16 17 18 19

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 "ConvBaseProjection.h"
#include "paddle/utils/Stat.h"

namespace paddle {

Y
yangyaming 已提交
20
ThreadLocalD<std::vector<MemoryHandlePtr>> ConvBaseProjection::convMem_;
W
wangyang59 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

ConvBaseProjection::ConvBaseProjection(const ProjectionConfig &config,
                                       ParameterPtr parameter,
                                       bool useGpu)
    : Projection(config, parameter, useGpu) {
  CHECK(useGpu);  // only support GPU
  getConvParams();
  initCudnn();

  size_t height = filterH_ * filterW_ * channels_ / groups_;
  size_t width = numFilters_;
  weight_.reset(new Weight(height, width, parameter));
  weightOffset_ = height * width / groups_;
}

void ConvBaseProjection::getConvParams() {
  const ConvConfig &conf = config_.conv_conf();
  paddingH_ = conf.padding_y();
  paddingW_ = conf.padding();

  strideH_ = conf.stride_y();
  strideW_ = conf.stride();

44 45 46 47 48
  dilationH_ = conf.dilation_y();
  dilationW_ = conf.dilation();
  CHECK_GT(dilationH_, 0);
  CHECK_GT(dilationW_, 0);

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
  filterH_ = conf.filter_size_y();
  filterW_ = conf.filter_size();

  configImgH_ = conf.has_img_size_y() ? conf.img_size_y() : conf.img_size();
  configImgW_ = conf.img_size();

  configOutH_ = conf.has_output_y() ? conf.output_y() : conf.output_x();
  configOutW_ = conf.output_x();

  configChannels_ = conf.channels();
  configNumFilters_ = config_.num_filters();

  isDeconv_ = (config_.type() == "conv") ? false : true;

  channels_ = (isDeconv_) ? configNumFilters_ : configChannels_;
  numFilters_ = (isDeconv_) ? configChannels_ : configNumFilters_;

  groups_ = conf.groups();
  CHECK_EQ(channels_ % groups_, 0);
  CHECK_EQ(numFilters_ % groups_, 0);
}

void ConvBaseProjection::initCudnn() {
  hl_create_filter_descriptor(&filterDesc_,
                              channels_ / groups_,
                              numFilters_ / groups_,
                              filterH_,
                              filterW_);
  hl_create_tensor_descriptor(&imageDesc_);
  hl_create_tensor_descriptor(&outputDesc_);
  hl_create_convolution_descriptor(&convDesc_,
                                   imageDesc_,
                                   filterDesc_,
                                   paddingH_,
                                   paddingW_,
                                   strideH_,
85 86 87
                                   strideW_,
                                   dilationH_,
                                   dilationW_);
W
wangyang59 已提交
88 89 90 91 92 93 94 95 96 97 98 99

  // initialize all to default algorithms
  fwdAlgo_ = 0;
  bwdFilterAlgo_ = 0;
  bwdDataAlgo_ = 0;
  fwdLimitBytes_ = 0;
  bwdDataLimitBytes_ = 0;
  bwdFilterLimitBytes_ = 0;
  workSpaceInBytes_ = 0;
}

void ConvBaseProjection::reshapeTensorDesc(int batchSize) {
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  // The stride between two consecutive samples in the output of ConvProjection
  // may not be numFilters_ * outputH_ * outputW_ (conv) or
  // channels_ * imageH_ * imageW_ (deconv)
  // for example, in the case of layer ConcatenateLayer2 with two
  // ConvProjection, the stride is the output_size of layer ConcatenateLayer2.
  // So the calculation of nStride is different from CudnnConvLayer.
  size_t nStrideImage, nStrideOutput;
  if (isDeconv_) {
    nStrideImage = out_->value->getStride();
    nStrideOutput = numFilters_ * outputH_ * outputW_;
  } else {
    nStrideImage = channels_ * imageH_ * imageW_;
    nStrideOutput = out_->value->getStride();
  }

W
wangyang59 已提交
115 116 117 118 119
  hl_tensor_reshape(imageDesc_,
                    batchSize,
                    channels_ / groups_,
                    imageH_,
                    imageW_,
120
                    nStrideImage,
W
wangyang59 已提交
121 122 123 124 125 126 127 128 129
                    imageH_ * imageW_,
                    imageW_,
                    1);

  hl_tensor_reshape(outputDesc_,
                    batchSize,
                    numFilters_ / groups_,
                    outputH_,
                    outputW_,
130
                    nStrideOutput,
W
wangyang59 已提交
131 132 133
                    outputH_ * outputW_,
                    outputW_,
                    1);
134 135 136 137 138 139 140

  hl_reset_convolution_descriptor(convDesc_,
                                  imageDesc_,
                                  filterDesc_,
                                  paddingH_,
                                  paddingW_,
                                  strideH_,
141 142 143
                                  strideW_,
                                  dilationH_,
                                  dilationW_);
W
wangyang59 已提交
144 145 146 147 148
}

void ConvBaseProjection::reshape(int batchSize) {
  size_t width = calOutputSize();
  CHECK_EQ(width, out_->value->getWidth());
W
wangyang59 已提交
149
  CHECK_EQ(calInputSize(), in_->value->getWidth());
W
wangyang59 已提交
150

W
wanghaoshuang 已提交
151
  reshapeTensorDesc(batchSize);
152 153 154 155
  bool useDilation = false;
  if (dilationH_ > 1 || dilationW_ > 1) {
    useDilation = true;
  }
W
wanghaoshuang 已提交
156 157 158 159 160 161 162 163 164
  hl_conv_workspace(imageDesc_,
                    outputDesc_,
                    filterDesc_,
                    convDesc_,
                    &fwdAlgo_,
                    &fwdLimitBytes_,
                    &bwdDataAlgo_,
                    &bwdDataLimitBytes_,
                    &bwdFilterAlgo_,
165 166
                    &bwdFilterLimitBytes_,
                    useDilation);
W
wanghaoshuang 已提交
167 168 169 170 171 172 173 174

  size_t maxWorkSpace = 0;
  maxWorkSpace = std::max(fwdLimitBytes_, bwdDataLimitBytes_);
  maxWorkSpace = std::max(maxWorkSpace, bwdFilterLimitBytes_);
  workSpaceInBytes_ = maxWorkSpace;

  VLOG(3) << getName() << " Fwd / BwdData / BwdFilter algo: " << fwdAlgo_
          << " / " << bwdDataAlgo_ << " / " << bwdFilterAlgo_;
W
wangyang59 已提交
175 176 177
}

void *ConvBaseProjection::getSpaceBytes(size_t size) {
Y
yangyaming 已提交
178
  std::vector<MemoryHandlePtr> &convMem = *convMem_;
W
wangyang59 已提交
179 180 181 182 183 184
  if (convMem.empty()) {
    int numDevices = hl_get_device_count();
    convMem.resize(numDevices);
  }

  int devId = hl_get_device();
Y
yangyaming 已提交
185 186 187
  MemoryHandlePtr localMem = convMem[devId];
  if (NULL == localMem || size > localMem->getAllocSize()) {
    localMem = std::make_shared<GpuMemoryHandle>(size);
W
wangyang59 已提交
188
  }
Y
yangyaming 已提交
189
  return localMem->getBuf();
W
wangyang59 已提交
190 191 192 193 194 195 196 197 198 199
}

ConvBaseProjection::~ConvBaseProjection() {
  hl_destroy_tensor_descriptor(imageDesc_);
  hl_destroy_tensor_descriptor(outputDesc_);
  hl_destroy_filter_descriptor(filterDesc_);
  hl_destroy_convolution_descriptor(convDesc_);
}

}  // namespace paddle