1

I am using hydra composition with the following structure:

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

...

  • data/dataset_01.yaml
# @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

  • model/bert.yaml
# @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"
  • entry point
@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.

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
Celso França
  • 653
  • 8
  • 31

1 Answers1

2

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"
Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
  • Hello, actually I did not want to interpolate the name of the model (which is in the same file `model/bert.yaml`). Instead, I would like to interpolate the name of the data that is in an external file (`data/dataset_01.yaml`) – Celso França Jan 04 '21 at 00:03
  • The question sure is confusing. I updated my answer. – Omry Yadan Jan 04 '21 at 03:13
  • Thanks for the answer. I had already tried this approach, but without success. When print the result config (`print(OmegaConf.to_yaml(config))`) results in: `path: ../resources/predictions/bert_${data.name}_predictions.pt` – Celso França Jan 04 '21 at 04:33
  • 1
    OmegaConf.to_yaml() does not resolve interpolations by default. you can pass resolve=True to do that. Note that when you are accessing a field the interpolation is resolved. – Omry Yadan Jan 04 '21 at 07:38
  • I strongly suggest that you go over the OmegaConf docs or slides (linked from the project README). – Omry Yadan Jan 04 '21 at 07:39
  • Using `print(OmegaConf.to_yaml(hparams, resolve=True))` prints the desired result. However, when using directly `hparams.model.predictions` still giving: `../resources/predictions/bert_${data.name}_predictions.pt` – Celso França Jan 04 '21 at 14:46
  • Please file a bug with a minimal repro. I am having a hard time believing this. – Omry Yadan Jan 05 '21 at 09:24
  • This is happing because only part of the configuration is passed to instantiate the model (`model/bert.yaml`). And in this context, there is no way for `OmegaConf` to interpolate the key `data.name` since it is not in scope. So, is there an approach to causes the interpolation at the beginning of the application? – Celso França Jan 06 '21 at 02:06
  • Your analysis is incorrect. StackOverflow is not a good support channel. Please file an issue with a minimal working example against Hydra to ask your question. – Omry Yadan Jan 06 '21 at 07:06
  • Please try to follow the advice here when asking the question: https://stackoverflow.com/help/minimal-reproducible-example – Omry Yadan Jan 06 '21 at 07:22
  • @CelsoFrança, I believe the answer I provided is correct (given your discovery that the issue was related to PyTorch Lightning). Do you mind accepting the answer? – Omry Yadan Jan 14 '21 at 06:57