MCPcopy
hub / github.com/microsoft/Cream / SmoothedValue

Class SmoothedValue

EfficientViT/classification/utils.py:15–75  ·  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

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

Callers 1

log_everyMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected