Hydra: [Bug] utils.instantiate fails with dict kwarg inputs

Created on 19 Mar 2020  路  6Comments  路  Source: facebookresearch/hydra

馃悰 Bug

utils.instantiate fails with dict kwarg inputs

To reproduce

Minimal Code/Config snippet to reproduce

Suppose I have a config group file

model:
  class: models.Net
  params:
    some: 0
    values: 1

Let's say I want to pass in a dictionary to models.Net as follows:
utils.instantiate(cfg.model, some_config_argument={'test': 'dict'})

Stack trace/error message

This will throw KeyError: 'Accessing unknown key in a struct : model.params.test'

from params.merge_with(OmegaConf.create(kwargs)) in the instantiate function.

Expected Behavior

Expected to succeed and pass in the dict kwarg to the class constructor

System information

  • Hydra Version : 0.11.0
  • Python version : 3.7.6
  • Virtual environment type and version : Conda
  • Operating system : OS X
bug

All 6 comments

Try Hydra 0.11.3, there have been a few changes around instantiate.

Same behavior for 0.11.3

Can you give more context on what you are trying to do?
This will not get fixed in the current stable version, 1.0 first release candidate is not far out.

Can you also try with the version from master? there have been even more changes there to instantiate.

Actually this seems fixed on master. Sorry for not trying that before filing.

What I was trying to do was eventually call utils.instantiate twice - once to initialize a class and then once inside that class to initialize an optimizer. I was passing the DictConfig for the optimizer in as a kwarg to the outer class, and then calling utils.instantiate inside the outer class constructor.

I realize there might be some better way to specify the optimizer as part of the configuration file for the outer class using variable interpolation, but I wasn't sure how to do so in a way that would be reusable to all the variants in the configuration group corresponding to the outer class.

  1. Awesome that it works on master. if you can stick to master to give it some testing as it evolves please do. (generally all tests are passing in master).

  2. If I am understanding your problem correctly, try something like:

config.yaml

defaults:
  - optimizer: adam

foobar:
  cls: FooBar
  params:
    opt_cfg: ???

optimizer/adam.yaml

foobar:
  params:
    opt_cfg:
      cls: torch.Adam
      params:
        alpha: 0.1
        beta: 0.99

closing as fixed in master. feel free to join the chat if you have questions.

Was this page helpful?
0 / 5 - 0 ratings