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

Simplify submodule access from top-level Module #2609

Closed
cgarciae opened this issue Nov 14, 2022 · 1 comment
Closed

Simplify submodule access from top-level Module #2609

cgarciae opened this issue Nov 14, 2022 · 1 comment
Assignees
Labels
Priority: P2 - no schedule Best effort response and resolution. We have no plan to work on this at the moment.

Comments

@cgarciae
Copy link
Collaborator

cgarciae commented Nov 14, 2022

Its common for users to want to get access to a submodule defined in setup from the outside, however they soon find out that .setup doesn't run upon construction and then they have to figure out how to get access to this. E.g:

class MyModule(nn.Module):
  def setup(self):
    self.submodule = MySubModule()
  ...
  
module = MyModule()
submodule = module.submodule  #<== ERROR

Our current solution as proposed in the Transfer Learning guide is to use nn.apply with a helper function that extracts the needed objects (since the Module is bounded at this point):

def extract_submodule(module):
    submodule = module.submodule.clone() # clone to avoid leakage
    submodule_variables = module.submodule.variables
    return submodule, submodule_variables

submodule, submodule_variables = nn.apply(extract_submodule, module)(variables)

Since this pattern is both common and has some rough edges (users need to remember to use .clone which is really more of an internal API) it would be good to create a helper function to automate this use case.

@cgarciae cgarciae self-assigned this Nov 14, 2022
@cgarciae cgarciae added the Priority: P2 - no schedule Best effort response and resolution. We have no plan to work on this at the moment. label Nov 14, 2022
@marcvanzee
Copy link
Collaborator

Closing this since it seems to be a duplicate of #2478

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: P2 - no schedule Best effort response and resolution. We have no plan to work on this at the moment.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants