Group of timers.
| 235 | |
| 236 | |
| 237 | class Timers: |
| 238 | """Group of timers.""" |
| 239 | |
| 240 | def __init__(self): |
| 241 | self.timers = {} |
| 242 | |
| 243 | def __call__(self, name): |
| 244 | if name not in self.timers: |
| 245 | self.timers[name] = _Timer(name) |
| 246 | return self.timers[name] |
| 247 | |
| 248 | def write(self, names, writer, iteration, normalizer=1.0, reset=False): |
| 249 | """Write timers to a tensorboard writer""" |
| 250 | # currently when using add_scalars, |
| 251 | # torch.utils.add_scalars makes each timer its own run, which |
| 252 | # polutes the runs list, so we just add each as a scalar |
| 253 | assert normalizer > 0.0 |
| 254 | for name in names: |
| 255 | value = self.timers[name].elapsed(reset=reset) / normalizer |
| 256 | writer.add_scalar(name + "-time", value, iteration) |
| 257 | |
| 258 | def log(self, names, normalizer=1.0, reset=True): |
| 259 | """Log a group of timers.""" |
| 260 | assert normalizer > 0.0 |
| 261 | string = "time (ms)" |
| 262 | for name in names: |
| 263 | elapsed_time = self.timers[name].elapsed(reset=reset) * 1000.0 / normalizer |
| 264 | string += " | {}: {:.2f}".format(name, elapsed_time) |
| 265 | if torch.distributed.is_initialized(): |
| 266 | if torch.distributed.get_rank() == (torch.distributed.get_world_size() - 1): |
| 267 | print(string, flush=True) |
| 268 | else: |
| 269 | print(string, flush=True) |