Skip to content

Introduce ResettableHandler interface and attach() method#3564

Merged
vfdev-5 merged 1 commit intopytorch:masterfrom
TahaZahid05:feature/reset_on_start
Feb 25, 2026
Merged

Introduce ResettableHandler interface and attach() method#3564
vfdev-5 merged 1 commit intopytorch:masterfrom
TahaZahid05:feature/reset_on_start

Conversation

@TahaZahid05
Copy link
Copy Markdown
Collaborator

@TahaZahid05 TahaZahid05 commented Feb 20, 2026

Partially Addresses #3535

Description:
Introduces a ResettableHandler abstract base class to establish a formalized interface for stateful handlers. EarlyStopping is the first adopter of this pattern, implementing both reset() and attach() methods.

This mirrors the existing Metric.attach() lifecycle pattern, avoiding any Engine-level modifications while providing a clean, self-contained way to manage handler state across runs.

API

from ignite.engine import Engine, Events
from ignite.handlers import EarlyStopping

trainer = Engine(train_fn)
evaluator = Engine(eval_fn)

handler = EarlyStopping(patience=5, score_function=score_fn, trainer=trainer)

# Attaches the handler's check to the evaluator (Events.EPOCH_COMPLETED)
# AND automatically attaches the handler's `reset()` to the trainer (Events.STARTED)
handler.attach(
    engine=evaluator, 
    event=Events.EPOCH_COMPLETED, 
    reset_engine=trainer, 
    reset_event=Events.STARTED
)

Check list:

  • New tests are added (if a new feature is added)
  • New doc strings: description and/or example code are in RST format
  • Documentation is updated (if required)

@TahaZahid05
Copy link
Copy Markdown
Collaborator Author

@vfdev-5 I have refactored the PR to use the Metric.attach() pattern as we discussed.

Key changes:

  • Reverted all modifications to Engine (engine.py and events.py are now untouched).
  • Added the ResettableHandler ABC in ignite/base/mixins.py with an abstract reset() method to establish the formalized interface.
  • Refactored EarlyStopping to inherit from the ABC, implementing reset() and attach().
  • Added test coverage in test_early_stopping.py verifying that attach() registers both the evaluation event and the reset callback (including cross-engine scenarios).
  • Sphinx documentation configuration updated to recognize the new ABC.

Let me know what you think!

Future Direction:
If you are happy with how this pattern turned out for EarlyStopping, I'd be more than happy to help migrate the other stateful handlers in follow-up PRs. Just let me know if that's something you'd want!

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Feb 21, 2026

@TahaZahid05 can you please also update the PR description. Thanks a lot for working on this topic!

@TahaZahid05 TahaZahid05 changed the title Add ResetableHandler interface and automated reset logic Introduce ResettableHandler interface and attach() method Feb 22, 2026
@TahaZahid05 TahaZahid05 changed the title Introduce ResettableHandler interface and attach() method Introduce ResettableHandler interface and attach() method Feb 22, 2026
@TahaZahid05 TahaZahid05 requested a review from vfdev-5 February 23, 2026 00:25
@TahaZahid05 TahaZahid05 force-pushed the feature/reset_on_start branch from bf0ccca to 2184a22 Compare February 24, 2026 15:47
Copy link
Copy Markdown
Collaborator

@vfdev-5 vfdev-5 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @TahaZahid05 !

@TahaZahid05
Copy link
Copy Markdown
Collaborator Author

@vfdev-5 can you guide me regarding the errors? I built it locally and ran fine, and here it seems to be some error regarding Sampler, Dataset, DataLoader but i didn't touch those.

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Feb 24, 2026

"Build docs" errors are failing unrelated. Pyrefly are related to the change but we should avoid circular import of Engine

@TahaZahid05
Copy link
Copy Markdown
Collaborator Author

@vfdev-5

Thanks. Should I just use a TYPE_CHECKING block to import Engine and then use a string i.e engine: "Engine" in the attach method?

@vfdev-5
Copy link
Copy Markdown
Collaborator

vfdev-5 commented Feb 24, 2026

@vfdev-5

Thanks. Should I just use a TYPE_CHECKING block to import Engine and then use a string i.e engine: "Engine" in the attach method?

Try that and see if it helps

@TahaZahid05 TahaZahid05 force-pushed the feature/reset_on_start branch from 2365c80 to 9a3a353 Compare February 25, 2026 01:10
@TahaZahid05
Copy link
Copy Markdown
Collaborator Author

TahaZahid05 commented Feb 25, 2026

@vfdev-5
Thanks. Should I just use a TYPE_CHECKING block to import Engine and then use a string i.e engine: "Engine" in the attach method?

Try that and see if it helps

Done. Ran locally as well and it passed. I have also retained type hints for attach() by using # type: ignore[override] as i saw it had been implemented in other functions such as for ProgressBar in tqdm_logger.py as well.

Thanks.

@vfdev-5 vfdev-5 enabled auto-merge February 25, 2026 09:29
@vfdev-5 vfdev-5 added this pull request to the merge queue Feb 25, 2026
Merged via the queue into pytorch:master with commit ff116eb Feb 25, 2026
24 checks passed
@TahaZahid05 TahaZahid05 deleted the feature/reset_on_start branch March 4, 2026 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs module: base Base module module: engine Engine module module: handlers Core Handlers module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants