DepthwiseConvOp.cpp 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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. */

#include "DepthwiseConvOp.h"
16
#include "ConvOp.h"
17 18 19 20 21 22

namespace paddle {

template <class T>
class DepthwiseConvFunctor<DEVICE_TYPE_CPU, T> {
public:
23
  void operator()(const T* inputData,
24 25 26 27 28
                  const T* filterData,
                  int batchSize,
                  int outputChannels,
                  int outputHeight,
                  int outputWidth,
29
                  int inputChannels,
30 31
                  int inputHeight,
                  int inputWidth,
32
                  int filterMultiplier,
33 34 35 36 37 38 39
                  int filterHeight,
                  int filterWidth,
                  int strideH,
                  int strideW,
                  int paddingH,
                  int paddingW,
                  T* outputData) {
40
    // TODO(zhaolong) : cpu implementation of depthwise convolution
41 42 43 44 45 46
  }
};

template <class T>
class DepthwiseConvGradInputFunctor<DEVICE_TYPE_CPU, T> {
public:
47
  void operator()(const T* outputGrad,
48 49 50 51 52
                  const T* filterData,
                  int batchSize,
                  int outputChannels,
                  int outputHeight,
                  int outputWidth,
53
                  int inputChannels,
54 55
                  int inputHeight,
                  int inputWidth,
56
                  int filterMultiplier,
57 58 59 60 61 62 63
                  int filterHeight,
                  int filterWidth,
                  int strideH,
                  int strideW,
                  int paddingH,
                  int paddingW,
                  T* inputGrad) {}
64
  // TODO(zhaolong) : cpu implementation of depthwise convolution
65 66 67 68 69
};

template <class T>
class DepthwiseConvGradFilterFunctor<DEVICE_TYPE_CPU, T> {
public:
70
  void operator()(const T* outputGrad,
71 72 73 74 75
                  const T* inputData,
                  int batchSize,
                  int outputChannels,
                  int outputHeight,
                  int outputWidth,
76
                  int inputChannels,
77 78
                  int inputHeight,
                  int inputWidth,
79
                  int filterMultiplier,
80 81 82 83 84 85 86 87
                  int filterHeight,
                  int filterWidth,
                  int strideH,
                  int strideW,
                  int paddingH,
                  int paddingW,
                  T* colData,
                  T* filterGrad) {}
88
  // TODO(zhaolong) : cpu implementation of depthwise convolution
89 90 91
};

/*
92
 * \brief Forward calculation of depthwise convolution.
93 94 95 96 97 98 99 100
 */
template <DeviceType Device>
class DepthwiseConvFunction : public ConvFunctionBase {
public:
  void init(const FuncConfig& config) override {
    ConvFunctionBase::init(config);
  }

X
xzl 已提交
101
  void check(const BufferArgs& inputs, const BufferArgs& outputs) override {
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    const TensorShape& input = inputs[0].shape();
    const TensorShape& filter = inputs[1].shape();
    const TensorShape& output = outputs[0].shape();
    checkShape(input, filter, output);
  }

  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
    CHECK_EQ(numInputs_, inputs.size());
    CHECK_EQ(numOutputs_, outputs.size());
    check(inputs, outputs);

    const TensorShape& input = inputs[0].shape();
    const TensorShape& filter = inputs[1].shape();
    const TensorShape& output = outputs[0].shape();

    size_t batchSize = input[0];
118
    size_t inputChannels = input[1];
119 120
    size_t inputHeight = input[2];
    size_t inputWidth = input[3];
121 122 123 124 125
    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];
126
    size_t filterMultiplier = outputChannels / groups_;
X
xzl 已提交
127
    CHECK_EQ(inputChannels, groups_);
128 129 130 131 132 133

    real* inputData = inputs[0].data<real>();
    real* filterData = inputs[1].data<real>();
    real* outputData = outputs[0].data<real>();

    DepthwiseConvFunctor<Device, real> depthwiseConv;
134
    depthwiseConv(inputData,
135 136 137 138 139
                  filterData,
                  batchSize,
                  outputChannels,
                  outputHeight,
                  outputWidth,
140
                  inputChannels,
141 142
                  inputHeight,
                  inputWidth,
143
                  filterMultiplier,
144 145 146 147 148 149 150 151 152 153 154
                  filterHeight,
                  filterWidth,
                  strideH(),
                  strideW(),
                  paddingH(),
                  paddingW(),
                  outputData);
  }
};

/*
155
 * \brief Backward input calculation of depthwise convolution.
156 157 158 159 160 161 162 163
 */
template <DeviceType Device>
class DepthwiseConvGradInputFunction : public ConvFunctionBase {
public:
  void init(const FuncConfig& config) override {
    ConvFunctionBase::init(config);
  }

X
xzl 已提交
164
  void check(const BufferArgs& inputs, const BufferArgs& outputs) override {
165 166 167 168 169 170 171 172 173
    const TensorShape& output = inputs[0].shape();
    const TensorShape& filter = inputs[1].shape();
    const TensorShape& input = outputs[0].shape();
    checkShape(input, filter, output);
  }

  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
    CHECK_EQ(numInputs_, inputs.size());
    CHECK_EQ(numOutputs_, outputs.size());
174
    CHECK_EQ(outputs[0].getArgType(), ADD_TO);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    check(inputs, outputs);
    CHECK_EQ(outputs[0].getArgType(), ADD_TO);
    const TensorShape& output = inputs[0].shape();
    const TensorShape& filter = inputs[1].shape();
    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];
    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];
190
    size_t filterMultiplier = outputChannels / groups_;
X
xzl 已提交
191
    CHECK_EQ(inputChannels, groups_);
192 193 194 195 196 197

    real* outputGrad = inputs[0].data<real>();
    real* filterData = inputs[1].data<real>();
    real* inputGrad = outputs[0].data<real>();

    DepthwiseConvGradInputFunctor<Device, real> depthwiseConvGradInput;
198
    depthwiseConvGradInput(outputGrad,
199 200 201 202 203
                           filterData,
                           batchSize,
                           outputChannels,
                           outputHeight,
                           outputWidth,
204
                           inputChannels,
205 206
                           inputHeight,
                           inputWidth,
207
                           filterMultiplier,
208 209 210 211 212 213 214 215 216 217 218
                           filterHeight,
                           filterWidth,
                           strideH(),
                           strideW(),
                           paddingH(),
                           paddingW(),
                           inputGrad);
  }
};

/*
219
 * \brief Backward filter calculation of depthwise convolution.
220 221 222 223 224 225 226 227
 */
template <DeviceType Device>
class DepthwiseConvGradFilterFunction : public ConvFunctionBase {
public:
  void init(const FuncConfig& config) override {
    ConvFunctionBase::init(config);
  }

X
xzl 已提交
228
  void check(const BufferArgs& inputs, const BufferArgs& outputs) override {
229 230 231 232 233 234 235
    const TensorShape& output = inputs[0].shape();
    const TensorShape& input = inputs[1].shape();
    const TensorShape& filter = outputs[0].shape();
    checkShape(input, filter, output);
  }

  void calc(const BufferArgs& inputs, const BufferArgs& outputs) override {
236 237
    CHECK_EQ(numInputs_, inputs.size());
    CHECK_EQ(numOutputs_, outputs.size());
238
    CHECK_EQ(outputs[0].getArgType(), ADD_TO);
239 240 241 242 243 244 245 246 247 248 249 250 251 252
    check(inputs, outputs);
    const TensorShape& output = inputs[0].shape();
    const TensorShape& input = inputs[1].shape();
    const TensorShape& filter = outputs[0].shape();

    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];
253
    size_t filterMultiplier = outputChannels / groups_;
X
xzl 已提交
254
    CHECK_EQ(inputChannels, groups_);
255 256 257 258 259

    real* outputGrad = inputs[0].data<real>();
    real* inputData = inputs[1].data<real>();
    real* filterGrad = outputs[0].data<real>();

260 261
    int size = outputChannels * filterHeight * filterWidth * outputHeight *
               outputWidth;
262 263 264 265 266
    resizeBuffer<Device>(size);
    real* colData = reinterpret_cast<real*>(memory_->getBuf());

    DepthwiseConvGradFilterFunctor<Device, real> depthwiseConvGradFilter;

267 268 269 270 271 272 273 274 275
    depthwiseConvGradFilter(outputGrad,
                            inputData,
                            batchSize,
                            outputChannels,
                            outputHeight,
                            outputWidth,
                            inputChannels,
                            inputHeight,
                            inputWidth,
276
                            filterMultiplier,
277 278 279 280 281 282 283 284
                            filterHeight,
                            filterWidth,
                            strideH(),
                            strideW(),
                            paddingH(),
                            paddingW(),
                            colData,
                            filterGrad);
285 286 287 288 289 290 291 292 293 294
  }
};

REGISTER_TYPED_FUNC(DepthwiseConv, CPU, DepthwiseConvFunction);
REGISTER_TYPED_FUNC(DepthwiseConvGradInput,
                    CPU,
                    DepthwiseConvGradInputFunction);
REGISTER_TYPED_FUNC(DepthwiseConvGradFilter,
                    CPU,
                    DepthwiseConvGradFilterFunction);
295
#ifdef PADDLE_WITH_GPU
296 297 298 299 300 301 302 303 304 305
REGISTER_TYPED_FUNC(DepthwiseConv, GPU, DepthwiseConvFunction);
REGISTER_TYPED_FUNC(DepthwiseConvGradInput,
                    GPU,
                    DepthwiseConvGradInputFunction);
REGISTER_TYPED_FUNC(DepthwiseConvGradFilter,
                    GPU,
                    DepthwiseConvGradFilterFunction);
#endif

}  // namespace paddle