File size: 2,506 Bytes
c61ccee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
r"""This package adds support for NVIDIA Tools Extension (NVTX) used in profiling."""

from contextlib import contextmanager

try:
    from torch._C import _nvtx
except ImportError:

    class _NVTXStub:
        @staticmethod
        def _fail(*args, **kwargs):
            raise RuntimeError(
                "NVTX functions not installed. Are you sure you have a CUDA build?"
            )

        rangePushA = _fail
        rangePop = _fail
        markA = _fail

    _nvtx = _NVTXStub()  # type: ignore[assignment]

__all__ = ["range_push", "range_pop", "range_start", "range_end", "mark", "range"]


def range_push(msg):
    """

    Push a range onto a stack of nested range span.  Returns zero-based depth of the range that is started.



    Args:

        msg (str): ASCII message to associate with range

    """
    return _nvtx.rangePushA(msg)


def range_pop():
    """Pop a range off of a stack of nested range spans.  Returns the  zero-based depth of the range that is ended."""
    return _nvtx.rangePop()


def range_start(msg) -> int:
    """

    Mark the start of a range with string message. It returns an unique handle

    for this range to pass to the corresponding call to rangeEnd().



    A key difference between this and range_push/range_pop is that the

    range_start/range_end version supports range across threads (start on one

    thread and end on another thread).



    Returns: A range handle (uint64_t) that can be passed to range_end().



    Args:

        msg (str): ASCII message to associate with the range.

    """
    return _nvtx.rangeStartA(msg)


def range_end(range_id) -> None:
    """

    Mark the end of a range for a given range_id.



    Args:

        range_id (int): an unique handle for the start range.

    """
    _nvtx.rangeEnd(range_id)


def mark(msg):
    """

    Describe an instantaneous event that occurred at some point.



    Args:

        msg (str): 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().



    Args:

        msg (str): message to associate with the range

    """
    range_push(msg.format(*args, **kwargs))
    try:
        yield
    finally:
        range_pop()