Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a tutorial about How to make your model prunable by MMRazor #541

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# How to make your model prunable by MMRazor

We make much effort to make MMRazor can prune models automatically. However, there are also some cases we need help to handle. In this tutorial, we will show you how to make your model prunable by MMRazor when MMRazor can't take it automatically.

We introduce you to how MMRazor prunes a model and what requirements are needed for the models in each step.

1. First, we need a demo input for your model to conduct forward. We have implemented some demo inputs for some models. You can implement your demo input if they can't satisfy your model. Please refer to [demo input](../../../../mmrazor/models/task_modules/demo_inputs/demo_inputs.py) for more details.
2. We will trace your model using Fx tracer. Fx tracer requires models to satisfy some requirements to make sure the models are traceable. MMRazor has a modified fx tracer to make it more robust by automatically wrapping untraceable parts. However, it's also limited. It would help to change your model to make it traceable when tracing fails. Please refer to [fx tracer](https://pytorch.org/docs/stable/fx.html) for more details.
3. Then, we will convert the traced graph to a ChannelGraph. ChannelGraph is used to analyze the channel dependency with ChannelNodes. Please implement ChannelNodes for the ops that are not pre-defined in MMRazor. Please refer to [ChannelNode](../../../../mmrazor/structures/graph/channel_nodes.py) for more details.

The above steps should be able to solve most of problems, if not, please feel free to open an issue.
1 change: 1 addition & 0 deletions docs/en/user_guides/pruning_user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Please refer to the following documents for more details.
- Development tutorials
- [How to prune your model](../advanced_guides/tutorials/how_to_prune_your_model.md)
- [How to use config tool of pruning](../advanced_guides/tutorials/how_to_use_config_tool_of_pruning.md)
- [How to make your model prunable](../advanced_guides/tutorials/how_to_make_your_model_prunable.md)
- READMEs
- [MutableChannel](../../../mmrazor/models/mutables/mutable_channel/MutableChannel.md)
- [ChannelMutator](../../../mmrazor/models/mutables/mutable_channel/units/mutable_channel_unit.ipynb)
Expand Down
6 changes: 5 additions & 1 deletion tools/pruning/get_channel_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ def parse_args():
def main():
args = parse_args()
config = Config.fromfile(args.config)
default_scope = config['default_scope']

if 'default_scope' in config:
default_scope = config['default_scope']
else:
default_scope = 'mmrazor'

model = MODELS.build(config['model'])
if isinstance(model, BaseAlgorithm):
Expand Down