Computes and stores the average and current value
| 16 | |
| 17 | |
| 18 | class AverageMeter: |
| 19 | """Computes and stores the average and current value""" |
| 20 | |
| 21 | def __init__(self): |
| 22 | self._use_gpu = get_dist_backend() == 'nccl' |
| 23 | self.reset() |
| 24 | |
| 25 | def reset(self): |
| 26 | # local |
| 27 | self._val = 0 |
| 28 | self._sum = 0 |
| 29 | self._count = 0 |
| 30 | # global |
| 31 | self._history_avg = 0 |
| 32 | self._history_count = 0 |
| 33 | self._avg = None |
| 34 | |
| 35 | def update(self, val, n=1): |
| 36 | self._val = val |
| 37 | self._sum += val * n |
| 38 | self._count += n |
| 39 | self._avg = None |
| 40 | |
| 41 | @property |
| 42 | def val(self): |
| 43 | return self._val |
| 44 | |
| 45 | @property |
| 46 | def count(self): |
| 47 | return self._count + self._history_count |
| 48 | |
| 49 | @property |
| 50 | def avg(self): |
| 51 | if self._avg is None: |
| 52 | # compute avg |
| 53 | r = self._history_count / max(1, self._history_count + self._count) |
| 54 | _avg = self._sum / max(1, self._count) |
| 55 | self._avg = r * self._history_avg + (1.0 - r) * _avg |
| 56 | return self._avg |
| 57 | |
| 58 | def sync(self): |
| 59 | buf = torch.tensor([self._sum, self._count], |
| 60 | dtype=torch.float32) |
| 61 | if self._use_gpu: |
| 62 | buf = buf.cuda() |
| 63 | dist.all_reduce(buf, op=dist.ReduceOp.SUM) |
| 64 | _sum, _count = buf.tolist() |
| 65 | _avg = _sum / max(1, _count) |
| 66 | r = self._history_count / max(1, self._history_count + _count) |
| 67 | |
| 68 | self._history_avg = r * self._history_avg + (1.0 - r) * _avg |
| 69 | self._history_count += _count |
| 70 | |
| 71 | self._sum = 0 |
| 72 | self._count = 0 |
| 73 | |
| 74 | self._avg = None |
no outgoing calls
no test coverage detected