Coverage for mlos_bench/mlos_bench/optimizers/manual_optimizer.py: 95%

22 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-21 01:50 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5""" 

6Manual config suggestor (Optimizer) for mlos_bench that proposes an explicit sequence of 

7configurations. 

8 

9This is useful for testing and validation, as it allows you to run a sequence of 

10configurations in a cyclic fashion. 

11 

12TODO: Add an example configuration. 

13""" 

14 

15import logging 

16 

17from mlos_bench.optimizers.mock_optimizer import MockOptimizer 

18from mlos_bench.services.base_service import Service 

19from mlos_bench.tunables.tunable import TunableValue 

20from mlos_bench.tunables.tunable_groups import TunableGroups 

21 

22_LOG = logging.getLogger(__name__) 

23 

24 

25class ManualOptimizer(MockOptimizer): 

26 """Optimizer that proposes an explicit sequence of tunable values.""" 

27 

28 def __init__( 

29 self, 

30 tunables: TunableGroups, 

31 config: dict, 

32 global_config: dict | None = None, 

33 service: Service | None = None, 

34 ): 

35 super().__init__(tunables, config, global_config, service) 

36 self._tunable_values_cycle: list[dict[str, TunableValue]] = config.get( 

37 "tunable_values_cycle", [] 

38 ) 

39 assert len(self._tunable_values_cycle) > 0, "No tunable values provided." 

40 max_cycles = int(config.get("max_cycles", 1)) 

41 self._max_suggestions = min( 

42 self._max_suggestions, 

43 max_cycles * len(self._tunable_values_cycle), 

44 ) 

45 

46 def suggest(self) -> TunableGroups: 

47 """Always produce the same sequence of explicit suggestions, in a cycle.""" 

48 tunables = super().suggest() 

49 cycle_index = (self._iter - 1) % len(self._tunable_values_cycle) 

50 tunables.assign(self._tunable_values_cycle[cycle_index]) 

51 _LOG.info("Iteration %d :: Suggest: %s", self._iter, tunables) 

52 return tunables 

53 

54 @property 

55 def supports_preload(self) -> bool: 

56 return False