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

[Feature] Add dynamic bricks #228

Merged
merged 12 commits into from
Aug 19, 2022
Merged

Conversation

wutongshenqiu
Copy link
Contributor

Motivation

Add dynamic bricks

Modification

  1. Add dynamic Conv2d and corresponding unittest/docstring.
  2. Add dynamic Linear and corresponding unittest/docstring.
  3. Add dynamic BatchNormxd and corresponding unittest/docstring.
  4. Delete MutableManageMixin

BC-breaking (Optional)

No.

@codecov
Copy link

codecov bot commented Aug 11, 2022

Codecov Report

Merging #228 (5eba88c) into dev-1.x (8775b03) will decrease coverage by 0.06%.
The diff coverage is 0.00%.

@@            Coverage Diff             @@
##           dev-1.x    #228      +/-   ##
==========================================
- Coverage     0.62%   0.55%   -0.07%     
==========================================
  Files          125     130       +5     
  Lines         4641    5181     +540     
  Branches       719     810      +91     
==========================================
  Hits            29      29              
- Misses        4607    5147     +540     
  Partials         5       5              
Flag Coverage Δ
unittests 0.55% <0.00%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
mmrazor/engine/runner/evolution_search_loop.py 0.00% <0.00%> (ø)
mmrazor/engine/runner/subnet_sampler_loop.py 0.00% <0.00%> (ø)
...zor/models/algorithms/pruning/slimmable_network.py 0.00% <0.00%> (ø)
...mrazor/models/architectures/dynamic_op/__init__.py 0.00% <0.00%> (ø)
mmrazor/models/architectures/dynamic_op/base.py 0.00% <0.00%> (ø)
...models/architectures/dynamic_op/bricks/__init__.py 0.00% <0.00%> (ø)
...ls/architectures/dynamic_op/bricks/dynamic_conv.py 0.00% <0.00%> (ø)
...itectures/dynamic_op/bricks/dynamic_conv_mixins.py 0.00% <0.00%> (ø)
.../architectures/dynamic_op/bricks/dynamic_linear.py 0.00% <0.00%> (ø)
.../architectures/dynamic_op/bricks/dynamic_mixins.py 0.00% <0.00%> (ø)
... and 7 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@pppppM pppppM requested a review from LKJacky August 15, 2022 05:21

from torch import nn

from mmrazor.models.mutables.mutable_channel import MutableChannel
from mmrazor.models.mutables.base_mutable import BaseMutable


class DynamicOP(ABC):
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion:
add an interface, called convert_from(module). It is used to convert a basic torch module to a dynamic op.

raise RuntimeError(f'current choice of mutable {type(mutable)} '
'can not be None at runtime')

return current_choice


Copy link
Collaborator

@LKJacky LKJacky Aug 15, 2022

Choose a reason for hiding this comment

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

Suggestion:
add unified interfaces, mutate_in_channels and mutate_out_channels, to ChannelDynamicOP, rather than using different methods to register mutables in different dynamic ops.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Preferably in each Dynamic OP, there may be mutate_kernel_size in Conv, mutate_num_head in MultiHeadAttention.
The variable of each module is xx, and the corresponding api is mutate_xx.

Copy link
Collaborator

Choose a reason for hiding this comment

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

My opinion is not only leaving two apis mutate_in_channels and mutate_out_channels, just using the same api name to register mutable channel, rather than 'mutate_in_channels' in dyanmicconv and 'mutate_num_features' in dynamicbn.

@@ -83,7 +83,9 @@ def num_choices(self) -> int:

def convert_choice_to_mask(self, choice: int) -> torch.Tensor:
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion: let MutableChannel only deal with mask, and implement the process which converts int/float/choice to mask, in another Mutable

Copy link
Collaborator

@LKJacky LKJacky left a comment

Choose a reason for hiding this comment

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

I have some suggestions, and the detail can be found in comments



@CONV_LAYERS.register_module()
class DynamicConv2d(nn.Conv2d, ChannelDynamicOP):
Copy link
Contributor

Choose a reason for hiding this comment

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

expand DynamicConv2d to DynamicConvXd by inheriting from _ConvNd

@pppppM pppppM merged commit d190037 into open-mmlab:dev-1.x Aug 19, 2022
LKJacky pushed a commit to LKJacky/mmrazor that referenced this pull request Aug 20, 2022
* add dynamic bricks

* add dynamic conv2d test

* add tests for dynamic linear and dynamic norm

* add docstring for dynamic conv2d

* add docstring for dynamic linear

* add docstring for dynamic batchnorm

* Refactor the dynamic op ( put more logic into the mixin )

* fix UT

* Fix UT ( fileio was moved to mmengine)

* derived mutable adds choices property

* Unify the register interface of mutable in dynamic op

* Unified getter interface of mutable in dynamic op

Co-authored-by: gaojianfei <gaojianfei@sensetime.com>
Co-authored-by: pppppM <gjf_mail@126.com>
humu789 pushed a commit to humu789/mmrazor that referenced this pull request Feb 13, 2023
* mmcls fix ut

* force add data folder

* remove debug info

* fix ut bugs
humu789 pushed a commit to humu789/mmrazor that referenced this pull request Feb 13, 2023
* check in the 2nd tutorial

* update index

* add new line

* trailing whitespace

* describe how to get srcnn.pth and face.png

* rename file name

* update index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants