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 to_pyro_module_() helper #2197

Merged
merged 8 commits into from
Dec 2, 2019
Merged

Add a to_pyro_module_() helper #2197

merged 8 commits into from
Dec 2, 2019

Conversation

fritzo
Copy link
Member

@fritzo fritzo commented Dec 1, 2019

Addresses #2161

This aims to make it easier to convert 3rd party nn.Module instances to PyroModules, as described in @simeneide's forum post.

The safest way to do this appears to be to update the .__class__ attribute in-place:

module.__class__ = PyroModule[module.__class__]
module._pyro_... = ...  # initialize PyroModule attributes

While this appears intrusive, it is equivalent to replacing nn.Linear -> PyroModule[nn.Linear] and so forth in the 3rd-party code that created the instance. It should therefore also be compatible with serialization.

🐛 Bugfixes

This PR also fixes two bugs surfaced by new tests:

  • A bug whereby ._pyro_set_supermodule() was being bypassed due to nn.Module subclasses calling .add_module() rather than .__setitem__() directly.
  • A recursion bug in tests.common.assert_close("foo", "foo")

Tested

  • added a test that to_pyro_module_(m) is equivalent to recursively inserting PyroModule[-]
  • strengthen existing tests

@fritzo fritzo added awaiting review and removed WIP labels Dec 1, 2019
@neerajprad neerajprad merged commit bfd8580 into dev Dec 2, 2019
@fritzo fritzo deleted the to-pyro-module branch January 15, 2020 00:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants