-
Notifications
You must be signed in to change notification settings - Fork 53
/
utils.py
53 lines (37 loc) · 1.13 KB
/
utils.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
# -*- coding: utf-8 -*-
"""
@File : utils.py
@Time : 2019-12-25 11:39
@Author : yizuotian
@Description :
"""
import torch
import torch.distributed as dist
def is_dist_avail_and_initialized():
if not dist.is_available():
return False
if not dist.is_initialized():
return False
return True
def get_world_size():
if not is_dist_avail_and_initialized():
return 1
return dist.get_world_size()
def get_rank():
if not is_dist_avail_and_initialized():
return 0
return dist.get_rank()
def is_main_process():
return get_rank() == 0
def save_on_master(*args, **kwargs):
if is_main_process():
torch.save(*args, **kwargs)
def _add_weight_history(writer, net, epoch):
for name, param in net.named_parameters():
writer.add_histogram(name, param.clone().cpu().data.numpy(), epoch)
def add_weight_history_on_master(writer, net, epoch):
if is_main_process():
_add_weight_history(writer, net, epoch)
def add_scalar_on_master(writer, tag, scalar_value, global_step):
if is_main_process():
writer.add_scalar(tag, scalar_value, global_step)