|
|
|
@ -23,19 +23,20 @@
|
|
|
|
|
- `framework::OperatorWithKernel`:继承自OperatorBase,Op有计算函数,称作有Kernel。
|
|
|
|
|
- `class OpProtoAndCheckerMaker`:描述该Op的输入、输出、属性、注释,主要用于Python API接口生成
|
|
|
|
|
|
|
|
|
|
依据是否包含kernel,将Op分为两种:包含Kernel的Op和不包含kernel的Op,前者Op的定义继承自`OperatorBase`,后者继承自`OperatorWithKernel`。本教程主要介绍带Kernel的Op如何写,简单总结Op需要包含的内容如下:
|
|
|
|
|
依据是否包含kernel,可以将Op分为两种:包含Kernel的Op和不包含kernel的Op,前者Op的定义继承自`OperatorBase`,后者继承自`OperatorWithKernel`。本教程主要介绍带Kernel的Op如何写,简单总结Op需要包含的内容如下:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
内容 | 定义位置
|
|
|
|
|
-------------- | :----------------------
|
|
|
|
|
OpProtoMake定义 | `.cc`文件,Backward Op不需要定义OpProtoMake
|
|
|
|
|
Op定义 | `.cc`文件
|
|
|
|
|
Kernel实现 | CPU、GPU共享Kernel在`.h`文件,否则,CPU可以在`.cc`文件,GPU可在`.cu`文件。
|
|
|
|
|
注册Op | Op注册在`.cc`文件;Kernel注册CPU在`.cc`文件,GPU在`.cu`文件
|
|
|
|
|
OpProtoMake定义 | `*_op.cc`文件,Backward Op不需要定义OpProtoMake
|
|
|
|
|
Op定义 | `*_op.cc`文件
|
|
|
|
|
Kernel实现 | CPU、GPU共享Kernel在`*_op.h`文件,否则,CPU可以在`*_op.cc`文件,GPU可在`*_op.cu`文件。
|
|
|
|
|
注册Op | Op注册在`*_op.cc`文件;Kernel注册CPU在`*_op.cc`文件,GPU在`*_op.cu`文件
|
|
|
|
|
|
|
|
|
|
实现新的op都添加至目录[paddle/operators](https://github.com/PaddlePaddle/Paddle/tree/develop/paddle/operators)下,文件命名以`*_op.h`(如有) 、 `*_op.cc` 、`*_op.cu`(如有)结尾。
|
|
|
|
|
|
|
|
|
|
下面以矩阵乘操作,即[MulOp](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/mul_op.cc)为例来介绍如何写带Kernel的Operator。
|
|
|
|
|
|
|
|
|
|
下面以矩阵乘操作,即[MulOp](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/mul_op.cc)为例来介绍如何写带Kernel的Operator。
|
|
|
|
|
|
|
|
|
|
## 实现C++类
|
|
|
|
|
|
|
|
|
@ -44,7 +45,7 @@ Kernel实现 | CPU、GPU共享Kernel在`.h`文件,否则,CPU可以在`
|
|
|
|
|
|
|
|
|
|
矩阵乘的公式:$Out = X * Y$, 可见该计算由两个输入,一个输出组成。首先定义`ProtoMaker`来描述该Op的输入、输出及注释:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
```cpp
|
|
|
|
|
class MulOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
|
public:
|
|
|
|
|
MulOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
|
|
|
@ -60,19 +61,19 @@ The equation is: Out = X * Y
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[`MulOpMaker`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/mul_op.cc#L43)继承自`framework::OpProtoAndCheckerMaker`,构造函数包括2个:
|
|
|
|
|
[`MulOpMaker`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/mul_op.cc#L43)继承自`framework::OpProtoAndCheckerMaker`,构造函数包括2个参数:
|
|
|
|
|
|
|
|
|
|
- `framework::OpProto` : 前者存储Op的输入输出和参数属性,将用于Python API接口的生成。
|
|
|
|
|
- `framework::OpAttrChecker` :后者用于检查参数属性的合法性。
|
|
|
|
|
|
|
|
|
|
构造函数里通过`AddInput`添加输入参数,通过`AddOutput`添加输出参数,通过`AddComment`添加该Op的注释,这些函数会将对应内容添加到`OpProto`中。
|
|
|
|
|
|
|
|
|
|
在`MulOp`中添加两个输入`X`和`Y`,添加了一个输出`Out`,并解释了各自含义,该命名尽可能的规范。
|
|
|
|
|
在`MulOp`中添加两个输入`X`和`Y`,添加了一个输出`Out`,并解释了各自含义,命名请遵守命名规范。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
再举个[`ScaleOp`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/scale_op.cc#L37)的例子:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
```cpp
|
|
|
|
|
template <typename AttrType>
|
|
|
|
|
class ScaleOpMaker : public framework::OpProtoAndCheckerMaker {
|
|
|
|
|
public:
|
|
|
|
@ -88,7 +89,7 @@ The equation is: Out = scale*X
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
在这个例子里,两处不同:
|
|
|
|
|
这个例子有两处不同:
|
|
|
|
|
|
|
|
|
|
- `AddInput("X","...").NotInGradient()` : 表示`X`这个输入不参与`ScaleOp`对应的梯度Op计算之中。
|
|
|
|
|
- `AddAttr<AttrType>("scale", "...").SetDefault(1.0);` : 增加`scale`系数,作为参数属性,并且设置默认值为1.0。
|
|
|
|
@ -97,7 +98,7 @@ The equation is: Out = scale*X
|
|
|
|
|
### 2. 定义Operator类
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```c++
|
|
|
|
|
```cpp
|
|
|
|
|
class MulOp : public framework::OperatorWithKernel {
|
|
|
|
|
public:
|
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
@ -122,13 +123,13 @@ class MulOp : public framework::OperatorWithKernel {
|
|
|
|
|
|
|
|
|
|
[`MulOp`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/mul_op.cc#L22)继承自`OperatorWithKernel`。`public`成员:
|
|
|
|
|
|
|
|
|
|
```c++
|
|
|
|
|
```cpp
|
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
这句表示使用基类`OperatorWithKernel`的构造函数,也可写成:
|
|
|
|
|
|
|
|
|
|
```c++
|
|
|
|
|
```cpp
|
|
|
|
|
MulOp(const std::string &type, const framework::VariableNameMap &inputs,
|
|
|
|
|
const framework::VariableNameMap &outputs,
|
|
|
|
|
const framework::AttributeMap &attrs)
|
|
|
|
@ -144,7 +145,7 @@ MulOp(const std::string &type, const framework::VariableNameMap &inputs,
|
|
|
|
|
|
|
|
|
|
### 3. 定义OpKernel类
|
|
|
|
|
|
|
|
|
|
```C++
|
|
|
|
|
```cpp
|
|
|
|
|
template <typename Place, typename T>
|
|
|
|
|
class MulKernel : public framework::OpKernel {
|
|
|
|
|
public:
|
|
|
|
@ -176,7 +177,7 @@ class MulKernel : public framework::OpKernel {
|
|
|
|
|
|
|
|
|
|
在`.cc`文件中注册前向、反向Op类,注册CPU Kernel。
|
|
|
|
|
|
|
|
|
|
```c++
|
|
|
|
|
```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::CPUPlace, float>);
|
|
|
|
@ -190,7 +191,7 @@ REGISTER_OP_CPU_KERNEL(mul_grad,
|
|
|
|
|
|
|
|
|
|
在 `.cu`文件中注册GPU Kernel。
|
|
|
|
|
|
|
|
|
|
```c++
|
|
|
|
|
```cpp
|
|
|
|
|
namespace ops = paddle::operators;
|
|
|
|
|
REGISTER_OP_GPU_KERNEL(mul, ops::MulKernel<paddle::platform::GPUPlace, float>);
|
|
|
|
|
REGISTER_OP_GPU_KERNEL(mul_grad,
|
|
|
|
@ -199,13 +200,9 @@ REGISTER_OP_GPU_KERNEL(mul_grad,
|
|
|
|
|
|
|
|
|
|
### 5. 编译
|
|
|
|
|
|
|
|
|
|
在[paddle/operators/CMakeLists.txt](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/CMakeLists.txt)文件中添加编译。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
op_library(mul_op SRCS mul_op.cc mul_op.cu DEPS math_function)
|
|
|
|
|
```
|
|
|
|
|
无需修改[paddle/operators/CMakeLists.txt](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/CMakeLists.txt)文件,`paddle/operators` 目录下新增的 `*_op.cc` 文件会被自动加入编译。
|
|
|
|
|
|
|
|
|
|
下面命令可以编译:
|
|
|
|
|
直接执行下面命令可进行编译:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
make mul_op
|
|
|
|
@ -238,17 +235,7 @@ make mul_op
|
|
|
|
|
|
|
|
|
|
- 生成库
|
|
|
|
|
|
|
|
|
|
在 [`paddle/pybind/CMakeLists.txt`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/pybind/CMakeLists.txt)文件添加类到`DEPS`中,使得该Op可以链接到生成的lib库中。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
if(WITH_PYTHON)
|
|
|
|
|
cc_library(paddle_pybind SHARED
|
|
|
|
|
SRCS pybind.cc
|
|
|
|
|
DEPS pybind python backward
|
|
|
|
|
mul_op
|
|
|
|
|
minus_op)
|
|
|
|
|
endif(WITH_PYTHON)
|
|
|
|
|
```
|
|
|
|
|
无需修改 [`paddle/pybind/CMakeLists.txt`](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/pybind/CMakeLists.txt)文件,`paddle/operators` 目录下新增的 `*_op.cc` 文件会被自动被添加链接至生成的lib库中。
|
|
|
|
|
|
|
|
|
|
## 实现单元测试
|
|
|
|
|
|
|
|
|
@ -258,7 +245,7 @@ make mul_op
|
|
|
|
|
|
|
|
|
|
前向Op单测继承自`unittest.TestCase`,并定义元类`__metaclass__ = OpTestMeta`,具体单测流程在`OpTestMeta`里完成。需在`setUp`函数定义输入输出和属性参数,以及Python对比的输出值。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
```python
|
|
|
|
|
import unittest
|
|
|
|
|
import numpy as np
|
|
|
|
|
from gradient_checker import GradientChecker, create_op
|
|
|
|
@ -286,7 +273,7 @@ class TestMulOp(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
反向Op单测继承自`GradientChecker`,而`GradientChecker`集成自`unittest.TestCase`,所以反向单测函数需要`test_`开头。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
```python
|
|
|
|
|
class MulGradOpTest(GradientChecker):
|
|
|
|
|
def test_mul(self):
|
|
|
|
|
op = create_op("mul")
|
|
|
|
|