This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Support Speedup for Slim Pruner. #4008
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,7 +85,7 @@ def reshape_break_channel_dependency(op_node): | |
|
||
|
||
class ChannelDependency(Dependency): | ||
def __init__(self, model=None, dummy_input=None, traced_model=None): | ||
def __init__(self, model=None, dummy_input=None, traced_model=None, prune_type='Filter'): | ||
""" | ||
This model analyze the channel dependencies between the conv | ||
layers in a model. | ||
|
@@ -98,7 +98,18 @@ def __init__(self, model=None, dummy_input=None, traced_model=None): | |
traced_model : torch._C.Graph | ||
if we alreay has the traced graph of the target model, we donnot | ||
need to trace the model again. | ||
""" | ||
prune_type: str | ||
This parameter indicates the channel pruning type: 1) `Filter` | ||
prune the filter of the convolution layer to prune the corresponding | ||
channels 2) prune the channel in the batchnorm layer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 ) |
||
""" | ||
self.prune_type = prune_type | ||
self.target_types = [] | ||
if self.prune_type == 'Filter': | ||
self.target_types.extend(['Conv2d', 'Linear', 'ConvTranspose2d']) | ||
elif self.prune_type == 'Batchnorm': | ||
self.target_types.append('BatchNorm2d') | ||
|
||
super(ChannelDependency, self).__init__( | ||
model, dummy_input, traced_model) | ||
|
||
|
@@ -114,12 +125,13 @@ def _get_parent_layers(self, node): | |
parent_layers: list | ||
nearest father conv/linear layers for the target worknode. | ||
""" | ||
|
||
parent_layers = [] | ||
queue = [] | ||
queue.append(node) | ||
while queue: | ||
curnode = queue.pop(0) | ||
if curnode.op_type == 'Conv2d' or curnode.op_type == 'Linear' or curnode.op_type == 'ConvTranspose2d': | ||
if curnode.op_type in self.target_types: | ||
# find the first met conv | ||
parent_layers.append(curnode.name) | ||
continue | ||
|
@@ -130,6 +142,7 @@ def _get_parent_layers(self, node): | |
parents = [self.graph.name_to_node[name] for name in parents] | ||
for parent in parents: | ||
queue.append(parent) | ||
|
||
return parent_layers | ||
|
||
def build_dependency(self): | ||
|
@@ -193,7 +206,7 @@ def export(self, filepath): | |
csv_w = csv.writer(csvf, delimiter=',') | ||
csv_w.writerow(header) | ||
for node in self.graph.nodes_py.nodes_op: | ||
if node.op_type != 'Conv2d' or node in visited: | ||
if node.op_type not in self.target_types or node in visited: | ||
continue | ||
setid += 1 | ||
row = ['Set %d' % setid] | ||
|
@@ -220,7 +233,7 @@ def dependency_sets(self): | |
d_sets = [] | ||
visited = set() | ||
for node in self.graph.nodes_py.nodes_op: | ||
if (node.op_type != 'Conv2d' and node.op_type != 'Linear') or node in visited: | ||
if node.op_type not in self.target_types or node in visited: | ||
continue | ||
tmp_set = set() | ||
if node.name not in self.dependency: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add blank line