I'm attempting to use Hydra's compose API to launch runs programmatically instead of via CLI. This works for the most part. However, overriding hydra.run.dir
to change the base directory doesn't seem to have the effect when using the compose API. I.e.:
with hydra.experimental.initialize_config_module(config_module=config_module):
cfg = hydra.experimental.compose(
config_name=config_name,
overrides=["hydra.run.dir=/tmp/workdir", ...],
return_hydra_config=True
)
hydra.core.hydra_config.HydraConfig.instance().set_config(cfg)
with omegaconf.open_dict(cfg):
del cfg["hydra"]
generates a DictConfig
with the appropriate entry for hydra.run.dir
, but the working directory is not changed.
The compose API documentation states that not using @hydra.main
entails forfeiting Hydra's working directory management. Is there a workaround for this?
The Compose API is stateless and is intentionally not changing the working directory, configuring the logging or changing global state and not integrating with the command line. If you need these functionalities you should consider using @hydra.main()
.
As a workaround, you can call chdir programmatically on your end (with os.chdir). You will probably also need to mkdir first.