Skip to content

Commit 7869a05

Browse files
authored
update new_op doc (#7672)
* update new_op doc * follow comment
1 parent 47622d7 commit 7869a05

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

doc/howto/dev/new_op_en.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
- [Implementing C++ Types](#implementing-c-types)
55
- [Defining ProtoMaker](#defining-protomaker)
66
- [Defining Operator](#defining-operator)
7-
- [Registering Operator](#registering-operator)
7+
- [Defining OpKernel](#defining-opkernel)
8+
- [Registering Operator and OpKernel](#registering-operator-and-opkernel)
89
- [Compilation](#compilation)
910
- [Python Binding](#python-binding)
1011
- [Unit Tests](#unit-tests)
@@ -16,12 +17,13 @@
1617

1718
Here are the base types needed. For details, please refer to the design docs.
1819

19-
- `framework::OperatorBase`: Operator (Op)base class.
20-
- `framework::OpKernel`: Base class for Op computation.
21-
- `framework::OperatorWithKernel`: Inherited from OperatorBase, describing an operator with computation.
2220
- `class OpProtoAndCheckerMaker`: Describes an Operator's input, output, attributes and description, mainly used to interface with Python API.
21+
- `framework::OperatorBase`: Operator (Op)base class.
22+
- `framework::OpKernel`: Base class for Op computation kernel.
23+
- `framework::OperatorWithKernel`: Inherited from OperatorBase, describing an operator with computation kernels.
24+
2325

24-
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:
26+
Operators can be categorized into two groups: operator with kernel(s) and operator without kernel(s). An operator with kernel(s) inherits from `OperatorWithKernel` while the one without kernel(s) inherits from `OperatorBase`. This tutorial focuses on implementing operators with kernels. In short, an operator includes the following information:
2527

2628

2729
Information | Where is it defined
@@ -32,7 +34,7 @@ Kernel implementation | The kernel methods shared between CPU and CUDA are
3234
Registering the Op | Ops are registered in `.cc` files; For Kernel registration, `.cc` files contain the CPU implementation, while `.cu` files contain the CUDA implementation.
3335

3436

35-
New Operator implementations are added to the list [paddle/operators](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/operators), with file names in the format `*_op.h` (if applicable), `*_op.cc`, `*_op.cu` (if applicable).** The system will use the naming scheme to automatically build operators and their corresponding Python extensions. **
37+
New Operator implementations are added to the list [paddle/operators](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/operators), with file names in the format `*_op.h` (if applicable), `*_op.cc`, `*_op.cu` (if applicable).** The system will use the naming scheme to automatically build operators and their corresponding Python extensions.**
3638

3739

3840
Let's take matrix multiplication operator, [MulOp](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/mul_op.cc), as an example to introduce the writing of an Operator with Kernel.
@@ -156,7 +158,8 @@ Usually `OpProtoMaker` and `Op`'s type definitions are written in `.cc` files, w
156158
- `typename T` denotes data type, such as `float` or `double`.
157159
158160
`MulKernel` types need to rewrite the interface for `Compute`.
159-
- `Compute` takes one input variable `const framework::ExecutionContext& context`.
161+
162+
- `Compute` takes one input parameter: `const framework::ExecutionContext& context`.
160163
- Compared with `InferShapeContext`, `ExecutionContext` includes device types, and can similarly extract input, output, and attribute variables.
161164
- `Compute` implements the computation logics of an `OpKernel`.
162165
@@ -177,7 +180,7 @@ Usually `OpProtoMaker` and `Op`'s type definitions are written in `.cc` files, w
177180
};
178181
```
179182

180-
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.**
183+
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.**
181184

182185
`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).
183186

@@ -188,13 +191,14 @@ This concludes the forward implementation of an operator. Next its operation and
188191

189192
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`**.
190193

191-
### Registering Operator
194+
### Registering Operator and OpKernel
192195

193196
- In `.cc` files, register forward and backward operator classes and the CPU kernel.
194197

195198
```cpp
196199
namespace ops = paddle::operators;
197200
REGISTER_OP(mul, ops::MulOp, ops::MulOpMaker, mul_grad, ops::MulOpGrad);
201+
198202
REGISTER_OP_CPU_KERNEL(mul, ops::MulKernel<paddle::platform::CPUDeviceContext, float>);
199203
REGISTER_OP_CPU_KERNEL(mul_grad,
200204
ops::MulGradKernel<paddle::platform::CPUDeviceContext, float>);
@@ -204,6 +208,7 @@ The definition of its corresponding backward operator, if applicable, is similar
204208

205209
- `REGISTER_OP` registers the `ops::MulOp` class, type named `mul`, its type `ProtoMaker` is `ops::MulOpMaker`, registering `ops::MulOpGrad` as `mul_grad`.
206210
- `REGISTER_OP_WITHOUT_GRADIENT` registers an operator without gradient.
211+
207212
- `REGISTER_OP_CPU_KERNEL` registers `ops::MulKernel` class and specialized template types `paddle::platform::CPUPlace` and `float`, which also registers `ops::MulGradKernel`.
208213

209214

@@ -225,6 +230,7 @@ The definition of its corresponding backward operator, if applicable, is similar
225230
Run the following commands to compile.
226231

227232
```
233+
# maybe you need to rerun cmake
228234
make mul_op
229235
```
230236

0 commit comments

Comments
 (0)