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

[Unified TorchTrainer] Add PyTorch Lightning Trainer Utilities #37989

Conversation

woshiyyya
Copy link
Member

@woshiyyya woshiyyya commented Aug 1, 2023

Why are these changes needed?

Add Lightning related utilities for Unified TorchTrainer.

  • 1 basic example using new API
  • User guides for Lightning + TorchTrainer

POC example: Text Classification with Ray Data + Lightning

Rendered Doc: https://anyscale-ray--37989.com.readthedocs.build/en/37989/

New utilities

  • Strategy Class: RayDDPStrategy, RayFSDPStrategy, RayDeepSpeedStrategy
  • Environment Class: RayLightningEnvironment
  • Metrics and checkpoint reporting: RayTrainReportCallback
  • prepare_trainer(): To check if the users correctly configured the pl.Trainer

Users can inject these utilities to run Lightning code in TorchTrainer.

Metrics and checkpoint reporting

We propose to let the users define the logs in a lightning callback themselves.

from pytorch_lightning.callbacks import Callback
class RayTrainReportCallback(Callback):
    def on_train_epoch_end(self, trainer: Trainer, pl_module: LightningModule) -> None:
        with TemporaryDirectory() as tmpdir:
            # Fetch metrics
            metrics = trainer.callback_metrics
            metrics = {k: v.item() for k, v in metrics.items()}
           
            # Save checkpoint to local
            ckpt_path = os.path.join(tmpdir, f"ckpt_epoch_{trainer.current_epoch}.pth")
            trainer.save_checkpoint(ckpt_path, weights_only=False)

            # Report to train session
            checkpoint = Checkpoint.from_directory(tmpdir)
            ray.train.report(metrics=metrics, checkpoint=checkpoint)

The users can choose whether to report the metrics to Ray Train or not.

from pytorch_lightning.callbacks import ModelCheckpoint

def train_loop_per_worker():
    # Report to ray train
    trainer = pl.Trainer(
         callbacks=[RayTrainReportCallback()]
    )

   # Keep using the original lightning's logics
   trainer = pl.Trainer(
         callbacks=[ModelCheckpoint(**kwags)]
    )

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
@woshiyyya woshiyyya marked this pull request as ready for review August 1, 2023 21:02
Copy link
Contributor

@matthewdeng matthewdeng left a comment

Choose a reason for hiding this comment

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

Looks really clean. I think we should showcase one example with the previous API compare it with the new API.

python/ray/train/lightning/lightning_utils.py Outdated Show resolved Hide resolved
python/ray/train/lightning/lightning_utils.py Outdated Show resolved Hide resolved
python/ray/train/lightning/lightning_utils.py Outdated Show resolved Hide resolved
python/ray/train/lightning/lightning_utils.py Outdated Show resolved Hide resolved
python/ray/train/tests/lightning_test_utils.py Outdated Show resolved Hide resolved
woshiyyya and others added 2 commits August 1, 2023 23:35
Co-authored-by: matthewdeng <matthew.j.deng@gmail.com>
Signed-off-by: Yunxuan Xiao <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
@scottsun94
Copy link
Contributor

If the user chooses not to report to train, we won't be able to print any related metrics in the train output right?

What will happen to this training result table?

Training completed after 0 iterations at 2023-06-26 16:06:41. Total running time: 24min 41s
╭──────────────────────────────────────────────────────╮
│ Training result                                      │
├──────────────────────────────────────────────────────┤
│ config/train_loop_config/args   ...weight_decay=0.0) │
╰──────────────────────────────────────────────────────╯

Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
@woshiyyya woshiyyya added the tests-ok The tagger certifies test failures are unrelated and assumes personal liability. label Aug 3, 2023
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Copy link
Contributor

@krfricke krfricke left a comment

Choose a reason for hiding this comment

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

Read half of it, will continue later today. Looks great so far! Would be great to get a version that renders in RTD for preview.

datamodule = MyLightningDataModule(...)

trainer = pl.Trainer(
...
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: let's use comments here so that the python code at least would parse correctly (even if it isn't executed)

Suggested change
...
# ...

Copy link
Contributor

Choose a reason for hiding this comment

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

Technically this actually works in python!

woshiyyya and others added 5 commits August 8, 2023 10:25
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
@woshiyyya
Copy link
Member Author

Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Copy link
Contributor

@matthewdeng matthewdeng left a comment

Choose a reason for hiding this comment

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

Generally looks great to me, I think that we can polish and shift around some of the documentation content in a follow up PR.

Co-authored-by: matthewdeng <matthew.j.deng@gmail.com>
Signed-off-by: Yunxuan Xiao <xiaoyunxuan1998@gmail.com>
Copy link
Contributor

@krfricke krfricke left a comment

Choose a reason for hiding this comment

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

This is great, minor nits

woshiyyya and others added 4 commits August 9, 2023 12:07
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
@matthewdeng matthewdeng merged commit 0dd32ed into ray-project:master Aug 10, 2023
120 of 124 checks passed
shrekris-anyscale pushed a commit to shrekris-anyscale/ray that referenced this pull request Aug 10, 2023
…roject#37989)

Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: Yunxuan Xiao <xiaoyunxuan1998@gmail.com>
Co-authored-by: matthewdeng <matthew.j.deng@gmail.com>
Signed-off-by: Shreyas Krishnaswamy <shrekris@anyscale.com>
NripeshN pushed a commit to NripeshN/ray that referenced this pull request Aug 15, 2023
…roject#37989)

Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: Yunxuan Xiao <xiaoyunxuan1998@gmail.com>
Co-authored-by: matthewdeng <matthew.j.deng@gmail.com>
Signed-off-by: NripeshN <nn2012@hw.ac.uk>
harborn pushed a commit to harborn/ray that referenced this pull request Aug 17, 2023
…roject#37989)

Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: Yunxuan Xiao <xiaoyunxuan1998@gmail.com>
Co-authored-by: matthewdeng <matthew.j.deng@gmail.com>
Signed-off-by: harborn <gangsheng.wu@intel.com>
harborn pushed a commit to harborn/ray that referenced this pull request Aug 17, 2023
…roject#37989)

Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: Yunxuan Xiao <xiaoyunxuan1998@gmail.com>
Co-authored-by: matthewdeng <matthew.j.deng@gmail.com>
arvind-chandra pushed a commit to lmco/ray that referenced this pull request Aug 31, 2023
…roject#37989)

Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: Yunxuan Xiao <xiaoyunxuan1998@gmail.com>
Co-authored-by: matthewdeng <matthew.j.deng@gmail.com>
Signed-off-by: e428265 <arvind.chandramouli@lmco.com>
vymao pushed a commit to vymao/ray that referenced this pull request Oct 11, 2023
…roject#37989)

Signed-off-by: woshiyyya <xiaoyunxuan1998@gmail.com>
Signed-off-by: Yunxuan Xiao <xiaoyunxuan1998@gmail.com>
Co-authored-by: matthewdeng <matthew.j.deng@gmail.com>
Signed-off-by: Victor <vctr.y.m@example.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests-ok The tagger certifies test failures are unrelated and assumes personal liability.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants