Coverage for mlos_bench/mlos_bench/services/types/config_loader_type.py: 100%
11 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-05 00:36 +0000
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-05 00:36 +0000
1#
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4#
5"""
6Protocol interface for helper functions to lookup and load configs.
7"""
9from typing import Any, Dict, List, Iterable, Optional, Union, Protocol, runtime_checkable, TYPE_CHECKING
11from mlos_bench.config.schemas import ConfigSchema
12from mlos_bench.tunables.tunable import TunableValue
15# Avoid's circular import issues.
16if TYPE_CHECKING:
17 from mlos_bench.tunables.tunable_groups import TunableGroups
18 from mlos_bench.services.base_service import Service
19 from mlos_bench.environments.base_environment import Environment
22@runtime_checkable
23class SupportsConfigLoading(Protocol):
24 """
25 Protocol interface for helper functions to lookup and load configs.
26 """
28 def resolve_path(self, file_path: str,
29 extra_paths: Optional[Iterable[str]] = None) -> str:
30 """
31 Prepend the suitable `_config_path` to `path` if the latter is not absolute.
32 If `_config_path` is `None` or `path` is absolute, return `path` as is.
34 Parameters
35 ----------
36 file_path : str
37 Path to the input config file.
38 extra_paths : Iterable[str]
39 Additional directories to prepend to the list of search paths.
41 Returns
42 -------
43 path : str
44 An actual path to the config or script.
45 """
47 def load_config(self, json_file_name: str, schema_type: Optional[ConfigSchema]) -> Union[dict, List[dict]]:
48 """
49 Load JSON config file. Search for a file relative to `_config_path`
50 if the input path is not absolute.
51 This method is exported to be used as a service.
53 Parameters
54 ----------
55 json_file_name : str
56 Path to the input config file.
57 schema_type : Optional[ConfigSchema]
58 The schema type to validate the config against.
60 Returns
61 -------
62 config : Union[dict, List[dict]]
63 Free-format dictionary that contains the configuration.
64 """
66 def build_environment(self, # pylint: disable=too-many-arguments
67 config: dict,
68 tunables: "TunableGroups",
69 global_config: Optional[dict] = None,
70 parent_args: Optional[Dict[str, TunableValue]] = None,
71 service: Optional["Service"] = None) -> "Environment":
72 """
73 Factory method for a new environment with a given config.
75 Parameters
76 ----------
77 config : dict
78 A dictionary with three mandatory fields:
79 "name": Human-readable string describing the environment;
80 "class": FQN of a Python class to instantiate;
81 "config": Free-format dictionary to pass to the constructor.
82 tunables : TunableGroups
83 A (possibly empty) collection of groups of tunable parameters for
84 all environments.
85 global_config : Optional[dict]
86 Global parameters to add to the environment config.
87 parent_args : Optional[Dict[str, TunableValue]]
88 An optional reference of the parent CompositeEnv's const_args used to
89 expand dynamic config parameters from.
90 service: Optional[Service]
91 An optional service object (e.g., providing methods to
92 deploy or reboot a VM, etc.).
94 Returns
95 -------
96 env : Environment
97 An instance of the `Environment` class initialized with `config`.
98 """
100 def load_environment_list( # pylint: disable=too-many-arguments
101 self,
102 json_file_name: str,
103 tunables: "TunableGroups",
104 global_config: Optional[dict] = None,
105 parent_args: Optional[Dict[str, TunableValue]] = None,
106 service: Optional["Service"] = None) -> List["Environment"]:
107 """
108 Load and build a list of environments from the config file.
110 Parameters
111 ----------
112 json_file_name : str
113 The environment JSON configuration file.
114 Can contain either one environment or a list of environments.
115 tunables : TunableGroups
116 A (possibly empty) collection of tunables to add to the environment.
117 global_config : Optional[dict]
118 Global parameters to add to the environment config.
119 parent_args : Optional[Dict[str, TunableValue]]
120 An optional reference of the parent CompositeEnv's const_args used to
121 expand dynamic config parameters from.
122 service : Optional[Service]
123 An optional reference of the parent service to mix in.
125 Returns
126 -------
127 env : List[Environment]
128 A list of new benchmarking environments.
129 """
131 def load_services(self, json_file_names: Iterable[str],
132 global_config: Optional[Dict[str, Any]] = None,
133 parent: Optional["Service"] = None) -> "Service":
134 """
135 Read the configuration files and bundle all service methods
136 from those configs into a single Service object.
138 Parameters
139 ----------
140 json_file_names : list of str
141 A list of service JSON configuration files.
142 global_config : dict
143 Global parameters to add to the service config.
144 parent : Service
145 An optional reference of the parent service to mix in.
147 Returns
148 -------
149 service : Service
150 A collection of service methods.
151 """