MCPcopy Create free account
hub / github.com/Alpha-VLLM/LLaMA2-Accessory / SmoothedValue

Class SmoothedValue

accessory/util/misc.py:149–208  ·  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

147
148
149class SmoothedValue(object):
150 """Track a series of values and provide access to smoothed values over a
151 window or the global series average.
152 """
153
154 def __init__(self, window_size=1000, fmt=None):
155 if fmt is None:
156 fmt = "{median:.4f} ({avg:.4f})"
157 self.deque = deque(maxlen=window_size)
158 self.total = 0.0
159 self.count = 0
160 self.fmt = fmt
161
162 def update(self, value, n=1):
163 self.deque.append(value)
164 self.count += n
165 self.total += value * n
166
167 def synchronize_between_processes(self):
168 """
169 Warning: does not synchronize the deque!
170 """
171 if not is_dist_avail_and_initialized():
172 return
173 t = torch.tensor([self.count, self.total], dtype=torch.float64, device='cuda')
174 dist.barrier()
175 dist.all_reduce(t)
176 t = t.tolist()
177 self.count = int(t[0])
178 self.total = t[1]
179
180 @property
181 def median(self):
182 d = torch.tensor(list(self.deque))
183 return d.median().item()
184
185 @property
186 def avg(self):
187 d = torch.tensor(list(self.deque), dtype=torch.float32)
188 return d.mean().item()
189
190 @property
191 def global_avg(self):
192 return self.total / self.count
193
194 @property
195 def max(self):
196 return max(self.deque)
197
198 @property
199 def value(self):
200 return self.deque[-1]
201
202 def __str__(self):
203 return self.fmt.format(
204 median=self.median,
205 avg=self.avg,
206 global_avg=self.global_avg,

Callers 1

log_everyMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected