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

Add assertions to TPU accelerator for device availability #11799

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added a `_Stateful` support for `LightningDataModule` ([#11637](https://github.com/PyTorchLightning/pytorch-lightning/pull/11637))


- Added checks to `TPUAccelerator.setup_environment` to assert device availability ([#11799](https://github.com/PyTorchLightning/pytorch-lightning/pull/11799))
Copy link
Contributor

@rohitgr7 rohitgr7 Feb 8, 2022

Choose a reason for hiding this comment

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

Suggested change
- Added checks to `TPUAccelerator.setup_environment` to assert device availability ([#11799](https://github.com/PyTorchLightning/pytorch-lightning/pull/11799))
- Added checks to `TPUAccelerator` to assert TPU device availability ([#11799](https://github.com/PyTorchLightning/pytorch-lightning/pull/11799))



### Changed

- Implemented a new native and rich format in `_print_results` method of the `EvaluationLoop` ([#11332](https://github.com/PyTorchLightning/pytorch-lightning/pull/11332))
Expand Down
12 changes: 11 additions & 1 deletion pytorch_lightning/accelerators/tpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import torch

from pytorch_lightning.accelerators.accelerator import Accelerator
from pytorch_lightning.utilities import _XLA_AVAILABLE
from pytorch_lightning.utilities.exceptions import MisconfigurationException
from pytorch_lightning.utilities.imports import _XLA_AVAILABLE

if _XLA_AVAILABLE:
import torch_xla.core.xla_model as xm
Expand All @@ -25,6 +26,15 @@
class TPUAccelerator(Accelerator):
"""Accelerator for TPU devices."""

def __init__(self) -> None:
"""
Raises:
MisconfigurationException:
If the TPU device is not available.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
If the TPU device is not available.
If the `torch_xla` is not installed.

"""
if not _XLA_AVAILABLE:
Copy link
Contributor

Choose a reason for hiding this comment

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

should we add another one for TPU_AVAILABLE?
#3104

raise MisconfigurationException("The TPU Accelerator requires torch_xla and a TPU device to run.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
raise MisconfigurationException("The TPU Accelerator requires torch_xla and a TPU device to run.")
raise MisconfigurationException("The TPU Accelerator requires the `torch_xla` library and `TPU devices` to run.")


def get_device_stats(self, device: Union[str, torch.device]) -> Dict[str, Any]:
"""Gets stats for the given TPU device.

Expand Down