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

[Fix] fix the bug in Recorders when nn.Module contains the 'inplace' … #446

Merged
merged 1 commit into from
Feb 7, 2023

Conversation

cape-zck
Copy link
Contributor

@cape-zck cape-zck commented Feb 2, 2023

…attribute

Motivation

Find all nn.Modules in the model that contain the 'inplace' attribute and set them to False in order to prevent occur error in Recorders using recursion algorithm.

Modification

Write a new function, it will disassemble the Args architecture .If type 'nn.Module' is detected, determine if it contains an 'inplace' attribute and set False if it does. If none, get the OrderedDict and then iterate through the dictionary to continue the recursive search.
And call this function in class BaseAlgorithm init and class SingleTeacherDistill init function.
Because my academic level is limited and this is my first pulling request, the implementation method and quality may be poor. I sincerely hope that I can get some suggestions from you.

BC-breaking (Optional)

Does the modification introduce changes that break the backward compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here and update the documentation.

Checklist

Before PR:

  • Pre-commit or other linting tools are used to fix the potential lint issues.
  • Bug fixes are fully covered by unit tests, the case that causes the bug should be added in the unit tests.
  • The modification is covered by complete unit tests. If not, please add more unit tests to ensure the correctness.
  • The documentation has been modified accordingly, like docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with those projects, like MMDet or MMSeg.
  • CLA has been signed and all committers have signed the CLA in this PR.

@pppppM pppppM requested a review from wilxy February 3, 2023 03:07
Copy link
Contributor

@wilxy wilxy left a comment

Choose a reason for hiding this comment

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

Thanks for your constructive PR, it can help MMRazor improve the robustness on different algorithms and models.
Please make further modification based on my comment to merge this PR into MMRazor.

@@ -79,6 +79,10 @@ def __init__(self,
# Cannot assign module before Module.__init__()
self.architecture = architecture

# Find all nn.Modules in the model that contain the 'inplace' attribute
# and set them to False
self.set_module_inplace_false(architecture, 'self.architecture')
Copy link
Contributor

Choose a reason for hiding this comment

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

set_module_inplace_false will increase memory usage while some algorithms may be sensitive to the memory usage.
It‘s better to set set_module_inplace_false as an optional operation here, and we can control whether to execute it by config file.
The default value can be True, that is, set_module_inplace_false is performed by default.

Copy link
Contributor Author

@cape-zck cape-zck Feb 3, 2023

Choose a reason for hiding this comment

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

Thanks for your suggestions. I have already made modification. InBaseAlgorithmclass and SingleTeacherDistill class add the optionalmodule_inplace: bool = False and teacher_module_inplace: bool = False. This optional controls whether to allow module inplace attribute True.

Copy link
Contributor

@wilxy wilxy left a comment

Choose a reason for hiding this comment

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

I think this PR solves the problem of the failure on distilling BN module, and approve merging these changes.

@@ -39,6 +39,8 @@ class BaseAlgorithm(BaseModel):
config of :class:`BaseDataPreprocessor`. Defaults to None.
init_cfg (dict): The weight initialized config for
:class:`BaseModule`.
module_inplace(bool): Whether to allow student module inplace
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 the base class of all algorithms. Some algorithms (like NAS and Pruning) do not contain student module, so the docstring here need to be modified.

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'm sorry for ignoring this problem. Now I have modified the document. Thank you very much!

@pppppM pppppM merged commit 8482f1e into open-mmlab:dev-1.x Feb 7, 2023
humu789 pushed a commit to humu789/mmrazor that referenced this pull request Feb 13, 2023
* rewrite sync batchnorm

* export panet and psenet

* resolution

* align fp16 for panet

* refine codes

* enable satrn for trt

* refine docs

* docstring

* docstring

* add ut and refine codes

* fix ut

* resolve comments and move panet-fp16 to doc

* remove ut

* refine ut

* resolve comments

* use size instead of img_scale

* use size of MultiScaleAug

Co-authored-by: dongchunyu.vendor <dongchunyu@pjlab.org.cn>
@jason102811
Copy link

Hi @cape-zck !First of all, we want to express our gratitude for your significant PR in the MMRazor. Your contribution is highly appreciated, and we are grateful for your efforts in helping improve this open-source project during your personal time. We believe that many developers will benefit from your PR.

We would also like to invite you to join our Special Interest Group (SIG) private channel on Discord, where you can share your experiences, ideas, and build connections with like-minded peers. To join the SIG channel, simply message moderator— OpenMMLab on Discord or briefly share your open-source contributions in the #introductions channel and we will assist you. Look forward to seeing you there! Join us :https://discord.gg/UjgXkPWNqA

If you have WeChat account,welcome to join our community on WeChat. You can add our assistant :openmmlabwx. Please add "mmsig + Github ID" as a remark when adding friends:)
Thank you again for your contribution❤

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