MCPcopy Create free account
hub / github.com/alinlab/SelfPatch / SmoothedValue

Class SmoothedValue

utils.py:204–263  ·  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

202
203
204class SmoothedValue(object):
205 """Track a series of values and provide access to smoothed values over a
206 window or the global series average.
207 """
208
209 def __init__(self, window_size=20, fmt=None):
210 if fmt is None:
211 fmt = "{median:.6f} ({global_avg:.6f})"
212 self.deque = deque(maxlen=window_size)
213 self.total = 0.0
214 self.count = 0
215 self.fmt = fmt
216
217 def update(self, value, n=1):
218 self.deque.append(value)
219 self.count += n
220 self.total += value * n
221
222 def synchronize_between_processes(self):
223 """
224 Warning: does not synchronize the deque!
225 """
226 if not is_dist_avail_and_initialized():
227 return
228 t = torch.tensor([self.count, self.total], dtype=torch.float64, device='cuda')
229 dist.barrier()
230 dist.all_reduce(t)
231 t = t.tolist()
232 self.count = int(t[0])
233 self.total = t[1]
234
235 @property
236 def median(self):
237 d = torch.tensor(list(self.deque))
238 return d.median().item()
239
240 @property
241 def avg(self):
242 d = torch.tensor(list(self.deque), dtype=torch.float32)
243 return d.mean().item()
244
245 @property
246 def global_avg(self):
247 return self.total / self.count
248
249 @property
250 def max(self):
251 return max(self.deque)
252
253 @property
254 def value(self):
255 return self.deque[-1]
256
257 def __str__(self):
258 return self.fmt.format(
259 median=self.median,
260 avg=self.avg,
261 global_avg=self.global_avg,

Callers 1

log_everyMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected