Home > Enterprise >  What is the correct way of accessing Hydra's current output directory?
What is the correct way of accessing Hydra's current output directory?

Time:07-17

Assuming I prevent Hydra from changing the current working directory, how can I still retrieve the job's output directory (i.e., the folder Hydra created for storing the results of the particular job) from main()?

Ideally, I'd like a method that works regardless of whether it's a regular run or a multi-run. I know I can access the configuration through hydra.core.hydra_config.HydraConfig.get(), but it's unclear whether any of the settings there always map to the output folder. The relevant tutorial page discusses how to set the output folder but not how to retrieve it in the case we didn't change the cwd.

CodePudding user response:

Assuming that hydra.job.chdir is false, the python program will not automatically change directories when the function decorated by @hydra.main is invoked. This means any files you write or data that you save from your main function should show up in the same folder where you started the python process. You can check what this folder is with any of the following:

If hydra.job.chdir is true, invoking the @main-decorated function will change the python program's working directory to the path hydra.run.dir (for single-run mode) or to the concatenated path hydra.sweep.dir/hydra.sweep.subdir for multirun mode.

Calling os.getcwd() will work to figure out what this is at runtime. You can also use the ${hydra:} resolver to calculate this path in your config:

single_run_dir: ${hydra:run.dir}
multi_run_dir: ${hydra:sweep.dir}/${hydra:sweep.subdir}

See also the Customizing working directory pattern doc.

CodePudding user response:

Found it.

hydra_cfg = hydra.core.hydra_config.HydraConfig.get()
hydra_cfg['runtime']['output_dir']
  • Related