MCPcopy Create free account
hub / github.com/csuhan/OneLLM / SmoothedValue

Class SmoothedValue

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

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

Callers 1

log_everyMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected