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

[Feature Request] Reuse instantiated object in multiple locations #2567

Closed
nikhilxb opened this issue Jan 31, 2023 · 1 comment
Closed

[Feature Request] Reuse instantiated object in multiple locations #2567

nikhilxb opened this issue Jan 31, 2023 · 1 comment
Labels
enhancement Enhanvement request

Comments

@nikhilxb
Copy link

🚀 Feature Request

I would like to reuse a single instantiated object in multiple locations in a config.

Motivation

Is your feature request related to a problem? Please describe.

Here is an example in machine learning: I would like model.encoder1 == model.encoder2 so that they can share weights. However, the below config would result in 2 different instances (with different weights).

model:
  _target_: models.MyModel
  encoder1:
    _target_: models.MyEncoder
  encoder2:
    _target_: models.MyEncoder

More generally, there are times when I want to configure a deeply nested object where certain nodes in the tree are shared.

Pitch

Describe the solution you'd like
Maybe something along the lines of:

model:
  _target_: models.MyModel
  encoder1:
    _target_: models.MyEncoder
  encoder2: ${object:model.encoder1}

It seems that with just the default variable interpolation, the config is interpolated before instantiation, resulting in 2 different instances.

Describe alternatives you've considered

  • Rewriting MyModel: that works in this simple case, but there are complex/more nested cases in which I want to share modules. Rewriting it all would defeat the modularity that is the benefit of using Hydra. Sometimes I will try both cases: shared weights and non-shared weights.

Are you willing to open a pull request? (See CONTRIBUTING)
Maybe, but I'm unfamiliar with the codebase and Hydra internals, so I wouldn't trust myself unless it's an easy fix.

@nikhilxb nikhilxb added the enhancement Enhanvement request label Jan 31, 2023
@omry
Copy link
Collaborator

omry commented Feb 2, 2023

Thanks. This is a dup of #1393.

@omry omry closed this as completed Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhanvement request
Projects
None yet
Development

No branches or pull requests

2 participants