|
|
|
@ -4,6 +4,7 @@ import pickle |
|
|
|
|
import warnings |
|
|
|
|
from collections import OrderedDict |
|
|
|
|
|
|
|
|
|
import numpy as np |
|
|
|
|
import torch |
|
|
|
|
import torch.distributed as dist |
|
|
|
|
from mmcv.runner import OptimizerHook, get_dist_info |
|
|
|
@ -151,3 +152,36 @@ def all_reduce_dict(py_dict, op='sum', group=None, to_float=True): |
|
|
|
|
if isinstance(py_dict, OrderedDict): |
|
|
|
|
out_dict = OrderedDict(out_dict) |
|
|
|
|
return out_dict |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sync_random_seed(seed=None, device='cuda'): |
|
|
|
|
"""Make sure different ranks share the same seed. |
|
|
|
|
|
|
|
|
|
All workers must call this function, otherwise it will deadlock. |
|
|
|
|
This method is generally used in `DistributedSampler`, |
|
|
|
|
because the seed should be identical across all processes |
|
|
|
|
in the distributed group. |
|
|
|
|
|
|
|
|
|
Args: |
|
|
|
|
seed (int, Optional): The seed. Default to None. |
|
|
|
|
device (str): The device where the seed will be put on. |
|
|
|
|
Default to 'cuda'. |
|
|
|
|
|
|
|
|
|
Returns: |
|
|
|
|
int: Seed to be used. |
|
|
|
|
""" |
|
|
|
|
if seed is None: |
|
|
|
|
seed = np.random.randint(2**31) |
|
|
|
|
assert isinstance(seed, int) |
|
|
|
|
|
|
|
|
|
rank, world_size = get_dist_info() |
|
|
|
|
|
|
|
|
|
if world_size == 1: |
|
|
|
|
return seed |
|
|
|
|
|
|
|
|
|
if rank == 0: |
|
|
|
|
random_num = torch.tensor(seed, dtype=torch.int32, device=device) |
|
|
|
|
else: |
|
|
|
|
random_num = torch.tensor(0, dtype=torch.int32, device=device) |
|
|
|
|
dist.broadcast(random_num, src=0) |
|
|
|
|
return random_num.item() |
|
|
|
|