Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
5fddd288
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
提交
5fddd288
编写于
10月 10, 2017
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'feature/add_persistable_in_var_desc' into dev_opdesc_in_python
上级
a281b383
86451b30
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
76 addition
and
18 deletion
+76
-18
python/paddle/v2/framework/graph.py
python/paddle/v2/framework/graph.py
+76
-18
未找到文件。
python/paddle/v2/framework/graph.py
浏览文件 @
5fddd288
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.core
as
core
import
paddle.v2.framework.proto.framework_pb2
as
framework_pb2
import
collections
import
collections
__all__
=
[
'Block'
,
'Variable'
,
'Program'
,
'Operator'
]
__all__
=
[
'Block'
,
'Variable'
,
'Program'
,
'Operator'
]
def
get_all_op_protos
():
"""
Get all registered op proto from PaddlePaddle C++ end.
:return: A list of registered OpProto.
"""
protostrs
=
core
.
get_all_op_protos
()
ret_values
=
[]
for
pbstr
in
protostrs
:
op_proto
=
framework_pb2
.
OpProto
.
FromString
(
str
(
pbstr
))
ret_values
.
append
(
op_proto
)
return
ret_values
class
OpProtoHolder
(
object
):
@
classmethod
def
instance
(
cls
):
if
not
hasattr
(
cls
,
'_instance'
):
cls
.
_instance
=
cls
()
return
cls
.
_instance
def
__init__
(
self
):
assert
not
hasattr
(
self
.
__class__
,
'_instance'
),
'Please use `instance()` to get OpProtoHolder opject!'
op_protos
=
get_all_op_protos
()
self
.
op_proto_map
=
{}
for
proto
in
op_protos
:
sefl
.
op_proto_map
[
proto
.
type
]
=
proto
def
get_op_proto
(
self
,
type
):
assert
type
in
self
.
op_proto_map
,
"Operator with type
\"
%s
\"
has not been registered."
%
type
return
self
.
op_proto_map
[
type
]
class
Variable
(
object
):
class
Variable
(
object
):
def
__init__
(
self
,
block
,
name
=
None
,
shape
=
None
,
dtype
=
None
,
def
__init__
(
self
,
block
,
name
=
None
,
shape
=
None
,
dtype
=
None
,
lod_level
=
None
):
lod_level
=
None
):
...
@@ -36,27 +71,50 @@ class Variable(object):
...
@@ -36,27 +71,50 @@ class Variable(object):
class
Operator
(
object
):
class
Operator
(
object
):
def
__init__
(
self
,
def
__init__
(
self
,
block
,
desc
,
type
,
inputs
=
None
,
outputs
=
None
,
block
,
proto
,
type
=
None
,
inputs
=
None
,
outputs
=
None
,
attrs
=
None
):
attrs
=
None
):
self
.
block
=
block
self
.
block
=
block
self
.
proto
=
proto
self
.
desc
=
desc
if
type
is
not
None
:
self
.
proto
=
OpProtoHolder
.
instance
().
get_op_proto
(
type
)
# TODO.
self
.
desc
.
set_type
(
type
)
pass
if
inputs
is
not
None
:
if
inputs
is
not
None
:
# TODO
for
in_proto
in
self
.
proto
.
inputs
:
pass
in_argus
=
inputs
[
in_proto
.
name
]
if
not
isinstance
(
in_argus
,
list
):
in_argus
=
[
in_argus
]
if
not
in_proto
.
duplicable
and
len
(
in_argus
)
>
1
:
raise
ValueError
(
"Input %s expects only one input, but %d are given."
%
(
in_proto
.
name
,
len
(
in_argus
)))
in_argu_names
=
[]
for
argu
in
in_argus
:
in_argu_names
.
append
(
argu
.
name
())
self
.
desc
.
set_input
(
in_proto
.
name
,
in_argu_names
)
if
outputs
is
not
None
:
if
outputs
is
not
None
:
# TODO
for
out_proto
in
self
.
proto
.
outputs
:
pass
out_argus
=
outputs
[
out_proto
.
name
]
if
not
isinstance
(
out_argus
,
list
):
out_argus
=
[
out_argus
]
if
not
out_proto
.
duplicable
and
len
(
out_argus
)
>
1
:
raise
ValueError
(
"Output %s expects only one output, but %d are given."
%
(
out_proto
.
name
,
len
(
out_argus
)))
out_argu_names
=
[]
for
argu
in
out_argus
:
out_argu_names
.
append
(
argu
.
name
())
self
.
desc
.
set_output
(
out_proto
.
name
,
out_argu_names
)
if
attrs
is
not
None
:
if
attrs
is
not
None
:
# TODO
for
attr
in
self
.
proto
.
attrs
:
pass
attr_name
=
attr
.
name
if
not
attr_name
in
attrs
:
continue
if
not
isinstance
(
attrs
[
attr_name
],
Block
):
self
.
desc
.
set_attr
(
attr_name
,
attrs
[
attr_name
])
else
:
self
.
desc
.
set_block_attr
(
attr_name
,
attrs
[
attr_name
].
desc
)
# TODO: Getters
# TODO: Getters
...
@@ -80,8 +138,8 @@ class Block(object):
...
@@ -80,8 +138,8 @@ class Block(object):
return
Variable
(
self
,
*
args
,
**
kwargs
)
return
Variable
(
self
,
*
args
,
**
kwargs
)
def
append_op
(
self
,
*
args
,
**
kwargs
):
def
append_op
(
self
,
*
args
,
**
kwargs
):
op_
proto
=
self
.
proto
.
append_op
()
op_
desc
=
self
.
proto
.
append_op
()
op
=
Operator
(
self
,
op_
proto
,
*
args
,
**
kwargs
)
op
=
Operator
(
self
,
op_
desc
,
*
args
,
**
kwargs
)
self
.
ops
.
append
(
op
)
self
.
ops
.
append
(
op
)
return
op
return
op
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录