You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`framework::OpKernel`: Base class for Op computation kernel.
23
+
-`framework::OperatorWithKernel`: Inherited from OperatorBase, describing an operator with computation kernels.
24
+
23
25
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:
25
27
26
28
27
29
Information | Where is it defined
@@ -32,7 +34,7 @@ Kernel implementation | The kernel methods shared between CPU and CUDA are
32
34
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.
33
35
34
36
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.**
36
38
37
39
38
40
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
156
158
- `typename T` denotes data type, such as `float` or `double`.
157
159
158
160
`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`.
160
163
- Compared with `InferShapeContext`, `ExecutionContext` includes device types, and can similarly extract input, output, and attribute variables.
161
164
- `Compute` implements the computation logics of an `OpKernel`.
162
165
@@ -177,7 +180,7 @@ Usually `OpProtoMaker` and `Op`'s type definitions are written in `.cc` files, w
177
180
};
178
181
```
179
182
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.**
181
184
182
185
`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).
183
186
@@ -188,13 +191,14 @@ This concludes the forward implementation of an operator. Next its operation and
188
191
189
192
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`**.
190
193
191
-
### Registering Operator
194
+
### Registering Operator and OpKernel
192
195
193
196
- In `.cc` files, register forward and backward operator classes and the CPU kernel.
@@ -204,6 +208,7 @@ The definition of its corresponding backward operator, if applicable, is similar
204
208
205
209
- `REGISTER_OP` registers the `ops::MulOp` class, type named `mul`, its type `ProtoMaker` is `ops::MulOpMaker`, registering `ops::MulOpGrad` as `mul_grad`.
206
210
- `REGISTER_OP_WITHOUT_GRADIENT` registers an operator without gradient.
211
+
207
212
- `REGISTER_OP_CPU_KERNEL` registers `ops::MulKernel` class and specialized template types `paddle::platform::CPUPlace` and `float`, which also registers `ops::MulGradKernel`.
208
213
209
214
@@ -225,6 +230,7 @@ The definition of its corresponding backward operator, if applicable, is similar
0 commit comments