Skip to content

feat(early_stopping): add get_default_score_fn and get_default_event_filter helpers (#3411)#3745

Draft
MukundaKatta wants to merge 1 commit into
pytorch:masterfrom
MukundaKatta:feat/early-stopping-enhancements
Draft

feat(early_stopping): add get_default_score_fn and get_default_event_filter helpers (#3411)#3745
MukundaKatta wants to merge 1 commit into
pytorch:masterfrom
MukundaKatta:feat/early-stopping-enhancements

Conversation

@MukundaKatta
Copy link
Copy Markdown

Summary

Closes part of #3411. Two enhancements explicitly suggested by maintainer @vfdev-5 in the issue thread:

  1. EarlyStopping.get_default_score_fn — analogous to Checkpoint.get_default_score_fn, supports score_sign=-1.0 for error-like metrics.
  2. EarlyStopping.get_default_event_filter — events filtering helper for warmup that consults self.trainer.state.epoch (fixes the gotcha that evaluators reset their epoch counter on each run()).

The maintainer pushed back on PR #3661's min_evals parameter and explicitly preferred event filtering, which is why this PR ships the filter helper instead.

Files

  • ignite/handlers/early_stopping.py (+88) — both helpers, additive, BC-safe.
  • tests/ignite/handlers/test_early_stopping.py (+136) — 9 new tests covering positive/negative sign, invalid sign, integration with mode="min", invalid after, zero/non-zero warmup, full engine integration, and attach signature compatibility.

Test plan

  • 32 pass, 7 distributed deselected (env-only). Existing 23 tests unaffected.
  • ruff check + ruff format --check clean.
  • Backwards compatible — no existing behavior changed.

…filter helpers

Adds two helper methods to EarlyStopping mirroring patterns in Checkpoint and
the maintainer's preferred design from the issue thread:

- EarlyStopping.get_default_score_fn(metric_name, score_sign=1.0): a static
  helper that builds a score_function reading engine.state.metrics[metric_name]
  and applying score_sign (use -1.0 for error-like metrics).

- EarlyStopping.get_default_event_filter(after): an instance method that
  returns a (engine, event) -> bool filter consulting the trainer's epoch
  counter, so callers can implement a warmup window via Events.COMPLETED(
  event_filter=handler.get_default_event_filter(after=N)) without coupling
  the warmup logic to the handler. Uses the trainer epoch (not the host
  engine's event count) so the warmup is well-defined when the handler is
  attached to an evaluator that re-runs from scratch each epoch.

Both helpers are additive with backwards-compatible defaults and do not
change any existing public behavior.

Closes part of pytorch#3411
@github-actions github-actions Bot added the module: handlers Core Handlers module label Apr 25, 2026
def event_filter(engine: Engine, event: int) -> bool:
# The host engine's `event` counter resets per `run()` (e.g. evaluators
# called repeatedly), so look at the trainer's epoch counter instead.
return trainer.state.epoch > after
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could add support for iteration too, so user can specify minimum iterations allowed

self.threshold_mode = state_dict.get("threshold_mode", self.threshold_mode)

@staticmethod
def get_default_score_fn(metric_name: str, score_sign: float = 1.0) -> Callable:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the use of this in early stopping?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: handlers Core Handlers module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants