0

I'm new to Hydra, so sorry for the nooby question. Update: Thanks, Omry Yadan for pointing out an example would be helpful. Indeed while putting it together I realize that I was not providing enough information. My question revolves around the use of Structured Config schemas.

I have the following minimal example for reproduction. My folder structure is:

./my_app.py
./config/config.yaml
./config/strategy/Strategy1.yaml
./config/strategy/Strategy2.yaml

The individual files contain the following content: ./my_app.py:

from dataclasses import dataclass, field

import hydra
from hydra.core.config_store import ConfigStore
from omegaconf import MISSING, DictConfig, OmegaConf


@dataclass
class StrategyConfig:
    name: str = MISSING


@dataclass
class Strategy1Config(StrategyConfig):
    name: str = "strategy1"
    alpha: float = MISSING


@dataclass
class Strategy2Config(StrategyConfig):
    name: str = "strategy2"


@dataclass
class MyConfig:
    strategy: StrategyConfig = field(default_factory=StrategyConfig)


cs = ConfigStore.instance()
cs.store(name="base_config", node=MyConfig)
cs.store(group="strategy", name="base_strategy1", node=Strategy1Config)
cs.store(group="strategy", name="base_strategy2", node=Strategy2Config)


@hydra.main(version_base=None, config_path="config", config_name="config")
def my_app(cfg: MyConfig) -> None:
    print(OmegaConf.to_yaml(cfg))


if __name__ == "__main__":
    my_app()

./config/config.yaml:

defaults:
  - base_config
  - strategy: Strategy1
  - _self_
strategy:
  alpha: 0.1

./config/strategy/Strategy1.yaml:

defaults:
  - base_strategy1
name: strategy1
alpha: ???

./config/strategy/Strategy2.yaml:

defaults:
  - base_strategy2
name: strategy2

Running the default command python my_app.py or equivalently python my_app.py strategy=Strategy1:

strategy:
  name: strategy1
  alpha: 0.1

Running python my_app.py strategy=Strategy2, we get this error:

In 'config': ConfigKeyError raised while composing config:
Key 'alpha' not in 'Strategy2Config'
    full_key: strategy.alpha
    object_type=Strategy2Config

What I expect the outcome to be however is:

strategy:
  name: strategy2

i.e. I expect it to ignore the setting of alpha to 0.1 in config.yaml Indeed, when commenting out those two lines in config.yaml, I get the expected output. When removing the notion of schemas and "base_strategy", the above command outputs:

strategy:
  name: strategy2
  alpha: 0.1

Which I understand, but would like to avoid.

I expect I have an incomplete understanding of Structured Config Schemas in hydra.

Thanks for any pointers.

Mat

Mathew
  • 307
  • 1
  • 11
  • Please update your question to include a fully reproducible example. It will help answering your question without guessing what you have actually done. – Omry Yadan Jul 18 '23 at 03:21

0 Answers0