|
|
|
@ -23,15 +23,18 @@
|
|
|
|
|
- `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`文件
|
|
|
|
|
Kernel实现 | CPU、GPU共享Kernel实现在`.h`文件中,否则,CPU 实现在`.cc`文件中,GPU 实现在`.cu`文件中。
|
|
|
|
|
注册Op | Op注册实现在`.cc`文件;Kernel注册CPU实现在`.cc`文件中,GPU实现在`.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。
|
|
|
|
@ -44,7 +47,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 +63,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 +91,7 @@ The equation is: Out = scale*X
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
在这个例子里,两处不同:
|
|
|
|
|
这个例子有两处不同:
|
|
|
|
|
|
|
|
|
|
- `AddInput("X","...").NotInGradient()` : 表示`X`这个输入不参与`ScaleOp`对应的梯度Op计算之中。
|
|
|
|
|
- `AddAttr<AttrType>("scale", "...").SetDefault(1.0);` : 增加`scale`系数,作为参数属性,并且设置默认值为1.0。
|
|
|
|
@ -97,7 +100,7 @@ The equation is: Out = scale*X
|
|
|
|
|
### 2. 定义Operator类
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```c++
|
|
|
|
|
```cpp
|
|
|
|
|
class MulOp : public framework::OperatorWithKernel {
|
|
|
|
|
public:
|
|
|
|
|
using framework::OperatorWithKernel::OperatorWithKernel;
|
|
|
|
@ -122,13 +125,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 +147,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:
|
|
|
|
@ -178,7 +181,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>);
|
|
|
|
@ -192,7 +195,7 @@ REGISTER_OP_CPU_KERNEL(mul_grad,
|
|
|
|
|
|
|
|
|
|
在 `.cu`文件中注册GPU Kernel。请注意,如果GPU Kernel的实现是基于Eigen unsupported模块,那么在 `.cu`的最前面请加上宏定义 `#define EIGEN_USE_GPU`
|
|
|
|
|
|
|
|
|
|
```c++
|
|
|
|
|
```cpp
|
|
|
|
|
// if use Eigen unsupported module before include head files
|
|
|
|
|
#define EIGEN_USE_GPU
|
|
|
|
|
|
|
|
|
@ -204,17 +207,18 @@ REGISTER_OP_GPU_KERNEL(mul_grad,
|
|
|
|
|
|
|
|
|
|
### 5. 编译
|
|
|
|
|
|
|
|
|
|
在[paddle/operators/CMakeLists.txt](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/CMakeLists.txt)文件中添加编译。
|
|
|
|
|
- 简单**无特殊依赖**的OP无需修改CMakeList.txt文件。[paddle/operators/CMakeLists.txt](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/CMakeLists.txt) 会自动将 `paddle/operators` 目录下新增的 `*_op.cc` 文件加入编译。
|
|
|
|
|
- 较为复杂、**有额外依赖** 的operator仍需要修改[paddle/operators/CMakeLists.txt](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/CMakeLists.txt)。如,`mul_op` 依赖 `math_function`,需要在`CMakeLists.txt`中添加如下内容:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
op_library(mul_op SRCS mul_op.cc mul_op.cu DEPS math_function)
|
|
|
|
|
```
|
|
|
|
|
```
|
|
|
|
|
op_library(mul_op SRCS mul_op.cc mul_op.cu DEPS math_function) +
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
下面命令可以编译:
|
|
|
|
|
- 运行下面命令可以进行编译:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
make mul_op
|
|
|
|
|
```
|
|
|
|
|
```
|
|
|
|
|
make mul_op
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 绑定Python
|
|
|
|
|
|
|
|
|
@ -243,27 +247,17 @@ 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库中。
|
|
|
|
|
|
|
|
|
|
## 实现单元测试
|
|
|
|
|
|
|
|
|
|
单测包括对比前向Op不同设备(CPU、GPU)的实现、对比反向OP不同设备(CPU、GPU)的实现、反向Op的梯度测试。下面介绍介绍[`MulOp`的单测](https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/v2/framework/tests/test_mul_op.py)。
|
|
|
|
|
|
|
|
|
|
### 前向Operator单测
|
|
|
|
|
### 前向Operator单元测试
|
|
|
|
|
|
|
|
|
|
前向Op单测继承自`unittest.TestCase`,并定义元类`__metaclass__ = OpTestMeta`,具体单测流程在`OpTestMeta`里完成。需在`setUp`函数定义输入输出和属性参数,以及Python对比的输出值。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
```python
|
|
|
|
|
import unittest
|
|
|
|
|
import numpy as np
|
|
|
|
|
from gradient_checker import GradientChecker, create_op
|
|
|
|
@ -287,11 +281,11 @@ class TestMulOp(unittest.TestCase):
|
|
|
|
|
- `self.outputs` : 定义输出,并得到Python结算结果。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 反向Operator单测
|
|
|
|
|
### 反向Operator单元测试
|
|
|
|
|
|
|
|
|
|
反向Op单测继承自`GradientChecker`,而`GradientChecker`集成自`unittest.TestCase`,所以反向单测函数需要`test_`开头。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
```cpp
|
|
|
|
|
class TestMulGradOp(GradientChecker):
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.op = create_op("mul")
|
|
|
|
@ -337,21 +331,22 @@ class TestMulGradOp(GradientChecker):
|
|
|
|
|
- `test_ignore_x`和`test_ignore_y`分支测试只需要计算一个输入梯度的情况。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 编译和执行
|
|
|
|
|
### 编译和执行单元测试
|
|
|
|
|
|
|
|
|
|
单测完成之后,在[`python/paddle/v2/framework/tests/CMakeLists.txt`](https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/v2/framework/tests/CMakeLists.txt)里添加编译:
|
|
|
|
|
单测完成之后,在[`python/paddle/v2/framework/tests/CMakeLists.txt`](https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/v2/framework/tests/CMakeLists.txt)里添加以下内容将单测加入工程中:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
py_test(test_mul_op SRCS test_mul_op.py)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
编译时需要打开`WITH_TESTING`, 即 `cmake paddle_dir -DWITH_TESTING=ON`,编译成功之后执行单测命令为:
|
|
|
|
|
请注意,**不同于Op的编译测试,运行单元测试测时需要编译整个工程**,并且编译时需要打开`WITH_TESTING`, 即`cmake paddle_dir -DWITH_TESTING=ON`。编译成功后,执行下面的命令来运行单测:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
```bash
|
|
|
|
|
make test ARGS="-R test_mul_op -V"
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
或者:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
```bash
|
|
|
|
|
ctest -R test_mul_op
|
|
|
|
|
```
|
|
|
|
|