MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / SmoothedValue

Class SmoothedValue

SwissArmyTransformer/examples/mae/util/misc.py:24–83  ·  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

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

Callers 1

log_everyMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected