0

I have the following project setup:

configs/
├── default.yaml
└── trainings
    ├── data_config
    │   └── default.yaml
    ├── simple.yaml
    └── schema.yaml

The content of the files are as follows:

app.py:

from dataclasses import dataclass
from enum import Enum
from pathlib import Path

from omegaconf import MISSING, DictConfig, OmegaConf

import hydra
from hydra.core.config_store import ConfigStore

CONFIGS_DIR_PATH = Path(__file__).parent / "configs"
TRAININGS_DIR_PATH = CONFIGS_DIR_PATH / "trainings"


class Sampling(Enum):
    UPSAMPLING = 1
    DOWNSAMPLING = 2


@dataclass
class DataConfig:
    sampling: Sampling = MISSING


@dataclass
class TrainerConfig:
    project_name: str = MISSING
    data_config: DataConfig = MISSING


# @hydra.main(version_base="1.2", config_path=CONFIGS_DIR_PATH, config_name="default")
@hydra.main(version_base="1.2", config_path=TRAININGS_DIR_PATH, config_name="simple")
def run(configuration: DictConfig):
    sampling = OmegaConf.to_container(cfg=configuration, resolve=True)["data_config"]["sampling"]
    print(f"{sampling} Type: {type(sampling)}")


def register_schemas():
    config_store = ConfigStore.instance()
    config_store.store(name="base_schema", node=TrainerConfig)


if __name__ == "__main__":
    register_schemas()
    run()

configs/default.yaml:

defaults:
  - /trainings@: simple
  - _self_
project_name: test

configs/trainings/simple.yaml:

defaults:
  - base_schema
  - data_config: default
  - _self_

project_name: test

configs/trainings/schema.yaml:

defaults:
  - data_config: default
  - _self_

project_name: test

configs/trainings/data_config/default.yaml:

defaults:
  - _self_
sampling: DOWNSAMPLING

Now, when I run app.py as shown above, I get the expected result (namely, "DOWNSAMPLING" gets resolved to an enum type). However, when I try to run the application where it constructs the configuration from the default.yaml in the parent directory then I get this error:

So, when the code is like so:

...
@hydra.main(version_base="1.2", config_path=CONFIGS_DIR_PATH, config_name="default")
# @hydra.main(version_base="1.2", config_path=TRAININGS_DIR_PATH, config_name="simple")
def run(configuration: DictConfig):
...

I get the error below:

In 'trainings/simple': Could not load 'trainings/base_schema'.

Config search path:
        provider=hydra, path=pkg://hydra.conf
        provider=main, path=file:///data/code/demos/hydra/configs
        provider=schema, path=structured://

Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.

I do not understand why specifying the schema to be used is causing this issue. Would someone have an idea why and what could be done to fix the problem?

Gros Lalo
  • 1,078
  • 7
  • 20

1 Answers1

1

If you are using default lists in more than one config file I strongly suggest that you fully read andf understand The Defaults List page. Configs addressed in the defaults list are relative to the config group of the containing config. The error is telling you that Hydra is looking for base_schema in trainings, because the defaults list that loads base_schema is in trainings.

Either put base_schema inside trainings when you register it:

config_store.store(group="trainings", name="base_schema", node=TrainerConfig)

Or use absolute addressing in the defaults list when addressing it (e.g. in configs/trainings/simple.yaml):

defaults:
  - /base_schema
  - data_config: default
  - _self_
Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
  • I have tried putting base_schema inside trainings during registration but that does not work. However using absolute addressing does work. Note that absolute addressing for schema validation via "base_" is not documented (I could not find it and hence this post). Side note, search function on the docs page is broken. – Gros Lalo Nov 30 '22 at 05:33
  • 1. I had a mistake in my example. corrected it. 2. absolute addressing is by prefixing with /. it is documented in the link I provided. 3. search seems to work for me. – Omry Yadan Dec 01 '22 at 19:12
  • Yes it is mentioned that path for config-groups are relative but is `base_` prefix is already modifying a CONFIG and it is not obvious that it needs the `/`. As to the documentation, I tested on both latest versions of chromium and firefox and it does not work. Error reported is "this page crashed t is undefined". – Gros Lalo Dec 05 '22 at 08:35