Coverage for mlos_bench/mlos_bench/tests/optimizers/toy_optimization_loop_test.py: 100%

61 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-22 01:18 +0000

1# 

2# Copyright (c) Microsoft Corporation. 

3# Licensed under the MIT License. 

4# 

5"""Toy optimization loop to test the optimizers on mock benchmark environment.""" 

6 

7import logging 

8from typing import Tuple 

9 

10import pytest 

11 

12from mlos_bench.environments.base_environment import Environment 

13from mlos_bench.environments.mock_env import MockEnv 

14from mlos_bench.optimizers.base_optimizer import Optimizer 

15from mlos_bench.optimizers.convert_configspace import tunable_values_to_configuration 

16from mlos_bench.optimizers.mlos_core_optimizer import MlosCoreOptimizer 

17from mlos_bench.optimizers.mock_optimizer import MockOptimizer 

18from mlos_bench.tunables.tunable_groups import TunableGroups 

19from mlos_core.optimizers.bayesian_optimizers.smac_optimizer import SmacOptimizer 

20from mlos_core.util import config_to_dataframe 

21 

22# For debugging purposes output some warnings which are captured with failed tests. 

23DEBUG = True 

24logger = logging.debug 

25if DEBUG: 

26 logger = logging.warning 

27 

28 

29def _optimize(env: Environment, opt: Optimizer) -> Tuple[float, TunableGroups]: 

30 """Toy optimization loop.""" 

31 assert opt.not_converged() 

32 

33 while opt.not_converged(): 

34 

35 with env as env_context: 

36 

37 tunables = opt.suggest() 

38 

39 logger("tunables: %s", str(tunables)) 

40 # pylint: disable=protected-access 

41 if isinstance(opt, MlosCoreOptimizer) and isinstance(opt._opt, SmacOptimizer): 

42 config = tunable_values_to_configuration(tunables) 

43 config_df = config_to_dataframe(config) 

44 logger("config: %s", str(config)) 

45 try: 

46 logger("prediction: %s", opt._opt.surrogate_predict(configs=config_df)) 

47 except RuntimeError: 

48 pass 

49 

50 assert env_context.setup(tunables) 

51 

52 (status, _ts, output) = env_context.run() 

53 assert status.is_succeeded() 

54 assert output is not None 

55 score = output["score"] 

56 assert isinstance(score, float) 

57 assert 60 <= score <= 120 

58 logger("score: %s", str(score)) 

59 

60 opt.register(tunables, status, output) 

61 

62 (best_score, best_tunables) = opt.get_best_observation() 

63 assert best_score is not None and len(best_score) == 1 

64 assert isinstance(best_tunables, TunableGroups) 

65 return (best_score["score"], best_tunables) 

66 

67 

68def test_mock_optimization_loop(mock_env_no_noise: MockEnv, mock_opt: MockOptimizer) -> None: 

69 """Toy optimization loop with mock environment and optimizer.""" 

70 (score, tunables) = _optimize(mock_env_no_noise, mock_opt) 

71 assert score == pytest.approx(64.9, 0.01) 

72 assert tunables.get_param_values() == { 

73 "vmSize": "Standard_B2ms", 

74 "idle": "halt", 

75 "kernel_sched_migration_cost_ns": 117026, 

76 "kernel_sched_latency_ns": 149827706, 

77 } 

78 

79 

80def test_mock_optimization_loop_no_defaults( 

81 mock_env_no_noise: MockEnv, 

82 mock_opt_no_defaults: MockOptimizer, 

83) -> None: 

84 """Toy optimization loop with mock environment and optimizer.""" 

85 (score, tunables) = _optimize(mock_env_no_noise, mock_opt_no_defaults) 

86 assert score == pytest.approx(60.97, 0.01) 

87 assert tunables.get_param_values() == { 

88 "vmSize": "Standard_B2s", 

89 "idle": "halt", 

90 "kernel_sched_migration_cost_ns": 49123, 

91 "kernel_sched_latency_ns": 234760738, 

92 } 

93 

94 

95def test_flaml_optimization_loop(mock_env_no_noise: MockEnv, flaml_opt: MlosCoreOptimizer) -> None: 

96 """Toy optimization loop with mock environment and FLAML optimizer.""" 

97 (score, tunables) = _optimize(mock_env_no_noise, flaml_opt) 

98 assert score == pytest.approx(60.15, 0.01) 

99 assert tunables.get_param_values() == { 

100 "vmSize": "Standard_B2s", 

101 "idle": "halt", 

102 "kernel_sched_migration_cost_ns": -1, 

103 "kernel_sched_latency_ns": 13718105, 

104 } 

105 

106 

107# @pytest.mark.skip(reason="SMAC is not deterministic") 

108def test_smac_optimization_loop(mock_env_no_noise: MockEnv, smac_opt: MlosCoreOptimizer) -> None: 

109 """Toy optimization loop with mock environment and SMAC optimizer.""" 

110 (score, tunables) = _optimize(mock_env_no_noise, smac_opt) 

111 expected_score = 70.33 

112 expected_tunable_values = { 

113 "vmSize": "Standard_B2s", 

114 "idle": "mwait", 

115 "kernel_sched_migration_cost_ns": 297669, 

116 "kernel_sched_latency_ns": 290365137, 

117 } 

118 assert score == pytest.approx(expected_score, 0.01) 

119 assert tunables.get_param_values() == expected_tunable_values