_stats.py 837 B

12345678910111213141516171819202122
  1. # mypy: allow-untyped-defs
  2. # NOTE! PLEASE KEEP THIS FILE *FREE* OF TORCH DEPS! IT SHOULD BE IMPORTABLE ANYWHERE.
  3. # IF YOU FEEL AN OVERWHELMING URGE TO ADD A TORCH DEP, MAKE A TRAMPOLINE FILE A LA torch._dynamo.utils
  4. # AND SCRUB AWAY TORCH NOTIONS THERE.
  5. import collections
  6. import functools
  7. from typing import OrderedDict
  8. simple_call_counter: OrderedDict[str, int] = collections.OrderedDict()
  9. def count_label(label):
  10. prev = simple_call_counter.setdefault(label, 0)
  11. simple_call_counter[label] = prev + 1
  12. def count(fn):
  13. @functools.wraps(fn)
  14. def wrapper(*args, **kwargs):
  15. if fn.__qualname__ not in simple_call_counter:
  16. simple_call_counter[fn.__qualname__] = 0
  17. simple_call_counter[fn.__qualname__] = simple_call_counter[fn.__qualname__] + 1
  18. return fn(*args, **kwargs)
  19. return wrapper