Resuming from checkpoint with max_epochs issue
❓ Questions/Help/Support
We have built a system that uses pytorch-ignite under the hood that will automatically output checkpoints at the end of each epoch.
We have found that if a model is trained for n epochs with a config that specifies it must run for n epochs and we then reload the model from its checkpoint and train again with no changes to the config, then it runs for n epochs again. We would have expected that it would not run 0 epochs, since the checkpoint was taken at the end of training.
If we load the checkpoint and set the number of epochs to m, then it runs for m - n epochs (and raises as exception if m < n). This behavior makes sense, and is expected, but the behavior when m == n is not.
Is there an intuitive reason why the logic here https://github.com/pytorch/ignite/blob/master/ignite/engine/engine.py#L877
is < instead of <=?
Partly this is due to how the evaluator engine is working when it runs and reruns from 0 to max_epochs (usually 1) every time it is called in the validation handler:
@trainer.on(Events.EPOCH_COMPLETED)
def run_validation():
state = evaluator.run(val_data) # here we expect the engine to run from 0 every time it is called.
# ... do something with metrics
We wanted to improve the resuming API for a long time. Here is detailed logic we have now: https://github.com/pytorch/ignite/wiki/Roadmap#runresume-logic-improvements (click on details)
# (re)start from 0 to 5
engine.run(data, max_epochs=5) -> Engine run starting with max_epochs=5 => state.epoch=5
# continue from 5 to 7
engine.run(data, max_epochs=7) -> Engine run resuming from iteration 50, epoch 5 until 7 epochs => state.epoch=7
# error
engine.run(data, max_epochs=4) -> ValueError: Argument max_epochs should be larger than the start epoch
# restart from 0 to 7 (As state.epoch == max_epochs(=7), this should be like that as we always do: evaluator.run(data) without any other instructions)
engine.run(data, max_epochs=7) -> Engine run starting with max_epochs=7 => state.epoch=7
# forced restart from 0 to 5
engine.state.max_epochs = None
engine.run(data, max_epochs=5) -> Engine run starting with max_epochs=5 => state.epoch=5
# forced restart from 0 to 9, instead of continue from state.epoch=7
engine.state.max_epochs = None
engine.run(data, max_epochs=9) -> Engine run starting with max_epochs=9 => state.epoch=9
Hi @vfdev-5 , this is probably happening due to auto_reset logic getting triggered due to 'is done' being true. Can I work on this issue, and make a PR?
Hi @vfdev-5 , this is probably happening due to auto_reset logic getting triggered due to 'is done' being true. Can I work on this issue, and make a PR?
I'm not totally following your point, if you can explain your point before coding anything.
When a user tries to resume training from a checkpoint where state.epoch == 3 and sets max_epochs=3, the engine sees it is as 'already completed' and resets the state to 0, starting the training from scratch instead of staying at epoch 3. This creates an inconsistency: run() sometimes treats max_epochs as a duration (run for X more) and sometimes as a destination (run until X), depending on whether an reset was triggered or not.
To tackle thid, I’ll add a engine.reset() method. This gives users a clear way to wipe the state themselves when they actually want to start fresh. For example: If I tell the engine max_epochs=10, it should mean "Stop at 10." If the engine is already at epoch 10 when I call run(), it should just realize it’s already at finished and exit immediately, rather than resetting everything to zero.
FWIW, I definitely agree with that last sentence. That inconsistency is what prompted this issue.
If the engine is already at epoch 10 when I call run(), it should just realize it’s already at finished and exit immediately, rather than resetting everything to zero.
I'm not sure if this is a good behavior to exit immediately as I do not think that we intentionally want to call the engine to do nothing. We had at some point a reset method, but i had its own problems (maybe we can resolve them still). Finally, the API for evaluator to call reset all the time when we want to run it from 0 will be BC breaking and more verbose.
@drewoldag I hear you point about the inconsistency and we can try to figure out a BC compatible way to make it less inconsistent with a flag in the engine.run function. The best way to move forward is to present a new behavior "table" like in the comment above.
How about when the run is called and the max_epoch == state.max_epoch meaning its a rerun from a checkpoint we do nothing and the run exits without training potentially with a warning. While when the max_epoch is None we reset and start from 0 epoch like the current evaluator behaviour?