Setting HydraConfig parameters in structured configs #2051
-
🚀 Feature RequestHello everyone, I was curious if there was a good way to override hydra related settings inside of a structured config rather than in the main MotivationThe idea is that I want to tuck away some of the hyrda config settings away from the user defined .yaml config. This would allow the definition of different structured configs with specific hydra settings already set. A good example would be a default schema and a debug schema (with hydra.verbose on and a different hydra.job_logging). Is your feature request related to a problem? Please describe. PitchHere would be a little example of the functionality I am looking to do using the docs SQL database configs. In
In
In
This currently does not work due to hydra saying
Describe alternatives you've considered Are you willing to open a pull request? (See CONTRIBUTING) Additional contextNone |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
When figuring out how to write a structured config, it's sometimes productive to start with a yaml config and work backwards from there. hydra:
run:
dir: "sql_outputs/${hydra:job.override_dirname}/${hydra:job.name}" The equivalent structured config should have a field called from dataclasses import dataclass, field
@dataclass
class Foo:
hydra: Any = field(default_factory=lambda: {
"run": {
"dir": "sql_outputs/${hydra:job.override_dirname}/${hydra:job.name}"
}
})
Using the defaults list is certainly a viable option. Once again starting with a yaml file: defaults:
- foo
- bar: baz
- override alpha/beta: gamma The equivalent structured config should look something like this: from dataclasses import dataclass, field
@dataclass
class MyConf:
defaults: List[Any] = field(default_factory=lambda: [
"foo",
{"bar": "baz"},
{"override alpha/beta": "gamma"},
]) You can check that the data stored in the yaml file is equivalent to the data from the structured config file using the following technique: from omegaconf import OmegaConf
from dataclasses import dataclass, field
conf1 = OmegaConf.create("""
defaults:
- foo
- bar: baz
- override alpha/beta: gamma
""")
@dataclass
class MyConf:
defaults: List[Any] = field(default_factory=lambda: [
"foo",
{"bar": "baz"},
{"override alpha/beta": "gamma"},
])
conf2 = OmegaConf.create(MyConf)
assert conf1 == conf2 In general, anything that can be done using yaml can also be done using python objects. |
Beta Was this translation helpful? Give feedback.
-
@NickGeneva feel free to follow up here if you have any additional questions.
Btw this restriction does hold for |
Beta Was this translation helpful? Give feedback.
When figuring out how to write a structured config, it's sometimes productive to start with a yaml config and work backwards from there.
For example, suppose you want to produce a structured config that is equivalent to the following yaml:
The equivalent structured config should have a field called
hydra
with a default value given by the python dictionary{"run": {"dir": "sql_outputs/${hydra:job.override_dirname}/${hydra:job.name}"}}
.Thus, we have the following: