Search code examples
fb-hydra

override complete config for submodule with hydra


So, I have a hydra model config (autoencoder.yaml) defined as:

_target_: student.models.AutoEncoder
defaults:
  - dataloader: msa
  - encoder: default_encoder
  - decoder: vae_decoder
  - scheduler: null
  - optimizer: adam
  - preprocessing: null

batch_size: 256

Now in the encoder folder, I have the following YAML configs:

- default_encoder.yaml
- vae_encoder.yaml

and my base config file is as:

# @package _global_

defaults:
  - _self_
  - model: autoencoder.yaml
 
  # enable color logging
  - override hydra/hydra_logging: colorlog
  - override hydra/job_logging: colorlog

work_dir: ${hydra:runtime.cwd}
seed: null
name: "default"

Now, I can call this as is with:

python myapp.py seed=42  #works

but when I do something like:

python myapp.py ++model.encoder=vae_encoder

It comes with the error:

Top level config has to be OmegaConf DictConfig, plain dict, or a Structured Config class or instance

How can I just replace the underlying object through composition with hydra? Basically, when I do this or ++model.encoder=vae_encoder, it replaces this as a string rather than referring to the yaml file


Solution

  • When modifying the defaults list, use a slash ('/') instead of a period ('.') as the separator for path components:

    python myapp.py model/encoder=vae_encoder
    

    instead of

    python myapp.py model.encoder=vae_encoder