-
-
Notifications
You must be signed in to change notification settings - Fork 663
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] Recursive class instantiation #419
Comments
Hi, this is an interesting solution. I am a bit torn about extending instantiate to support it because it feels like there is quiet a bit more surface area introduced by recursive instantiation that you solution is not touching. class: foo
params:
a: 10
b:
class: bar
params:
z : 20 This opens up the question of supporting code that is already taking such structure and instantiates the inner variables manually. Another thing that is not solvable with this approach is passing additional parameters to the nested objects. I think this can be a second instantiate function, say: If you think you can work on this I can tell you where to write the tests and we can figure out together what are enough tests to get this into Hydra. About your other suggested idea: We can talk about your other suggestion in the context of structured configs. I suspect something like would not be supported right now but it's worth taking a look at it. |
class: foo
params:
a: 10
b:
c:
class: bar
params:
z : 20
d:
class: bar
params:
z: 20 Yes this should be covered.
You could use the kwargs merge logic to pass the weights but it would not be ideal. Maybe if you could pass a flat dict to the instantiate function it would be easier. Something like this: class Pipeline:
def __init__(self, optimizer: torch.optim.Adam):
... class: Pipeline
params:
optimizer:
class: torch.optim.Adam
params:
lr: 1e-3
params: ??? merge_with = {'pipeline.optimizer.params': model.weights()}
hydra.utils.instantiate(cfg, **merge_with)
I would be happy to do that :)
I really like this new logic but I think it interacts with the instantiation it a weird manner. On one hand, you register your structured configs, but on the other hand, you don't register your class and are supposed to pass an import path in the config. I think you should be able to register classes directly. The is the system implemented in AllenNLP if you want to have a look. The logic can be found in: |
Structured configs are new territory and I didn't even consider how they interact with instantiate yet. Here is what I suggest: If that thing becomes very good and popular I can consider folding it into the core of Hydra. |
Created #566 to track this. (no concrete plans to implement yet). |
🚀 Feature Request
Motivation
Related to Nested object instantiation [Feature Request]
Is your feature request related to a problem? Please describe.
One of the current limitation of the
hydra.utils.instantiate(...)
method it that you can't use it for nested instantiation.Let's say you have:
I would like to be able to instantiate
Bar
with the following config:Pitch
Describe the solution you'd like
One very simple solution would be to check the argument we give to
clazz
one by one. Something like this:This solution is not ideal but it works.
Are you willing to open a pull request? (See CONTRIBUTING)
Yes. I'm willing to work on it but I don't know the codebase at all ATM.
Additional context
Another cool thing that could be added to this method is to add type checking to
args
andkwargs
. It could be done by usinginspect.signature(clazz.__init__)
and then checking the arguments vs the returned annotations. This could also allow us to track if the class has default values for some of its arguments. Should I open another feature request for this?The text was updated successfully, but these errors were encountered: