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

10 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-10-07 01:52 +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 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_file_name: 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_file_name : str 

63 Path to the input config file. 

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 """ 

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

83 

84 Parameters 

85 ---------- 

86 config : dict 

87 A dictionary with three mandatory fields: 

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

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

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

91 tunables : TunableGroups 

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

93 all environments. 

94 global_config : Optional[dict] 

95 Global parameters to add to the environment config. 

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

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

98 expand dynamic config parameters from. 

99 service: Optional[Service] 

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

101 deploy or reboot a VM, etc.). 

102 

103 Returns 

104 ------- 

105 env : Environment 

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

107 """ 

108 

109 def load_environment_list( # pylint: disable=too-many-arguments 

110 self, 

111 json_file_name: str, 

112 tunables: "TunableGroups", 

113 global_config: Optional[dict] = None, 

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

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

116 ) -> List["Environment"]: 

117 """ 

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

119 

120 Parameters 

121 ---------- 

122 json_file_name : str 

123 The environment JSON configuration file. 

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

125 tunables : TunableGroups 

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

127 global_config : Optional[dict] 

128 Global parameters to add to the environment config. 

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

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

131 expand dynamic config parameters from. 

132 service : Optional[Service] 

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

134 

135 Returns 

136 ------- 

137 env : List[Environment] 

138 A list of new benchmarking environments. 

139 """ 

140 

141 def load_services( 

142 self, 

143 json_file_names: Iterable[str], 

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

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

146 ) -> "Service": 

147 """ 

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

149 into a single Service object. 

150 

151 Parameters 

152 ---------- 

153 json_file_names : list of str 

154 A list of service JSON configuration files. 

155 global_config : dict 

156 Global parameters to add to the service config. 

157 parent : Service 

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

159 

160 Returns 

161 ------- 

162 service : Service 

163 A collection of service methods. 

164 """