Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed EMAHandler warm-up behaviour #2333

Merged
merged 14 commits into from
Dec 20, 2021
Merged

Conversation

sandylaker
Copy link
Contributor

@sandylaker sandylaker commented Nov 24, 2021

Fixes #2294

Description:

  • Fixed EMAHandler warm-up behaviour

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)

@github-actions github-actions bot added the module: handlers Core Handlers module label Nov 24, 2021
@vfdev-5
Copy link
Collaborator

vfdev-5 commented Nov 24, 2021

Thanks for the PR @sandylaker !
Unfortunately, as we already released a stable version with the args you are removing here, we can not just remove them without deprecation cycle. Let's do the following, we keep the docstring as it was and add deprecation message suggesting to use StateParamScheduler and that we will remove these args in future releases.
In the code we can first fix #2294 issue and also show deprecation warning if any of these params is used.

@sandylaker
Copy link
Contributor Author

@vfdev-5 Thanks for pointing out that.

  1. Do we have a specific class or function/decorator for the deprecation warning? Or should I adopt the Python's built-in DeprecationWarning?
  2. Besides warning, should we keep using the internal function for updating the momentum? I.e., we just throw a warning when the handler performs warm-up. But in this case the StateParamScheduler still cannot control the momentum.

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Nov 24, 2021

Do we have a specific class or function/decorator for the deprecation warning? Or should I adopt the Python's built-in DeprecationWarning?

@sandylaker I think we can do as we are doing for seed arg in Engine.run,

seed: Optional[int] = None,

Besides warning, should we keep using the internal function for updating the momentum? I.e., we just throw a warning when the handler performs warm-up. But in this case the StateParamScheduler still cannot control the momentum.

Sorry, I didn't quite follow everything in the code for StateParamScheduler, but if it is either one or another, then let's again do as for seed <=> if user uses it the warning is shown but no seed is set = nothing done.

cc @fco-dv

@sandylaker
Copy link
Contributor Author

sandylaker commented Nov 24, 2021

@vfdev-5 So that means: we encourage the users to use StateParamScheduler. If they create a handler like this

ema_handler = EMAHandler(momentum=0.2, momentum_warmup=0.02, warmup_iters=100)

Then,

  1. a warning message is shown and saying that momentum_warmup and warmup_iters will be deprecated in the future.
  2. The warm-up args have no actual effect, no warmup-up will be performed. I.e., the momentum will be constant, unless users use StateParamScheduler to control it. This also means that _update_ema_momentum and _get_momentum can be removed, we just read the momentum from Engine.state.
  3. We keep the current docstring and the args.

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Nov 24, 2021

@sandylaker yes, I was thinking about something like that. Do you think this is too restrictive ? In terms of API, I agree that it would require 2-3 additional lines with a state param scheduler in order to have the same behaviour. On the other hand we can keep both things: momentum_warmup, warmup_iters args and usage of state param handler and clearly say in docs for EMAHandler that if momentum_warmup, warmup_iters args are specified then they will overwrite any other scheduling (via state param scheduler or just a manual control).
What do you think or any other suggestions ?

sandylaker added 2 commits November 25, 2021 20:21
Signed-off-by: sandylaker <yawei.li@tum.de>
…uler.py

Signed-off-by: sandylaker <yawei.li@tum.de>
@sandylaker
Copy link
Contributor Author

@vfdev-5 I think it is better to do like this:

  1. a warning message is shown and saying that momentum_warmup and warmup_iters will be deprecated in the future.
  2. The warm-up args have no actual effect, no warmup-up will be performed. I.e., the momentum will be constant, unless users use StateParamScheduler to control it. This also means that _update_ema_momentum and _get_momentum can be removed, we just read the momentum from Engine.state.
  3. We keep the current docstring and the args.

Because some users hope to schedule the momentum with a custom schedule. So we can provide this feature in the next version.
At the same time, we will warn the users who is using the current version that they the warmup function will be deprecated.
Users only need to update their code when they have set the warmup args.

So I think doing this will not break the BC.

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Nov 29, 2021

@sandylaker sorry for delay, OK, I agree with you and let's make it as you are suggesting.

Copy link
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.

Thanks for the updates @sandylaker (and sorry for delay)
I left few other comments...

It would be nice to provide an explicit example in the docs on how to do the warm-up. Further more I wonder how could we simplify more explicit state param scheduler creation and make it a one-liner... @fco-dv any ideas ?

ignite/handlers/ema_handler.py Outdated Show resolved Hide resolved
ignite/handlers/ema_handler.py Outdated Show resolved Hide resolved
ignite/handlers/ema_handler.py Show resolved Hide resolved
engine_1.add_event_handler(
Events.ITERATION_COMPLETED, check_ema_momentum, momentum_warmup_1, momentum, warmup_iters
)
engine_1.run(range(2))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't we make at least 10 iters to properly check the scheduler ?

engine_2.add_event_handler(
Events.ITERATION_COMPLETED, check_ema_momentum, momentum_warmup_2, momentum, warmup_iters
)
engine_2.run(range(2))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here, IMO

Copy link
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.

Thanks a lot for the updates @sandylaker !
I'll update the tests and can merge the PR then

tests/ignite/handlers/test_ema_handler.py Outdated Show resolved Hide resolved
tests/ignite/handlers/test_ema_handler.py Outdated Show resolved Hide resolved
@vfdev-5 vfdev-5 merged commit 62742a9 into pytorch:master Dec 20, 2021
@sandylaker sandylaker deleted the ema_momentum branch December 21, 2021 17:50
Ishan-Kumar2 pushed a commit to Ishan-Kumar2/ignite that referenced this pull request Dec 26, 2021
* deprecate warmup function in ema_handler.py

Signed-off-by: sandylaker <yawei.li@tum.de>

* keep the previous API but throw warnings

Signed-off-by: sandylaker <yawei.li@tum.de>

* revert changes to state_param_scheduler.py and test_state_param_scheduler.py

Signed-off-by: sandylaker <yawei.li@tum.de>

* use `LambdaStateScheduler` to schedule EMA momentum

* fix docs

* fix mypy

* Apply suggestions from code review

Co-authored-by: vfdev <vfdev.5@gmail.com>
@vfdev-5 vfdev-5 changed the title Ema momentum Fixed EMAHandler warm-up behaviour Jan 13, 2022
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.

EMAHandler with warm-up counter-intuitive.
3 participants