MCPcopy
hub / github.com/hustvl/Vim / SmoothedValue

Class SmoothedValue

vim/utils.py:18–77  ·  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

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

Callers 1

log_everyMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected