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

Refactorize registry macro #3373

Merged
merged 13 commits into from
Aug 11, 2017

Conversation

JiayiFeng
Copy link
Collaborator

@JiayiFeng JiayiFeng commented Aug 10, 2017

Fixes #3372

  1. Extracting operator kernel register struct from macro REGISTER_OP_KERNEL. And making it an independent class OpKernelRegistrar.
  2. Invoking static registrars in touch functions(old name: XXX_handle), to make sure registrars are going to be linked in.
  3. Renaming some macros, functions, and variables.

ATTENTION: For many gradient operators have not been completed, the macro REGISTER_OP_KERNEL() is disabled for the present.

@wangkuiyi wangkuiyi changed the title Refactor registry macro Refactorize registry macro Aug 10, 2017
@@ -307,22 +307,38 @@ class OpRegistry {
}
};

class Registrar {
public:
void Touch() {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need a comment here for Touch.

Copy link
Collaborator

Choose a reason for hiding this comment

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

// In our design, various kinds of classes, e.g., operators and kernels, have their corresponding registry and registrar. The action of registration is in the constructor of a global registrar variable, which, however, are not used in the code that calls package framework, and would be removed from the generated binary file by the linker. To avoid such removal, we add Touch to all registrar classes and make USE_OP macros to call this method. So, as long as the callee code calls USE_OP, the global registrar variable won't be removed by the linker.

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

template <typename OpType, typename ProtoMakerType>
class OpRegisterHelper {
class OpRegistrar : public Registrar {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems that using struct instead of class could save a line public:.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In google code style:
"Use a struct only for passive objects that carry data; everything else is a class."

https://google.github.io/styleguide/cppguide.html#Structs_vs._Classes

class OpKernelRegistrar : public Registrar {
public:
explicit OpKernelRegistrar(const char* op_type) {
::paddle::framework::OperatorWithKernel::OpKernelKey key;
Copy link
Collaborator

Choose a reason for hiding this comment

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

::paddle::framework::OperatorWithKernel

==>

OperatorWithKernel

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

}

/**
* Macro to Forbid user register Gradient Operator.
* Macro to Register OperatorKernel.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Register => register

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

// `NO_GRAD` to disable micro USE_OP_GRADIENT(op_type). Otherwise the code can't
// be compiled. `NO_GRAD` should be removed after all gradient ops are
// compeleted.
#define NO_GRAD
Copy link
Collaborator

Choose a reason for hiding this comment

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

这个有点儿危险。要是忘了删除 NO_GRAD 呢?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

其实在原来的代码中,完全就没有对gradient op被链接进来的保证,完全依靠将正反向op写在同一个文件里来把反向op链接进来。所以原来的代码里很多op没写反向也能编译链接通过,其实都是不正常的

#endif

#define USE_NO_GRAD_OP(op_type) \
Copy link
Collaborator

Choose a reason for hiding this comment

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

USE_OP_WITHOUT_GRAD_OP

Copy link
Collaborator

Choose a reason for hiding this comment

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

I remember that #3319 have some proposals about macro names. How about using them?

Copy link
Collaborator Author

@JiayiFeng JiayiFeng Aug 10, 2017

Choose a reason for hiding this comment

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

In my mind, we have two choices:

  1. USE_OP \ USE_OP_WITHOUT_KERNEL (our current design)
  2. USE_OP_WITH_KERNEL \ USE_OP (proposed in Refactorize op_registry.* #3319)

Actually, most operators have their kernels. So we will write left macro much more frequently than the right one. I think we'd better keep the frequently used macro short?

@wangkuiyi wangkuiyi merged commit 19ab1dc into PaddlePaddle:develop Aug 11, 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

Successfully merging this pull request may close these issues.

None yet

2 participants