|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# |
| 4 | +# This source code is licensed under the MIT license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-strict |
| 8 | + |
| 9 | +from unittest.mock import MagicMock, patch |
| 10 | + |
| 11 | +from ax.early_stopping.experiment_replay import ( |
| 12 | + estimate_hypothetical_early_stopping_savings, |
| 13 | +) |
| 14 | +from ax.utils.common.testutils import TestCase |
| 15 | +from ax.utils.testing.core_stubs import ( |
| 16 | + get_branin_experiment, |
| 17 | + get_branin_experiment_with_timestamp_map_metric, |
| 18 | +) |
| 19 | +from pyre_extensions import none_throws |
| 20 | + |
| 21 | + |
| 22 | +class TestEstimateHypotheticalEarlyStoppingSavings(TestCase): |
| 23 | + def test_returns_none_for_non_map_metric_experiment(self) -> None: |
| 24 | + """Test that None is returned when experiment has no MapMetric.""" |
| 25 | + exp = get_branin_experiment(has_optimization_config=True) |
| 26 | + metric = none_throws(exp.optimization_config).objective.metric |
| 27 | + |
| 28 | + result = estimate_hypothetical_early_stopping_savings( |
| 29 | + experiment=exp, |
| 30 | + metric=metric, |
| 31 | + ) |
| 32 | + |
| 33 | + self.assertIsNone(result) |
| 34 | + |
| 35 | + def test_returns_none_for_multi_objective(self) -> None: |
| 36 | + """Test that None is returned for multi-objective experiments.""" |
| 37 | + exp = get_branin_experiment_with_timestamp_map_metric(multi_objective=True) |
| 38 | + # Use first metric from optimization config for multi-objective |
| 39 | + metric = list(none_throws(exp.optimization_config).metrics.values())[0] |
| 40 | + |
| 41 | + result = estimate_hypothetical_early_stopping_savings( |
| 42 | + experiment=exp, |
| 43 | + metric=metric, |
| 44 | + ) |
| 45 | + |
| 46 | + self.assertIsNone(result) |
| 47 | + |
| 48 | + def test_returns_none_for_constrained_experiment(self) -> None: |
| 49 | + """Test that None is returned for experiments with outcome constraints.""" |
| 50 | + exp = get_branin_experiment_with_timestamp_map_metric( |
| 51 | + with_outcome_constraint=True |
| 52 | + ) |
| 53 | + metric = none_throws(exp.optimization_config).objective.metric |
| 54 | + |
| 55 | + result = estimate_hypothetical_early_stopping_savings( |
| 56 | + experiment=exp, |
| 57 | + metric=metric, |
| 58 | + ) |
| 59 | + |
| 60 | + self.assertIsNone(result) |
| 61 | + |
| 62 | + @patch("ax.early_stopping.experiment_replay.replay_experiment") |
| 63 | + def test_returns_none_when_replay_fails( |
| 64 | + self, mock_replay_experiment: MagicMock |
| 65 | + ) -> None: |
| 66 | + """Test that None is returned when replay_experiment fails.""" |
| 67 | + exp = get_branin_experiment_with_timestamp_map_metric() |
| 68 | + metric = none_throws(exp.optimization_config).objective.metric |
| 69 | + mock_replay_experiment.return_value = None |
| 70 | + |
| 71 | + result = estimate_hypothetical_early_stopping_savings( |
| 72 | + experiment=exp, |
| 73 | + metric=metric, |
| 74 | + ) |
| 75 | + |
| 76 | + self.assertIsNone(result) |
| 77 | + mock_replay_experiment.assert_called_once() |
| 78 | + |
| 79 | + @patch("ax.early_stopping.experiment_replay.estimate_early_stopping_savings") |
| 80 | + @patch("ax.early_stopping.experiment_replay.replay_experiment") |
| 81 | + def test_returns_savings_on_successful_replay( |
| 82 | + self, |
| 83 | + mock_replay_experiment: MagicMock, |
| 84 | + mock_estimate_savings: MagicMock, |
| 85 | + ) -> None: |
| 86 | + """Test that savings are returned when replay succeeds.""" |
| 87 | + exp = get_branin_experiment_with_timestamp_map_metric() |
| 88 | + metric = none_throws(exp.optimization_config).objective.metric |
| 89 | + mock_replayed_exp = MagicMock() |
| 90 | + mock_replay_experiment.return_value = mock_replayed_exp |
| 91 | + mock_estimate_savings.return_value = 0.25 |
| 92 | + |
| 93 | + result = estimate_hypothetical_early_stopping_savings( |
| 94 | + experiment=exp, |
| 95 | + metric=metric, |
| 96 | + ) |
| 97 | + |
| 98 | + self.assertEqual(result, 0.25) |
| 99 | + mock_estimate_savings.assert_called_once_with(experiment=mock_replayed_exp) |
| 100 | + |
| 101 | + @patch("ax.early_stopping.experiment_replay.replay_experiment") |
| 102 | + def test_returns_none_when_exception_raised( |
| 103 | + self, mock_replay_experiment: MagicMock |
| 104 | + ) -> None: |
| 105 | + """Test that None is returned when replay fails due to invalid experiment |
| 106 | + state (e.g., missing name) or internal orchestration errors. |
| 107 | + """ |
| 108 | + exp = get_branin_experiment_with_timestamp_map_metric() |
| 109 | + metric = none_throws(exp.optimization_config).objective.metric |
| 110 | + mock_replay_experiment.side_effect = ValueError("Experiment's name is None.") |
| 111 | + |
| 112 | + result = estimate_hypothetical_early_stopping_savings( |
| 113 | + experiment=exp, |
| 114 | + metric=metric, |
| 115 | + ) |
| 116 | + |
| 117 | + self.assertIsNone(result) |
| 118 | + mock_replay_experiment.assert_called_once() |
0 commit comments