Skip to content

Utils

instantiate_from_config

instantiate_from_config(
    config_path: str,
    overrides: dict[str, Any] | None = None,
) -> Module

Instantiates a module from a jsonnet config file.

Parameters:

  • config_path (str) –

    The path to the jsonnet config file.

  • overrides (dict[str, Any] | None, default: None ) –

    A dictionary of overrides to apply to the config.

Returns:

  • Module

    The instantiated module.

Examples:

>>> from flexeval import instantiate_from_config
>>> eval_setup = instantiate_from_config("aio")
Source code in flexeval/utils/module_utils.py
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
45
46
47
48
49
50
51
52
53
54
def instantiate_from_config(
    config_path: str,
    overrides: dict[str, Any] | None = None,
) -> Module:
    """
    Instantiates a module from a jsonnet config file.

    Args:
        config_path: The path to the jsonnet config file.
        overrides: A dictionary of overrides to apply to the config.

    Returns:
        The instantiated module.

    Examples:
        >>> from flexeval import instantiate_from_config
        >>> eval_setup = instantiate_from_config("aio")
    """
    resolved_config_path = ConfigNameResolver()(config_path)

    if resolved_config_path is None:
        msg = f'Config name "{config_path}" not found in the specified path nor in the preset config directories.'
        raise ValueError(msg)

    config = json.loads(_jsonnet.evaluate_file(resolved_config_path))
    module_class = getattr(flexeval, config["class_path"])

    parser = ArgumentParser(parser_mode="jsonnet")
    parser.add_argument("--module", type=module_class, required=True, enable_path=True)

    args_to_parse = ["--module", resolved_config_path]
    overrides = overrides or {}
    for key, value in overrides.items():
        args_to_parse += [f"--module.{key}", str(value)]

    args = parser.parse_args(args_to_parse)
    instantiated_config = parser.instantiate_classes(args)
    return instantiated_config.module