2021-02-26 10:09:08 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2022-01-12 19:44:51 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Any
|
2021-02-01 13:34:59 +00:00
|
|
|
|
|
|
|
import torch
|
|
|
|
|
2021-11-30 08:31:23 +00:00
|
|
|
import pytorch_lightning as pl
|
2021-08-13 16:35:31 +00:00
|
|
|
from pytorch_lightning.plugins.io.checkpoint_plugin import CheckpointIO
|
2021-11-19 00:39:01 +00:00
|
|
|
from pytorch_lightning.plugins.precision import PrecisionPlugin
|
2021-12-23 14:01:23 +00:00
|
|
|
from pytorch_lightning.strategies.strategy import Strategy
|
2021-05-22 20:19:24 +00:00
|
|
|
from pytorch_lightning.utilities import _XLA_AVAILABLE
|
2022-01-12 19:44:51 +00:00
|
|
|
from pytorch_lightning.utilities.types import _DEVICE
|
2021-02-01 13:34:59 +00:00
|
|
|
|
|
|
|
|
2021-12-21 23:56:14 +00:00
|
|
|
class SingleDeviceStrategy(Strategy):
|
|
|
|
"""Strategy that handles communication on a single device."""
|
2021-02-01 13:34:59 +00:00
|
|
|
|
2021-08-13 16:35:31 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-01-12 19:44:51 +00:00
|
|
|
device: _DEVICE,
|
|
|
|
accelerator: pl.accelerators.accelerator.Accelerator | None = None,
|
|
|
|
checkpoint_io: CheckpointIO | None = None,
|
|
|
|
precision_plugin: PrecisionPlugin | None = None,
|
2021-08-13 16:35:31 +00:00
|
|
|
):
|
2021-12-16 04:41:34 +00:00
|
|
|
super().__init__(accelerator=accelerator, checkpoint_io=checkpoint_io, precision_plugin=precision_plugin)
|
2022-01-12 19:44:51 +00:00
|
|
|
self._root_device = torch.device(device)
|
2021-03-14 17:14:27 +00:00
|
|
|
self.global_rank = 0
|
|
|
|
self.local_rank = 0
|
|
|
|
self.world_size = 1
|
2021-02-01 13:34:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def on_tpu(self) -> bool:
|
2021-05-22 20:19:24 +00:00
|
|
|
return self.root_device.type == "xla" and _XLA_AVAILABLE
|
2021-02-01 13:34:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def on_gpu(self) -> bool:
|
2021-05-22 20:19:24 +00:00
|
|
|
return self.root_device.type == "cuda" and torch.cuda.is_available()
|
2021-02-01 13:34:59 +00:00
|
|
|
|
2022-01-12 19:44:51 +00:00
|
|
|
def reduce(self, tensor: Any | torch.Tensor, *args: Any, **kwargs: Any) -> Any | torch.Tensor:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Reduces a tensor from several distributed processes to one aggregated tensor. As this plugin only
|
|
|
|
operates with a single device, the reduction is simply the identity.
|
2021-02-20 12:30:21 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
tensor: the tensor to sync and reduce
|
|
|
|
*args: ignored
|
|
|
|
**kwargs: ignored
|
|
|
|
|
|
|
|
Return:
|
|
|
|
the unmodified input as reduction is not needed for single process operation
|
|
|
|
"""
|
|
|
|
return tensor
|
2021-02-01 13:34:59 +00:00
|
|
|
|
2022-01-12 19:44:51 +00:00
|
|
|
def all_gather(self, tensor: torch.Tensor, group: Any | None = None, sync_grads: bool = False) -> torch.Tensor:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Perform a all_gather on all processes."""
|
2021-03-14 17:14:27 +00:00
|
|
|
return tensor
|
|
|
|
|
2021-02-01 13:34:59 +00:00
|
|
|
@property
|
|
|
|
def root_device(self) -> torch.device:
|
2022-01-12 19:44:51 +00:00
|
|
|
return self._root_device
|
2021-02-01 13:34:59 +00:00
|
|
|
|
|
|
|
def model_to_device(self) -> None:
|
2021-12-15 15:37:21 +00:00
|
|
|
self.model.to(self.root_device)
|
2021-02-01 13:34:59 +00:00
|
|
|
|
2022-01-12 19:44:51 +00:00
|
|
|
def setup(self, trainer: pl.Trainer) -> None:
|
2021-02-01 13:34:59 +00:00
|
|
|
self.model_to_device()
|
2021-11-30 08:31:23 +00:00
|
|
|
super().setup(trainer)
|
2021-02-01 13:34:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_global_zero(self) -> bool:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def barrier(self, *args, **kwargs) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def broadcast(self, obj: object, src: int = 0) -> object:
|
|
|
|
return obj
|
2021-05-22 20:19:24 +00:00
|
|
|
|
|
|
|
def teardown(self) -> None:
|
2021-12-22 03:47:14 +00:00
|
|
|
super().teardown()
|
2021-05-22 20:19:24 +00:00
|
|
|
if self.on_gpu:
|
|
|
|
# GPU teardown
|
|
|
|
self.lightning_module.cpu()
|
|
|
|
# clean up memory
|
2021-06-30 11:04:24 +00:00
|
|
|
torch.cuda.empty_cache()
|