Unverified Commit 6a6bdeed authored by Panacea's avatar Panacea Committed by GitHub
Browse files

Add examples for 'op_partial_names' and use more proper words. (#4225)

parent d1eaa556
...@@ -136,6 +136,7 @@ class LevelPruner(BasicPruner): ...@@ -136,6 +136,7 @@ class LevelPruner(BasicPruner):
- sparsity_per_layer : Equals to sparsity. - sparsity_per_layer : Equals to sparsity.
- op_types : Operation types to prune. - op_types : Operation types to prune.
- op_names : Operation names to prune. - op_names : Operation names to prune.
- op_partial_names: An auxiliary field collecting matched op_names in model, then this will convert to op_names.
- exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning. - exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning.
""" """
super().__init__(model, config_list) super().__init__(model, config_list)
...@@ -170,6 +171,7 @@ class NormPruner(BasicPruner): ...@@ -170,6 +171,7 @@ class NormPruner(BasicPruner):
- sparsity_per_layer : Equals to sparsity. - sparsity_per_layer : Equals to sparsity.
- op_types : Conv2d and Linear are supported in NormPruner. - op_types : Conv2d and Linear are supported in NormPruner.
- op_names : Operation names to prune. - op_names : Operation names to prune.
- op_partial_names: An auxiliary field collecting matched op_names in model, then this will convert to op_names.
- exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning. - exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning.
p p
The order of norm. The order of norm.
...@@ -229,6 +231,7 @@ class L1NormPruner(NormPruner): ...@@ -229,6 +231,7 @@ class L1NormPruner(NormPruner):
- sparsity_per_layer : Equals to sparsity. - sparsity_per_layer : Equals to sparsity.
- op_types : Conv2d and Linear are supported in L1NormPruner. - op_types : Conv2d and Linear are supported in L1NormPruner.
- op_names : Operation names to prune. - op_names : Operation names to prune.
- op_partial_names: An auxiliary field collecting matched op_names in model, then this will convert to op_names.
- exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning. - exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning.
mode mode
'normal' or 'dependency_aware'. 'normal' or 'dependency_aware'.
...@@ -260,6 +263,7 @@ class L2NormPruner(NormPruner): ...@@ -260,6 +263,7 @@ class L2NormPruner(NormPruner):
- sparsity_per_layer : Equals to sparsity. - sparsity_per_layer : Equals to sparsity.
- op_types : Conv2d and Linear are supported in L2NormPruner. - op_types : Conv2d and Linear are supported in L2NormPruner.
- op_names : Operation names to prune. - op_names : Operation names to prune.
- op_partial_names: An auxiliary field collecting matched op_names in model, then this will convert to op_names.
- exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning. - exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning.
mode mode
'normal' or 'dependency_aware'. 'normal' or 'dependency_aware'.
...@@ -291,6 +295,7 @@ class FPGMPruner(BasicPruner): ...@@ -291,6 +295,7 @@ class FPGMPruner(BasicPruner):
- sparsity_per_layer : Equals to sparsity. - sparsity_per_layer : Equals to sparsity.
- op_types : Conv2d and Linear are supported in FPGMPruner. - op_types : Conv2d and Linear are supported in FPGMPruner.
- op_names : Operation names to prune. - op_names : Operation names to prune.
- op_partial_names: An auxiliary field collecting matched op_names in model, then this will convert to op_names.
- exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning. - exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning.
mode mode
'normal' or 'dependency_aware'. 'normal' or 'dependency_aware'.
...@@ -351,6 +356,7 @@ class SlimPruner(BasicPruner): ...@@ -351,6 +356,7 @@ class SlimPruner(BasicPruner):
- max_sparsity_per_layer : Always used with total_sparsity. Limit the max sparsity of each layer. - max_sparsity_per_layer : Always used with total_sparsity. Limit the max sparsity of each layer.
- op_types : Only BatchNorm2d is supported in SlimPruner. - op_types : Only BatchNorm2d is supported in SlimPruner.
- op_names : Operation names to prune. - op_names : Operation names to prune.
- op_partial_names: An auxiliary field collecting matched op_names in model, then this will convert to op_names.
- exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning. - exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning.
trainer trainer
A callable function used to train model or just inference. Take model, optimizer, criterion as input. A callable function used to train model or just inference. Take model, optimizer, criterion as input.
...@@ -444,6 +450,7 @@ class ActivationPruner(BasicPruner): ...@@ -444,6 +450,7 @@ class ActivationPruner(BasicPruner):
- sparsity_per_layer : Equals to sparsity. - sparsity_per_layer : Equals to sparsity.
- op_types : Conv2d and Linear are supported in ActivationPruner. - op_types : Conv2d and Linear are supported in ActivationPruner.
- op_names : Operation names to prune. - op_names : Operation names to prune.
- op_partial_names: An auxiliary field collecting matched op_names in model, then this will convert to op_names.
- exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning. - exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning.
trainer trainer
A callable function used to train model or just inference. Take model, optimizer, criterion as input. A callable function used to train model or just inference. Take model, optimizer, criterion as input.
...@@ -564,6 +571,7 @@ class TaylorFOWeightPruner(BasicPruner): ...@@ -564,6 +571,7 @@ class TaylorFOWeightPruner(BasicPruner):
- max_sparsity_per_layer : Always used with total_sparsity. Limit the max sparsity of each layer. - max_sparsity_per_layer : Always used with total_sparsity. Limit the max sparsity of each layer.
- op_types : Conv2d and Linear are supported in TaylorFOWeightPruner. - op_types : Conv2d and Linear are supported in TaylorFOWeightPruner.
- op_names : Operation names to prune. - op_names : Operation names to prune.
- op_partial_names: An auxiliary field collecting matched op_names in model, then this will convert to op_names.
- exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning. - exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning.
trainer trainer
A callable function used to train model or just inference. Take model, optimizer, criterion as input. A callable function used to train model or just inference. Take model, optimizer, criterion as input.
...@@ -683,6 +691,7 @@ class ADMMPruner(BasicPruner): ...@@ -683,6 +691,7 @@ class ADMMPruner(BasicPruner):
- rho : Penalty parameters in ADMM algorithm. - rho : Penalty parameters in ADMM algorithm.
- op_types : Operation types to prune. - op_types : Operation types to prune.
- op_names : Operation names to prune. - op_names : Operation names to prune.
- op_partial_names: An auxiliary field collecting matched op_names in model, then this will convert to op_names.
- exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning. - exclude : Set True then the layers setting by op_types and op_names will be excluded from pruning.
trainer trainer
A callable function used to train model or just inference. Take model, optimizer, criterion as input. A callable function used to train model or just inference. Take model, optimizer, criterion as input.
......
...@@ -57,6 +57,6 @@ def validate_op_types(model, op_types, logger): ...@@ -57,6 +57,6 @@ def validate_op_types(model, op_types, logger):
def validate_op_types_op_names(data): def validate_op_types_op_names(data):
if not ('op_types' in data or 'op_names' in data or 'op_partial_names' in data): if not ('op_types' in data or 'op_names' in data or 'op_partial_names' in data):
raise SchemaError('At least one of the followings must be specified: op_types, op_names or op_partial_names.') raise SchemaError('At least one of the followings must be specified: op_types, op_names or op_partial_names.')
return True return True
...@@ -13,6 +13,22 @@ def config_list_canonical(model: Module, config_list: List[Dict]) -> List[Dict]: ...@@ -13,6 +13,22 @@ def config_list_canonical(model: Module, config_list: List[Dict]) -> List[Dict]:
''' '''
Split the config by op_names if 'sparsity' or 'sparsity_per_layer' in config, Split the config by op_names if 'sparsity' or 'sparsity_per_layer' in config,
and set the sub_config['total_sparsity'] = config['sparsity_per_layer']. and set the sub_config['total_sparsity'] = config['sparsity_per_layer'].
And every item in 'op_partial_names' will match corresponding 'op_names' in model,
then convert 'op_partial_names' to 'op_names' in config.
Example::
model = models.resnet18()
config_list = [{'op_types': ['Conv2d'], 'sparsity': 0.8, 'op_partial_names': ['conv1']}]
pruner = L1NormPruner(model, config_list)
pruner.compress()
pruner.show_pruned_weights()
In this process, the config_list will implicitly convert to the following:
[{'op_types': ['Conv2d'], 'sparsity_per_layer': 0.8,
'op_names': ['conv1', 'layer1.0.conv1', 'layer1.1.conv1',
'layer2.0.conv1', 'layer2.1.conv1', 'layer3.0.conv1', 'layer3.1.conv1',
'layer4.0.conv1', 'layer4.1.conv1']}]
''' '''
for config in config_list: for config in config_list:
if 'sparsity' in config: if 'sparsity' in config:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment