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

[Feature] Add OptimizerHook #716

Merged
merged 5 commits into from
Nov 26, 2022
Merged

[Feature] Add OptimizerHook #716

merged 5 commits into from
Nov 26, 2022

Conversation

aakhundov
Copy link
Contributor

@aakhundov aakhundov commented Nov 26, 2022

Description

The new type of hook, OptimizerHook, is added as suggested in #704.

Motivation and Context

See the suggested change in #704.

Types of changes

What types of changes does your code introduce? Remove all that do not apply:

  • New feature (non-breaking change which adds core functionality)
  • Documentation (initial update in the documentation)

Backward compatibility with the existing optimizer parameter of Trainer.__init__ is respected.
Minor breaking change: previously grad_norm key in the losses_td is now grad_norm_0.

Checklist

Go over all the following points, and put an x in all the boxes that apply.
If you are unsure about any of these, don't hesitate to ask. We are here to help!

  • I have read the CONTRIBUTION guide (required)
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.

The change made in the documentation is very initial: optimizer hook is just briefly mentioned in the list of possible hooks and the OptimizerHook class is added to the "Trainer and hooks" table. Probably, wider documentation coverage of the new hook type is needed.

cc @BY571

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 26, 2022
@aakhundov
Copy link
Contributor Author

@vmoens As currently implemented, the new hook's signature is different from other hooks: besides accepting (and returning) a TensorDict instance, it also accepts the clip norm parameters and the index i in the trainer._optimizer_ops list (for distinguishing each hook's grad_norm_{i} key added to the returned TensorDict).

If we want the optimizer hook to be more similar to other hooks, we could:

  1. Add clip_norm and clip_norm_grad constructor parameters to the OptimizerHook.__init__. Then, when creating the "default" optimizer hook from its optimizer argument, Trainer.__init__ will just pass its own clip_norm and clip_norm_grad arguments as is to the OptimizerHook.__init__. Bonus: users will be able to set different gradient clipping configuration for each hook separately.

  2. Return grad_norm value from the OptimizerHook.__call__ instead of the updated TensorDict parameter. Then in Trainer._optimizer_hook we could set the grad_norm in the losses_td with the respective index, without the need to pass the index to the hook call.

  3. Move detaching the losses_td outside of Trainer._optimizer_hook: to be done by the calling Trainer.optim_steps right after the Trainer._optimizer_hook returns.

Importantly, even after the above modifications, Trainer._optimizer_hook will be different from other Trainer._*_hook functions, as it will need to set grad_norm in its TensorDict argument besides the usual for-loop over the hook instances.

Please let me know how you'd like to proceed. Thanks!

@codecov
Copy link

codecov bot commented Nov 26, 2022

Codecov Report

Merging #716 (860e922) into main (3105819) will increase coverage by 0.12%.
The diff coverage is 97.87%.

@@            Coverage Diff             @@
##             main     #716      +/-   ##
==========================================
+ Coverage   88.77%   88.89%   +0.12%     
==========================================
  Files         122      122              
  Lines       21151    21273     +122     
==========================================
+ Hits        18776    18910     +134     
+ Misses       2375     2363      -12     
Flag Coverage Δ
habitat-gpu 24.35% <18.75%> (-0.02%) ⬇️
linux-cpu 85.00% <97.87%> (+0.12%) ⬆️
linux-gpu 85.88% <97.87%> (+0.13%) ⬆️
linux-jumanji 29.28% <18.75%> (-0.03%) ⬇️
linux-outdeps-gpu 72.28% <97.87%> (+0.20%) ⬆️
linux-stable-cpu 84.86% <97.87%> (+0.13%) ⬆️
linux-stable-gpu 85.55% <97.87%> (+0.12%) ⬆️
linux_examples-gpu 42.68% <18.75%> (-0.06%) ⬇️
macos-cpu 84.68% <97.87%> (+0.13%) ⬆️
olddeps-gpu 74.94% <97.87%> (+0.19%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
torchrl/trainers/trainers.py 77.66% <93.75%> (+2.71%) ⬆️
test/test_trainer.py 98.23% <100.00%> (+0.30%) ⬆️
torchrl/envs/vec_env.py 69.06% <0.00%> (+0.50%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@vmoens vmoens added the enhancement New feature or request label Nov 26, 2022
Copy link
Contributor

@vmoens vmoens left a comment

Choose a reason for hiding this comment

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

Great stuff!
Thanks a lot for this!

@vmoens
Copy link
Contributor

vmoens commented Nov 26, 2022

Regarding this

Importantly, even after the above modifications, Trainer.optimizer_hook will be different from other Trainer.*_hook functions, as it will need to set grad_norm in its TensorDict argument besides the usual for-loop over the hook instances.

It's ok, it's backward compatible and it suits most needs.
Thanks again for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants