Skip to content

Commit 365498d

Browse files
shrutipatel31facebook-github-bot
authored andcommitted
Extract early stopping replay utilities to OSS (facebook#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. Reviewed By: bernardbeckerman Differential Revision: D90150341
1 parent f6bdb15 commit 365498d

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

ax/early_stopping/experiment_replay.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
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
23+
from ax.exceptions.core import UnsupportedError
2124
from ax.generation_strategy.generation_strategy import (
2225
GenerationStep,
2326
GenerationStrategy,
@@ -29,6 +32,11 @@
2932

3033
logger: Logger = get_logger(__name__)
3134

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

3341
def replay_experiment(
3442
historical_experiment: Experiment,
@@ -105,3 +113,55 @@ 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+
) -> float:
123+
"""Estimate hypothetical early stopping savings using experiment replay.
124+
125+
This function replays the experiment with a default early stopping strategy
126+
to calculate what savings would have been achieved if early stopping were
127+
enabled.
128+
129+
Args:
130+
experiment: The experiment to analyze.
131+
metric: The metric to use for early stopping replay.
132+
max_pending_trials: Maximum number of pending trials for the replay
133+
orchestrator. Defaults to 5.
134+
135+
Returns:
136+
Estimated savings as a fraction (0.0 to 1.0).
137+
138+
Raises:
139+
UnsupportedError: If early stopping savings cannot be estimated.
140+
This can happen when:
141+
- No default early stopping strategy is available for this experiment
142+
(e.g., multi-objective, constrained, or non-MapMetric experiments)
143+
- The experiment data does not have progression data for replay
144+
- The experiment replay fails due to invalid experiment state
145+
"""
146+
default_ess = get_default_ess_or_none(experiment=experiment)
147+
if default_ess is None:
148+
raise UnsupportedError(
149+
"No default early stopping strategy available (multi-objective, "
150+
"constrained, or non-MapMetric experiment)."
151+
)
152+
153+
replayed_experiment = replay_experiment(
154+
historical_experiment=experiment,
155+
num_samples_per_curve=REPLAY_NUM_POINTS_PER_CURVE,
156+
max_replay_trials=MAX_REPLAY_TRIALS,
157+
metric=metric,
158+
max_pending_trials=max_pending_trials,
159+
early_stopping_strategy=default_ess,
160+
)
161+
162+
if replayed_experiment is None:
163+
raise UnsupportedError(
164+
"Experiment data does not have progression data for replay."
165+
)
166+
167+
return estimate_early_stopping_savings(experiment=replayed_experiment)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
)
14+
from ax.exceptions.core import UnsupportedError
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 UnsupportedError is raised 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 self.assertRaises(UnsupportedError) as e:
37+
estimate_hypothetical_early_stopping_savings(
38+
experiment=exp,
39+
metric=metric,
40+
)
41+
42+
self.assertIn(
43+
"No default early stopping strategy available",
44+
str(e.exception),
45+
)
46+
47+
def test_estimate_hypothetical_ess_no_progression_data(self) -> None:
48+
"""Test that UnsupportedError is raised when experiment has no progression
49+
data."""
50+
with patch(
51+
"ax.early_stopping.experiment_replay.replay_experiment",
52+
return_value=None,
53+
):
54+
with self.assertRaises(UnsupportedError) as e:
55+
estimate_hypothetical_early_stopping_savings(
56+
experiment=self.exp,
57+
metric=self.metric,
58+
)
59+
60+
self.assertIn(
61+
"Experiment data does not have progression data for replay",
62+
str(e.exception),
63+
)
64+
65+
def test_estimate_hypothetical_ess_success(self) -> None:
66+
"""Test that savings are returned when replay succeeds."""
67+
with (
68+
patch(
69+
"ax.early_stopping.experiment_replay.replay_experiment",
70+
) as mock_replay,
71+
patch(
72+
"ax.early_stopping.experiment_replay.estimate_early_stopping_savings",
73+
return_value=0.25,
74+
) as mock_estimate,
75+
):
76+
result = estimate_hypothetical_early_stopping_savings(
77+
experiment=self.exp,
78+
metric=self.metric,
79+
)
80+
81+
self.assertEqual(result, 0.25)
82+
mock_replay.assert_called_once()
83+
mock_estimate.assert_called_once()
84+
85+
def test_estimate_hypothetical_ess_exception(self) -> None:
86+
"""Test that exceptions from replay propagate to the caller."""
87+
with patch(
88+
"ax.early_stopping.experiment_replay.replay_experiment",
89+
side_effect=ValueError("Experiment's name is None."),
90+
):
91+
with self.assertRaises(ValueError) as e:
92+
estimate_hypothetical_early_stopping_savings(
93+
experiment=self.exp,
94+
metric=self.metric,
95+
)
96+
97+
self.assertIn("Experiment's name is None.", str(e.exception))

0 commit comments

Comments
 (0)