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

Analysis utils #2435

Merged
merged 49 commits into from
Jun 16, 2020
Merged

Analysis utils #2435

merged 49 commits into from
Jun 16, 2020

Conversation

zheng-ningxin
Copy link
Contributor

@zheng-ningxin zheng-ningxin commented May 15, 2020

Add several analysis tools for the neural network: (1) sensitivity analysis, (2) topology analysis

Sensitivity Analysis Tool

  1. 'sensitivity_analysis.py' automatically analyze the sensitivity to the prune ratio for each layer in the model. In addition, the sensitivity results can also be visualized for user observation.

Topology Analysis Tools

1 'shape_dependency.py' analyze the network architecture and find the layers that have shape dependencies.
2 'mask_conflict.py' this module can fix the mask conflict between the layers has channel dependency. This module should be called before the 'speedup' ,so that, the speedup module can handle the models with residual connections.

Ningxin added 9 commits May 14, 2020 01:26
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Ningxin added 13 commits May 18, 2020 04:04
model should be set to eval mode before the jit.trace call.

Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
In the original way, addmm will also triger the dependency set searching,
which may lead to a wrong dependency set.

Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
mask_conflict can fix the mask conflict of the layers that
has channel dependency. This part should be called before the
speedup function, so that, the speedup module can handle the model
with residual connection/concat operations.

Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
update the interface. if we alreay have the traced graph of the model
we donnot need to trace the model again.

Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Add unittest for tools in analysis_utils to verify the correctness of the
visulization, channel dependency, and mask conflict.

Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
@QuanluZhang
Copy link
Contributor

Thanks @zheng-ningxin , could you add doc (e.g., how to use the sensitivity tool, the requirement of this tool) under https://github.com/microsoft/nni/tree/master/docs/en_US/Compressor?

setup.py Outdated Show resolved Hide resolved
Ningxin added 4 commits June 15, 2020 06:28
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
s_analyzer.export(os.path.join(outdir, filename))
```

Two key parameters of SensitivityAnalysis are model, and val_func. 'model' is the neural network that to be analyzed and the 'val_func' is the validation function that returns the model accuracy/loss/ or other metrics on the validation dataset. Due to different scenarios may have different ways to calculate the loss/accuracy, so users should prepare a function that returns the model accuracy/loss on the dataset and pass it to SensitivityAnalysis.
Copy link
Contributor

Choose a reason for hiding this comment

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

'model' -> model
'val_func' -> val_func

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~ I have fixed it, please review on the latest version~

Ningxin added 2 commits June 16, 2020 01:35
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Two key parameters of SensitivityAnalysis are model, and val_func. model is the neural network that to be analyzed and the val_func is the validation function that returns the model accuracy/loss/ or other metrics on the validation dataset. Due to different scenarios may have different ways to calculate the loss/accuracy, so users should prepare a function that returns the model accuracy/loss on the dataset and pass it to SensitivityAnalysis.
SensitivityAnalysis can export the sensitivity results as a csv file usage is shown in the example above.

Futhermore, users can specify the sparsities values used to prune for each layer by optinal parameter 'sparsities'.
Copy link
Contributor

Choose a reason for hiding this comment

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

optinal -> optional

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 have fixed several grammar errors, please review on the latest version, thanks~

Comment on lines 42 to 45
minimize: The analysis stops when the validation metric return by the val_func lower than early_stop_value.
maximize: The analysis stops when the validation metric return by the val_func larger than early_stop_value.
dropped: The analysis stops when the validation metric has dropped by early_stop_value.
raised: The analysis stops when the validation metric has raised by early_stop_value.
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 itemize these four values. you can check the rendered version.

Copy link
Contributor

Choose a reason for hiding this comment

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

get confused about dropped and raised. what is the difference between dropped/raised and minimize/maximize?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dropped is used in the cases that you want to stop when the accuracy has dropped by 5%, for example.
minimize is used in the cases that you want to keep the accuracy larger than a specified threshold during the analysis.

```python
s_analyzer = SensitivityAnalysis(model=net, val_func=val, sparsities=[0.25, 0.5, 0.75], early_stop_mode='dropped', early_stop_value=0.1)
```
If users only want to analyze several specified convolutional layers, users can specify the target conv layers by the 'sepcified_layers' parameter in analysis function. For example
Copy link
Contributor

Choose a reason for hiding this comment

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

how to specify a conv layer, using module name or module type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sensitivity analyisis only analyze the conv layers, so we use the module name to specify the layers.

Copy link
Contributor

Choose a reason for hiding this comment

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

then better to make it clear that the layers are specified through PyTorch module name.

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, I'll update the doc and make this clear, thanks.

features.10,0.55468,0.5394,0.49576,0.4291,0.3591,0.28138,0.14256,0.05446,0.01578
```

## Topology
Copy link
Contributor

Choose a reason for hiding this comment

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

-> Topology Analysis

@@ -0,0 +1,109 @@
# Analysis Utils for Model Compression
Copy link
Contributor

Choose a reason for hiding this comment

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

could add

.. contents::

after this line

Copy link
Contributor

Choose a reason for hiding this comment

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

in order for users to easily get what is the content of this doc

Ningxin added 6 commits June 16, 2020 02:57
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
Signed-off-by: Ningxin <Ningxin.Zheng@microsoft.com>
@QuanluZhang QuanluZhang merged commit c3d7b4c into microsoft:master Jun 16, 2020
@chicm-ms chicm-ms mentioned this pull request Jul 1, 2020
24 tasks
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