Coverage for mlos_bench/mlos_bench/services/types/config_loader_type.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-20 00:44 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Protocol interface for helper functions to lookup and load configs.""" 

6 

7from typing import ( 

8 TYPE_CHECKING, 

9 Any, 

10 Dict, 

11 Iterable, 

12 List, 

13 Optional, 

14 Protocol, 

15 Union, 

16 runtime_checkable, 

17) 

18 

19from mlos_bench.config.schemas.config_schemas import ConfigSchema 

20from mlos_bench.tunables.tunable import TunableValue 

21 

22# Avoid's circular import issues. 

23if TYPE_CHECKING: 

24 from mlos_bench.environments.base_environment import Environment 

25 from mlos_bench.services.base_service import Service 

26 from mlos_bench.tunables.tunable_groups import TunableGroups 

27 

28 

29@runtime_checkable 

30class SupportsConfigLoading(Protocol): 

31 """Protocol interface for helper functions to lookup and load configs.""" 

32 

33 def resolve_path(self, file_path: str, extra_paths: Optional[Iterable[str]] = None) -> str: 

34 """ 

35 Prepend the suitable `_config_path` to `path` if the latter is not absolute. If 

36 `_config_path` is `None` or `path` is absolute, return `path` as is. 

37 

38 Parameters 

39 ---------- 

40 file_path : str 

41 Path to the input config file. 

42 extra_paths : Iterable[str] 

43 Additional directories to prepend to the list of search paths. 

44 

45 Returns 

46 ------- 

47 path : str 

48 An actual path to the config or script. 

49 """ 

50 

51 def load_config( 

52 self, 

53 json: str, 

54 schema_type: Optional[ConfigSchema], 

55 ) -> Union[dict, List[dict]]: 

56 """ 

57 Load JSON config file. Search for a file relative to `_config_path` if the input 

58 path is not absolute. This method is exported to be used as a service. 

59 

60 Parameters 

61 ---------- 

62 json : str 

63 Path to the input config file or a JSON string. 

64 schema_type : Optional[ConfigSchema] 

65 The schema type to validate the config against. 

66 

67 Returns 

68 ------- 

69 config : Union[dict, List[dict]] 

70 Free-format dictionary that contains the configuration. 

71 """ 

72 

73 def build_environment( # pylint: disable=too-many-arguments 

74 self, 

75 config: dict, 

76 tunables: "TunableGroups", 

77 global_config: Optional[dict] = None, 

78 parent_args: Optional[Dict[str, TunableValue]] = None, 

79 service: Optional["Service"] = None, 

80 ) -> "Environment": 

81 # pylint: disable=too-many-arguments,too-many-positional-arguments 

82 """ 

83 Factory method for a new environment with a given config. 

84 

85 Parameters 

86 ---------- 

87 config : dict 

88 A dictionary with three mandatory fields: 

89 "name": Human-readable string describing the environment; 

90 "class": FQN of a Python class to instantiate; 

91 "config": Free-format dictionary to pass to the constructor. 

92 tunables : TunableGroups 

93 A (possibly empty) collection of groups of tunable parameters for 

94 all environments. 

95 global_config : Optional[dict] 

96 Global parameters to add to the environment config. 

97 parent_args : Optional[Dict[str, TunableValue]] 

98 An optional reference of the parent CompositeEnv's const_args used to 

99 expand dynamic config parameters from. 

100 service: Optional[Service] 

101 An optional service object (e.g., providing methods to 

102 deploy or reboot a VM, etc.). 

103 

104 Returns 

105 ------- 

106 env : Environment 

107 An instance of the `Environment` class initialized with `config`. 

108 """ 

109 

110 def load_environment_list( 

111 self, 

112 json: str, 

113 tunables: "TunableGroups", 

114 global_config: Optional[dict] = None, 

115 parent_args: Optional[Dict[str, TunableValue]] = None, 

116 service: Optional["Service"] = None, 

117 ) -> List["Environment"]: 

118 # pylint: disable=too-many-arguments,too-many-positional-arguments 

119 """ 

120 Load and build a list of environments from the config file. 

121 

122 Parameters 

123 ---------- 

124 json : str 

125 The environment JSON configuration file or a JSON string. 

126 Can contain either one environment or a list of environments. 

127 tunables : TunableGroups 

128 A (possibly empty) collection of tunables to add to the environment. 

129 global_config : Optional[dict] 

130 Global parameters to add to the environment config. 

131 parent_args : Optional[Dict[str, TunableValue]] 

132 An optional reference of the parent CompositeEnv's const_args used to 

133 expand dynamic config parameters from. 

134 service : Optional[Service] 

135 An optional reference of the parent service to mix in. 

136 

137 Returns 

138 ------- 

139 env : List[Environment] 

140 A list of new benchmarking environments. 

141 """ 

142 

143 def load_services( 

144 self, 

145 jsons: Iterable[str], 

146 global_config: Optional[Dict[str, Any]] = None, 

147 parent: Optional["Service"] = None, 

148 ) -> "Service": 

149 """ 

150 Read the configuration files and bundle all service methods from those configs 

151 into a single Service object. 

152 

153 Parameters 

154 ---------- 

155 jsons : list of str 

156 A list of service JSON configuration files or JSON strings. 

157 global_config : dict 

158 Global parameters to add to the service config. 

159 parent : Service 

160 An optional reference of the parent service to mix in. 

161 

162 Returns 

163 ------- 

164 service : Service 

165 A collection of service methods. 

166 """