Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
efd40b66
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
efd40b66
编写于
9月 05, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update the doc for how to write the operators.
上级
843a8b1e
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
64 addition
and
77 deletion
+64
-77
doc/howto/dev/new_op_cn.md
doc/howto/dev/new_op_cn.md
+64
-77
未找到文件。
doc/howto/dev/new_op_cn.md
浏览文件 @
efd40b66
...
...
@@ -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
++
```
c
pp
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
++
```
c
pp
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
```
这句表示使用基类
`OperatorWithKernel`
的构造函数,也可写成:
```
c
++
```
c
pp
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
++
```
c
pp
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
++
```
c
pp
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")
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录