Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
c1fdacd4
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c1fdacd4
编写于
1月 15, 2019
作者:
X
Xin Pan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add imperative mode design
test=develop
上级
8652a899
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
148 addition
and
0 deletion
+148
-0
paddle/fluid/imperative/README.md
paddle/fluid/imperative/README.md
+148
-0
未找到文件。
paddle/fluid/imperative/README.md
0 → 100644
浏览文件 @
c1fdacd4
# Overview
Imperative Programming
# Related Works
## Pytorch
https://pytorch.org/
## TensorFlow Eager
https://www.tensorflow.org/guide/eager
# Design
## API
```
python
class
Layer
(
object
):
def
__call__
(
inputs
):
# build some parameter once.
# ...
return
self
.
apply
(
inputs
):
def
apply
(
inputs
):
# forward logic with paddle operators. backward auto-generated.
class
PyLayer
(
core
.
PyLayer
):
def
__call__
(
cls
,
inputs
):
# trace the logic.
@
staticmethod
def
forward
(
inputs
):
# any forward logic implemented with numpy io.
@
static
method
# any backward logic implemented with numpy io.
```
## Tracer
Python Variable -> C++ VarBase -> C++ Variable -> C++ Tensor
```
cpp
class
Tracer
{
public:
explicit
Tracer
(
framework
::
BlockDesc
*
root_block
)
:
root_block_
(
root_block
)
{}
virtual
~
Tracer
()
{}
void
Trace
(
OpBase
*
op
,
const
std
::
map
<
std
::
string
,
std
::
vector
<
VarBase
*>>&
inputs
,
const
std
::
map
<
std
::
string
,
std
::
vector
<
VarBase
*>>&
outputs
,
framework
::
BlockDesc
*
block
,
const
bool
stop_gradient
=
false
);
std
::
vector
<
VarBase
*>
PyTrace
(
OpBase
*
op
,
const
std
::
vector
<
VarBase
*>&
inputs
,
bool
stop_gradient
=
false
);
};
```
## Autodiff
Lots of research already.
https://autodiff-workshop.github.io/
## Tests
*
All op tests run once in static graph, once in imperative mode.
## Refactor
*
All function layers with parameters converted to class Layers.
*
Models converted to imperative mode.
# Examples
```
python
class
MyLayer
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
MyLayer
,
self
).
__init__
()
def
forward
(
self
,
inputs
):
x
=
fluid
.
layers
.
relu
(
inputs
)
x
=
fluid
.
layers
.
elementwise_mul
(
x
,
x
)
x
=
fluid
.
layers
.
reduce_sum
(
x
)
return
[
x
]
class
MyPyLayer
(
fluid
.
imperative
.
PyLayer
):
def
__init__
(
self
):
super
(
MyPyLayer
,
self
).
__init__
()
@
staticmethod
def
forward
(
inputs
):
return
np
.
tanh
(
inputs
[
0
])
@
staticmethod
def
backward
(
inputs
):
return
np
.
array
(
dout
)
*
(
1
-
np
.
square
(
np
.
array
(
out
)))
class
MLP
(
fluid
.
imperative
.
Layer
):
def
__init__
(
self
):
super
(
MLP
,
self
).
__init__
()
self
.
_fc1
=
FC
(
3
,
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Constant
(
value
=
0.1
)))
self
.
_fc2
=
FC
(
4
,
fluid
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Constant
(
value
=
0.1
)))
def
forward
(
self
,
inputs
):
x
=
self
.
_fc1
(
inputs
)
x
=
self
.
_fc2
(
x
)
x
=
fluid
.
layers
.
reduce_sum
(
x
)
return
x
np_inp
=
np
.
array
([[
1.0
,
2.0
],
[
3.0
,
4.0
]],
dtype
=
np
.
float32
)
with
fluid
.
imperative
.
guard
():
var_inp
=
fluid
.
imperative
.
base
.
to_variable
(
np_inp
)
mlp
=
MLP
()
out
=
mlp
(
var_inp
)
dy_out
=
out
.
_numpy
()
out
.
_backward
()
```
# Plan
2.
1,3 fulltime, Can run a few simple models. (Currently, 2 20% engs)
4.
1, 4 fulltime, Can run 6 models, Performance 70% Pytorch. Release alpha.
6.
1, 5 fulltime, Performance close to Pytorch, can run multi-devices. Release Beta.
8.
1, 5 fulltime, Works in general. Covert current models to use imperative mode.
12.
1, 5 fulltime, Can compile to static graph, support more optimizations.
# Discussion
TODO.
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录