-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathddp.py
More file actions
33 lines (23 loc) · 676 Bytes
/
Copy pathddp.py
File metadata and controls
33 lines (23 loc) · 676 Bytes
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
from functools import lru_cache
from typing import Optional
import torch.distributed as dist
from torch import nn
def convert2syncBN(network: nn.Module):
return nn.SyncBatchNorm.convert_sync_batchnorm(network)
def disable_output():
def print_pass(*args):
pass
import builtins
builtins.print = print_pass
class DDPMixin:
@property
@lru_cache()
def rank(self) -> Optional[int]:
try:
return dist.get_rank()
except (AssertionError, AttributeError, RuntimeError):
return None
@property
@lru_cache()
def on_master(self) -> bool:
return (self.rank == 0) or (self.rank is None)