-
Notifications
You must be signed in to change notification settings - Fork 223
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
Conversation
There was a problem hiding this 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
.
mmrazor/models/algorithms/base.py
Outdated
@@ -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') |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. InBaseAlgorithm
class 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.
There was a problem hiding this 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.
mmrazor/models/algorithms/base.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
* 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>
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:) |
…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:
After PR: