Fix state_dict handling for max_iters in Engine#3729
Fix state_dict handling for max_iters in Engine#3729TahaZahid05 wants to merge 9 commits intopytorch:masterfrom
Conversation
a0cac99 to
4fefdb6
Compare
5ce98fc to
6e60bfc
Compare
|
@vfdev-5 done! |
More descriptive way to show tuple of tuples Co-authored-by: vfdev <vfdev.5@gmail.com>
|
Yes, it is fine, let's provide appropriate type hint for the structure
…On Sat, Apr 11, 2026, 21:49 Taha Zahid ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In tests/ignite/base/test_mixins.py
<#3729 (comment)>:
> + s.load_state_dict({"a": 1, "b": 2})
+
+ # Test one-of optional keys - having all from one group
+ with pytest.raises(ValueError, match=r"should contain only one of '\('c', 'd'\)'"):
+ s.load_state_dict({"a": 1, "b": 2, "c": 3, "d": 4, "e": 5})
+
+ # Test one-of optional keys - having all from another group
+ with pytest.raises(ValueError, match=r"should contain only one of '\('e', 'f'\)'"):
+ s.load_state_dict({"a": 1, "b": 2, "c": 3, "e": 5, "f": 6})
+
+ # Valid state dict
+ s.load_state_dict({"a": 1, "b": 2, "c": 3, "e": 5})
+ print("Valid state dict loaded successfully")
+
+
+def test_empty_optional_groups():
@vfdev-5 <https://github.com/vfdev-5> If we raise error for
_state_dict_one_of_opt_keys containing empty tuples then we should also
change its initialization to a single tuple as well for it to pass the
test? Is that fine?
# _state_dict_one_of_opt_keys: tuple = ((),)
_state_dict_one_of_opt_keys: tuple = ()
—
Reply to this email directly, view it on GitHub
<#3729 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AASYOH7G6AKEJFLSUMD2POL4VKOTNAVCNFSM6AAAAACXTJ6XTKVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHM2DAOJUGE2TIMZUGY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
| keys: tuple[str, ...] = self._state_dict_all_req_keys + (self._state_dict_one_of_opt_keys[0],) | ||
| keys: tuple[str, ...] = self._state_dict_all_req_keys | ||
| # We add iteration by default to get exact measure of progress | ||
| keys += ("iteration",) |
There was a problem hiding this comment.
Why we do not add it to self._state_dict_all_req_keys directly ?
There was a problem hiding this comment.
@vfdev-5 @aaishwarymishra Won't it be BC? any existing code that relies only on epoch rather than iteration may fail?
There was a problem hiding this comment.
Pull request overview
This PR extends Ignite’s Engine state (de)serialization to properly support runs terminated by max_iters, including mutually-exclusive termination/progress keys and additional validation to prevent invalid resume states.
Changes:
- Update
Engine.state_dict()to serialize exactly one termination key (max_epochsormax_iters) alongside progress. - Update
Serializable.load_state_dict()to validate groups of mutually exclusive optional keys (e.g., (iteration|epoch) and (max_epochs|max_iters)). - Add/expand tests covering
max_itersserialization, resumption, validation errors, and checkpointing.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
ignite/engine/engine.py |
Adds max_iters serialization/deserialization support, validation helpers, and run-time handling of mutually exclusive termination params. |
ignite/base/mixins.py |
Changes optional-key validation to support grouped “one-of” requirements. |
tests/ignite/engine/test_engine_state_dict.py |
Expands integration/unit tests for max_iters state dict behavior, resume cases, and error validation. |
tests/ignite/base/test_mixins.py |
Adds tests for the new grouped optional-key validation logic in Serializable.load_state_dict(). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| keys: tuple[str, ...] = self._state_dict_all_req_keys + (self._state_dict_one_of_opt_keys[0],) | ||
| keys: tuple[str, ...] = self._state_dict_all_req_keys | ||
| # We add iteration by default to get exact measure of progress | ||
| keys += ("iteration",) |
There was a problem hiding this comment.
If iteration is always getting included why not we just add it to _state_dict_all_req_keys
Fixes #1521
Description:
This PR builds up on #3439 to implement
max_itershandling in state serialization and deserialization duringEngineruns.Key Changes:
Engine.state_dict()now correctly exports exactly one ofmax_itersormax_epochsdepending on which condition theEnginerun was configured with._state_dict_one_of_opt_keysto accept groups of mutually exclusive requirements, enablingEngine.load_state_dict()to cleanly accept eitheriteration/max_itersorepoch/max_epochsand correctly continue the engine's resume state from either.Engineto prevent loading impossible future iterations while safely resuming training.Check list: