Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Constraint-aware one-shot pruners #2657

Merged
merged 111 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from 109 commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
3515b29
constraint-aware pruner
Jul 5, 2020
1c84925
Constrained Structure pruner.
Jul 6, 2020
3ed53cb
Constrained pruner.
Jul 6, 2020
a415206
Constrained one-shot pruner.
Jul 6, 2020
5fa19fc
Constraint aware pruner.
Jul 7, 2020
bed63fe
Constrained one-shot pruner.
Jul 7, 2020
12c24e5
Constrained one shot pruner.
Jul 7, 2020
45e62c2
Constrained-aware one-shot pruner.
Jul 7, 2020
aeb8aaf
Update the doc.
Jul 7, 2020
70dac7c
reformat the unit test.
Jul 7, 2020
5362328
Add test case for constrained-aware pruners.
Jul 8, 2020
bd375a4
Remove the unnecessary log function.
Jul 8, 2020
9d0fb79
fix pylint errors.
Jul 8, 2020
211a047
Add the docs for the constrained pruners.
Jul 13, 2020
a11cf48
empty commit
Jul 13, 2020
899b6f9
Merge branch 'master' of https://github.com/microsoft/nni into constr…
Jul 13, 2020
439426f
Add an accuracy comparsion benchmark for Constrained Pruner.
Jul 20, 2020
a08dac6
update
Jul 20, 2020
39eadcc
Merge branch 'master' of https://github.com/microsoft/nni into constr…
Jul 23, 2020
fab3315
update the benchmark
Jul 23, 2020
4bc3c48
Update constrained pruner benchmark.
Jul 24, 2020
6c32a7c
update
Jul 25, 2020
9923ea1
update
Jul 27, 2020
5a35c8e
update
Jul 28, 2020
00eb006
fix a bug.
Jul 28, 2020
65676ad
update.
Jul 28, 2020
50d3468
update
Jul 28, 2020
d358cb2
update
Aug 2, 2020
174233c
tmp branch
Aug 3, 2020
b36589c
update
Aug 3, 2020
482e500
update
Aug 4, 2020
e5b262e
support imagenet for auto_pruners_torch
Aug 4, 2020
2591abc
update
Aug 4, 2020
0ee36b8
and a switch for the constrained pruner
Aug 4, 2020
19df319
update
Aug 4, 2020
b2f03b7
update
Aug 4, 2020
e263349
update
Aug 4, 2020
fca51bf
update
Aug 5, 2020
0fabf61
update
Aug 5, 2020
1ac7050
update
Aug 6, 2020
e55fe10
bug in the sm pruner
Aug 6, 2020
c1f9a45
update
Aug 6, 2020
9aa1558
add one more mile stone
Aug 6, 2020
e9b39fb
fix a bug caused by the expand and clone
Aug 7, 2020
d7cc452
add a constrained switch for the auto compress pruner
Aug 7, 2020
9183b93
add support for imagenet
Aug 11, 2020
e3226ee
unfinish
Aug 21, 2020
eca8577
attention pruner unfinished
Aug 24, 2020
ad58382
update
Aug 25, 2020
90c1c47
merge from master
Aug 25, 2020
12c289e
update
Aug 26, 2020
4029b1c
update
Aug 26, 2020
f3098fb
update
Aug 27, 2020
1fd32f5
update
Aug 27, 2020
755ce8b
update
Aug 27, 2020
1639867
updata
Aug 27, 2020
fe78c59
update
Aug 27, 2020
5c54faf
update
Aug 27, 2020
f5d4060
update
Aug 28, 2020
e5bcd6a
add no dependency
Aug 29, 2020
f625f81
use softmax in the attention pruner
Aug 31, 2020
e5f3e01
update
Aug 31, 2020
fcc984c
update
Aug 31, 2020
13d4f38
update
Sep 1, 2020
b7bac26
update
Sep 2, 2020
b3d1ac9
update
Sep 3, 2020
90d1e45
add the unit test.
Sep 3, 2020
cf7c936
update
Sep 3, 2020
85fc79f
update
Sep 3, 2020
b593ba3
update
Sep 3, 2020
6682cb3
update
Sep 3, 2020
25beb8f
update doc string
Sep 4, 2020
649ecfd
update the documentation
Sep 4, 2020
fb09b3f
Remove the attention pruner.
Sep 4, 2020
da5525a
remove the mobilenet_v2 for cifar10
Sep 4, 2020
0920efe
reset the auto_pruners_torch.py
Sep 4, 2020
74f4ec4
update the example to the new interface.
Sep 4, 2020
45966c9
fix pylint errors
Sep 4, 2020
e02fb90
update the example
Sep 4, 2020
cf626f8
fix a bug when counting flops
Sep 7, 2020
f444ebc
add several new one-shot pruners
Sep 8, 2020
a0d1e97
support more one_shot prunersw
Sep 8, 2020
59f0fe1
test
Sep 8, 2020
68e4563
fix a bug in the original apoz pruner
Sep 8, 2020
bddb70f
update
Sep 9, 2020
646324a
update
Sep 9, 2020
c7ba084
update
Sep 9, 2020
7a54cd6
update
Sep 9, 2020
c42de2c
update
Sep 9, 2020
9b9bb09
update
Sep 10, 2020
82f4fdb
update the unit test
Sep 10, 2020
8438ff5
update the examples
Sep 10, 2020
f9028f5
rm the test_dependency_aware
Sep 10, 2020
8afa53a
update
Sep 10, 2020
5c6d60e
update
Sep 10, 2020
78f3fc6
update the doc
Sep 10, 2020
2125e98
update rst
Sep 10, 2020
ae62671
Merge branch 'master' of https://github.com/microsoft/nni into constr…
Sep 11, 2020
8963a01
update
Sep 11, 2020
3691f23
Merge branch 'master' of https://github.com/microsoft/nni into constr…
Sep 13, 2020
29029ee
update doc
Sep 14, 2020
a8f3f74
update the doc
Sep 14, 2020
9bf7667
update doc
Sep 14, 2020
b7b7150
update the doc
Sep 14, 2020
e68cec0
update
Sep 16, 2020
c9c5329
update
Sep 16, 2020
ef51a10
update
Sep 16, 2020
4acabaa
update
Sep 16, 2020
80aec67
add some evaluation results
Sep 16, 2020
3a73e30
update
Sep 21, 2020
d5bbe48
update the doc
Sep 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions docs/en_US/Compressor/DependencyAware.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Dependency-aware Mode for Filter Pruning

Currently, we have several filter pruning algorithm for the convolutional layers: FPGM Pruner, L1Filter Pruner, L2Filter Pruner, Activation APoZ Rank Filter Pruner, Activation Mean Rank Filter Pruner, Taylor FO On Weight Pruner. In these filter pruning algorithms, the pruner will prune each convolutional layer separately. While pruning a convolution layer, the algorithm will quantify the importance of each filter based on some specific rules(such as l1-norm), and prune the less important filters.

As [dependency analysis utils](./CompressionUtils.md) shows, if the output channels of two convolutional layers(conv1, conv2) are added together, then these two conv layers have channel dependency with each other(more details please see [Compression Utils](./CompressionUtils.md)). Take the following figure as an example.
![](../../img/mask_conflict.jpg)

If we prune the first 50% of output channels(filters) for conv1, and prune the last 50% of output channels for conv2. Although both layers have pruned 50% of the filters, the speedup module still needs to add zeros to align the output channels. In this case, we cannot harvest the speed benefit from the model pruning.


To better gain the speed benefit of the model pruning, we add a dependency-aware mode for the Filter Pruner. In the dependency-aware mode, the pruner prunes the model not only based on the l1 norm of each filter, but also the topology of the whole network architecture.

In the dependency-aware mode(`dependency_aware` is set `True`), the pruner will try to prune the same output channels for the layers that have the channel dependencies with each other, as shown in the following figure.

![](../../img/dependency-aware.jpg)

Take the dependency-aware mode of L1Filter Pruner as an example. Specifically, the pruner will calculate the L1 norm (for example) sum of all the layers in the dependency set for each channel. Obviously, the number of channels that can actually be pruned of this dependency set in the end is determined by the minimum sparsity of layers in this dependency set(denoted by `min_sparsity`). According to the L1 norm sum of each channel, the pruner will prune the same `min_sparsity` channels for all the layers. Next, the pruner will additionally prune `sparsity` - `min_sparsity` channels for each convolutional layer based on its own L1 norm of each channel. For example, suppose the output channels of `conv1` , `conv2` are added together and the configured sparsities of `conv1` and `conv2` are 0.3, 0.2 respectively. In this case, the `dependency-aware pruner` will

- First, prune the same 20% of channels for `conv1` and `conv2` according to L1 norm sum of `conv1` and `conv2`.
- Second, the pruner will additionally prune 10% channels for `conv1` according to the L1 norm of each channel of `conv1`.

In addition, for the convolutional layers that have more than one filter group, `dependency-aware pruner` will also try to prune the same number of the channels for each filter group. Overall, this pruner will prune the model according to the L1 norm of each filter and try to meet the topological constrains(channel dependency, etc) to improve the final speed gain after the speedup process.

In the dependency-aware mode, the pruner will provide a better speed gain from the model pruning.

## Usage
In this section, we will show how to enable the dependency-aware mode for the filter pruner. Currently, only the one-shot pruners such as FPGM Pruner, L1Filter Pruner, L2Filter Pruner, Activation APoZ Rank Filter Pruner, Activation Mean Rank Filter Pruner, Taylor FO On Weight Pruner, support the dependency-aware mode.

To enable the dependency-aware mode for `L1FilterPruner`:
```python
from nni.compression.torch import L1FilterPruner
config_list = [{ 'sparsity': 0.8, 'op_types': ['Conv2d'] }]
# dummy_input is necessary for the dependency_aware mode
dummy_input = torch.ones(1, 3, 224, 224).cuda()
pruner = L1FilterPruner(model, config_list, dependency_aware=True, dummy_input=dummy_input)
pruner.compress()
```

To enable the dependency-aware mode for `L2FilterPruner`:
```python
from nni.compression.torch import L2FilterPruner
config_list = [{ 'sparsity': 0.8, 'op_types': ['Conv2d'] }]
# dummy_input is necessary for the dependency_aware mode
dummy_input = torch.ones(1, 3, 224, 224).cuda()
pruner = L2FilterPruner(model, config_list, dependency_aware=True, dummy_input=dummy_input)
pruner.compress()
```

To enable the dependency-aware mode for `FPGMPruner`:
```python
from nni.compression.torch import FPGMPruner
config_list = [{
'sparsity': 0.5,
'op_types': ['Conv2d']
}]
# dummy_input is necessary for the dependency_aware mode
dummy_input = torch.ones(1, 3, 224, 224).cuda()
pruner = FPGMPruner(model, config_list, dependency_aware=True, dummy_input=dummy_input)
pruner.compress()
```

To enable the dependency-aware mode for `ActivationAPoZRankFilterPruner`
```python
from nni.compression.torch import ActivationAPoZRankFilterPruner
config_list = [{
'sparsity': 0.5,
'op_types': ['Conv2d']
}]
# dummy_input is necessary for the dependency_aware mode
dummy_input = torch.ones(1, 3, 224, 224).cuda()
pruner = ActivationAPoZRankFilterPruner(model, config_list, statistics_batch_num=1, , dependency_aware=True, dummy_input=dummy_input)
pruner.compress()
```

To enable the dependency-aware mode for `ActivationMeanRankFilterPruner`:

```python
from nni.compression.torch import ActivationMeanRankFilterPruner
config_list = [{
'sparsity': 0.5,
'op_types': ['Conv2d']
}]
# dummy_input is necessary for the dependency-aware mode and the
# dummy_input should be on the same device with the model
dummy_input = torch.ones(1, 3, 224, 224).cuda()
pruner = ActivationMeanRankFilterPruner(model, config_list, statistics_batch_num=1, dependency_aware=True, dummy_input=dummy_input)
pruner.compress()
```

To enable the dependency-aware mode for `TaylorFOWeightFilterPruner`:
```python
from nni.compression.torch import TaylorFOWeightFilterPruner
config_list = [{
'sparsity': 0.5,
'op_types': ['Conv2d']
}]
dummy_input = torch.ones(1, 3, 224, 224).cuda()
pruner = TaylorFOWeightFilterPruner(model, config_list, statistics_batch_num=1, dependency_aware=True, dummy_input=dummy_input)
pruner.compress()
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can simplify this example code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, how about we just keep the example of L1FilterPruner and remove the others?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, and above this line pruner = L1FilterPruner(model, config_list, dependency_aware=True, dummy_input=dummy_input) you can use comments to show the usage of other pruners, one comment line for each pruner


## Evaluation
In order to compare the performance of the pruner with or without the dependency-aware mode, we use L1FilterPruner to prune the Mobilenet_v2 separately when the dependency-aware mode is turned on and off. To simplify the experiment, we use the uniform pruning which means we allocate the same sparsity for all convolutional layers in the model.
We trained a Mobilenet_v2 model on the cifar10 dataset and prune the model based on this pretrained checkpoint. The following figure shows the accuracy and FLOPs of the model pruned by different pruners.
![](../../img/mobilev2_l1_cifar.jpg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this figure looks great!


In the figure, the `Dependency-aware` represents the L1FilterPruner with dependency-aware mode enabled. `L1 Filter` is the normal `L1FilterPruner` without the dependency-aware mode, and the `No-Dependency` means pruner only prunes the layers that has no channel dependency with other layers. As we can see in the figure, when the dependency-aware mode enabled, the pruner can bring higher accuracy under the same Flops.
20 changes: 19 additions & 1 deletion docs/en_US/Compressor/Pruner.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ FPGMPruner prune filters with the smallest geometric median.

![](../../img/fpgm_fig1.png)

>Previous works utilized “smaller-norm-less-important” criterion to prune filters with smaller norm values in a convolutional neural network. In this paper, we analyze this norm-based criterion and point out that its effectiveness depends on two requirements that are not always met: (1) the norm deviation of the filters should be large; (2) the minimum norm of the filters should be small. To solve this problem, we propose a novel filter pruning method, namely Filter Pruning via Geometric Median (FPGM), to compress the model regardless of those two requirements. Unlike previous methods, FPGM compresses CNN models by pruning filters with redundancy, rather than those with “relatively less” importance.
>Previous works utilized “smaller-norm-less-important” criterion to prune filters with smaller norm values in a convolutional neural network. In this paper, we analyze this norm-based criterion and point out that its effectiveness depends on two requirements that are not always met: (1) the norm deviation of the filters should be large; (2) the minimum norm of the filters should be small. To solve this problem, we propose a novel filter pruning method, namely Filter Pruning via Geometric Median (FPGM), to compress the model regardless of those two requirements. Unlike previous methods, FPGM compresses CNN models by pruning filters with redundancy, rather than those with “relatively less” importance.

We also provide a dependency-aware mode for this pruner to get better speedup from the pruning. Please reference [dependency-aware](./DependencyAware.md) for more details.

### Usage

Expand Down Expand Up @@ -154,6 +156,8 @@ This is an one-shot pruner, In ['PRUNING FILTERS FOR EFFICIENT CONVNETS'](https:
> 4. A new kernel matrix is created for both the ![](http://latex.codecogs.com/gif.latex?i)th and ![](http://latex.codecogs.com/gif.latex?i+1)th layers, and the remaining kernel
> weights are copied to the new model.

In addition, we also provide a dependency-aware mode for the L1FilterPruner. For more details about the dependency-aware mode, please reference [dependency-aware mode](./DependencyAware.md).

### Usage

PyTorch code
Expand Down Expand Up @@ -189,6 +193,8 @@ The experiments code can be found at [examples/model_compress]( https://github.c

This is a structured pruning algorithm that prunes the filters with the smallest L2 norm of the weights. It is implemented as a one-shot pruner.

We also provide a dependency-aware mode for this pruner to get better speedup from the pruning. Please reference [dependency-aware](./DependencyAware.md) for more details.

### Usage

PyTorch code
Expand All @@ -200,6 +206,7 @@ pruner = L2FilterPruner(model, config_list)
pruner.compress()
```


### User configuration for L2Filter Pruner

##### PyTorch
Expand All @@ -208,6 +215,7 @@ pruner.compress()
```
***


## ActivationAPoZRankFilter Pruner

ActivationAPoZRankFilter Pruner is a pruner which prunes the filters with the smallest importance criterion `APoZ` calculated from the output activations of convolution layers to achieve a preset level of network sparsity. The pruning criterion `APoZ` is explained in the paper [Network Trimming: A Data-Driven Neuron Pruning Approach towards Efficient Deep Architectures](https://arxiv.org/abs/1607.03250).
Expand All @@ -216,6 +224,8 @@ The APoZ is defined as:

![](../../img/apoz.png)

We also provide a dependency-aware mode for this pruner to get better speedup from the pruning. Please reference [dependency-aware](./DependencyAware.md) for more details.

### Usage

PyTorch code
Expand All @@ -234,6 +244,8 @@ Note: ActivationAPoZRankFilterPruner is used to prune convolutional layers withi

You can view [example](https://github.com/microsoft/nni/blob/master/examples/model_compress/model_prune_torch.py) for more information.



### User configuration for ActivationAPoZRankFilter Pruner

##### PyTorch
Expand All @@ -247,6 +259,8 @@ You can view [example](https://github.com/microsoft/nni/blob/master/examples/mod

ActivationMeanRankFilterPruner is a pruner which prunes the filters with the smallest importance criterion `mean activation` calculated from the output activations of convolution layers to achieve a preset level of network sparsity. The pruning criterion `mean activation` is explained in section 2.2 of the paper[Pruning Convolutional Neural Networks for Resource Efficient Inference](https://arxiv.org/abs/1611.06440). Other pruning criteria mentioned in this paper will be supported in future release.

We also provide a dependency-aware mode for this pruner to get better speedup from the pruning. Please reference [dependency-aware](./DependencyAware.md) for more details.

### Usage

PyTorch code
Expand All @@ -265,6 +279,7 @@ Note: ActivationMeanRankFilterPruner is used to prune convolutional layers withi

You can view [example](https://github.com/microsoft/nni/blob/master/examples/model_compress/model_prune_torch.py) for more information.


### User configuration for ActivationMeanRankFilterPruner

##### PyTorch
Expand All @@ -273,6 +288,7 @@ You can view [example](https://github.com/microsoft/nni/blob/master/examples/mod
```
***


## TaylorFOWeightFilter Pruner

TaylorFOWeightFilter Pruner is a pruner which prunes convolutional layers based on estimated importance calculated from the first order taylor expansion on weights to achieve a preset level of network sparsity. The estimated importance of filters is defined as the paper [Importance Estimation for Neural Network Pruning](http://jankautz.com/publications/Importance4NNPruning_CVPR19.pdf). Other pruning criteria mentioned in this paper will be supported in future release.
Expand All @@ -281,6 +297,8 @@ TaylorFOWeightFilter Pruner is a pruner which prunes convolutional layers based

![](../../img/importance_estimation_sum.png)

We also provide a dependency-aware mode for this pruner to get better speedup from the pruning. Please reference [dependency-aware](./DependencyAware.md) for more details.

### Usage

PyTorch code
Expand Down
2 changes: 1 addition & 1 deletion docs/en_US/model_compression.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ For details, please refer to the following tutorials:

Overview <Compressor/Overview>
Quick Start <Compressor/QuickStart>
Pruners <Compressor/Pruner>
Pruning <pruning>
Quantizers <Compressor/Quantizer>
Automatic Model Compression <Compressor/AutoCompression>
Model Speedup <Compressor/ModelSpeedup>
Expand Down
17 changes: 17 additions & 0 deletions docs/en_US/pruning.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#################
Pruning
#################

NNI provides several pruning algorithms that support fine-grained weight pruning and structural filter pruning.
It supports Tensorflow and PyTorch with unified interface.
For users to prune their models, they only need to add several lines in their code.
For the structural filter pruning, NNI also provides a dependency-aware mode. In the dependency-aware mode, the
filter pruner will get better speed gain after the speedup.

For details, please refer to the following tutorials:

.. toctree::
:maxdepth: 2

Pruners <Compressor/Pruner>
Dependency Aware Mode <Compressor/DependencyAware>
Binary file added docs/img/dependency-aware.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/mask_conflict.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/mobilev2_l1_cifar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading