2021-01-31 12:48:14 +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-02-17 23:38:39 +00:00
|
|
|
from typing import Dict
|
|
|
|
|
2021-01-31 12:48:14 +00:00
|
|
|
import torch
|
|
|
|
|
2021-12-22 20:23:30 +00:00
|
|
|
from pytorch_lightning.strategies.ddp import DDPStrategy
|
2021-06-01 11:51:50 +00:00
|
|
|
from pytorch_lightning.utilities.apply_func import apply_to_collection
|
|
|
|
from pytorch_lightning.utilities.types import _METRIC_COLLECTION
|
2021-01-31 12:48:14 +00:00
|
|
|
|
|
|
|
|
2021-12-21 19:21:00 +00:00
|
|
|
class DDP2Strategy(DDPStrategy):
|
2021-07-26 11:37:35 +00:00
|
|
|
"""DDP2 behaves like DP in one node, but synchronization across nodes behaves like in DDP."""
|
2021-01-31 12:48:14 +00:00
|
|
|
|
2022-02-17 23:38:39 +00:00
|
|
|
strategy_name = "ddp2"
|
2021-11-01 11:41:57 +00:00
|
|
|
|
2021-04-13 18:07:40 +00:00
|
|
|
@property
|
|
|
|
def global_rank(self) -> int:
|
|
|
|
return self.node_rank
|
|
|
|
|
|
|
|
@property
|
|
|
|
def world_size(self) -> int:
|
|
|
|
return self.num_nodes
|
|
|
|
|
2021-06-01 11:51:50 +00:00
|
|
|
def reduce(self, collection: _METRIC_COLLECTION, *args, **kwargs) -> _METRIC_COLLECTION:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Reduces a collection of tensors from all processes. It can be applied to just a single tensor. In DDP2,
|
|
|
|
the reduction here is only across local devices within the node.
|
2021-01-31 12:48:14 +00:00
|
|
|
|
2021-02-20 12:30:21 +00:00
|
|
|
Args:
|
2021-06-01 11:51:50 +00:00
|
|
|
collection: The collection of tensors to sync and reduce.
|
2021-02-20 12:30:21 +00:00
|
|
|
*args: ignored for DDP2
|
|
|
|
**kwargs: ignored for DDP2
|
2021-01-31 12:48:14 +00:00
|
|
|
|
2021-02-20 12:30:21 +00:00
|
|
|
Return:
|
2021-06-01 11:51:50 +00:00
|
|
|
Reduced tensor values or the same value if it was not or did not contain a tensor.
|
2021-02-20 12:30:21 +00:00
|
|
|
"""
|
|
|
|
|
2021-06-01 11:51:50 +00:00
|
|
|
def mean(t: torch.Tensor) -> torch.Tensor:
|
|
|
|
original_dtype = t.dtype
|
|
|
|
return t.float().mean().to(original_dtype)
|
2021-02-20 12:30:21 +00:00
|
|
|
|
2021-06-01 11:51:50 +00:00
|
|
|
return apply_to_collection(collection, torch.Tensor, mean)
|
2021-01-31 12:48:14 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def root_device(self):
|
|
|
|
return self.parallel_devices[0]
|
|
|
|
|
|
|
|
def model_to_device(self):
|
|
|
|
# no need to do anything when model is wrapped in torch.nn.DataParallel
|
|
|
|
pass
|
|
|
|
|
|
|
|
@property
|
|
|
|
def distributed_sampler_kwargs(self):
|
|
|
|
distributed_sampler_kwargs = dict(num_replicas=self.num_nodes, rank=self.global_rank)
|
|
|
|
return distributed_sampler_kwargs
|
|
|
|
|
2021-04-07 11:35:57 +00:00
|
|
|
@property
|
|
|
|
def _is_single_process_single_device(self) -> bool:
|
|
|
|
return False
|
|
|
|
|
2021-05-08 11:25:51 +00:00
|
|
|
def set_world_ranks(self) -> None:
|
|
|
|
if self.cluster_environment is None:
|
|
|
|
return
|
2021-04-13 18:07:40 +00:00
|
|
|
self.cluster_environment.set_global_rank(self.node_rank)
|
|
|
|
self.cluster_environment.set_world_size(self.num_nodes)
|
2022-02-17 23:38:39 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def register_strategies(cls, strategy_registry: Dict) -> None:
|
|
|
|
strategy_registry.register(
|
|
|
|
cls.strategy_name,
|
|
|
|
cls,
|
|
|
|
description=f"{cls.__class__.__name__}",
|
|
|
|
)
|