GemmConvOp.cpp 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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 "ConvOp.h"
16
#include "GemmFunctor.h"
17
#include "Im2Col.h"
18 19 20 21 22
#include "paddle/math/MemoryHandle.h"

namespace paddle {

/*
23
 * \brief Forward calculation of convolution.
24 25 26 27 28 29 30 31
 */
template <DeviceType Device>
class GemmConvFunction : public ConvFunctionBase {
public:
  void init(const FuncConfig& config) override {
    ConvFunctionBase::init(config);
  }

H
hedaoyuan 已提交
32 33 34 35 36 37 38 39
  virtual void check(const BufferArgs& inputs,
                     const BufferArgs& outputs) override {
    const TensorShape& input = inputs[0].shape();
    const TensorShape& filter = inputs[1].shape();
    const TensorShape& output = outputs[0].shape();
    checkShape(input, filter, output);
  }

40
  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
41 42
    CHECK_EQ(numInputs_, inputs.size());
    CHECK_EQ(numOutputs_, outputs.size());
H
hedaoyuan 已提交
43
    check(inputs, outputs);
44 45 46 47 48 49 50 51 52 53 54 55
    // TODO(hedaoyuan): Need to define some index macros,
    // to avoid useing 0 and 1.
    const TensorShape& input = inputs[0].shape();
    const TensorShape& filter = inputs[1].shape();
    const TensorShape& output = outputs[0].shape();

    real beta;
    if (outputs[0].getArgType() == ADD_TO) {
      beta = 1.0;
    } else {
      beta = 0.0;
    }
56

H
hedaoyuan 已提交
57 58 59 60 61 62 63 64 65
    size_t batchSize = input[0];
    size_t inputChannels = input[1];
    size_t inputHeight = input[2];
    size_t inputWidth = input[3];
    size_t filterHeight = getFilterHeight(filter);
    size_t filterWidth = getFilterWidth(filter);
    size_t outputChannels = output[1];
    size_t outputHeight = output[2];
    size_t outputWidth = output[3];
66 67 68 69

    real* inputData = inputs[0].data<real>();
    real* filterData = inputs[1].data<real>();
    real* outputData = outputs[0].data<real>();
70 71 72 73 74 75 76 77 78
    TensorShape imShape =
        TensorShape({inputChannels / groups_, inputHeight, inputWidth});
    TensorShape colShape = TensorShape({inputChannels / groups_,
                                        filterHeight,
                                        filterWidth,
                                        outputHeight,
                                        outputWidth});

    resizeBuffer<Device>(colShape.getElements());
79 80
    real* colData = reinterpret_cast<real*>(memory_->getBuf());

81
    Im2ColFunctor<kCFO, Device, real> im2col;
82
    GemmFunctor<Device, real> gemm;
83
    size_t inputOffset = imShape.getElements();
84 85
    size_t outputOffset =
        (outputChannels / groups_) * outputHeight * outputWidth;
H
hedaoyuan 已提交
86 87
    size_t filterOffset = filter.getElements() / groups_;

88
    for (size_t i = 0; i < batchSize; i++) {
89
      for (size_t g = 0; g < groups_; g++) {
90
        im2col(inputData + g * inputOffset,
91 92 93
               imShape,
               colData,
               colShape,
94 95 96
               strideH(),
               strideW(),
               paddingH(),
97
               paddingW());
98

H
Bug fix  
hedaoyuan 已提交
99
        int M = outputChannels / groups_;
100
        int N = outputHeight * outputWidth;
H
Bug fix  
hedaoyuan 已提交
101
        int K = inputChannels / groups_ * filterHeight * filterWidth;
102 103 104
        gemm(CblasNoTrans,
             CblasNoTrans,
             M,
105 106 107 108 109 110 111
             N,
             K,
             1.0f,
             filterData + g * filterOffset,
             K,
             colData,
             N,
112
             beta,
113 114
             outputData + g * outputOffset,
             N);
115
      }
H
hedaoyuan 已提交
116 117
      inputData += inputChannels * inputHeight * inputWidth;
      outputData += outputChannels * outputHeight * outputWidth;
118 119 120 121
    }
  }
};

122 123 124 125 126 127 128 129 130 131
/*
 * \brief Backward input calculation of convolution.
 */
template <DeviceType Device>
class GemmConvGradInputFunction : public ConvFunctionBase {
public:
  void init(const FuncConfig& config) override {
    ConvFunctionBase::init(config);
  }

H
hedaoyuan 已提交
132 133 134 135 136 137 138 139
  virtual void check(const BufferArgs& inputs,
                     const BufferArgs& outputs) override {
    const TensorShape& output = inputs[0].shape();
    const TensorShape& filter = inputs[1].shape();
    const TensorShape& input = outputs[0].shape();
    checkShape(input, filter, output);
  }

140 141 142
  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
    CHECK_EQ(numInputs_, inputs.size());
    CHECK_EQ(numOutputs_, outputs.size());
H
hedaoyuan 已提交
143
    check(inputs, outputs);
H
hedaoyuan 已提交
144 145 146
    // Since the implementation of Col2ImFunctor is ADD_TO,
    // this function only supports ADD_TO mode.
    CHECK_EQ(outputs[0].getArgType(), ADD_TO);
147
    const TensorShape& output = inputs[0].shape();
148
    const TensorShape& filter = inputs[1].shape();
149 150 151 152 153 154
    const TensorShape& input = outputs[0].shape();

    size_t batchSize = input[0];
    size_t inputChannels = input[1];
    size_t inputHeight = input[2];
    size_t inputWidth = input[3];
H
hedaoyuan 已提交
155 156
    size_t filterHeight = getFilterHeight(filter);
    size_t filterWidth = getFilterWidth(filter);
157 158 159 160 161 162 163
    size_t outputChannels = output[1];
    size_t outputHeight = output[2];
    size_t outputWidth = output[3];

    real* outputGrad = inputs[0].data<real>();
    real* filterData = inputs[1].data<real>();
    real* inputGrad = outputs[0].data<real>();
164 165 166 167 168 169 170 171 172
    TensorShape imShape =
        TensorShape({inputChannels / groups_, inputHeight, inputWidth});
    TensorShape colShape = TensorShape({inputChannels / groups_,
                                        filterHeight,
                                        filterWidth,
                                        outputHeight,
                                        outputWidth});

    resizeBuffer<Device>(colShape.getElements());
173 174
    real* colData = reinterpret_cast<real*>(memory_->getBuf());

175
    Col2ImFunctor<kCFO, Device, real> col2im;
176
    GemmFunctor<Device, real> gemm;
177
    size_t inputOffset = imShape.getElements();
H
format  
hedaoyuan 已提交
178
    size_t outputOffset =
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
        (outputChannels / groups_) * outputHeight * outputWidth;
    size_t filterOffset = filter.getElements() / groups_;

    for (size_t i = 0; i < batchSize; i++) {
      for (size_t g = 0; g < groups_; g++) {
        int K = outputChannels / groups_;
        int N = outputHeight * outputWidth;
        int M = inputChannels / groups_ * filterHeight * filterWidth;
        gemm(CblasTrans,
             CblasNoTrans,
             M,
             N,
             K,
             1.0f,
             filterData + g * filterOffset,
             M,
             outputGrad + g * outputOffset,
             N,
             0.0f,
             colData,
             N);
200 201 202 203
        col2im(inputGrad + g * inputOffset,
               imShape,
               colData,
               colShape,
204 205 206
               strideH(),
               strideW(),
               paddingH(),
207
               paddingW());
208 209 210 211
      }
      inputGrad += inputChannels * inputHeight * inputWidth;
      outputGrad += outputChannels * outputHeight * outputWidth;
    }
212 213 214 215 216 217 218 219 220 221 222 223 224
  }
};

/*
 * \brief Backward filter calculation of convolution.
 */
template <DeviceType Device>
class GemmConvGradFilterFunction : public ConvFunctionBase {
public:
  void init(const FuncConfig& config) override {
    ConvFunctionBase::init(config);
  }

H
hedaoyuan 已提交
225 226 227 228 229 230 231 232
  virtual void check(const BufferArgs& inputs,
                     const BufferArgs& outputs) override {
    const TensorShape& output = inputs[0].shape();
    const TensorShape& input = inputs[1].shape();
    const TensorShape& filter = outputs[0].shape();
    checkShape(input, filter, output);
  }

233 234 235
  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
    CHECK_EQ(numInputs_, inputs.size());
    CHECK_EQ(numOutputs_, outputs.size());
H
hedaoyuan 已提交
236
    check(inputs, outputs);
237
    const TensorShape& output = inputs[0].shape();
238
    const TensorShape& input = inputs[1].shape();
239 240
    const TensorShape& filter = outputs[0].shape();

241 242 243 244 245 246 247
    real beta;
    if (outputs[0].getArgType() == ADD_TO) {
      beta = 1.0;
    } else {
      beta = 0.0;
    }

248 249 250 251
    size_t batchSize = input[0];
    size_t inputChannels = input[1];
    size_t inputHeight = input[2];
    size_t inputWidth = input[3];
H
hedaoyuan 已提交
252 253
    size_t filterHeight = getFilterHeight(filter);
    size_t filterWidth = getFilterWidth(filter);
254 255 256 257 258 259 260
    size_t outputChannels = output[1];
    size_t outputHeight = output[2];
    size_t outputWidth = output[3];

    real* outputGrad = inputs[0].data<real>();
    real* inputData = inputs[1].data<real>();
    real* filterGrad = outputs[0].data<real>();
261 262 263 264 265 266 267 268 269
    TensorShape imShape =
        TensorShape({inputChannels / groups_, inputHeight, inputWidth});
    TensorShape colShape = TensorShape({inputChannels / groups_,
                                        filterHeight,
                                        filterWidth,
                                        outputHeight,
                                        outputWidth});

    resizeBuffer<Device>(colShape.getElements());
270 271
    real* colData = reinterpret_cast<real*>(memory_->getBuf());

272
    Im2ColFunctor<kCFO, Device, real> im2col;
273
    GemmFunctor<Device, real> gemm;
274
    size_t inputOffset = imShape.getElements();
275 276 277 278 279 280
    size_t outputOffset =
        (outputChannels / groups_) * outputHeight * outputWidth;
    size_t filterOffset = filter.getElements() / groups_;
    for (size_t i = 0; i < batchSize; i++) {
      for (size_t g = 0; g < groups_; g++) {
        im2col(inputData + g * inputOffset,
281 282 283
               imShape,
               colData,
               colShape,
284 285 286
               strideH(),
               strideW(),
               paddingH(),
287
               paddingW());
288 289 290 291 292 293 294 295 296 297 298 299 300 301

        int M = outputChannels / groups_;
        int K = outputHeight * outputWidth;
        int N = inputChannels / groups_ * filterHeight * filterWidth;
        gemm(CblasNoTrans,
             CblasTrans,
             M,
             N,
             K,
             1.0f,
             outputGrad + g * outputOffset,
             K,
             colData,
             K,
302
             i == 0 ? beta : 1.0f,
303 304 305
             filterGrad + g * filterOffset,
             N);
      }
306 307
      inputData += inputChannels * inputHeight * inputWidth;
      outputGrad += outputChannels * outputHeight * outputWidth;
308
    }
309 310 311
  }
};

312
REGISTER_TYPED_FUNC(GemmConv, CPU, GemmConvFunction);
313 314
REGISTER_TYPED_FUNC(GemmConvGradInput, CPU, GemmConvGradInputFunction);
REGISTER_TYPED_FUNC(GemmConvGradFilter, CPU, GemmConvGradFilterFunction);
H
hedaoyuan 已提交
315
#ifndef PADDLE_ONLY_CPU
316
REGISTER_TYPED_FUNC(GemmConv, GPU, GemmConvFunction);
317 318
REGISTER_TYPED_FUNC(GemmConvGradInput, GPU, GemmConvGradInputFunction);
REGISTER_TYPED_FUNC(GemmConvGradFilter, GPU, GemmConvGradFilterFunction);
H
hedaoyuan 已提交
319
#endif
320 321

}  // namespace paddle