Skip to content

Commit 37c0d8a

Browse files
committed
Revert "Remove all deprecated args, kwargs for v0.5.0 (#1396) (#1397)"
This reverts commit a85da62.
1 parent dc7e668 commit 37c0d8a

File tree

8 files changed

+89
-6
lines changed

8 files changed

+89
-6
lines changed

ignite/contrib/engines/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def setup_common_training_handlers(
4848
with_pbars: bool = True,
4949
with_pbar_on_iters: bool = True,
5050
log_every_iters: int = 100,
51+
device: Optional[Union[str, torch.device]] = None,
5152
stop_on_nan: bool = True,
5253
clear_cuda_cache: bool = True,
5354
save_handler: Optional[Union[Callable, BaseSaveHandler]] = None,
@@ -91,7 +92,10 @@ def setup_common_training_handlers(
9192
class to use to store ``to_save``. See :class:`~ignite.handlers.checkpoint.Checkpoint` for more details.
9293
Argument is mutually exclusive with ``output_path``.
9394
kwargs: optional keyword args to be passed to construct :class:`~ignite.handlers.checkpoint.Checkpoint`.
95+
device: deprecated argument, it will be removed in v0.5.0.
9496
"""
97+
if device is not None:
98+
warnings.warn("Argument device is unused and deprecated. It will be removed in v0.5.0")
9599

96100
if idist.get_world_size() > 1:
97101
_setup_common_distrib_training_handlers(

ignite/engine/engine.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ def state_dict_user_keys(self) -> List:
643643
return self._state_dict_user_keys
644644

645645
def state_dict(self) -> OrderedDict:
646-
"""Returns a dictionary containing engine's state: "epoch_length", "max_epochs" and "iteration" and
646+
"""Returns a dictionary containing engine's state: "seed", "epoch_length", "max_epochs" and "iteration" and
647647
other state values defined by `engine.state_dict_user_keys`
648648
649649
.. code-block:: python
@@ -676,8 +676,8 @@ def save_engine(_):
676676
def load_state_dict(self, state_dict: Mapping) -> None:
677677
"""Setups engine from `state_dict`.
678678
679-
State dictionary should contain keys: `iteration` or `epoch`, `max_epochs` and `epoch_length`.
680-
If `engine.state_dict_user_keys` contains keys, they should be also present in the state dictionary.
679+
State dictionary should contain keys: `iteration` or `epoch` and `max_epochs`, `epoch_length` and
680+
`seed`. If `engine.state_dict_user_keys` contains keys, they should be also present in the state dictionary.
681681
Iteration and epoch values are 0-based: the first iteration or epoch is zero.
682682
683683
This method does not remove any custom attributes added by user.
@@ -776,12 +776,13 @@ def run(
776776
max_epochs: Optional[int] = None,
777777
max_iters: Optional[int] = None,
778778
epoch_length: Optional[int] = None,
779+
seed: Optional[int] = None,
779780
) -> State:
780781
"""Runs the ``process_function`` over the passed data.
781782
782783
Engine has a state and the following logic is applied in this function:
783784
784-
- At the first call, new state is defined by `max_epochs`, `max_iters`, `epoch_length`, if provided.
785+
- At the first call, new state is defined by `max_epochs`, `max_iters`, `epoch_length`, `seed`, if provided.
785786
A timer for total and per-epoch time is initialized when Events.STARTED is handled.
786787
- If state is already defined such that there are iterations to run until `max_epochs` and no input arguments
787788
provided, state is kept and used in the function.
@@ -801,6 +802,7 @@ def run(
801802
This argument should not change if run is resuming from a state.
802803
max_iters: Number of iterations to run for.
803804
`max_iters` and `max_epochs` are mutually exclusive; only one of the two arguments should be provided.
805+
seed: Deprecated argument. Please, use `torch.manual_seed` or :meth:`~ignite.utils.manual_seed`.
804806
805807
Returns:
806808
State: output state.
@@ -829,6 +831,12 @@ def switch_batch(engine):
829831
trainer.run(train_loader, max_epochs=2)
830832
831833
"""
834+
if seed is not None:
835+
warnings.warn(
836+
"Argument seed is deprecated. It will be removed in 0.5.0. "
837+
"Please, use torch.manual_seed or ignite.utils.manual_seed"
838+
)
839+
832840
if data is not None and not isinstance(data, Iterable):
833841
raise TypeError("Argument data should be iterable")
834842

ignite/engine/events.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,18 @@ def __or__(self, other: Any) -> "EventsList":
203203
return EventsList() | self | other
204204

205205

206-
class EventEnum(CallableEventWithFilter, Enum):
206+
class CallableEvents(CallableEventWithFilter):
207+
# For backward compatibility
208+
def __init__(self, *args: Any, **kwargs: Any) -> None:
209+
super(CallableEvents, self).__init__(*args, **kwargs)
210+
warnings.warn(
211+
"Class ignite.engine.events.CallableEvents is deprecated. It will be removed in 0.5.0. "
212+
"Please, use ignite.engine.EventEnum instead",
213+
DeprecationWarning,
214+
)
215+
216+
217+
class EventEnum(CallableEventWithFilter, Enum): # type: ignore[misc]
207218
"""Base class for all :class:`~ignite.engine.events.Events`. User defined custom events should also inherit
208219
this class.
209220

ignite/handlers/checkpoint.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class Checkpoint(Serializable):
102102
Input of the function is ``(engine, event_name)``. Output of function should be an integer.
103103
Default is None, global_step based on attached engine. If provided, uses function output as global_step.
104104
To setup global step from another engine, please use :meth:`~ignite.handlers.global_step_from_engine`.
105+
archived: Deprecated argument as models saved by ``torch.save`` are already compressed.
105106
filename_pattern: If ``filename_pattern`` is provided, this pattern will be used to render
106107
checkpoint filenames. If the pattern is not defined, the default pattern would be used. See Note for
107108
details.
@@ -288,6 +289,7 @@ def __init__(
288289
score_name: Optional[str] = None,
289290
n_saved: Union[int, None] = 1,
290291
global_step_transform: Optional[Callable] = None,
292+
archived: bool = False,
291293
filename_pattern: Optional[str] = None,
292294
include_self: bool = False,
293295
greater_or_equal: bool = False,
@@ -320,6 +322,8 @@ def __init__(
320322

321323
if global_step_transform is not None and not callable(global_step_transform):
322324
raise TypeError(f"global_step_transform should be a function, got {type(global_step_transform)} instead.")
325+
if archived:
326+
warnings.warn("Argument archived is deprecated and will be removed in 0.5.0")
323327

324328
self.to_save = to_save
325329
self.filename_prefix = filename_prefix
@@ -878,6 +882,11 @@ class ModelCheckpoint(Checkpoint):
878882
879883
Behaviour of this class has been changed since v0.3.0.
880884
885+
Argument ``save_as_state_dict`` is deprecated and should not be used. It is considered as True.
886+
887+
Argument ``save_interval`` is deprecated and should not be used. Please, use events filtering instead, e.g.
888+
:attr:`~ignite.engine.events.Events.ITERATION_STARTED(every=1000)`
889+
881890
There is no more internal counter that has been used to indicate the number of save actions. User could
882891
see its value `step_number` in the filename, e.g. `{filename_prefix}_{name}_{step_number}.pt`. Actually,
883892
`step_number` is replaced by current engine's epoch if `score_function` is specified and current iteration
@@ -906,6 +915,7 @@ class ModelCheckpoint(Checkpoint):
906915
Input of the function is `(engine, event_name)`. Output of function should be an integer.
907916
Default is None, global_step based on attached engine. If provided, uses function output as global_step.
908917
To setup global step from another engine, please use :meth:`~ignite.handlers.global_step_from_engine`.
918+
archived: Deprecated argument as models saved by `torch.save` are already compressed.
909919
filename_pattern: If ``filename_pattern`` is provided, this pattern will be used to render
910920
checkpoint filenames. If the pattern is not defined, the default pattern would be used.
911921
See :class:`~ignite.handlers.checkpoint.Checkpoint` for details.
@@ -952,19 +962,38 @@ def __init__(
952962
self,
953963
dirname: Union[str, Path],
954964
filename_prefix: str = "",
965+
save_interval: Optional[Callable] = None,
955966
score_function: Optional[Callable] = None,
956967
score_name: Optional[str] = None,
957968
n_saved: Union[int, None] = 1,
958969
atomic: bool = True,
959970
require_empty: bool = True,
960971
create_dir: bool = True,
972+
save_as_state_dict: bool = True,
961973
global_step_transform: Optional[Callable] = None,
974+
archived: bool = False,
962975
filename_pattern: Optional[str] = None,
963976
include_self: bool = False,
964977
greater_or_equal: bool = False,
965978
save_on_rank: int = 0,
966979
**kwargs: Any,
967980
):
981+
if not save_as_state_dict:
982+
raise ValueError(
983+
"Argument save_as_state_dict is deprecated and should be True."
984+
"This argument will be removed in 0.5.0."
985+
)
986+
if save_interval is not None:
987+
msg = (
988+
"Argument save_interval is deprecated and should be None. This argument will be removed in 0.5.0."
989+
"Please, use events filtering instead, e.g. Events.ITERATION_STARTED(every=1000)"
990+
)
991+
if save_interval == 1:
992+
# Do not break for old version who used `save_interval=1`
993+
warnings.warn(msg)
994+
else:
995+
# No choice
996+
raise ValueError(msg)
968997

969998
disk_saver = DiskSaver(
970999
dirname,
@@ -984,6 +1013,7 @@ def __init__(
9841013
n_saved=n_saved,
9851014
global_step_transform=global_step_transform,
9861015
filename_pattern=filename_pattern,
1016+
archived=archived,
9871017
include_self=include_self,
9881018
greater_or_equal=greater_or_equal,
9891019
save_on_rank=save_on_rank,

tests/ignite/contrib/engines/test_common.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ def test_asserts_setup_common_training_handlers():
165165
)
166166
trainer.run([1])
167167

168+
with pytest.warns(UserWarning, match=r"Argument device is unused and deprecated"):
169+
setup_common_training_handlers(trainer, device="cpu")
170+
168171

169172
def test_no_warning_with_train_sampler(recwarn):
170173
from torch.utils.data import RandomSampler

tests/ignite/engine/test_custom_events.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@
66

77
import ignite.distributed as idist
88
from ignite.engine import Engine, Events
9-
from ignite.engine.events import CallableEventWithFilter, EventEnum, EventsList
9+
from ignite.engine.events import CallableEvents, CallableEventWithFilter, EventEnum, EventsList
10+
11+
12+
def test_deprecated_callable_events_class():
13+
engine = Engine(lambda engine, batch: 0)
14+
15+
with pytest.warns(DeprecationWarning, match=r"Class ignite\.engine\.events\.CallableEvents is deprecated"):
16+
17+
class CustomEvents(CallableEvents, Enum):
18+
TEST_EVENT = "test_event"
19+
20+
with pytest.raises(TypeError, match=r"Value at \d of event_names should be a str or EventEnum"):
21+
engine.register_events(*CustomEvents)
1022

1123

1224
def test_custom_events():

tests/ignite/engine/test_engine.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,9 @@ def test_run_asserts(self):
520520
with pytest.raises(ValueError, match=r"Input data has zero size. Please provide non-empty data"):
521521
engine.run([])
522522

523+
with pytest.warns(UserWarning, match="Argument seed is deprecated"):
524+
engine.run([0, 1, 2, 3, 4], seed=1234)
525+
523526
def test_state_get_event_attrib_value(self):
524527
state = State()
525528
state.iteration = 10

tests/ignite/handlers/test_checkpoint.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ def test_checkpoint_wrong_input():
6363
with pytest.raises(TypeError, match=r"global_step_transform should be a function."):
6464
Checkpoint(to_save, lambda x: x, score_function=lambda e: 123, score_name="acc", global_step_transform=123)
6565

66+
with pytest.warns(UserWarning, match=r"Argument archived is deprecated"):
67+
Checkpoint(to_save, lambda x: x, score_function=lambda e: 123, score_name="acc", archived=True)
68+
6669
with pytest.raises(ValueError, match=r"Cannot have key 'checkpointer' if `include_self` is True"):
6770
Checkpoint({"checkpointer": model}, lambda x: x, include_self=True)
6871

@@ -550,12 +553,21 @@ def test_model_checkpoint_args_validation(dirname):
550553
with pytest.raises(ValueError, match=r"with extension '.pt' are already present "):
551554
ModelCheckpoint(nonempty, _PREFIX)
552555

556+
with pytest.raises(ValueError, match=r"Argument save_interval is deprecated and should be None"):
557+
ModelCheckpoint(existing, _PREFIX, save_interval=42)
558+
553559
with pytest.raises(ValueError, match=r"Directory path '\S+' is not found"):
554560
ModelCheckpoint(dirname / "non_existing_dir", _PREFIX, create_dir=False)
555561

562+
with pytest.raises(ValueError, match=r"Argument save_as_state_dict is deprecated and should be True"):
563+
ModelCheckpoint(existing, _PREFIX, create_dir=False, save_as_state_dict=False)
564+
556565
with pytest.raises(TypeError, match=r"global_step_transform should be a function"):
557566
ModelCheckpoint(existing, _PREFIX, create_dir=False, global_step_transform=1234)
558567

568+
with pytest.warns(UserWarning, match=r"Argument archived is deprecated"):
569+
ModelCheckpoint(existing, _PREFIX, create_dir=False, archived=True)
570+
559571
h = ModelCheckpoint(dirname, _PREFIX, create_dir=False)
560572
assert h.last_checkpoint is None
561573
with pytest.raises(RuntimeError, match=r"No objects to checkpoint found."):

0 commit comments

Comments
 (0)