Skip to content

Commit d1b20e5

Browse files
shrutipatel31facebook-github-bot
authored andcommitted
Extract early stopping replay utilities to OSS (#4744)
Summary: Adds the `estimate_hypothetical_early_stopping_savings()` function to the OSS module. This function estimates potential compute savings by replaying an experiment with a default early stopping strategy. Key changes: - Added `estimate_hypothetical_early_stopping_savings()` to `experiment_replay.py` which combines `get_default_ess_or_none()`, `replay_experiment()`, and `estimate_early_stopping_savings()` into a single utility - Added constants `MAX_REPLAY_TRIALS`, `REPLAY_NUM_POINTS_PER_CURVE`, and `MAX_PENDING_TRIALS` to `experiment_replay.py` - Added optional `minimize` parameter to `replay_experiment()` to explicitly control optimization direction - Updated `ax_sweep_orchestrator.py` to use the new `estimate_hypothetical_early_stopping_savings()` function - Added unit tests for the new function in `test_experiment_replay.py` Differential Revision: D90150341
1 parent aa29dd8 commit d1b20e5

File tree

2 files changed

+158
-1
lines changed

2 files changed

+158
-1
lines changed

ax/early_stopping/experiment_replay.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
from ax.core.optimization_config import OptimizationConfig
1818
from ax.core.parameter import ParameterType, RangeParameter
1919
from ax.core.search_space import SearchSpace
20+
from ax.early_stopping.dispatch import get_default_ess_or_none
2021
from ax.early_stopping.strategies.base import BaseEarlyStoppingStrategy
22+
from ax.early_stopping.utils import estimate_early_stopping_savings
2123
from ax.generation_strategy.generation_strategy import (
2224
GenerationStep,
2325
GenerationStrategy,
@@ -29,6 +31,11 @@
2931

3032
logger: Logger = get_logger(__name__)
3133

34+
# Constants for experiment replay
35+
MAX_REPLAY_TRIALS: int = 50
36+
REPLAY_NUM_POINTS_PER_CURVE: int = 20
37+
MAX_PENDING_TRIALS: int = 5
38+
3239

3340
def replay_experiment(
3441
historical_experiment: Experiment,
@@ -38,6 +45,7 @@ def replay_experiment(
3845
max_pending_trials: int,
3946
early_stopping_strategy: BaseEarlyStoppingStrategy | None,
4047
logging_level: int = logging.ERROR,
48+
minimize: bool | None = None,
4149
) -> Experiment | None:
4250
"""A utility function for replaying a historical experiment's data
4351
by initializing a Orchestrator that quickly steps through the existing data.
@@ -60,7 +68,7 @@ def replay_experiment(
6068
lower_is_better=metric.lower_is_better,
6169
)
6270
optimization_config = OptimizationConfig(
63-
objective=Objective(metric=replay_metric),
71+
objective=Objective(metric=replay_metric, minimize=minimize),
6472
)
6573
runner = MapDataReplayRunner(replay_metric=replay_metric)
6674

@@ -105,3 +113,53 @@ def replay_experiment(
105113
orchestrator.run_all_trials()
106114
logger.info(f"Replayed the experiment in {perf_counter() - start_time} seconds.")
107115
return experiment
116+
117+
118+
def estimate_hypothetical_early_stopping_savings(
119+
experiment: Experiment,
120+
metric: Metric,
121+
max_pending_trials: int = MAX_PENDING_TRIALS,
122+
minimize: bool | None = None,
123+
) -> float | None:
124+
"""Estimate hypothetical early stopping savings using experiment replay.
125+
126+
This function replays the experiment with a default early stopping strategy
127+
to calculate what savings would have been achieved if early stopping were
128+
enabled.
129+
130+
Note: Returns None for multi-objective, constrained, or non-MapMetric
131+
experiments, as `get_default_ess_or_none` does not provide a default
132+
early stopping strategy for these experiment types.
133+
134+
Args:
135+
experiment: The experiment to analyze.
136+
metric: The metric to use for early stopping replay.
137+
max_pending_trials: Maximum number of pending trials for the replay
138+
orchestrator. Defaults to 5.
139+
minimize: Whether the metric should be minimized. If None, it will be
140+
inferred from the metric's lower_is_better attribute.
141+
142+
Returns:
143+
Estimated savings as a fraction (0.0 to 1.0), or None if:
144+
- No default early stopping strategy is available for this experiment
145+
(e.g., multi-objective, constrained, or non-MapMetric experiments)
146+
- The experiment replay failed
147+
"""
148+
default_ess = get_default_ess_or_none(experiment=experiment)
149+
if default_ess is None:
150+
return None
151+
152+
replayed_experiment = replay_experiment(
153+
historical_experiment=experiment,
154+
num_samples_per_curve=REPLAY_NUM_POINTS_PER_CURVE,
155+
max_replay_trials=MAX_REPLAY_TRIALS,
156+
metric=metric,
157+
max_pending_trials=max_pending_trials,
158+
early_stopping_strategy=default_ess,
159+
minimize=minimize,
160+
)
161+
162+
if replayed_experiment is None:
163+
return None
164+
165+
return estimate_early_stopping_savings(experiment=replayed_experiment)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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)

0 commit comments

Comments
 (0)