0

I am using structured config (@dataclass-based) with hydra 1.1 and trying to interpolate some values. Specifically, I would like to set the working directory by substituting some of my config values.

I was able to do this consulting the documentation when using yaml:


run:
  dir: outputs
  name: try1

hydra:
  run:
    dir: ${run.dir}/${run.name}

How can I do this using structured configs?

nzer0
  • 315
  • 2
  • 10
  • Which part of your config above are you trying to replace with a structured config? Do you want to replace the `"run"` part that contains the `"outputs"` and `"try1"` strings, or do you want to replace the `"hydra"` part that contains the interpolation? – Jasha Oct 03 '21 at 16:39
  • The "hydra" part. I would like the run directory to be dynamically changed as I give a nickname (`try1`) to my experiment. In the above example, I want `hydra.run.dir` to be `outputs/try1`. – nzer0 Oct 06 '21 at 08:10

1 Answers1

1

Below is an example showing how to create a structured config that can be used to override Hydra's default output directory.

# config.yaml
defaults:
  - my_run_dir
  - _self_

run:
  dir: outputs
  name: try1
# my_app.py
from dataclasses import dataclass
import hydra
from hydra.conf import RunDir
from omegaconf import DictConfig, OmegaConf
from hydra.core.config_store import ConfigStore

cs = ConfigStore.instance()

@dataclass
class MyRunDir(RunDir):
    dir: str = "${run.dir}/${run.name}"

cs.store(name="my_run_dir", node=MyRunDir, package="hydra.run")

@hydra.main(config_path=".", config_name="config")
def my_app(cfg: DictConfig) -> None:
    from hydra.core.hydra_config import HydraConfig
    assert HydraConfig.get().run.dir == "outputs/try1"

if __name__ == "__main__":
    my_app()

When this python file is run, the directory outputs/try1 is created.

As an alternative to using the package="hydra.run" keyword argument to the cs.store method call, you could use an "@package" defaults list entry from config.yaml:

# config.yaml
defaults:
  - my_run_dir@hydra.run
...
# my_app.py
...
cs.store(name="my_run_dir", node=MyRunDir)  # omit the `package=` argument
...

See the Hydra docs on Packages for more info on this.

Jasha
  • 5,507
  • 2
  • 33
  • 44