Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the convolution-related code #2196

Closed
hedaoyuan opened this issue May 18, 2017 · 4 comments
Closed

Refactor the convolution-related code #2196

hedaoyuan opened this issue May 18, 2017 · 4 comments
Assignees

Comments

@hedaoyuan
Copy link
Contributor

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:

  • Draw Paddle's current convolutional module code structure, as well as ugly and not clear enough code;
  • Draw the reconstructed code structure.
  • Defines the data structure of the input, output and filter.(NCHW or NHWC? filter?)
  • Output size calculation. (At present, there are several places that define the calculation method of the output size.)
  • Optimize temporary memory. (卷积计算过程中会需要一些临时内存,比如exconv实现中,需要一个expandInput_来存储im2col后的临时数据;但是,并不需要每个卷积Layer实例都保存这样一块内存,虽然当前每个exconv layer保存一个expandInput_对象的方法,在多线程中,可以使得线程计算,但是同样也消耗的大量内存。)
  • A design document on how to reconstruct the convolution part of the code;
  • Migrate the code based on sgemm and cudnn to the new convolution module.
  • Adding a convolution implementation based on NNPACK;
@qingqing01
Copy link
Contributor

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
Copy link
Contributor Author

hedaoyuan commented May 18, 2017

Paddle's current convolutional module code structure

image

  • The three modules contain convolution, inherited from class Layer, class Projection, class Operator.
  • The solid line indicates the inheritance relationship, and the dotted line indicates the call relation.
  • ConvBaseLayer, ConvBaseProjection, and ConvBaseOperator all contain convolution parameters that have been converted from ConvConfig.

@hedaoyuan
Copy link
Contributor Author

Draw the reconstructed code structure

image

  • The refactoring described here does not involve modifying the current NN-Layer structure. Just move the convolution forward / backward correlation calculation to the Function.
  • ConvBaseFunction contains the basic parameters of convolution.
  • ConvGemmFunction and ConvCudnnFunction are two different implementations of convolution. And can add more different implementations.

@hedaoyuan
Copy link
Contributor Author

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;
}
...

@hedaoyuan hedaoyuan self-assigned this May 19, 2017
This was referenced May 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants