-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
update new_op doc #7672
Merged
jacquesqiao
merged 2 commits into
PaddlePaddle:develop
from
jacquesqiao:update-new-op-doc
Jan 19, 2018
Merged
update new_op doc #7672
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,8 @@ | |
- [Implementing C++ Types](#implementing-c-types) | ||
- [Defining ProtoMaker](#defining-protomaker) | ||
- [Defining Operator](#defining-operator) | ||
- [Registering Operator](#registering-operator) | ||
- [Defining OpKernel](#defining-opkernel) | ||
- [Registering Operator and OpKernel](#registering-operator-and-opkernel) | ||
- [Compilation](#compilation) | ||
- [Python Binding](#python-binding) | ||
- [Unit Tests](#unit-tests) | ||
|
@@ -16,12 +17,13 @@ | |
|
||
Here are the base types needed. For details, please refer to the design docs. | ||
|
||
- `framework::OperatorBase`: Operator (Op)base class. | ||
- `framework::OpKernel`: Base class for Op computation. | ||
- `framework::OperatorWithKernel`: Inherited from OperatorBase, describing an operator with computation. | ||
- `class OpProtoAndCheckerMaker`: Describes an Operator's input, output, attributes and description, mainly used to interface with Python API. | ||
- `framework::OperatorBase`: Operator (Op)base class. | ||
- `framework::OpKernel`: Base class for Op computation kernel. | ||
- `framework::OperatorWithKernel`: Inherited from OperatorBase, describing an operator with computation kernels. | ||
|
||
|
||
An operator can be differentiated by whether in has kernel methods. An operator with kernel inherits from `OperatorWithKernel` while the ones without inherit from `OperatorBase`. This tutorial focuses on implementing operators with kernels. In short, an operator includes the following information: | ||
An operator can be differentiated by whether in has computation kernels. An operator with kernel inherits from `OperatorWithKernel` while the ones without inherit from `OperatorBase`. This tutorial focuses on implementing operators with kernels. In short, an operator includes the following information: | ||
|
||
|
||
Information | Where is it defined | ||
|
@@ -156,7 +158,8 @@ Usually `OpProtoMaker` and `Op`'s type definitions are written in `.cc` files, w | |
- `typename T` denotes data type, such as `float` or `double`. | ||
|
||
`MulKernel` types need to rewrite the interface for `Compute`. | ||
- `Compute` takes one input variable `const framework::ExecutionContext& context`. | ||
|
||
- `Compute` takes one input parameter: `const framework::ExecutionContext& context`. | ||
- Compared with `InferShapeContext`, `ExecutionContext` includes device types, and can similarly extract input, output, and attribute variables. | ||
- `Compute` implements the computation logics of an `OpKernel`. | ||
|
||
|
@@ -177,7 +180,7 @@ Usually `OpProtoMaker` and `Op`'s type definitions are written in `.cc` files, w | |
}; | ||
``` | ||
|
||
Note that **different devices (CPU, CUDA)share an Op definition; whether or not they share the same `OpKernel` depends on whether `Compute` calls functions that support both devices.** | ||
Note that **different devices (CPU, CUDA)share one Op definition; whether or not they share the same `OpKernel` depends on whether `Compute` calls functions can support both devices.** | ||
|
||
`MulOp`'s CPU and CUDA share the same `Kernel`. A non-sharing `OpKernel` example can be seen in [`OnehotCrossEntropyOpKernel`](/~https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/cross_entropy_op.h#L43). | ||
|
||
|
@@ -188,13 +191,14 @@ This concludes the forward implementation of an operator. Next its operation and | |
|
||
The definition of its corresponding backward operator, if applicable, is similar to that of an forward operator. **Note that a backward operator does not include a `ProtoMaker`**. | ||
|
||
### Registering Operator | ||
### Registering Operator and OpKernel | ||
|
||
- In `.cc` files, register forward and backward operator classes and the CPU kernel. | ||
|
||
```cpp | ||
namespace ops = paddle::operators; | ||
REGISTER_OP(mul, ops::MulOp, ops::MulOpMaker, mul_grad, ops::MulOpGrad); | ||
|
||
REGISTER_OP_CPU_KERNEL(mul, ops::MulKernel<paddle::platform::CPUDeviceContext, float>); | ||
REGISTER_OP_CPU_KERNEL(mul_grad, | ||
ops::MulGradKernel<paddle::platform::CPUDeviceContext, float>); | ||
|
@@ -204,6 +208,7 @@ The definition of its corresponding backward operator, if applicable, is similar | |
|
||
- `REGISTER_OP` registers the `ops::MulOp` class, type named `mul`, its type `ProtoMaker` is `ops::MulOpMaker`, registering `ops::MulOpGrad` as `mul_grad`. | ||
- `REGISTER_OP_WITHOUT_GRADIENT` registers an operator without gradient. | ||
|
||
- `REGISTER_OP_CPU_KERNEL` registers `ops::MulKernel` class and specialized template types `paddle::platform::CPUPlace` and `float`, which also registers `ops::MulGradKernel`. | ||
|
||
|
||
|
@@ -225,6 +230,7 @@ The definition of its corresponding backward operator, if applicable, is similar | |
Run the following commands to compile. | ||
|
||
``` | ||
# maybe you need to rerun cmake | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you may need to rerun cmake. |
||
make mul_op | ||
``` | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OperatorWithKernel
while the ones without inheriting fromOperatorBase
." --> An operator with kernel(s) inherits fromOperatorWithKernel
while the one without kernel(s) inherits fromOperatorBase
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第37行的markdown标记有问题。
** The system will use the naming scheme to automatically build operators and their corresponding Python extensions. ** ,** 之后的空格应该去掉。