MCPcopy Create free account
hub / github.com/bic-L/MaxFormer / SmoothedValue

Class SmoothedValue

event/utils.py:11–70  ·  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

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

Callers 1

log_everyMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected