|
| 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 patch |
| 10 | + |
| 11 | +from ax.early_stopping.experiment_replay import ( |
| 12 | + estimate_hypothetical_early_stopping_savings, |
| 13 | + logger, |
| 14 | +) |
| 15 | +from ax.utils.common.testutils import TestCase |
| 16 | +from ax.utils.testing.core_stubs import ( |
| 17 | + get_branin_experiment, |
| 18 | + get_branin_experiment_with_timestamp_map_metric, |
| 19 | +) |
| 20 | +from pyre_extensions import none_throws |
| 21 | + |
| 22 | + |
| 23 | +class TestEstimateHypotheticalEss(TestCase): |
| 24 | + def setUp(self) -> None: |
| 25 | + super().setUp() |
| 26 | + # Experiment with MapMetric for tests that need a valid default ESS. |
| 27 | + self.exp = get_branin_experiment_with_timestamp_map_metric() |
| 28 | + self.metric = none_throws(self.exp.optimization_config).objective.metric |
| 29 | + |
| 30 | + def test_estimate_hypothetical_ess_no_default_strategy(self) -> None: |
| 31 | + """Test that None is returned when no default ESS is available.""" |
| 32 | + # Non-MapMetric experiment has no default ESS. |
| 33 | + exp = get_branin_experiment(has_optimization_config=True) |
| 34 | + metric = none_throws(exp.optimization_config).objective.metric |
| 35 | + |
| 36 | + with patch.object(logger, "info") as mock_info: |
| 37 | + result = estimate_hypothetical_early_stopping_savings( |
| 38 | + experiment=exp, |
| 39 | + metric=metric, |
| 40 | + ) |
| 41 | + |
| 42 | + self.assertIsNone(result) |
| 43 | + mock_info.assert_called_once() |
| 44 | + self.assertIn( |
| 45 | + "No default early stopping strategy available", |
| 46 | + mock_info.call_args[0][0], |
| 47 | + ) |
| 48 | + |
| 49 | + def test_estimate_hypothetical_ess_no_progression_data(self) -> None: |
| 50 | + """Test that None is returned when experiment has no progression data.""" |
| 51 | + with ( |
| 52 | + patch( |
| 53 | + "ax.early_stopping.experiment_replay.replay_experiment", |
| 54 | + return_value=None, |
| 55 | + ), |
| 56 | + patch.object(logger, "info") as mock_info, |
| 57 | + ): |
| 58 | + result = estimate_hypothetical_early_stopping_savings( |
| 59 | + experiment=self.exp, |
| 60 | + metric=self.metric, |
| 61 | + ) |
| 62 | + |
| 63 | + self.assertIsNone(result) |
| 64 | + mock_info.assert_called_once_with( |
| 65 | + "Experiment data does not have progression data for replay." |
| 66 | + ) |
| 67 | + |
| 68 | + def test_estimate_hypothetical_ess_success(self) -> None: |
| 69 | + """Test that savings are returned when replay succeeds.""" |
| 70 | + with ( |
| 71 | + patch( |
| 72 | + "ax.early_stopping.experiment_replay.replay_experiment", |
| 73 | + ) as mock_replay, |
| 74 | + patch( |
| 75 | + "ax.early_stopping.experiment_replay.estimate_early_stopping_savings", |
| 76 | + return_value=0.25, |
| 77 | + ) as mock_estimate, |
| 78 | + ): |
| 79 | + result = estimate_hypothetical_early_stopping_savings( |
| 80 | + experiment=self.exp, |
| 81 | + metric=self.metric, |
| 82 | + ) |
| 83 | + |
| 84 | + self.assertEqual(result, 0.25) |
| 85 | + mock_replay.assert_called_once() |
| 86 | + mock_estimate.assert_called_once() |
| 87 | + |
| 88 | + def test_estimate_hypothetical_ess_exception(self) -> None: |
| 89 | + """Test that None is returned when replay raises an exception.""" |
| 90 | + with ( |
| 91 | + patch( |
| 92 | + "ax.early_stopping.experiment_replay.replay_experiment", |
| 93 | + side_effect=ValueError("Experiment's name is None."), |
| 94 | + ), |
| 95 | + patch.object(logger, "info") as mock_info, |
| 96 | + ): |
| 97 | + result = estimate_hypothetical_early_stopping_savings( |
| 98 | + experiment=self.exp, |
| 99 | + metric=self.metric, |
| 100 | + ) |
| 101 | + |
| 102 | + self.assertIsNone(result) |
| 103 | + mock_info.assert_called_once_with( |
| 104 | + "Experiment replay failed with exception: Experiment's name is None." |
| 105 | + ) |
0 commit comments