PaddlePaddle / Paddle

PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)
http://www.paddlepaddle.org/
Apache License 2.0
22.24k stars 5.58k forks source link

Refactor the convolution-related code #2196

Closed hedaoyuan closed 7 years ago

hedaoyuan commented 7 years ago

For some reason, we need to reconstruct the convolution-related code #2177 . In addition to performance considerations, how to make paddle convolution-related code clearer and more readable is the primary purpose.

So, the whole refactoring process needs to figure out the following things:

qingqing01 commented 7 years ago

Defines the data structure of the input, output and filter.(NCHW or NHWC? filter?)

NNPACK only supports NCHW. cuDNN supports NCHW by default. Maybe we can only support NCHW layout.

Output size calculation. (At present, there are several places that define the calculation method of the output size.)

It's better to calculate output size each mini-batch.

hedaoyuan commented 7 years ago

Paddle's current convolutional module code structure

image

hedaoyuan commented 7 years ago

Draw the reconstructed code structure

image

hedaoyuan commented 7 years ago

Merge some ConvLayer

At present ConvLayer and ConvTransLayer are written two implementations. For example, ExpandConvLayer and ExpandConvTransLayer are implemented in ExpandConvLayer.cpp and ExpandConvTransLayer.cpp files respectively. By comparison found that the two code logic is basically the same. After adding the ConvFunction, we can combine the two codes into one code.

At present

// file: ExpandConvLayer.cpp
REGISTER_LAYER(exconv, ExpandConvLayer);
bool ExpandConvLayer::init(const LayerMap &layerMap,
                           const ParameterMap &parameterMap) {
  /* Initialize the basic convolutional parent class */
  ExpandConvBaseLayer::init(layerMap, parameterMap);
  return true;
}
...
// file: ExpandConvTransLayer.cpp
REGISTER_LAYER(exconvt, ExpandConvTransLayer);
bool ExpandConvTransLayer::init(const LayerMap &layerMap,
                                const ParameterMap &parameterMap) {
  /* Initialize the basic convolutional parent class */
  ExpandConvBaseLayer::init(layerMap, parameterMap);

  return true;
}
...

Use ConvFunction

// file: ExpandConvLayer.cpp
REGISTER_LAYER(exconv, ExpandConvLayer);
REGISTER_LAYER(exconvt, ExpandConvLayer);
bool ExpandConvLayer::init(const LayerMap &layerMap,
                           const ParameterMap &parameterMap) {
  /* Initialize the basic convolutional parent class */
  ExpandConvBaseLayer::init(layerMap, parameterMap);
  if (config.type() == "exconv") {
    forwardFunction = expandFwdOnce;
    backwardFunction = bpropActs
  } else { /* config.type() == "exconvt" */
    forwardFunction = bpropActs;
    backwardFunction = expandFwdOnce;
  }
  return true;
}
...