fb-hydraomegaconf

Interpolation using the selected config group option in Hydra


I am using hydra composition with the following structure:

├── configs
    │   ├── config.yaml
    │   ├── data
    │   │   ├── dataset_01.yaml
    │   │   └── dataset_02.yaml
    │   └── model
    │       ├── bert.yaml
    │       └── gpt.yaml
defaults:
  - model: bert
  - data: dataset_01

...

# @package _group_

name: "dataset_01"

train:
  path: "../resources/datasets/dataset_01/train.jsonl"
  num_samples: 1257391

test:
  path: "../resources/datasets/dataset_01/test.jsonl"
  num_samples: 71892

val:
  path: "../resources/datasets/dataset_01/val.jsonl"
  num_samples: 73805

# @package _group_

name: "bert"

encoder: "source.encoder.BertEncoder.BertEncoder"

encoder_hparams:
  architecture: "bert-base-uncased"

lr: 1e-7

tokenizer:
  architecture: "bert-base-uncased"

predictions:
  path: "../resources/predictions/bert_predictions.pt"
@hydra.main(config_path="configs/", config_name="config.yaml")
def perform_tasks(hparams):

    model = MyModel(hparams.model)

if __name__ == '__main__':
    perform_tasks()

In the context of hparams.model, there is no way for OmegaConf to interpolate the key data.name since it is not in scope. So, it would be great if there was an approach to causes the interpolation at the beginning of the application.


Solution

  • OmegaConf interpolation is absolute and is operating on the final config.

    Try this: With Hydra 1.1 or newer you can use hydra.runtime.choices which is a dictionary containing the config groups you have selected.

    You will be able to interpolate without adding the name field using hydra:runtime.choices.GROUP_NAME:

    predictions:
      path: "dir/bert_${hydra:runtime.choices.GROUP_NAME}_pred.pt"