PyTorch
File size: 1,449 Bytes
bd992b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
import json
from pathlib import Path

CONFIGS_PATH = Path(__file__).parent / "configs.json"


def _load_all() -> dict:
    if CONFIGS_PATH.exists():
        with open(CONFIGS_PATH) as f:
            return json.load(f)
    return {}


def load_config(model_name: str, n_params: int) -> dict:
    """
    Load saved kwargs for (model_name, n_params).
    Raises KeyError if no config has been saved yet for this combination.
    config_ui.py should be run to generate these configs.

    :param model_name: The name of the model to load the config for.
    :param n_params: The parameter count to load the config for.
    :return: The kwargs dictionary for that model config
    """
    configs = _load_all()
    model_configs = configs.get(model_name)
    if model_configs is None:
        raise KeyError(f"No config for {model_name}")
    key = str(n_params)
    if key not in model_configs:
        raise KeyError(f"No config for {model_name} with param count {n_params}")
    return dict(model_configs[key])


def has_config(model_name: str, n_params: int) -> bool:
    """
    Returns true if a saved config exists for (model_name, n_params).

    :param model_name: The name of the model to check for the config for.
    :param n_params: The parameter count to check for the config for.
    :return: True if the config exists for (model_name, n_params).
    """
    configs = _load_all()
    return str(n_params) in configs.get(model_name, {})