Override Hydra config with experiment config- extra folder hierarchy

68 Views Asked by At

I'm working through this Hydra doc example to override the main config with an experiment config. Differing from the Hydra example I have another level of folder hierarchy to gather all associated configs into a single sub-folder.

conf
├── data
│   ├── cifar100_data
│   │   ├── cifar100_extended.yaml
│   │   └── cifar100.yaml
│   └── default.yaml
├── experiment
│   └── cifar100.yaml
└── training_config.yaml

In my example, I have a basic training_config:

defaults:
   - data: default

data/default.yaml:

# @package data
dataset: mnist
n_classes: 10

and two config files for cifar100 data
cifar100_data/cifar100.yaml:

name: cifar100
n_classes: 100

cifar100_data/cifar100_extended.yaml:

# @package data
defaults:
  - cifar100
augmentations:
  - rotate

I'm trying to override training_config with experiment/cifar100 --> python my_app.py experiment=cifar100:

experiment/cifar100 try 1:

# @package _global_
defaults:
  - override /data/cifar100_data: cifar100_extended

error message:

hydra.errors.ConfigCompositionException: In 'experiment/cifar100': Could not override 'data/cifar100_data'. No match in the defaults list.

experiment/cifar100 try 2:

# @package _global_
defaults:
  - override /data: cifar100_data/cifar100_extended

error message:

hydra.errors.MissingConfigException: In 'data/cifar100_data/cifar100_extended': Could not load 'data/cifar100'.

How can I properly run such an experiment with an extra hierarchy compared to default config?

EDIT following the answer:
I've adjust cifar100_extended.yaml to:

# @package data
defaults:
  - cifar100_data/cifar100
augmentations:
  - rotate

as suggested in the answer, and was able to generate a valid config. However, now I can't run cifar100_extended.yaml directly, and recieving the following error

hydra.errors.MissingConfigException: In 'cifar100_data/cifar100_extended': Could not load 'cifar100_data/cifar100_data/cifar100'.
2

There are 2 best solutions below

4
Omry Yadan On

This a bit unusual. Your data config group should be data, not /data/cifar100_data, so the second form is the correct one:

# @package _global_
defaults:
  - override /data: cifar100_data/cifar100_extended

The error you are getting is:

hydra.errors.MissingConfigException: In 'data/cifar100_data/cifar100_extended': Could not load 'data/cifar100'.

This is telling you that while trying to data/cifar100_data/cifar100_extended, Hydra attempted to load data/cifar100 and could not find it. Indeed, that file is in data/cifar100_data/cifar100 and not in data/cifar100.

This looks like a bug because relative config group entries should be relative to the containing config, and cifa100 is right next to cifar100_extended.

The bug is related to your unusual construction of a config group with sub directories which is not something that is properly supported (normally the expectation is that each sub directory is its own config group).

A work around is to adjust cifar100_extended.yaml to:

# @package data
defaults:
  - cifar100_data/cifar100
augmentations:
  - rotate
0
Jasha On

I was able to get your original config working by removing the override keyword from the conf/experiment/cifar100.yaml file:

# @package _global_
defaults:
  - /data/cifar100_data: cifar100_extended
$ python test.py +experiment=cifar100
data:
  dataset: mnist
  n_classes: 100
  name: cifar100
  augmentations:
  - rotate

I'm not sure if this is your desired output, however. Did you want the cifar configuration to be nested under the data.cifar100_data package? If so, you can use the group@pkg syntax within your conf/experiment/cifar100.yaml file:

# @package _global_
defaults:
  - /data/[email protected]_data: cifar100_extended
$ python test.py +experiment=cifar100
data:
  dataset: mnist
  n_classes: 10
  cifar100_data:
    name: cifar100
    n_classes: 100
    augmentations:
    - rotate

The override keyword is useful you're overriding something that already appears elsewhere in your defaults tree. In this case, the experiment file is not overriding data/cifar100_data, which does not appear elsewhere in the defaults tree.