class_docstr: Timer for PyTorch code Comes in the form of a contextmanager:
  
  Example:
  >>> timer = Timer()
  ... for i in range(10):
  ...     with timer("expensive operation"):
  ...         x = torch.randn(100)
  ... print(timer.summary())
class_docstr_tok: ["Timer","for","PyTorch","code","Comes","in","the","form","of","a","contextmanager",":","Example",":",">",">",">","timer","=","Timer","(",")","...","for","i","in","range","(","10",")",":","...","with","timer","(","``","expensive","operation","''",")",":","...","x","=","torch.randn","(","100",")","...","print","(","timer.summary","(",")",")"]
code: def __call__(self, label, epoch=-1.0, verbosity=1):
      # Don't measure this if the verbosity level is too high
      if verbosity > self.verbosity_level:
          yield
          return
  
      # Measure the time
      self._cuda_sync()
      start = time.time()
      yield
      self._cuda_sync()
      end = time.time()
  
      # Update first and last occurrence of this label
      if label not in self.first_time:
          self.first_time[label] = start
      self.last_time[label] = end
  
      # Update the totals and call counts
      if label not in self.totals and self.skip_first:
          self.totals[label] = 0.0
          del self.first_time[label]
          self.call_counts[label] = 0
      elif label not in self.totals and not self.skip_first:
          self.totals[label] = end - start
          self.call_counts[label] = 1
      else:
          self.totals[label] += end - start
          self.call_counts[label] += 1
  
file_path: gluonts/src/gluonts/nursery/SCott/trainers/timer.py
imports: import time
  from contextlib import contextmanager
  from io import StringIO
  import numpy
  import torch
label_desc: Use image node_id 3 for calling the Timer obj's underlying member method code with example usage: obj.__call__(label, epoch, verbosity) without return types