utils.py 5.9 KB
Newer Older
C
chenjian 已提交
1
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
C
chenjian 已提交
2
#
C
chenjian 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
C
chenjian 已提交
6
#
C
chenjian 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
C
chenjian 已提交
8
#
C
chenjian 已提交
9 10 11 12 13 14 15 16
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import functools
from contextlib import ContextDecorator
17 18
from typing import Any
from warnings import warn
C
chenjian 已提交
19

C
chenjian 已提交
20
from paddle.fluid import core
21
from paddle.fluid.core import TracerEventType, _RecordEvent
C
chenjian 已提交
22

23
_is_profiler_used = False
24
_has_optimizer_wrapped = False
25

C
chenjian 已提交
26
_AllowedEventTypeList = [
27 28 29 30 31 32 33
    TracerEventType.Dataloader,
    TracerEventType.ProfileStep,
    TracerEventType.Forward,
    TracerEventType.Backward,
    TracerEventType.Optimization,
    TracerEventType.PythonOp,
    TracerEventType.PythonUserDefined,
C
chenjian 已提交
34 35 36 37 38
]


class RecordEvent(ContextDecorator):
    r"""
C
chenjian 已提交
39
    Interface for recording a time range by user defined.
C
chenjian 已提交
40

C
chenjian 已提交
41
    Args:
42 43 44 45
        name (str): Name of the record event.
        event_type (TracerEventType, optional): Optional, default value is
            `TracerEventType.PythonUserDefined`. It is reserved for internal
            purpose, and it is better not to specify this parameter.
C
chenjian 已提交
46 47 48

    Examples:
        .. code-block:: python
C
chenjian 已提交
49
            :name: code-example1
C
chenjian 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

            import paddle
            import paddle.profiler as profiler
            # method1: using context manager
            with profiler.RecordEvent("record_add"):
                data1 = paddle.randn(shape=[3])
                data2 = paddle.randn(shape=[3])
                result = data1 + data2
            # method2: call begin() and end()
            record_event = profiler.RecordEvent("record_add")
            record_event.begin()
            data1 = paddle.randn(shape=[3])
            data2 = paddle.randn(shape=[3])
            result = data1 + data2
            record_event.end()

66
    Note:
67
        RecordEvent will take effect only when :ref:`Profiler <api_paddle_profiler_Profiler>` is on and at the state of `RECORD`.
C
chenjian 已提交
68 69
    """

70
    def __init__(
71 72 73 74
        self,
        name: str,
        event_type: TracerEventType = TracerEventType.PythonUserDefined,
    ):
C
chenjian 已提交
75 76 77 78 79 80 81 82 83 84 85 86
        self.name = name
        self.event_type = event_type
        self.event = None

    def __enter__(self):
        self.begin()
        return self

    def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any):
        self.end()

    def begin(self):
C
chenjian 已提交
87
        r"""
88
        Record the time of beginning.
C
chenjian 已提交
89

90
        Examples:
C
chenjian 已提交
91

92 93 94 95 96 97 98 99 100 101 102
            .. code-block:: python
                :name: code-example2

                import paddle
                import paddle.profiler as profiler
                record_event = profiler.RecordEvent("record_sub")
                record_event.begin()
                data1 = paddle.randn(shape=[3])
                data2 = paddle.randn(shape=[3])
                result = data1 - data2
                record_event.end()
C
chenjian 已提交
103
        """
104 105
        if not _is_profiler_used:
            return
C
chenjian 已提交
106
        if self.event_type not in _AllowedEventTypeList:
107 108 109 110 111 112
            warn(
                "Only TracerEvent Type in [{}, {}, {}, {}, {}, {},{}]\
                  can be recorded.".format(
                    *_AllowedEventTypeList
                )
            )
C
chenjian 已提交
113 114 115 116 117
            self.event = None
        else:
            self.event = _RecordEvent(self.name, self.event_type)

    def end(self):
C
chenjian 已提交
118 119 120
        r'''
        Record the time of ending.

121
        Examples:
C
chenjian 已提交
122

123 124 125 126 127 128 129 130 131 132 133
            .. code-block:: python
                :name: code-example3

                import paddle
                import paddle.profiler as profiler
                record_event = profiler.RecordEvent("record_mul")
                record_event.begin()
                data1 = paddle.randn(shape=[3])
                data2 = paddle.randn(shape=[3])
                result = data1 * data2
                record_event.end()
C
chenjian 已提交
134
        '''
C
chenjian 已提交
135 136 137 138
        if self.event:
            self.event.end()


C
chenjian 已提交
139 140 141 142
def load_profiler_result(filename: str):
    r"""
    Load dumped profiler data back to memory.

C
chenjian 已提交
143
    Args:
C
chenjian 已提交
144 145 146
        filename(str): Name of the exported protobuf file of profiler data.

    Returns:
147
        ``ProfilerResult`` object, which stores profiling data.
C
chenjian 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    Examples:
        .. code-block:: python

            # required: gpu
            import paddle.profiler as profiler
            with profiler.Profiler(
                    targets=[profiler.ProfilerTarget.CPU, profiler.ProfilerTarget.GPU],
                    scheduler = (3, 10)) as p:
                for iter in range(10):
                    #train()
                    p.step()
            p.export('test_export_protobuf.pb', format='pb')
            profiler_result = profiler.load_profiler_result('test_export_protobuf.pb')
    """
    return core.load_profiler_result(filename)


166
def in_profiler_mode():
167
    return _is_profiler_used
168 169


C
chenjian 已提交
170 171 172 173
def wrap_optimizers():
    def optimizer_warpper(func):
        @functools.wraps(func)
        def warpper(*args, **kwargs):
174
            if in_profiler_mode():
175 176 177
                with RecordEvent(
                    'Optimization Step', event_type=TracerEventType.Optimization
                ):
178 179
                    return func(*args, **kwargs)
            else:
C
chenjian 已提交
180 181 182 183
                return func(*args, **kwargs)

        return warpper

184
    global _has_optimizer_wrapped
185
    if _has_optimizer_wrapped:
186
        return
C
chenjian 已提交
187
    import paddle.optimizer as optimizer
188

C
chenjian 已提交
189 190 191
    for classname in optimizer.__all__:
        if classname != 'Optimizer':
            classobject = getattr(optimizer, classname)
192
            if getattr(classobject, 'step', None) is not None:
C
chenjian 已提交
193
                classobject.step = optimizer_warpper(classobject.step)
194
    _has_optimizer_wrapped = True