Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
OpenDAS
nni
Commits
6e629908
Unverified
Commit
6e629908
authored
Mar 27, 2020
by
QuanluZhang
Committed by
GitHub
Mar 27, 2020
Browse files
[BUG] finding leaf modules (#2241)
parent
5c8cb258
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
31 deletions
+28
-31
src/sdk/pynni/nni/compression/speedup/torch/compressor.py
src/sdk/pynni/nni/compression/speedup/torch/compressor.py
+28
-31
No files found.
src/sdk/pynni/nni/compression/speedup/torch/compressor.py
View file @
6e629908
...
...
@@ -229,42 +229,39 @@ class ModelSpeedup:
list
a list of scope name of all the leaf modules
"""
pieces
=
[]
# each element is a dict
class
SNode
:
def
__init__
(
self
,
name
):
self
.
sname
=
name
self
.
childs
=
{}
root
=
None
for
node
in
graph
.
nodes
():
scope_name
=
node
.
scopeName
()
if
scope_name
==
''
:
continue
segs
=
scope_name
.
split
(
'/'
)
segs_len
=
len
(
segs
)
# increase the length of `pieces` if not enough
for
_
in
range
(
segs_len
-
len
(
pieces
)):
pieces
.
append
({})
# process internal segments of the scope name
# 'L' means leaf segment
# 'I' means internal segment
# internal segment can replace leaf segment at the same position of `pieces`
for
i
,
seg
in
enumerate
(
segs
[:
-
1
]):
seg_name_dict
=
pieces
[
i
]
if
seg
in
seg_name_dict
:
if
seg_name_dict
[
seg
][
0
]
==
'L'
:
seg_name_dict
[
seg
]
=
(
'I'
,
node
)
if
root
is
None
:
root
=
SNode
(
segs
[
0
])
curr
=
root
for
seg
in
segs
[
1
:]:
if
not
seg
in
curr
.
childs
:
curr
.
childs
[
seg
]
=
SNode
(
seg
)
curr
=
curr
.
childs
[
seg
]
leaf_nodes
=
[]
def
traverse_tree
(
node
,
scope_name
):
if
scope_name
==
''
:
sn
=
node
.
sname
else
:
sn
=
scope_name
+
'/'
+
node
.
sname
if
not
node
.
childs
:
if
node
.
sname
[
-
1
]
==
']'
:
leaf_nodes
.
append
(
sn
)
else
:
seg_name_dict
[
seg
]
=
(
'I'
,
node
)
# process the leaf segment of the scope name
last_segs_dict
=
pieces
[
len
(
segs
)
-
1
]
if
not
segs
[
-
1
]
in
last_segs_dict
:
last_segs_dict
[
segs
[
-
1
]]
=
(
'L'
,
node
)
# traverse `pieces` to obtain all the leaf modules which are labeled with 'L'
leaf_modules
=
[]
for
piece
in
pieces
:
for
_
,
value
in
piece
.
items
():
if
value
[
0
]
==
'L'
:
assert
value
[
1
].
scopeName
()
not
in
leaf_modules
# if this is a leaf module, the last segment of its scope name
# must be in pattern `xxx[xxx]`
if
value
[
1
].
scopeName
()[
-
1
]
==
']'
:
leaf_modules
.
append
(
value
[
1
].
scopeName
())
return
leaf_modules
for
key
in
node
.
childs
:
traverse_tree
(
node
.
childs
[
key
],
sn
)
traverse_tree
(
root
,
''
)
return
leaf_nodes
def
_build_graph
(
self
):
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment