profiler.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # mypy: allow-untyped-defs
  2. import contextlib
  3. import torch
  4. __all__ = [
  5. "start",
  6. "stop",
  7. "profile",
  8. "metal_capture",
  9. "is_metal_capture_enabled",
  10. "is_capturing_metal",
  11. ]
  12. def start(mode: str = "interval", wait_until_completed: bool = False) -> None:
  13. r"""Start OS Signpost tracing from MPS backend.
  14. The generated OS Signposts could be recorded and viewed in
  15. XCode Instruments Logging tool.
  16. Args:
  17. mode(str): OS Signpost tracing mode could be "interval", "event",
  18. or both "interval,event".
  19. The interval mode traces the duration of execution of the operations,
  20. whereas event mode marks the completion of executions.
  21. See document `Recording Performance Data`_ for more info.
  22. wait_until_completed(bool): Waits until the MPS Stream complete
  23. executing each encoded GPU operation. This helps generating single
  24. dispatches on the trace's timeline.
  25. Note that enabling this option would affect the performance negatively.
  26. .. _Recording Performance Data:
  27. https://developer.apple.com/documentation/os/logging/recording_performance_data
  28. """
  29. mode_normalized = mode.lower().replace(" ", "")
  30. torch._C._mps_profilerStartTrace(mode_normalized, wait_until_completed)
  31. def stop():
  32. r"""Stops generating OS Signpost tracing from MPS backend."""
  33. torch._C._mps_profilerStopTrace()
  34. @contextlib.contextmanager
  35. def profile(mode: str = "interval", wait_until_completed: bool = False):
  36. r"""Context Manager to enabling generating OS Signpost tracing from MPS backend.
  37. Args:
  38. mode(str): OS Signpost tracing mode could be "interval", "event",
  39. or both "interval,event".
  40. The interval mode traces the duration of execution of the operations,
  41. whereas event mode marks the completion of executions.
  42. See document `Recording Performance Data`_ for more info.
  43. wait_until_completed(bool): Waits until the MPS Stream complete
  44. executing each encoded GPU operation. This helps generating single
  45. dispatches on the trace's timeline.
  46. Note that enabling this option would affect the performance negatively.
  47. .. _Recording Performance Data:
  48. https://developer.apple.com/documentation/os/logging/recording_performance_data
  49. """
  50. try:
  51. start(mode, wait_until_completed)
  52. yield
  53. finally:
  54. stop()
  55. def is_metal_capture_enabled() -> bool:
  56. """Checks if `metal_capture` context manager is usable
  57. To enable metal capture, set MTL_CAPTURE_ENABLED envvar
  58. """
  59. return torch._C._mps_isCaptureEnabled() # type: ignore[attr-defined]
  60. def is_capturing_metal() -> bool:
  61. """Checks if metal capture is in progress"""
  62. return torch._C._mps_isCapturing() # type: ignore[attr-defined]
  63. @contextlib.contextmanager
  64. def metal_capture(fname: str):
  65. """Context manager that enables capturing of Metal calls into gputrace"""
  66. try:
  67. torch._C._mps_startCapture(fname) # type: ignore[attr-defined]
  68. yield
  69. # Drain all the work that were enqueued during the context call
  70. torch.mps.synchronize()
  71. finally:
  72. torch._C._mps_stopCapture() # type: ignore[attr-defined]