-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
88 lines (70 loc) · 2.5 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import random
import datetime
import os
import numpy as np
import torch
import torch.distributed as torchdist
class _Dist:
@staticmethod
def _run_dist() -> bool:
return "WORLD_SIZE" in os.environ
@staticmethod
def init():
"""Initialize distributed training env if launched as distributed"""
if _Dist._run_dist() and not torchdist.is_initialized():
torchdist.init_process_group(
torchdist.Backend.NCCL, timeout=datetime.timedelta(seconds=7200)
)
@staticmethod
def size() -> int:
return int(os.getenv("WORLD_SIZE", 1))
@staticmethod
def local_rank() -> int:
return int(os.getenv("LOCAL_RANK", 0))
@staticmethod
def rank() -> int:
return int(os.getenv("RANK", 0))
@staticmethod
def broadcast(tensor: torch.Tensor, src=0) -> torch.Tensor:
"""Broadcast tensor from rank 0 to all other ranks.
Args:
tensor: CUDA tensor to be broadcasted from rank 0
src: Source rank to broadcast the tensor from
Returns:
Broadcasted tensor
"""
if _Dist._run_dist():
torchdist.broadcast(tensor, src=src, group=torchdist.GroupMember.WORLD)
return tensor
@staticmethod
def allreduce(tensor: torch.Tensor) -> torch.Tensor:
"""All Reduce operation specifically for CUDA tensors.
Args:
tensor: CUDA tensor to be reduced
Returns:
Reduced tensor (average across all processes)
"""
if _Dist._run_dist():
torchdist.all_reduce(
tensor, op=torchdist.ReduceOp.SUM, group=torchdist.GroupMember.WORLD
)
tensor.div_(_Dist.size())
return tensor
# Create the namespace
dist = _Dist()
def dist_sync_grad(model: torch.nn.Module):
"""Average the gradients of a given model across all processes."""
params = [param for param in model.parameters()]
flat = torch.cat([param.grad.flatten() for param in params])
flat = dist.allreduce(flat)
flat = torch.nan_to_num(flat, nan=0.0, posinf=0.0, neginf=0.0)
grads = flat.split([param.numel() for param in params])
for param, grad in zip(params, grads):
param.grad = grad.reshape(param.shape)
#
def set_seed_all(seed=42, rank=0):
"""Sets the seed across numpy, torch and the built-in random module."""
seed = seed + rank * 1024
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)