Skip to content

Fix a bug that led to double-registration #781

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

Merged
merged 2 commits into from
Jun 20, 2021

Conversation

BenjaminBossan
Copy link
Collaborator

This is a bug introduced by a last minute change in #751

After cloneing a net, _module, _criteria, and _optimizers are already
populated. Then, when loading params, there is yet another registration,
i.e. a double registration. As a consequence, there would be two
'modules' etc. This is a fix for that.

The solution is a two-pronged: Don't return _modules etc. when cloning,
and make a safety check during registration. Either of those should be
sufficient, but it's probably better to be extra safe.

After cloning a net, _module, _criteria, and _optimizers are already
populated. Then, when loading params, there is yet another registration,
i.e. a double registration. As a consequence, there would be two
'modules' etc. This is a fix for that.
Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

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

Thank you for the fix!

if self.init_context_ == 'module':
# make sure to not double register -- this should never happen, but
# still better to check
if (self.init_context_ == 'module') and (name not in self._modules):
self._modules = self._modules[:] + [name]
Copy link
Member

@thomasjpfan thomasjpfan Jun 13, 2021

Choose a reason for hiding this comment

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

I think this is what fixed the bug. Would a set be a better data structure for _modules?

Comment on lines +1797 to +1798
to_exclude = {'_modules', '_criteria', '_optimizers'}
return {key: val for key, val in params.items() if key not in to_exclude}
Copy link
Member

Choose a reason for hiding this comment

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

On master, these attributes are not included in get_params.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What do you mean? On current master, after merging #751, get_params includes those.

Copy link
Member

Choose a reason for hiding this comment

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

Ah I was mistaken. (I was on a older version of master locally)

Can we add a test here:

def test_get_params_works(self, net_cls, module_cls):

Something like:

assert `_modules` not in params

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea. I added a separate test below because that one was already big enough.

Test that get_params does not return any of the unwanted keys.
@BenjaminBossan BenjaminBossan merged commit 1165a78 into master Jun 20, 2021
@BenjaminBossan BenjaminBossan deleted the bugfix/module-double-registration-after-clone branch October 3, 2021 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants