MCPcopy Create free account
hub / github.com/MotrixLab/AiOS / SmoothedValue

Class SmoothedValue

util/misc.py:51–111  ·  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

49
50
51class SmoothedValue(object):
52 """Track a series of values and provide access to smoothed values over a
53 window or the global series average."""
54 def __init__(self, window_size=20, fmt=None):
55 if fmt is None:
56 fmt = '{median:.4f} ({global_avg:.4f})'
57 self.deque = deque(maxlen=window_size)
58 self.total = 0.0
59 self.count = 0
60 self.fmt = fmt
61
62 def update(self, value, n=1):
63 self.deque.append(value)
64 self.count += n
65 self.total += value * n
66
67 def synchronize_between_processes(self):
68 """
69 Warning: does not synchronize the deque!
70 """
71 if not is_dist_avail_and_initialized():
72 return
73 t = torch.tensor([self.count, self.total],
74 dtype=torch.float64,
75 device='cuda')
76 dist.barrier()
77 dist.all_reduce(t)
78 t = t.tolist()
79 self.count = int(t[0])
80 self.total = t[1]
81
82 @property
83 def median(self):
84 d = torch.tensor(list(self.deque))
85 if d.shape[0] == 0:
86 return 0
87 return d.median().item()
88
89 @property
90 def avg(self):
91 d = torch.tensor(list(self.deque), dtype=torch.float32)
92 return d.mean().item()
93
94 @property
95 def global_avg(self):
96 return self.total / self.count
97
98 @property
99 def max(self):
100 return max(self.deque)
101
102 @property
103 def value(self):
104 return self.deque[-1]
105
106 def __str__(self):
107 return self.fmt.format(median=self.median,
108 avg=self.avg,

Callers 1

log_everyMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected