What is the correct way of accessing Hydra's current output directory?
Asked Answered
R

3

8

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.

Receiver answered 16/7, 2022 at 20:3 Comment(0)
R
11

Found it.

hydra_cfg = hydra.core.hydra_config.HydraConfig.get()
hydra_cfg['runtime']['output_dir']
Receiver answered 16/7, 2022 at 21:55 Comment(0)
C
3

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.

Cristionna answered 16/7, 2022 at 21:31 Comment(0)
S
0

The full path to the output dir can also be returned with:

hydra.core.hydra_config.HydraConfig.get().runtime.output_dir

See https://hydra.cc/docs/tutorials/basic/running_your_app/working_directory/

Schild answered 11/1 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.