Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
8894c67d
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
8894c67d
编写于
2月 02, 2018
作者:
Y
Yan Chunwei
提交者:
GitHub
2月 02, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add block graph image for debuging (#8026)
init debuger.
上级
2bd92754
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
347 addition
and
3 deletion
+347
-3
python/paddle/v2/fluid/debuger.py
python/paddle/v2/fluid/debuger.py
+73
-0
python/paddle/v2/fluid/framework.py
python/paddle/v2/fluid/framework.py
+2
-3
python/paddle/v2/fluid/graphviz.py
python/paddle/v2/fluid/graphviz.py
+272
-0
未找到文件。
python/paddle/v2/fluid/debuger.py
0 → 100644
浏览文件 @
8894c67d
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
re
from
graphviz
import
GraphPreviewGenerator
import
proto.framework_pb2
as
framework_pb2
def
draw_block_graphviz
(
block
,
highlights
=
None
,
path
=
"./temp.dot"
):
'''
Generate a debug graph for block.
Args:
block(Block): a block.
'''
graph
=
GraphPreviewGenerator
(
"some graph"
)
# collect parameters and args
protostr
=
block
.
desc
.
serialize_to_string
()
desc
=
framework_pb2
.
BlockDesc
.
FromString
(
str
(
protostr
))
def
need_highlight
(
name
):
if
highlights
is
None
:
return
False
for
pattern
in
highlights
:
assert
type
(
pattern
)
is
str
if
re
.
match
(
pattern
,
name
):
return
True
return
False
# draw parameters and args
vars
=
{}
for
var
in
desc
.
vars
:
shape
=
[
str
(
i
)
for
i
in
var
.
lod_tensor
.
tensor
.
dims
]
if
not
shape
:
shape
=
[
'null'
]
# create var
if
var
.
persistable
:
varn
=
graph
.
add_param
(
var
.
name
,
var
.
type
,
shape
,
highlight
=
need_highlight
(
var
.
name
))
else
:
varn
=
graph
.
add_arg
(
var
.
name
,
highlight
=
need_highlight
(
var
.
name
))
vars
[
var
.
name
]
=
varn
def
add_op_link_var
(
op
,
var
,
op2var
=
False
):
for
arg
in
var
.
arguments
:
if
arg
not
in
vars
:
# add missing variables as argument
vars
[
arg
]
=
graph
.
add_arg
(
arg
,
highlight
=
need_highlight
(
arg
))
varn
=
vars
[
arg
]
highlight
=
need_highlight
(
op
.
description
)
or
need_highlight
(
varn
.
description
)
if
op2var
:
graph
.
add_edge
(
op
,
varn
,
highlight
=
highlight
)
else
:
graph
.
add_edge
(
varn
,
op
,
highlight
=
highlight
)
for
op
in
desc
.
ops
:
opn
=
graph
.
add_op
(
op
.
type
,
highlight
=
need_highlight
(
op
.
type
))
for
var
in
op
.
inputs
:
add_op_link_var
(
opn
,
var
,
False
)
for
var
in
op
.
outputs
:
add_op_link_var
(
opn
,
var
,
True
)
graph
(
path
,
show
=
True
)
python/paddle/v2/fluid/framework.py
浏览文件 @
8894c67d
...
...
@@ -451,9 +451,8 @@ class Operator(object):
if
not
given
==
need
:
raise
ValueError
((
"Incorrect setting for output(s) of "
"operator
\"
%s
\"
. Need: [%s] Given: [%s]"
)
%
(
type
,
", "
.
join
(
str
(
e
)
for
e
in
need
),
", "
.
join
(
str
(
e
)
for
e
in
given
)))
(
type
,
", "
.
join
(
str
(
e
)
for
e
in
need
),
", "
.
join
(
str
(
e
)
for
e
in
given
)))
for
out_proto
in
proto
.
outputs
:
out_args
=
outputs
[
out_proto
.
name
]
...
...
python/paddle/v2/fluid/graphviz.py
0 → 100644
浏览文件 @
8894c67d
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
os
import
random
import
subprocess
import
logging
def
crepr
(
v
):
if
type
(
v
)
is
str
or
type
(
v
)
is
unicode
:
return
'"%s"'
%
v
return
str
(
v
)
class
Rank
(
object
):
def
__init__
(
self
,
kind
,
name
,
priority
):
'''
kind: str
name: str
priority: int
'''
self
.
kind
=
kind
self
.
name
=
name
self
.
priority
=
priority
self
.
nodes
=
[]
def
__str__
(
self
):
if
not
self
.
nodes
:
return
''
return
'{'
+
'rank={};'
.
format
(
self
.
kind
)
+
\
','
.
join
([
node
.
name
for
node
in
self
.
nodes
])
+
'}'
class
Graph
(
object
):
rank_counter
=
0
def
__init__
(
self
,
title
,
**
attrs
):
self
.
title
=
title
self
.
attrs
=
attrs
self
.
nodes
=
[]
self
.
edges
=
[]
self
.
rank_groups
=
{}
def
code
(
self
):
return
self
.
__str__
()
def
rank_group
(
self
,
kind
,
priority
):
name
=
"rankgroup-%d"
%
Graph
.
rank_counter
Graph
.
rank_counter
+=
1
rank
=
Rank
(
kind
,
name
,
priority
)
self
.
rank_groups
[
name
]
=
rank
return
name
def
node
(
self
,
label
,
prefix
,
description
=
""
,
**
attrs
):
node
=
Node
(
label
,
prefix
,
description
,
**
attrs
)
if
'rank'
in
attrs
:
rank
=
self
.
rank_groups
[
attrs
[
'rank'
]]
del
attrs
[
'rank'
]
rank
.
nodes
.
append
(
node
)
self
.
nodes
.
append
(
node
)
return
node
def
edge
(
self
,
source
,
target
,
**
attrs
):
edge
=
Edge
(
source
,
target
,
**
attrs
)
self
.
edges
.
append
(
edge
)
return
edge
def
compile
(
self
,
dot_path
):
file
=
open
(
dot_path
,
'w'
)
file
.
write
(
self
.
__str__
())
image_path
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
dot_path
[:
-
3
]
+
"pdf"
)
cmd
=
[
"dot"
,
"-Tpdf"
,
dot_path
,
"-o"
,
image_path
]
subprocess
.
Popen
(
cmd
,
stdin
=
subprocess
.
PIPE
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
logging
.
warning
(
"write block debug graph to {}"
.
format
(
image_path
))
return
image_path
def
show
(
self
,
dot_path
):
image
=
self
.
compile
(
dot_path
)
cmd
=
[
"open"
,
image
]
subprocess
.
Popen
(
cmd
,
stdin
=
subprocess
.
PIPE
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
)
def
_rank_repr
(
self
):
ranks
=
sorted
(
self
.
rank_groups
.
items
(),
cmp
=
lambda
a
,
b
:
a
[
1
].
priority
>
b
[
1
].
priority
)
repr
=
[]
for
x
in
ranks
:
repr
.
append
(
str
(
x
[
1
]))
return
'
\n
'
.
join
(
repr
)
+
'
\n
'
def
__str__
(
self
):
reprs
=
[
'digraph G {'
,
'title = {}'
.
format
(
crepr
(
self
.
title
)),
]
for
attr
in
self
.
attrs
:
reprs
.
append
(
"{key}={value};"
.
format
(
key
=
attr
,
value
=
crepr
(
self
.
attrs
[
attr
])))
reprs
.
append
(
self
.
_rank_repr
())
random
.
shuffle
(
self
.
nodes
)
reprs
+=
[
str
(
node
)
for
node
in
self
.
nodes
]
for
x
in
self
.
edges
:
reprs
.
append
(
str
(
x
))
reprs
.
append
(
'}'
)
return
'
\n
'
.
join
(
reprs
)
class
Node
(
object
):
counter
=
1
def
__init__
(
self
,
label
,
prefix
,
description
=
""
,
**
attrs
):
self
.
label
=
label
self
.
name
=
"%s_%d"
%
(
prefix
,
Node
.
counter
)
self
.
description
=
description
self
.
attrs
=
attrs
Node
.
counter
+=
1
def
__str__
(
self
):
reprs
=
'{name} [label={label} {extra} ];'
.
format
(
name
=
self
.
name
,
label
=
self
.
label
,
extra
=
','
+
','
.
join
(
"%s=%s"
%
(
key
,
crepr
(
value
))
for
key
,
value
in
self
.
attrs
.
items
())
if
self
.
attrs
else
""
)
return
reprs
class
Edge
(
object
):
def
__init__
(
self
,
source
,
target
,
**
attrs
):
'''
Link source to target.
:param source: Node
:param target: Node
:param graph: Graph
:param attrs: dic
'''
self
.
source
=
source
self
.
target
=
target
self
.
attrs
=
attrs
def
__str__
(
self
):
repr
=
"{source} -> {target} {extra}"
.
format
(
source
=
self
.
source
.
name
,
target
=
self
.
target
.
name
,
extra
=
""
if
not
self
.
attrs
else
"["
+
','
.
join
(
"{}={}"
.
format
(
attr
[
0
],
crepr
(
attr
[
1
]))
for
attr
in
self
.
attrs
.
items
())
+
"]"
)
return
repr
class
GraphPreviewGenerator
(
object
):
'''
Generate a graph image for ONNX proto.
'''
def
__init__
(
self
,
title
):
# init graphviz graph
self
.
graph
=
Graph
(
title
,
layout
=
"dot"
,
concentrate
=
"true"
,
rankdir
=
"TB"
,
)
self
.
op_rank
=
self
.
graph
.
rank_group
(
'same'
,
2
)
self
.
param_rank
=
self
.
graph
.
rank_group
(
'same'
,
1
)
self
.
arg_rank
=
self
.
graph
.
rank_group
(
'same'
,
0
)
def
__call__
(
self
,
path
=
'temp.dot'
,
show
=
False
):
if
not
show
:
self
.
graph
.
compile
(
path
)
else
:
self
.
graph
.
show
(
path
)
def
add_param
(
self
,
name
,
data_type
,
shape
,
highlight
=
False
):
label
=
'
\n
'
.
join
([
'<<table cellpadding="5">'
,
' <tr>'
,
' <td bgcolor="#2b787e">'
,
' <b>'
,
name
,
' </b>'
,
' </td>'
,
' </tr>'
,
' <tr>'
,
' <td>'
,
str
(
data_type
),
' </td>'
' </tr>'
,
' <tr>'
,
' <td>'
,
'[%s]'
%
'x'
.
join
(
shape
),
' </td>'
' </tr>'
,
'</table>>'
,
])
return
self
.
graph
.
node
(
label
,
prefix
=
"param"
,
description
=
name
,
shape
=
"none"
,
style
=
"rounded,filled,bold"
,
width
=
"1.3"
,
color
=
"#148b97"
if
not
highlight
else
"orange"
,
fontcolor
=
"#ffffff"
,
fontname
=
"Arial"
)
def
add_op
(
self
,
opType
,
**
kwargs
):
highlight
=
False
if
'highlight'
in
kwargs
:
highlight
=
kwargs
[
'highlight'
]
del
kwargs
[
'highlight'
]
return
self
.
graph
.
node
(
"<<B>%s</B>>"
%
opType
,
prefix
=
"op"
,
description
=
opType
,
shape
=
"box"
,
style
=
"rounded, filled, bold"
,
color
=
"#303A3A"
if
not
highlight
else
"orange"
,
fontname
=
"Arial"
,
fontcolor
=
"#ffffff"
,
width
=
"1.3"
,
height
=
"0.84"
,
)
def
add_arg
(
self
,
name
,
highlight
=
False
):
return
self
.
graph
.
node
(
crepr
(
name
),
prefix
=
"arg"
,
description
=
name
,
shape
=
"box"
,
style
=
"rounded,filled,bold"
,
fontname
=
"Arial"
,
fontcolor
=
"#999999"
,
color
=
"#dddddd"
if
not
highlight
else
"orange"
)
def
add_edge
(
self
,
source
,
target
,
**
kwargs
):
highlight
=
False
if
'highlight'
in
kwargs
:
highlight
=
kwargs
[
'highlight'
]
del
kwargs
[
'highlight'
]
return
self
.
graph
.
edge
(
source
,
target
,
color
=
"#00000"
if
not
highlight
else
"orange"
,
**
kwargs
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录