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

Refactor model compression examples #3326

Merged
merged 21 commits into from
Feb 4, 2021
Merged

Refactor model compression examples #3326

merged 21 commits into from
Feb 4, 2021

Conversation

colorjam
Copy link
Contributor

@colorjam colorjam commented Jan 22, 2021

Examples:

  1. Add mnist example mnist_torch.py for quick start.
  2. Merge reproduced codes into basic_pruners_torch.py.
  3. Add knowledge distillation example basic_pruners_kd_torch.py.

Doc:

  1. Remove pruners details.
  2. Refine KD example.

@colorjam colorjam changed the title Refactor compression examples Refactor model compression examples Jan 22, 2021
@J-shang J-shang closed this Jan 27, 2021
@J-shang J-shang reopened this Jan 27, 2021
Tensorflow
""""""""""

.. autoclass:: nni.algorithms.compression.tensorflow.pruning.LevelPruner
Copy link
Contributor

Choose a reason for hiding this comment

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

@liuzhe-lz do we still support tensorflow, at least provided one pruner?

Copy link
Contributor

@liuzhe-lz liuzhe-lz Feb 2, 2021

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@colorjam colorjam Feb 3, 2021

Choose a reason for hiding this comment

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

@liuzhe-lz @QuanluZhang Sorry for deleting by mistake 🥺
Upload the TensorFlow example naive_prune_tf.py.


This is an one-shot pruner, which prunes filters with the smallest geometric median
Copy link
Contributor

Choose a reason for hiding this comment

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

better to add a little bit more description

:target: ../../img/l1filter_pruner.png
:alt:

This is an one-shot pruner, which prunes the filters prunes filters in the **convolution layers**.
Copy link
Contributor

Choose a reason for hiding this comment

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

"prunes the filters prunes filters"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix it.


.. image:: ../../img/l1filter_pruner.png
:target: ../../img/l1filter_pruner.png
:alt:
Copy link
Contributor

Choose a reason for hiding this comment

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

so you think figure is not helpful?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, figures look redundant.

@@ -471,7 +418,8 @@ PyTorch code

pruner.update_epoch(epoch)

You can view :githublink:`example <examples/model_compress/pruning/model_prune_torch.py>` for more information.
You can view :githublink:`mnist example <examples/model_compress/pruning/basic_pruners_torch.py>` for more information.

Copy link
Contributor

Choose a reason for hiding this comment

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

there is no command for this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

@@ -4,39 +4,35 @@ Knowledge Distillation on NNI
KnowledgeDistill
----------------

Knowledge distillation support, in `Distilling the Knowledge in a Neural Network <https://arxiv.org/abs/1503.02531>`__\ , the compressed model is trained to mimic a pre-trained, larger model. This training setting is also referred to as "teacher-student", where the large model is the teacher and the small model is the student.
Knoiwledge Distillation (KD) is proposed in `Distilling the Knowledge in a Neural Network <https://arxiv.org/abs/1503.02531>`__\ , the compressed model is trained to mimic a pre-trained, larger model. This training setting is also referred to as "teacher-student", where the large model is the teacher and the small model is the student. KD is often used to fine-tune the pruned model.
Copy link
Contributor

Choose a reason for hiding this comment

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

knowledge

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix it

@@ -1,9 +0,0 @@
AGPruner:
Copy link
Contributor

Choose a reason for hiding this comment

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

what is this file used for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no used in the examples, looks redundant, so remove it.

@@ -62,30 +62,6 @@ def get_data(dataset, data_dir, batch_size, test_batch_size):
])),
batch_size=batch_size, shuffle=False, **kwargs)
criterion = torch.nn.CrossEntropyLoss()
elif dataset == 'imagenet':
Copy link
Contributor

Choose a reason for hiding this comment

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

why imagenet is removed?

Copy link
Contributor

Choose a reason for hiding this comment

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

BTW, what kind of pruners is auto pruner?

Copy link
Contributor

Choose a reason for hiding this comment

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

suggest to add one more example which combines auto tune and pruner, for example, tuning the sparsity.

Copy link
Contributor

Choose a reason for hiding this comment

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

suggest to add more detailed description at the top of each of these example code files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Imagenet is not used in comparison experiment, remove it for simplicity and clarity. Add more description at the top of the file, please review the latest version.

# Licensed under the MIT license.
'''
Examples for level pruner on mnist
'''
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a very simple example right? suggest to rename it to "naive_example_torch.py"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do you think "naive_prune_torch" is better?

Copy link
Contributor

Choose a reason for hiding this comment

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

agree

# Licensed under the MIT license.
'''
Examples for basic pruners
'''
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the difference between this file and basic_pruners_kd_torch.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

simplify the kd example, please review the latest version.

trialConcurrency: 1
trialGpuNumber: 0
tuner:
name: grid
Copy link
Contributor

Choose a reason for hiding this comment

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

the indent is strange

Copy link
Contributor

Choose a reason for hiding this comment

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

better to tell users how to start this experiment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix it

pruner.compress()

# after testing
nni.report_final_results(acc)
Copy link
Contributor

Choose a reason for hiding this comment

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

would be better to add simple code to show how is acc created

}

Then we need to modify our codes for few lines
The previous example manually choosed L2FilterPruner and pruned with a specified sparsity. Different sparsity and different pruners may have different effect on different models. This process can be done with NNI tuners.
Copy link
Contributor

Choose a reason for hiding this comment

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

"choosed" -> "chose"


Slim Pruner **prunes channels in the convolution layers by masking corresponding scaling factors in the later BN layers**\ , L1 regularization on the scaling factors should be applied in batch normalization (BN) layers while training, scaling factors of BN layers are** globally ranked** while pruning, so the sparse model can be automatically found given sparsity.

This is an one-shot pruner, which adds sparsity regularization on the scaling factors of batch normalization (BN) layers durting training to identify unimportant channels. . The channels with small scaling factor values will be pruned. For more details, please refer to `'Learning Efficient Convolutional Networks through Network Slimming' <https://arxiv.org/pdf/1708.06519.pdf>`__\.
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: "channels. ."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fix typos

@@ -45,7 +45,7 @@ After training, you get accuracy of the pruned model. You can export model weigh

pruner.export_model(model_path='pruned_vgg19_cifar10.pth', mask_path='mask_vgg19_cifar10.pth')

The complete code of model compression examples can be found :githublink:`here <examples/model_compress/pruning/model_prune_torch.py>`.
Please refer :githublink:`mnist example <examples/model_compress/pruning/mnist_torch.py>` for quick start.
Copy link
Contributor

Choose a reason for hiding this comment

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

there is no such file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing it out. The link has been updated.

@@ -471,7 +416,12 @@ PyTorch code

pruner.update_epoch(epoch)

You can view :githublink:`example <examples/model_compress/pruning/model_prune_torch.py>` for more information.
You can view :githublink:`mnist example <examples/model_compress/pruning/naive_example_torch.py>` for a quick start.
Copy link
Contributor

Choose a reason for hiding this comment

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

it is a little strange, why mention quick start here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I want to show the simplest example for the usage of pruners.
I will refactor the QuickStart.rst. Remove the quick start in this file.

'''
Examples for automatic pruners
Example for supported automatic pruning algorithms.
In this example, we present the usage of automatic pruners (NetAdapt, AutoCompressPruner). L1, L2, FPGM pruners are aims for comparsion.
Copy link
Contributor

Choose a reason for hiding this comment

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

"are aims for comparison" -> "are also executed for comparison purpose"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fix it.

'''
NNI example for supported basic pruning algorithms.
In this example, we show the end-to-end pruning process: pre-training -> pruning -> fine-tuning.
Note that pruners use masks to simiulate the real pruning. In order to obtain a real compressed model, model speed up is required.
Copy link
Contributor

Choose a reason for hiding this comment

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

-> simulate

# Licensed under the MIT license.

'''
NNI exmaple for fine-tuning the pruend model with KD.
Copy link
Contributor

Choose a reason for hiding this comment

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

'pruend' -> 'pruned'


'''
NNI exmaple for fine-tuning the pruend model with KD.
Run basic_pruners_torch.py first to get the pruend model.
Copy link
Contributor

Choose a reason for hiding this comment

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

'pruend' -> 'pruned'


def get_model_optimizer_scheduler(args, device, train_loader, test_loader, criterion):
if args.model == 'lenet':
model = LeNet().to(device)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not right, we should use the masked model instead of the original model.

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry, i mean the kd part

}

Then we need to modify our codes for few lines
The previous example manually chose L2FilterPruner and pruned with a specified sparsity. Different sparsity and different pruners may have different effect on different models. This process can be done with NNI tuners.
Copy link
Contributor

Choose a reason for hiding this comment

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

may have different effects...


Last, define our task and automatically tuning pruning methods with layers sparsity
Then, define a ``config`` file in YAML to automatically tuning model, pruning algorithm and sparisty.
Copy link
Contributor

Choose a reason for hiding this comment

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

sparisty -> sparsity

@@ -1,16 +1,15 @@
Supported Pruning Algorithms on NNI
===================================

We provide several pruning algorithms that support fine-grained weight pruning and structural filter pruning. **Fine-grained Pruning** generally results in unstructured models, which need specialized haredware or software to speed up the sparse network.** Filter Pruning** achieves acceleratation by removing the entire filter. We also provide an algorithm to control the** pruning schedule**.
We provide several pruning algorithms that support fine-grained weight pruning and structural filter pruning. **Fine-grained Pruning** generally results in unstructured models, which need specialized haredware or software to speed up the sparse network. **Filter Pruning** achieves acceleratation by removing the entire filter. Some pruning algorithms use one-shot method that prune weights at once based on an importance metric. Other pruning algorithms control the **pruning schedule** that prune weights during optimization, including some automatic pruning algorithms.
Copy link
Contributor

Choose a reason for hiding this comment

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

haredware -> hardware
acceleratation -> acceleration


Slim Pruner **prunes channels in the convolution layers by masking corresponding scaling factors in the later BN layers**\ , L1 regularization on the scaling factors should be applied in batch normalization (BN) layers while training, scaling factors of BN layers are** globally ranked** while pruning, so the sparse model can be automatically found given sparsity.

This is an one-shot pruner, which adds sparsity regularization on the scaling factors of batch normalization (BN) layers durting training to identify unimportant channels. The channels with small scaling factor values will be pruned. For more details, please refer to `'Learning Efficient Convolutional Networks through Network Slimming' <https://arxiv.org/pdf/1708.06519.pdf>`__\.
Copy link
Contributor

Choose a reason for hiding this comment

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

durting -> during?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Fix typo errors!

@J-shang J-shang merged commit a9dcc00 into microsoft:master Feb 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants