-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
[tune] Update pytorch-lightning integration API #38883
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[tune] Update pytorch-lightning integration API
Signed-off-by: Kai Fricke <kai@anyscale.com>
- Loading branch information
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
import inspect | ||
import logging | ||
import os | ||
import tempfile | ||
import warnings | ||
from contextlib import contextmanager | ||
from typing import Dict, List, Optional, Type, Union | ||
|
||
from pytorch_lightning import Callback, Trainer, LightningModule | ||
from ray import tune | ||
from ray.util import PublicAPI | ||
from ray import train | ||
from ray.util import log_once | ||
from ray.util.annotations import PublicAPI, Deprecated | ||
from ray.air.checkpoint import Checkpoint as LegacyCheckpoint | ||
from ray.train._checkpoint import Checkpoint | ||
from ray.train._internal.storage import _use_storage_context | ||
|
||
import os | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -72,55 +79,56 @@ def _handle(self, trainer: Trainer, pl_module: Optional[LightningModule]): | |
|
||
|
||
@PublicAPI | ||
class TuneReportCallback(TuneCallback): | ||
"""PyTorch Lightning to Ray Tune reporting callback | ||
|
||
Reports metrics to Ray Tune. | ||
|
||
.. note:: | ||
In Ray 2.4, we introduced | ||
:class:`LightningTrainer <ray.train.lightning.LightningTrainer>`, | ||
which provides native integration with PyTorch Lightning. Here is | ||
:ref:`a simple example <lightning_mnist_example>` of how to use | ||
``LightningTrainer``. | ||
class TuneReportCheckpointCallback(TuneCallback): | ||
"""PyTorch Lightning report and checkpoint callback | ||
|
||
Saves checkpoints after each validation step. Also reports metrics to Tune, | ||
which is needed for checkpoint registration. | ||
|
||
Args: | ||
metrics: Metrics to report to Tune. If this is a list, | ||
each item describes the metric key reported to PyTorch Lightning, | ||
and it will reported under the same name to Tune. If this is a | ||
dict, each key will be the name reported to Tune and the respective | ||
value will be the metric key reported to PyTorch Lightning. | ||
on: When to trigger checkpoint creations. Must be one of | ||
filename: Filename of the checkpoint within the checkpoint | ||
directory. Defaults to "checkpoint". | ||
save_checkpoints: If True (default), checkpoints will be saved and | ||
reported to Ray. If False, only metrics will be reported. | ||
on: When to trigger checkpoint creations and metric reports. Must be one of | ||
the PyTorch Lightning event hooks (less the ``on_``), e.g. | ||
"train_batch_start", or "train_end". Defaults to "validation_end". | ||
|
||
|
||
Example: | ||
|
||
.. code-block:: python | ||
|
||
import pytorch_lightning as pl | ||
from ray.tune.integration.pytorch_lightning import TuneReportCallback | ||
from ray.tune.integration.pytorch_lightning import ( | ||
TuneReportCheckpointCallback) | ||
|
||
# Report loss and accuracy to Tune after each validation epoch: | ||
trainer = pl.Trainer(callbacks=[TuneReportCallback( | ||
["val_loss", "val_acc"], on="validation_end")]) | ||
# Save checkpoint after each training batch and after each | ||
# validation epoch. | ||
trainer = pl.Trainer(callbacks=[TuneReportCheckpointCallback( | ||
metrics={"loss": "val_loss", "mean_accuracy": "val_acc"}, | ||
filename="trainer.ckpt", on="validation_end")]) | ||
|
||
# Same as above, but report as `loss` and `mean_accuracy`: | ||
trainer = pl.Trainer(callbacks=[TuneReportCallback( | ||
{"loss": "val_loss", "mean_accuracy": "val_acc"}, | ||
on="validation_end")]) | ||
|
||
""" | ||
|
||
def __init__( | ||
self, | ||
metrics: Optional[Union[str, List[str], Dict[str, str]]] = None, | ||
filename: str = "checkpoint", | ||
save_checkpoints: bool = True, | ||
on: Union[str, List[str]] = "validation_end", | ||
): | ||
super(TuneReportCallback, self).__init__(on=on) | ||
super(TuneReportCheckpointCallback, self).__init__(on=on) | ||
if isinstance(metrics, str): | ||
metrics = [metrics] | ||
self._save_checkpoints = save_checkpoints | ||
self._filename = filename | ||
self._metrics = metrics | ||
|
||
def _get_report_dict(self, trainer: Trainer, pl_module: LightningModule): | ||
|
@@ -146,102 +154,58 @@ def _get_report_dict(self, trainer: Trainer, pl_module: LightningModule): | |
|
||
return report_dict | ||
|
||
def _handle(self, trainer: Trainer, pl_module: LightningModule): | ||
report_dict = self._get_report_dict(trainer, pl_module) | ||
if report_dict is not None: | ||
tune.report(**report_dict) | ||
|
||
|
||
class _TuneCheckpointCallback(TuneCallback): | ||
"""PyTorch Lightning checkpoint callback | ||
|
||
Saves checkpoints after each validation step. | ||
|
||
.. note:: | ||
In Ray 2.4, we introduced | ||
:class:`LightningTrainer <ray.train.lightning.LightningTrainer>`, | ||
which provides native integration with PyTorch Lightning. Here is | ||
:ref:`a simple example <lightning_mnist_example>` of how to use | ||
``LightningTrainer``. | ||
|
||
Checkpoint are currently not registered if no ``tune.report()`` call | ||
is made afterwards. Consider using ``TuneReportCheckpointCallback`` | ||
instead. | ||
|
||
Args: | ||
filename: Filename of the checkpoint within the checkpoint | ||
directory. Defaults to "checkpoint". | ||
on: When to trigger checkpoint creations. Must be one of | ||
the PyTorch Lightning event hooks (less the ``on_``), e.g. | ||
"train_batch_start", or "train_end". Defaults to "validation_end". | ||
@contextmanager | ||
def _get_checkpoint( | ||
self, trainer: Trainer | ||
) -> Optional[Union[Checkpoint, LegacyCheckpoint]]: | ||
if not self._save_checkpoints: | ||
yield None | ||
return | ||
|
||
with tempfile.TemporaryDirectory() as checkpoint_dir: | ||
trainer.save_checkpoint(os.path.join(checkpoint_dir, self._filename)) | ||
|
||
""" | ||
if _use_storage_context(): | ||
checkpoint = Checkpoint.from_directory(checkpoint_dir) | ||
else: | ||
checkpoint = LegacyCheckpoint.from_directory(checkpoint_dir) | ||
|
||
def __init__( | ||
self, filename: str = "checkpoint", on: Union[str, List[str]] = "validation_end" | ||
): | ||
super(_TuneCheckpointCallback, self).__init__(on) | ||
self._filename = filename | ||
yield checkpoint | ||
|
||
def _handle(self, trainer: Trainer, pl_module: LightningModule): | ||
if trainer.sanity_checking: | ||
return | ||
step = f"epoch={trainer.current_epoch}-step={trainer.global_step}" | ||
with tune.checkpoint_dir(step=step) as checkpoint_dir: | ||
trainer.save_checkpoint(os.path.join(checkpoint_dir, self._filename)) | ||
|
||
|
||
@PublicAPI | ||
class TuneReportCheckpointCallback(TuneCallback): | ||
"""PyTorch Lightning report and checkpoint callback | ||
|
||
Saves checkpoints after each validation step. Also reports metrics to Tune, | ||
which is needed for checkpoint registration. | ||
|
||
Args: | ||
metrics: Metrics to report to Tune. If this is a list, | ||
each item describes the metric key reported to PyTorch Lightning, | ||
and it will reported under the same name to Tune. If this is a | ||
dict, each key will be the name reported to Tune and the respective | ||
value will be the metric key reported to PyTorch Lightning. | ||
filename: Filename of the checkpoint within the checkpoint | ||
directory. Defaults to "checkpoint". | ||
on: When to trigger checkpoint creations. Must be one of | ||
the PyTorch Lightning event hooks (less the ``on_``), e.g. | ||
"train_batch_start", or "train_end". Defaults to "validation_end". | ||
|
||
report_dict = self._get_report_dict(trainer, pl_module) | ||
if not report_dict: | ||
return | ||
|
||
Example: | ||
|
||
.. code-block:: python | ||
|
||
import pytorch_lightning as pl | ||
from ray.tune.integration.pytorch_lightning import ( | ||
TuneReportCheckpointCallback) | ||
|
||
# Save checkpoint after each training batch and after each | ||
# validation epoch. | ||
trainer = pl.Trainer(callbacks=[TuneReportCheckpointCallback( | ||
metrics={"loss": "val_loss", "mean_accuracy": "val_acc"}, | ||
filename="trainer.ckpt", on="validation_end")]) | ||
with self._get_checkpoint(trainer) as checkpoint: | ||
train.report(report_dict, checkpoint=checkpoint) | ||
|
||
|
||
""" | ||
class _TuneCheckpointCallback(TuneCallback): | ||
def __init__(self, *args, **kwargs): | ||
raise DeprecationWarning( | ||
"`ray.tune.integration.pytorch_lightning._TuneCheckpointCallback` " | ||
"is deprecated." | ||
Comment on lines
+190
to
+191
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Use TuneReportCheckpointCallback instead |
||
) | ||
|
||
_checkpoint_callback_cls = _TuneCheckpointCallback | ||
_report_callbacks_cls = TuneReportCallback | ||
|
||
@Deprecated | ||
class TuneReportCallback(TuneReportCheckpointCallback): | ||
def __init__( | ||
self, | ||
metrics: Optional[Union[str, List[str], Dict[str, str]]] = None, | ||
filename: str = "checkpoint", | ||
on: Union[str, List[str]] = "validation_end", | ||
): | ||
super(TuneReportCheckpointCallback, self).__init__(on) | ||
self._checkpoint = self._checkpoint_callback_cls(filename, on) | ||
self._report = self._report_callbacks_cls(metrics, on) | ||
|
||
def _handle(self, trainer: Trainer, pl_module: LightningModule): | ||
self._checkpoint._handle(trainer, pl_module) | ||
self._report._handle(trainer, pl_module) | ||
if log_once("tune_ptl_report_deprecated"): | ||
warnings.warn( | ||
"`ray.tune.integration.pytorch_lightning.TuneReportCallback` " | ||
"is deprecated. Use " | ||
"`ray.tune.integration.pytorch_lightning.TuneCheckpointReportCallback`" | ||
" instead." | ||
matthewdeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
super(TuneReportCallback, self).__init__( | ||
metrics=metrics, save_checkpoints=False, on=on | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 can just use
train.Checkpoint
now -- the alias is now gated by the feature flag.