Skip to content

allow list as toplevel instantiate input - #2014

Merged
Jasha10 merged 3 commits into
hydra-ecosystem:mainfrom
Jasha10:i1950
Feb 4, 2022
Merged

Jasha10 merged 3 commits into
hydra-ecosystem:mainfrom
Jasha10:i1950

Conversation

@Jasha10

@Jasha10 Jasha10 commented Feb 4, 2022

Copy link
Copy Markdown
Contributor

This PR enables the instantiate function to take a list or ListConfig as top-level input. Closes #1950.

Consider the following example:

# demo.py
from hydra.utils import instantiate

lst = [
    {"_target_": "pathlib.Path", "_args_": ["foo"]},
    {"_target_": "pathlib.Path", "_args_": ["bar"]},
]

paths = instantiate(lst)
print(paths)

Before this diff:

$ python demo.py
Traceback (most recent call last):
  File "/home/jasha10/hydra.git/demo.py", line 9, in <module>
    paths = instantiate(lst)
  File "/home/jasha10/hydra.git/hydra/_internal/instantiate/_instantiate2.py", line 193, in instantiate
    raise InstantiationException(
hydra.errors.InstantiationException: Top level config has to be OmegaConf DictConfig, plain dict, or a Structured Config class or instance

After this diff:

$ python demo.py
[PosixPath('foo'), PosixPath('bar')]

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 4, 2022
Comment thread tests/instantiate/test_instantiate.py Resolved
@Jasha10
Jasha10 marked this pull request as ready for review February 4, 2022 17:26
@Jasha10
Jasha10 merged commit fb2e0f4 into hydra-ecosystem:main Feb 4, 2022
@Jasha10
Jasha10 deleted the i1950 branch February 4, 2022 17:29
@jieru-hu

jieru-hu commented Feb 4, 2022

Copy link
Copy Markdown
Contributor

this is awesome! thank you @Jasha10

Jasha10 added a commit to Jasha10/hydra that referenced this pull request Mar 31, 2022
@Jasha10 Jasha10 mentioned this pull request Mar 31, 2022
MattiasDC pushed a commit to MattiasDC/hydra that referenced this pull request Apr 1, 2022
@libokj

libokj commented Mar 11, 2023 •

Copy link
Copy Markdown

Is it possible to instantiate a list of objects with nested composition in a YAML file? I've tried the two following ways and neither works. Maybe I have to resort to DictConfig?

I have YAML files for two different metrics

_target_: src.models.metrics.metric1
alpha: 0.01
_target_: src.models.metrics.metric2
alpha: 0.05

Config 1:

_target_: src.models.model1
defaults:
  - _self_
  - encoder1: cnn
  - encoder2: cnn
  - classifier: mlp
metrics:
  - metric1
  - metric2

Two metrics end up interpreted as strings, and metrics is a list of strings.

Config 2:

_target_: src.models.model1
defaults:
  - _self_
  - encoder1: cnn
  - encoder2: cnn
  - classifier: mlp
  - metrics:
    - metric1
    - metric2

Two metrics end up interpreted as objects, but metric2 params overwrite metric1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] instantiate support for ListConfig at top level

5 participants