def test_get_best_trial(self): scheduler = Scheduler( experiment=self.branin_experiment, # Has runner and metrics. generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions( init_seconds_between_polls= 0.1, # Short between polls so test is fast. ), ) self.assertIsNone(scheduler.get_best_parameters()) scheduler.run_n_trials(max_trials=1) trial, params, _arm = scheduler.get_best_trial() just_params, _just_arm = scheduler.get_best_parameters() just_params_unmodeled, _just_arm_unmodled = scheduler.get_best_parameters( use_model_predictions=False) with self.assertRaisesRegex(NotImplementedError, "Please use `get_best_parameters`"): scheduler.get_pareto_optimal_parameters() self.assertEqual(trial, 0) self.assertIn("x1", params) self.assertIn("x2", params) self.assertEqual(params, just_params) self.assertEqual(params, just_params_unmodeled)
def test_validate_early_stopping_strategy(self): class DummyEarlyStoppingStrategy(BaseEarlyStoppingStrategy): def should_stop_trials_early( self, trial_indices: Set[int], experiment: Experiment, **kwargs: Dict[str, Any], ) -> Set[int]: return {} with patch( f"{BraninMetric.__module__}.BraninMetric.is_available_while_running", return_value=False, ), self.assertRaises(ValueError): Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions( early_stopping_strategy=DummyEarlyStoppingStrategy()), ) # should not error Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions( early_stopping_strategy=DummyEarlyStoppingStrategy()), )
def test_run_multi_arm_generator_run_error(self, mock_gen): scheduler = Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions(total_trials=1), ) with self.assertRaisesRegex(SchedulerInternalError, ".* only one was expected"): scheduler.run_all_trials()
def test_base_report_results(self): self.branin_experiment.runner = NoReportResultsRunner() scheduler = Scheduler( experiment=self.branin_experiment, # Has runner and metrics. generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions(init_seconds_between_polls=0, ), ) self.assertEqual(scheduler.run_n_trials(max_trials=3), OptimizationResult())
def test_retries(self): # Check that retries will be performed for a retriable error. self.branin_experiment.runner = BrokenRunnerRuntimeError() scheduler = Scheduler( experiment=self.branin_experiment, generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions(total_trials=1), ) # Should raise after 3 retries. with self.assertRaisesRegex(RuntimeError, ".* testing .*"): scheduler.run_all_trials() self.assertEqual(scheduler.run_trial_call_count, 3)
def test_retries_nonretriable_error(self): # Check that no retries will be performed for `ValueError`, since we # exclude it from the retriable errors. self.branin_experiment.runner = BrokenRunnerValueError() scheduler = Scheduler( experiment=self.branin_experiment, generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions(total_trials=1), ) # Should raise right away since ValueError is non-retriable. with self.assertRaisesRegex(ValueError, ".* testing .*"): scheduler.run_all_trials() self.assertEqual(scheduler.run_trial_call_count, 1)
def test_optimization_complete(self, _): # With runners & metrics, `Scheduler.run_all_trials` should run. scheduler = Scheduler( experiment=self.branin_experiment, # Has runner and metrics. generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions( max_pending_trials=100, init_seconds_between_polls= 0.1, # Short between polls so test is fast. ), ) scheduler.run_n_trials(max_trials=1) # no trials should run if _gen_multiple throws an OptimizationComplete error self.assertEqual(len(scheduler.experiment.trials), 0)
def test_timeout(self): scheduler = Scheduler( experiment=self.branin_experiment, generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions( total_trials=8, init_seconds_between_polls= 0, # No wait between polls so test is fast. ), ) scheduler.run_all_trials( timeout_hours=0) # Forcing optimization to time out. self.assertEqual(len(scheduler.experiment.trials), 0) self.assertIn("aborted", scheduler.experiment._properties["run_trials_success"])
def test_set_ttl(self): scheduler = Scheduler( experiment=self.branin_experiment, generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions( total_trials=2, ttl_seconds_for_trials=1, init_seconds_between_polls= 0, # No wait between polls so test is fast. min_seconds_before_poll=0.0, ), ) scheduler.run_all_trials() self.assertTrue( all(t.ttl_seconds == 1 for t in scheduler.experiment.trials.values()))
def test_repr(self): scheduler = Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions( total_trials=0, tolerated_trial_failure_rate=0.2, init_seconds_between_polls=10, ), ) self.assertEqual( f"{scheduler}", ("Scheduler(experiment=Experiment(branin_test_experiment), " "generation_strategy=GenerationStrategy(name='Sobol+GPEI', " "steps=[Sobol for 5 trials, GPEI for subsequent trials]), " "options=SchedulerOptions(max_pending_trials=10, " "trial_type=<class 'ax.core.trial.Trial'>, " "total_trials=0, tolerated_trial_failure_rate=0.2, " "min_failed_trials_for_failure_rate_check=5, log_filepath=None, " "logging_level=20, ttl_seconds_for_trials=None, init_seconds_between_" "polls=10, min_seconds_before_poll=1.0, seconds_between_polls_backoff_" "factor=1.5, run_trials_in_batches=False, " "debug_log_run_metadata=False, early_stopping_strategy=None, " "suppress_storage_errors_after_retries=False))"), )
def test_validate_early_stopping_strategy(self): with patch( f"{BraninMetric.__module__}.BraninMetric.is_available_while_running", return_value=False, ), self.assertRaises(ValueError): Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions( early_stopping_strategy=DummyEarlyStoppingStrategy()), ) with patch.object( OptimizationConfig, "is_moo_problem", return_value=True ), self.assertRaisesRegex( UnsupportedError, "Early stopping is not supported on multi-objective problems", ): Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions( early_stopping_strategy=DummyEarlyStoppingStrategy()), ) with patch( f"{Scheduler.__module__}.len", return_value=1, ), self.assertRaisesRegex( UnsupportedError, "Early stopping is not supported on problems with outcome constraints.", ): Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions( early_stopping_strategy=DummyEarlyStoppingStrategy()), ) # should not error Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions( early_stopping_strategy=DummyEarlyStoppingStrategy()), )
def test_run_preattached_trials_only(self): # assert that pre-attached trials run when max_trials = number of # pre-attached trials scheduler = Scheduler( experiment=self.branin_experiment, # Has runner and metrics. generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions( init_seconds_between_polls= 0.1, # Short between polls so test is fast. ), ) trial = scheduler.experiment.new_trial() parameter_dict = {"x1": 5, "x2": 5} trial.add_arm(Arm(parameters=parameter_dict)) with self.assertRaisesRegex( UserInputError, "number of pre-attached candidate trials .* is greater than"): scheduler.run_n_trials(max_trials=0) scheduler.run_n_trials(max_trials=1) self.assertEqual(len(scheduler.experiment.trials), 1) self.assertDictEqual(scheduler.experiment.trials[0].arm.parameters, parameter_dict) self.assertTrue( # Make sure all trials got to complete. all(t.completed_successfully for t in scheduler.experiment.trials.values()))
def test_run_all_trials_using_runner_and_metrics(self, mock_get_pending): # With runners & metrics, `Scheduler.run_all_trials` should run. scheduler = Scheduler( experiment=self.branin_experiment, # Has runner and metrics. generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions( total_trials=8, init_seconds_between_polls= 0.1, # Short between polls so test is fast. ), ) scheduler.run_all_trials() # Check that we got pending feat. at least 8 times (1 for each new trial and # maybe more for cases where we tried to generate trials but ran into limit on # paralel., as polling trial statuses is randomized in Scheduler), # so some trials might not yet have come back. self.assertGreaterEqual(len(mock_get_pending.call_args_list), 8) self.assertTrue( # Make sure all trials got to complete. all(t.completed_successfully for t in scheduler.experiment.trials.values())) self.assertEqual(len(scheduler.experiment.trials), 8) # Check that all the data, fetched during optimization, was attached to the # experiment. dat = scheduler.experiment.fetch_data().df self.assertEqual(set(dat["trial_index"].values), set(range(8))) self.assertEqual( scheduler.experiment._properties[ ExperimentStatusProperties.RUN_TRIALS_STATUS], ["started", "success"], ) self.assertEqual( scheduler.experiment._properties[ ExperimentStatusProperties.NUM_TRIALS_RUN_PER_CALL], [8], ) self.assertEqual( scheduler.experiment._properties[ ExperimentStatusProperties.RESUMED_FROM_STORAGE_TIMESTAMPS], [], )
def test_stop_at_MAX_SECONDS_BETWEEN_POLLS(self): self.branin_experiment.runner = InfinitePollRunner() scheduler = Scheduler( experiment=self.branin_experiment, # Has runner and metrics. generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions( total_trials=8, init_seconds_between_polls= 1, # No wait between polls so test is fast. ), ) with patch.object(scheduler, "wait_for_completed_trials_and_report_results", return_value=None) as mock_await_trials: scheduler.run_all_trials(timeout_hours=1 / 60 / 15) # 4 second timeout. # We should be calling `wait_for_completed_trials_and_report_results` # N = total runtime / `MAX_SECONDS_BETWEEN_POLLS` times. self.assertEqual( len(mock_await_trials.call_args), 2, # MAX_SECONDS_BETWEEN_POLLS as patched in decorator )
def test_logging(self): with NamedTemporaryFile() as temp_file: Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions( total_trials=1, init_seconds_between_polls= 0, # No wait bw polls so test is fast. log_filepath=temp_file.name, ), ).run_all_trials() self.assertGreater(os.stat(temp_file.name).st_size, 0) self.assertIn("Running trials [0]", str(temp_file.readline())) temp_file.close()
def test_init(self): with self.assertRaisesRegex( UnsupportedError, "`Scheduler` requires that experiment specifies a `Runner`.", ): scheduler = Scheduler( experiment=self.branin_experiment_no_impl_runner_or_metrics, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions(total_trials=10), ) self.branin_experiment_no_impl_runner_or_metrics.runner = self.runner with self.assertRaisesRegex( UnsupportedError, ".*Metrics {'branin'} do not implement fetching logic.", ): scheduler = Scheduler( experiment=self.branin_experiment_no_impl_runner_or_metrics, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions(total_trials=10), ) scheduler = Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions( total_trials=0, tolerated_trial_failure_rate=0.2, init_seconds_between_polls=10, ), ) self.assertEqual(scheduler.experiment, self.branin_experiment) self.assertEqual(scheduler.generation_strategy, self.sobol_GPEI_GS) self.assertEqual(scheduler.options.total_trials, 0) self.assertEqual(scheduler.options.tolerated_trial_failure_rate, 0.2) self.assertEqual(scheduler.options.init_seconds_between_polls, 10) self.assertIsNone(scheduler._latest_optimization_start_timestamp) for status_prop in ExperimentStatusProperties: self.assertEqual( scheduler.experiment._properties[status_prop.value], []) scheduler.run_all_trials() # Runs no trials since total trials is 0. # `_latest_optimization_start_timestamp` should be set now. self.assertLessEqual( scheduler._latest_optimization_start_timestamp, current_timestamp_in_millis(), )
def test_get_best_trial_moo(self): experiment = get_branin_experiment_with_multi_objective() experiment.runner = self.runner scheduler = Scheduler( experiment=experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions(init_seconds_between_polls=0.1), ) scheduler.run_n_trials(max_trials=1) with self.assertRaisesRegex( NotImplementedError, "Please use `get_pareto_optimal_parameters`"): scheduler.get_best_trial() with self.assertRaisesRegex( NotImplementedError, "Please use `get_pareto_optimal_parameters`"): scheduler.get_best_parameters() self.assertIsNotNone(scheduler.get_pareto_optimal_parameters())
def test_suppress_all_storage_errors(self, mock_save_exp, _): init_test_engine_and_session_factory(force_init=True) config = SQAConfig() encoder = Encoder(config=config) decoder = Decoder(config=config) db_settings = DBSettings(encoder=encoder, decoder=decoder) Scheduler( experiment=self.branin_experiment, # Has runner and metrics. generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions( max_pending_trials=100, init_seconds_between_polls= 0.1, # Short between polls so test is fast. suppress_storage_errors_after_retries=True, ), db_settings=db_settings, ) self.assertEqual(mock_save_exp.call_count, 3)
def test_logging_level(self): # We don't have any warnings yet, so warning level of logging shouldn't yield # any logs as of now. with NamedTemporaryFile() as temp_file: Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GPEI_GS, options=SchedulerOptions( total_trials=3, init_seconds_between_polls= 0, # No wait bw polls so test is fast. log_filepath=temp_file.name, logging_level=WARNING, ), ).run_all_trials() # Ensure that the temp file remains empty self.assertEqual(os.stat(temp_file.name).st_size, 0) temp_file.close()
def test_stop_trial(self): # With runners & metrics, `Scheduler.run_all_trials` should run. scheduler = Scheduler( experiment=self.branin_experiment, # Has runner and metrics. generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions( init_seconds_between_polls= 0.1, # Short between polls so test is fast. ), ) with patch.object(scheduler.experiment.runner, "stop", return_value=None) as mock_runner_stop: scheduler.run_n_trials(max_trials=1) scheduler.stop_trial_runs(trials=[scheduler.experiment.trials[0]]) mock_runner_stop.assert_called_once()
def test_failure_rate(self): options = SchedulerOptions( total_trials=8, tolerated_trial_failure_rate=0.5, init_seconds_between_polls= 0, # No wait between polls so test is fast. min_failed_trials_for_failure_rate_check=2, ) self.branin_experiment.runner = RunnerWithFrequentFailedTrials() scheduler = Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GS_no_parallelism, options=options, ) with self.assertRaises(FailureRateExceededError): scheduler.run_all_trials() # Trials will have statuses: 0, 2 - FAILED, 1 - COMPLETED. Failure rate # is 0.5, and so if 2 of the first 3 trials are failed, we can fail # immediately. self.assertEqual(len(scheduler.experiment.trials), 3) # If all trials fail, we can be certain that the sweep will # fail after only 2 trials. num_preexisting_trials = len(scheduler.experiment.trials) self.branin_experiment.runner = RunnerWithAllFailedTrials() scheduler = Scheduler( experiment=self.branin_experiment, generation_strategy=self.sobol_GS_no_parallelism, options=options, ) self.assertEqual(scheduler._num_preexisting_trials, num_preexisting_trials) with self.assertRaises(FailureRateExceededError): scheduler.run_all_trials() self.assertEqual(len(scheduler.experiment.trials), num_preexisting_trials + 2)
def test_run_n_trials(self): # With runners & metrics, `Scheduler.run_all_trials` should run. scheduler = Scheduler( experiment=self.branin_experiment, # Has runner and metrics. generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions( init_seconds_between_polls= 0.1, # Short between polls so test is fast. ), ) scheduler.run_n_trials(max_trials=1) self.assertEqual(len(scheduler.experiment.trials), 1) scheduler.run_n_trials(max_trials=10) self.assertTrue( # Make sure all trials got to complete. all(t.completed_successfully for t in scheduler.experiment.trials.values())) # Check that all the data, fetched during optimization, was attached to the # experiment. dat = scheduler.experiment.fetch_data().df self.assertEqual(set(dat["trial_index"].values), set(range(11)))
def test_sqa_storage(self): init_test_engine_and_session_factory(force_init=True) config = SQAConfig() encoder = Encoder(config=config) decoder = Decoder(config=config) db_settings = DBSettings(encoder=encoder, decoder=decoder) experiment = self.branin_experiment # Scheduler currently requires that the experiment be pre-saved. with self.assertRaisesRegex(ValueError, ".* must specify a name"): experiment._name = None scheduler = Scheduler( experiment=experiment, generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions(total_trials=1), db_settings=db_settings, ) experiment._name = "test_experiment" NUM_TRIALS = 5 scheduler = Scheduler( experiment=experiment, generation_strategy=self.two_sobol_steps_GS, options=SchedulerOptions( total_trials=NUM_TRIALS, init_seconds_between_polls= 0, # No wait between polls so test is fast. ), db_settings=db_settings, ) # Check that experiment and GS were saved. exp, gs = scheduler._load_experiment_and_generation_strategy( experiment.name) self.assertEqual(exp, experiment) self.assertEqual(gs, self.two_sobol_steps_GS) scheduler.run_all_trials() # Check that experiment and GS were saved and test reloading with reduced state. exp, gs = scheduler._load_experiment_and_generation_strategy( experiment.name, reduced_state=True) self.assertEqual(len(exp.trials), NUM_TRIALS) self.assertEqual(len(gs._generator_runs), NUM_TRIALS) # Test `from_stored_experiment`. new_scheduler = Scheduler.from_stored_experiment( experiment_name=experiment.name, options=SchedulerOptions( total_trials=NUM_TRIALS + 1, init_seconds_between_polls= 0, # No wait between polls so test is fast. ), db_settings=db_settings, ) # Hack "resumed from storage timestamp" into `exp` to make sure all other fields # are equal, since difference in resumed from storage timestamps is expected. exp._properties[ ExperimentStatusProperties. RESUMED_FROM_STORAGE_TIMESTAMPS] = new_scheduler.experiment._properties[ ExperimentStatusProperties.RESUMED_FROM_STORAGE_TIMESTAMPS] self.assertEqual(new_scheduler.experiment, exp) self.assertEqual(new_scheduler.generation_strategy, gs) self.assertEqual( len(new_scheduler.experiment._properties[ ExperimentStatusProperties.RESUMED_FROM_STORAGE_TIMESTAMPS]), 1, )