Skip to content

Conversation

@reyoung
Copy link
Collaborator

@reyoung reyoung commented May 8, 2017

* Make paddle::Layer decoupled with FunctionBase.
@reyoung reyoung requested a review from hedaoyuan May 8, 2017 05:05
@reyoung reyoung requested a review from a user May 8, 2017 07:14
Copy link
Contributor

@hedaoyuan hedaoyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对照issue描述看了一下。把原先Layer.h里面的forward_, backward_, 以及createFunction单独移到一个WithFunction类里面,然后WithFunction被每个Layer public继承是,这个是issue中描述的将Function包与Layer解耦吗?感觉没有必要单独写成一个类啊?更倾向于这段逻辑应该是在Layer.h里面的(forward_和backward_本身应该是Layer的属性)。


#include <vector>
#include "NormLayer.h"
#include "WithFunction.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看起来每个Layer都需要 include "WithFunction.h" 直接放到Layer.h里面吧。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同理BufferArg.h这个好像也是每个Layer中都include了。

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will fix this ASAP.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Collaborator

@wangkuiyi wangkuiyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd thought that this PR is going to replace function::FunctionBase by std::function. But it is not. Instead, it is a wrapper which hides function::FunctionBase behind a std::function.

I believe that if we don't need a concept, we should remove it or replace it, not hide it.

//! Kernel Function type of Paddle.
//! Each layer will invoke this KernelType when forward/backward.
typedef std::function<Error(BufferArgs& inputs, BufferArgs& outputs)>
KernelType;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"kernel" is a polysemy. It could mean OS kernel, Linux kernel, CUDA kernel, etc. Here we need a specific name, e.g., KernelType ==> CUDAKernel.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CUDA is only for GPU. Perhaps we can use Function or FunctionType.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

See the License for the specific language governing permissions and
limitations under the License. */

#include "ContextProjection.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either in this PR or in the next one, we need to use full path with #includes. For example,

#include "ContextProjection.h" 

should we

#include "paddle/gserver/layers/ContextProjection.h"

This change should be done together with the change of CMakeLists.txt files to remove some pre-defined C++ include paths.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a cpp can always include the header file in its own directory.

* The config file api is context_projection.
*/
class ContextProjection : public Projection {
class ContextProjection : public Projection, public WithFunction {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There have been many discussions about why we need to avoid C++ multiple inheritances. E.g., http://stackoverflow.com/a/407928/724872. Instead, we should use "composition".

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, WithFunction is a temporary class during refactoring Paddle C++ Core. After we refactor Paddle C++ Core, all Layer and Project should be WithFunction and then Layer and Project will be a subclass of WithFunction.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I use FunctionList instead of WithFunction, make FunctionList as a member of Layer and Projection.

*/
class WithFunction {
protected:
std::vector<function::KernelType> forward_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If all classes derived from Projection need forward_ and backward_, we should define them in Projection; instead of here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

const std::string &name,
const FuncConfig &config,
bool useGPU) {
std::shared_ptr<FunctionBase> func;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #2050 which was claimed to be fixed with this PR, it says that we shoud replace FunctionBase with std::function. But here it shows that FunctionBase is not replaced at all. We just wrap it by a std::function. I believe that if a concept is not necessary, we should remove it, instead of hide it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to replace FunctionBase by plain function step by step. The first step is to use std::function to hide it.

The demo code of register function is here

reg_cpu_function(cos, [](const BufferArgs& ins, const BufferArgs& outs, real scale){ ... });
See the License for the specific language governing permissions and
limitations under the License. */

#include "ContextProjection.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a cpp can always include the header file in its own directory.

*/
class WithFunction {
protected:
std::vector<function::KernelType> forward_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need a vector here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be many functions used in one layer's forward/backward. The layer could be composed of functions.


#include <vector>
#include "NormLayer.h"
#include "WithFunction.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

//! Kernel Function type of Paddle.
//! Each layer will invoke this KernelType when forward/backward.
typedef std::function<Error(BufferArgs& inputs, BufferArgs& outputs)>
KernelType;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CUDA is only for GPU. Perhaps we can use Function or FunctionType.

@luotao1
Copy link
Contributor

luotao1 commented Feb 1, 2019

感谢您给PaddlePaddle贡献代码。由于Paddle V1/V2版本已不再维护,相关代码也已从develop分支上删除,因此关闭您的PR,欢迎您向Paddle最新版-Fluid贡献代码。
Thanks for contributing to PaddlePaddle! Since V1/V2 will not be maintained anymore, and related codes have been deleted from develop branch as well, we close this PR. Welcome to contribute to Fluid——the latest version of PaddlePaddle.

@luotao1 luotao1 closed this Feb 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants