MCPcopy Create free account
hub / github.com/LTH14/mar / SmoothedValue

Class SmoothedValue

util/misc.py:20–79  ·  view source on GitHub ↗

Track a series of values and provide access to smoothed values over a window or the global series average.

Source from the content-addressed store, hash-verified

18
19
20class SmoothedValue(object):
21 """Track a series of values and provide access to smoothed values over a
22 window or the global series average.
23 """
24
25 def __init__(self, window_size=20, fmt=None):
26 if fmt is None:
27 fmt = "{median:.4f} ({global_avg:.4f})"
28 self.deque = deque(maxlen=window_size)
29 self.total = 0.0
30 self.count = 0
31 self.fmt = fmt
32
33 def update(self, value, n=1):
34 self.deque.append(value)
35 self.count += n
36 self.total += value * n
37
38 def synchronize_between_processes(self):
39 """
40 Warning: does not synchronize the deque!
41 """
42 if not is_dist_avail_and_initialized():
43 return
44 t = torch.tensor([self.count, self.total], dtype=torch.float64, device='cuda')
45 dist.barrier()
46 dist.all_reduce(t)
47 t = t.tolist()
48 self.count = int(t[0])
49 self.total = t[1]
50
51 @property
52 def median(self):
53 d = torch.tensor(list(self.deque))
54 return d.median().item()
55
56 @property
57 def avg(self):
58 d = torch.tensor(list(self.deque), dtype=torch.float32)
59 return d.mean().item()
60
61 @property
62 def global_avg(self):
63 return self.total / self.count
64
65 @property
66 def max(self):
67 return max(self.deque)
68
69 @property
70 def value(self):
71 return self.deque[-1]
72
73 def __str__(self):
74 return self.fmt.format(
75 median=self.median,
76 avg=self.avg,
77 global_avg=self.global_avg,

Callers 1

log_everyMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected