Skip to content

Commit

Permalink
Initial pull request proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
chrish42 committed Aug 12, 2020
1 parent bda0007 commit 352a19e
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion torch/cuda/nvtx.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from contextlib import contextmanager

try:
from torch._C import _nvtx
except ImportError:
Expand All @@ -12,7 +14,7 @@ def _fail(*args, **kwargs):

_nvtx = _NVTXStub()

__all__ = ['range_push', 'range_pop', 'mark']
__all__ = ['range_push', 'range_pop', 'mark', 'range']


def range_push(msg):
Expand Down Expand Up @@ -42,3 +44,18 @@ def mark(msg):
msg (string): ASCII message to associate with the event.
"""
return _nvtx.markA(msg)


@contextmanager
def range(msg, *args, **kwargs):
"""
Context manager / decorator that pushes an NVTX range at the beginning
of its scope, and pops it at the end. If extra arguments are given,
they are passed as arguments to msg.format().
Arguments:
msg (string): message to associate with the range
"""
range_push(msg.format(*args, **kwargs))
yield
range_pop()

0 comments on commit 352a19e

Please sign in to comment.