profiler.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # mypy: allow-untyped-defs
  2. import contextlib
  3. import torch
  4. __all__ = ["start", "stop", "profile"]
  5. def start(mode: str = "interval", wait_until_completed: bool = False) -> None:
  6. r"""Start OS Signpost tracing from MPS backend.
  7. The generated OS Signposts could be recorded and viewed in
  8. XCode Instruments Logging tool.
  9. Args:
  10. mode(str): OS Signpost tracing mode could be "interval", "event",
  11. or both "interval,event".
  12. The interval mode traces the duration of execution of the operations,
  13. whereas event mode marks the completion of executions.
  14. See document `Recording Performance Data`_ for more info.
  15. wait_until_completed(bool): Waits until the MPS Stream complete
  16. executing each encoded GPU operation. This helps generating single
  17. dispatches on the trace's timeline.
  18. Note that enabling this option would affect the performance negatively.
  19. .. _Recording Performance Data:
  20. https://developer.apple.com/documentation/os/logging/recording_performance_data
  21. """
  22. mode_normalized = mode.lower().replace(" ", "")
  23. torch._C._mps_profilerStartTrace(mode_normalized, wait_until_completed)
  24. def stop():
  25. r"""Stops generating OS Signpost tracing from MPS backend."""
  26. torch._C._mps_profilerStopTrace()
  27. @contextlib.contextmanager
  28. def profile(mode: str = "interval", wait_until_completed: bool = False):
  29. r"""Context Manager to enabling generating OS Signpost tracing from MPS backend.
  30. Args:
  31. mode(str): OS Signpost tracing mode could be "interval", "event",
  32. or both "interval,event".
  33. The interval mode traces the duration of execution of the operations,
  34. whereas event mode marks the completion of executions.
  35. See document `Recording Performance Data`_ for more info.
  36. wait_until_completed(bool): Waits until the MPS Stream complete
  37. executing each encoded GPU operation. This helps generating single
  38. dispatches on the trace's timeline.
  39. Note that enabling this option would affect the performance negatively.
  40. .. _Recording Performance Data:
  41. https://developer.apple.com/documentation/os/logging/recording_performance_data
  42. """
  43. try:
  44. start(mode, wait_until_completed)
  45. yield
  46. finally:
  47. stop()