Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
81383916
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
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看板
提交
81383916
编写于
11月 28, 2018
作者:
X
Xin Pan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add OpBase and unify with VarBase
test=develop
上级
f6f06924
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
69 addition
and
46 deletion
+69
-46
paddle/fluid/imperative/CMakeLists.txt
paddle/fluid/imperative/CMakeLists.txt
+1
-1
paddle/fluid/imperative/layer.h
paddle/fluid/imperative/layer.h
+14
-6
paddle/fluid/imperative/tracer.h
paddle/fluid/imperative/tracer.h
+7
-3
paddle/fluid/pybind/imperative.h
paddle/fluid/pybind/imperative.h
+3
-3
paddle/fluid/pybind/pybind.cc
paddle/fluid/pybind/pybind.cc
+14
-5
python/paddle/fluid/framework.py
python/paddle/fluid/framework.py
+30
-28
未找到文件。
paddle/fluid/imperative/CMakeLists.txt
浏览文件 @
81383916
cc_library
(
layer SRCS layer.cc
)
cc_library
(
layer SRCS layer.cc
DEPS proto_desc
)
cc_library
(
tracer SRCS tracer.cc DEPS proto_desc
)
cc_library
(
engine SRCS engine.cc
)
paddle/fluid/imperative/layer.h
浏览文件 @
81383916
...
...
@@ -15,6 +15,7 @@
#pragma once
#include <vector>
#include "paddle/fluid/framework/op_desc.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/framework/var_desc.h"
#include "paddle/fluid/platform/enforce.h"
...
...
@@ -22,21 +23,28 @@
namespace
paddle
{
namespace
imperative
{
class
Var
iable
Base
{
class
VarBase
{
public:
Var
iable
Base
()
{}
virtual
~
Var
iable
Base
()
{}
VarBase
()
{}
virtual
~
VarBase
()
{}
framework
::
VarDesc
*
var_desc_
;
};
class
OpBase
{
public:
OpBase
()
{}
virtual
~
OpBase
()
{}
framework
::
OpDesc
*
op_desc_
;
};
class
Layer
{
public:
virtual
~
Layer
()
{}
virtual
std
::
vector
<
VariableBase
>
Forward
(
const
std
::
vector
<
VariableBase
>&
inputs
)
{
std
::
vector
<
VariableBase
>
vars
;
virtual
std
::
vector
<
VarBase
>
Forward
(
const
std
::
vector
<
VarBase
>&
inputs
)
{
std
::
vector
<
VarBase
>
vars
;
return
vars
;
}
...
...
paddle/fluid/imperative/tracer.h
浏览文件 @
81383916
...
...
@@ -14,6 +14,7 @@
#pragma once
#include <map>
#include <string>
#include <vector>
...
...
@@ -21,6 +22,7 @@
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/imperative/engine.h"
#include "paddle/fluid/imperative/layer.h"
namespace
paddle
{
namespace
imperative
{
...
...
@@ -29,11 +31,13 @@ class Tracer {
public:
Tracer
()
{}
void
Trace
(
framework
::
OpDesc
*
op_desc
)
{
void
Trace
(
OpBase
*
op
,
const
std
::
map
<
std
::
string
,
VarBase
*>&
inputs
,
const
std
::
map
<
std
::
string
,
VarBase
*>&
outputs
)
{
framework
::
OpDesc
*
op_desc
=
op
->
op_desc_
;
LOG
(
ERROR
)
<<
"tracer tracing "
<<
op_desc
->
Type
();
op_desc
->
InferShape
(
*
block_
);
op_desc
->
InferVarType
(
block_
);
std
::
unique_ptr
<
framework
::
OperatorBase
>
op
=
std
::
unique_ptr
<
framework
::
OperatorBase
>
op
_base
=
framework
::
OpRegistry
::
CreateOp
(
*
op_desc
);
for
(
const
std
::
string
&
vname
:
op_desc
->
InputArgumentNames
())
{
framework
::
Variable
*
var
=
scope_
->
Var
(
vname
);
...
...
@@ -57,7 +61,7 @@ class Tracer {
}
}
}
op
->
Run
(
*
scope_
,
platform
::
CPUPlace
());
op
_base
->
Run
(
*
scope_
,
platform
::
CPUPlace
());
}
void
SetScope
(
framework
::
Scope
*
scope
)
{
scope_
=
scope
;
}
...
...
paddle/fluid/pybind/imperative.h
浏览文件 @
81383916
...
...
@@ -26,9 +26,9 @@ class PyLayer : public imperative::Layer {
public:
using
imperative
::
Layer
::
Layer
;
// Inherit constructors
std
::
vector
<
imperative
::
Var
iable
Base
>
Forward
(
const
std
::
vector
<
imperative
::
Var
iable
Base
>&
inputs
)
override
{
PYBIND11_OVERLOAD
(
std
::
vector
<
imperative
::
Var
iable
Base
>
,
Layer
,
Forward
,
std
::
vector
<
imperative
::
VarBase
>
Forward
(
const
std
::
vector
<
imperative
::
VarBase
>&
inputs
)
override
{
PYBIND11_OVERLOAD
(
std
::
vector
<
imperative
::
VarBase
>
,
Layer
,
Forward
,
inputs
);
// NOLINT
}
...
...
paddle/fluid/pybind/pybind.cc
浏览文件 @
81383916
...
...
@@ -101,21 +101,30 @@ PYBIND11_MODULE(core, m) {
BindException
(
&
m
);
py
::
class_
<
imperative
::
Var
iableBase
>
(
m
,
"Variable
Base"
,
R"DOC()DOC"
)
py
::
class_
<
imperative
::
Var
Base
>
(
m
,
"Var
Base"
,
R"DOC()DOC"
)
.
def_property
(
"desc"
,
[](
const
imperative
::
Var
iable
Base
&
self
)
{
return
self
.
var_desc_
;
},
[](
imperative
::
Var
iable
Base
&
self
,
framework
::
VarDesc
*
var_desc
)
{
[](
const
imperative
::
VarBase
&
self
)
{
return
self
.
var_desc_
;
},
[](
imperative
::
VarBase
&
self
,
framework
::
VarDesc
*
var_desc
)
{
self
.
var_desc_
=
var_desc
;
},
py
::
return_value_policy
::
reference
);
py
::
class_
<
imperative
::
OpBase
>
(
m
,
"OpBase"
,
R"DOC()DOC"
)
.
def_property
(
"desc"
,
[](
const
imperative
::
OpBase
&
self
)
{
return
self
.
op_desc_
;
},
[](
imperative
::
OpBase
&
self
,
framework
::
OpDesc
*
op_desc
)
{
self
.
op_desc_
=
op_desc
;
},
py
::
return_value_policy
::
reference
);
py
::
class_
<
imperative
::
Layer
,
PyLayer
/* <--- trampoline*/
>
layer
(
m
,
"Layer"
);
layer
.
def
(
py
::
init
<>
())
.
def
(
"forward"
,
[](
imperative
::
Layer
&
self
,
const
std
::
vector
<
imperative
::
Var
iable
Base
>
&
inputs
)
{
const
std
::
vector
<
imperative
::
VarBase
>
&
inputs
)
{
return
self
.
Forward
(
inputs
);
})
.
def
(
"backward"
,
&
imperative
::
Layer
::
Backward
);
...
...
python/paddle/fluid/framework.py
浏览文件 @
81383916
...
...
@@ -212,7 +212,7 @@ def _debug_string_(proto, throw_on_error=True):
return
proto
.
__str__
()
class
Variable
(
core
.
Var
iable
Base
):
class
Variable
(
core
.
VarBase
):
"""
In Fluid, every input and output of an operator is a variable. In most
cases, variables are used for holding different kinds of data or training
...
...
@@ -507,7 +507,7 @@ class OpProtoHolder(object):
}
class
Operator
(
object
):
class
Operator
(
core
.
OpBase
):
"""
In Fluid, all the operation are represented by Operator, and Operator
is regarded as a build in an instruction of a Block. Users can use the
...
...
@@ -602,32 +602,33 @@ class Operator(object):
return
True
return
False
if
inputs
is
not
None
:
for
in_proto
in
proto
.
inputs
:
found
=
find_name
(
inputs
,
in_proto
.
name
)
assert
found
or
in_proto
.
dispensable
,
"Input {} not found"
.
format
(
in_proto
.
name
)
if
found
:
in_args
=
inputs
[
in_proto
.
name
]
if
not
isinstance
(
in_args
,
list
):
in_args
=
[
in_args
]
if
not
in_proto
.
duplicable
and
len
(
in_args
)
>
1
:
raise
ValueError
(
"Input %s expects only one input, but %d are given."
%
(
in_proto
.
name
,
len
(
in_args
)))
in_arg_names
=
[]
for
arg
in
in_args
:
if
isinstance
(
arg
,
six
.
string_types
):
in_arg_names
.
append
(
arg
)
elif
isinstance
(
arg
,
six
.
binary_type
):
in_arg_names
.
append
(
arg
.
decode
())
else
:
in_arg_names
.
append
(
cpt
.
to_text
(
arg
.
name
))
self
.
desc
.
set_input
(
in_proto
.
name
,
in_arg_names
)
else
:
self
.
desc
.
set_input
(
in_proto
.
name
,
[])
self
.
inputs
=
[]
if
not
inputs
else
inputs
for
in_proto
in
proto
.
inputs
:
found
=
find_name
(
self
.
inputs
,
in_proto
.
name
)
assert
found
or
in_proto
.
dispensable
,
"Input {} not found"
.
format
(
in_proto
.
name
)
if
found
:
in_args
=
self
.
inputs
[
in_proto
.
name
]
if
not
isinstance
(
in_args
,
list
):
in_args
=
[
in_args
]
if
not
in_proto
.
duplicable
and
len
(
in_args
)
>
1
:
raise
ValueError
(
"Input %s expects only one input, but %d are given."
%
(
in_proto
.
name
,
len
(
in_args
)))
in_arg_names
=
[]
for
arg
in
in_args
:
if
isinstance
(
arg
,
six
.
string_types
):
in_arg_names
.
append
(
arg
)
elif
isinstance
(
arg
,
six
.
binary_type
):
in_arg_names
.
append
(
arg
.
decode
())
else
:
in_arg_names
.
append
(
cpt
.
to_text
(
arg
.
name
))
self
.
desc
.
set_input
(
in_proto
.
name
,
in_arg_names
)
else
:
self
.
desc
.
set_input
(
in_proto
.
name
,
[])
self
.
outputs
=
[]
if
not
outputs
else
outputs
if
outputs
is
not
None
:
given
=
set
()
need
=
set
()
...
...
@@ -1222,7 +1223,8 @@ class Block(object):
if
_in_imperative_mode
():
op_desc
=
core
.
OpDesc
()
op
=
Operator
(
block
=
self
,
desc
=
op_desc
,
*
args
,
**
kwargs
)
_imperative_tracer
().
trace
(
op
.
desc
)
sys
.
stderr
.
write
(
'%s %s!!!
\n
'
%
(
type
(
op
.
inputs
),
type
(
op
.
outputs
)))
_imperative_tracer
().
trace
(
op
,
op
.
inputs
,
op
.
outputs
)
return
op_desc
=
self
.
desc
.
append_op
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录