-
Notifications
You must be signed in to change notification settings - Fork 313
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] Lightning integration example #2057
base: main
Are you sure you want to change the base?
[Feature] Lightning integration example #2057
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/2057
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
Hi @svnv-svsv-jm! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty impressive work! It'll take me some time to review it.
Cc'ing people who may be interested
@giadefa @albertbou92 @BY571 @tchaton
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm leaning towards accepting a lightning backend for the trainers but given the limited bandwidth on my time I won't have much time to scale the PPO trainer to others.
A recipe for other trainers would probably help others code their own trainer. For instance a tutorial (in a second PR)?
Are you considering adding other trainers?
Since this is a new "trainer" I think it should be moved to torchrl/trainers and we will need to make the 2 APIs somewhat compatible.
An example in torchrl/examples
would be welcome!
I didn't do a very "in-depth" review, just pieces that I could easily spot for a more homogeneous formatting within the lib!
torchrl/lightning/_base.py
Outdated
__all__ = ["BaseRL"] | ||
|
||
import typing as ty | ||
from loguru import logger |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unless this is packed with lightning, we won't be using it.
torchrl has a logger under torchrl._utils
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, forgot to remove loguru
, which is used in the project I copied most of the code from. I will check out torchrl
's logger or remove logging entirely for this.
torchrl/lightning/_base.py
Outdated
@@ -0,0 +1,205 @@ | |||
"""Creates a helper class for more complex models.""" | |||
|
|||
__all__ = ["BaseRL"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't use __all__
but import relevant classes in __init__.py
torchrl/lightning/_base.py
Outdated
@@ -0,0 +1,205 @@ | |||
"""Creates a helper class for more complex models.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing headers
torchrl/lightning/accelerators.py
Outdated
@@ -0,0 +1,33 @@ | |||
__all__ = ["find_device"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing headers
we don't use __all__
torchrl/lightning/loops.py
Outdated
this will never be called by the `pl.Trainer` in the `on_train_epoch_end` hook. | ||
We have to call it manually in the `training_step`.""" | ||
scheduler = self.lr_schedulers() | ||
assert isinstance(scheduler, LRScheduler) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no assert in the codebase
torchrl/lightning/loops.py
Outdated
batch_idx: int = 0, | ||
tag: str = "train", | ||
) -> Tensor: | ||
"""Common step.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we expand this a bit?
torchrl/lightning/loops.py
Outdated
loss = loss + value | ||
loss_dict[f"{key}/{tag}"] = value | ||
# Sanity check and return | ||
assert isinstance(loss, torch.Tensor) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no assert in codebase
torchrl/lightning/ppo.py
Outdated
"""Template for a PPO model on the pendulum env, for Lightning.""" | ||
|
||
__all__ = ["PPOPendulum"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing header, no __all__
torchrl/lightning/ppo.py
Outdated
import typing as ty | ||
|
||
import torch | ||
from tensordict import TensorDict # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the
# type: ignore
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My mypy
in my VSCode was complaining... So I mypy-ignored the line. This is not necessary in the codebase.
Co-authored-by: Vincent Moens <[email protected]>
I will take care of the comments, and add an example under |
Co-authored-by: Vincent Moens <[email protected]>
Co-authored-by: Vincent Moens <[email protected]>
Co-authored-by: Vincent Moens <[email protected]>
@svnv-svsv-jm I think that just moving it to |
I moved it by allowing |
Not sure that cuts it unfortunately. The long term goal is that there is no |
@vmoens No problem! Just wanted to make sure I understood correctly. So indeed I will just move |
Description
This PR offers a convenient
lightning.pytorch.LightningModule
base class, from which one can inherit to be able to train atorchrl
model usinglightning
.Motivation and Context
This PR is inspired by this issue: Lightning-Universe/lightning-bolts#986
Types of changes
What types of changes does your code introduce? Remove all that do not apply:
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!