diff --git a/ax/early_stopping/strategies/stability_gated.py b/ax/early_stopping/strategies/stability_gated.py index 436dcfbec26..defad59d54b 100644 --- a/ax/early_stopping/strategies/stability_gated.py +++ b/ax/early_stopping/strategies/stability_gated.py @@ -201,37 +201,28 @@ def __init__( def set_baseline_reference_curve( self, curve: list[tuple[float, float]] | None ) -> None: - """Set the baseline progression and mean values for single-trial decisions.""" + """Set the baseline progression and mean values for peer comparisons.""" self._baseline_reference_curve = curve def is_baseline_reference_curve_needed(self, experiment: Experiment) -> bool: - """Return whether attached data has one candidate and no cached reference.""" + """Request an uncached baseline once the objective metric has trial data.""" if self._baseline_reference_curve is not None: return False - metric_signature, _ = self._default_objective_and_direction( - experiment=experiment - ) data = experiment.lookup_data() if data.df.empty: return False - metric_rows = data.full_df[data.full_df["metric_signature"] == metric_signature] - return self._has_single_candidate_curve(metric_rows) - - @staticmethod - def _has_single_candidate_curve(df: pd.DataFrame) -> bool: - return df["trial_index"].nunique() == 1 + metric_signature, _ = self._default_objective_and_direction( + experiment=experiment + ) + return bool((data.full_df["metric_signature"] == metric_signature).any()) @overrides def _augment_early_stopping_data( self, df: pd.DataFrame, metric_signatures: list[str] ) -> pd.DataFrame: - """Inject baseline as a pseudo-trial when no peer candidate curve exists.""" - if ( - self._baseline_reference_curve is None - or not metric_signatures - or not self._has_single_candidate_curve(df) - ): + """Inject the supplied baseline as a peer in checkpoint comparisons.""" + if self._baseline_reference_curve is None or not metric_signatures: return df metric_signature = metric_signatures[0] diff --git a/ax/early_stopping/tests/test_strategies.py b/ax/early_stopping/tests/test_strategies.py index c2093199f17..555792309b1 100644 --- a/ax/early_stopping/tests/test_strategies.py +++ b/ax/early_stopping/tests/test_strategies.py @@ -1882,18 +1882,44 @@ def test_stability_gated_preserves_baseline_sampling_and_unknown_sem(self) -> No ) self.assertTrue(baseline_rows["sem"].isna().all()) - def test_stability_gated_does_not_need_baseline_with_peer_curve(self) -> None: + def test_stability_gated_ranks_baseline_with_peer_curves(self) -> None: + progressions = [1e9, 2e9, 3e9, 4e9] experiment, metric_signature = self._get_experiment_with_sges_data( { - 0: [(1.0, 0.1), (2.0, 0.09)], - 1: [(1.0, 0.11), (2.0, 0.10)], + 0: [(progression, 0.101) for progression in progressions], + 1: [(progression, 0.101) for progression in progressions], } ) strategy = StabilityGatedEarlyStoppingStrategy( - metric_signatures=[metric_signature] + metric_signatures=[metric_signature], + min_progression=1e9, + normalize_progressions=False, + check_interval=1e9, + window_size=2, + variance_threshold=1e-5, + gap_threshold=4e-4, + stability_count=2, + top_k_fraction=0.0, + min_top_k=1, + recent_best_lookback=0, + ) + self.assertTrue(strategy.is_baseline_reference_curve_needed(experiment)) + self.assertEqual( + strategy.should_stop_trials_early( + trial_indices={0, 1}, experiment=experiment + ), + {}, + ) + strategy.set_baseline_reference_curve( + curve=[(progression, 0.1) for progression in progressions] ) - self.assertFalse(strategy.is_baseline_reference_curve_needed(experiment)) + should_stop = strategy.should_stop_trials_early( + trial_indices={0, 1}, experiment=experiment + ) + self.assertEqual(set(should_stop), {0, 1}) + for reason in should_stop.values(): + self.assertIn("leader_trial=-1", none_throws(reason)) def test_stability_gated_does_not_stop_volatile_trial(self) -> None: experiment, metric_signature = self._get_experiment_with_sges_data(