File size: 1,406 Bytes
91263c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

"""Ensure user-defined pretraining & rl configs inherit from base configs."""


from ml_collections import ConfigDict

from .pretrain import get_config as get_pretrain_config
from .rl import get_config as get_rl_config

# Ref: https://github.com/deepmind/jaxline/blob/master/jaxline/base_config.py
def __validate_keys(
    base_config, 
    config, 
    base_filename, 
):
    """Validate keys."""
    for key in base_config.keys():
        if key not in config:
            raise ValueError(
                f"Key {key} missing from config. This config is required to have "
                f"keys: {list(base_config.keys())}. See base_configs/{base_filename} "
                "for more details.")
        if (isinstance(base_config[key], ConfigDict) and config[key] is not None):
            __validate_keys(base_config[key], config[key], base_filename)
            
def validate_config(config, mode):
    """Ensures a config inherits from a base config.

    Args:
        config: The child config to validate.
        mode: Can be one of 'pretraining' or 'rl'.

    Raises:
        ValueError: if the base config contains keys that are not present in config.
    """
    assert mode in ["pretrain", "rl"]
    base_config = get_rl_config() if mode == "rl" else get_pretrain_config()
    base_filename = "rl.py" if mode == "rl" else "pretrain.py"
    __validate_keys(base_config, config, base_filename)