profiler.py 5.2 KB
Newer Older
1
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved
2 3 4 5 6 7 8 9 10 11 12 13 14
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

15 16 17 18 19 20
from __future__ import print_function

import sys
import warnings

from ..fluid import core
21 22 23 24 25
from ..fluid.profiler import cuda_profiler  # noqa: F401
from ..fluid.profiler import start_profiler
from ..fluid.profiler import profiler  # noqa: F401
from ..fluid.profiler import stop_profiler
from ..fluid.profiler import reset_profiler
26 27 28 29 30 31 32 33 34 35 36
from .deprecated import deprecated

__all__ = [  # noqa
    'Profiler',
    'get_profiler',
    'ProfilerOptions',
    'cuda_profiler',
    'start_profiler',
    'profiler',
    'stop_profiler',
    'reset_profiler',
37
]
38 39


40 41 42 43 44 45
@deprecated(
    since="2.4.2",
    update_to="paddle.profiler.Profiler",
    level=1,
    reason="Please use new profiler tool, this profiler tool is no longer maintained.",
)
46 47 48 49 50 51 52 53 54 55
class ProfilerOptions(object):
    def __init__(self, options=None):
        self.options = {
            'state': 'All',
            'sorted_key': 'default',
            'tracer_level': 'Default',
            'batch_range': [0, sys.maxsize],
            'output_thread_detail': False,
            'profile_path': 'none',
            'timeline_path': 'none',
56
            'op_summary_path': 'none',
57 58 59 60 61 62 63 64 65 66 67 68 69 70
        }
        if options is not None:
            for key in self.options.keys():
                if options.get(key, None) is not None:
                    self.options[key] = options[key]

    # function to set one specified option
    def with_state(self, state):
        self.options['state'] = state
        return self

    def __getitem__(self, name):
        if self.options.get(name, None) is None:
            raise ValueError(
71 72
                "ProfilerOptions does not have an option named %s." % name
            )
73
        else:
74 75 76 77
            if (
                isinstance(self.options[name], str)
                and self.options[name] == 'none'
            ):
78 79 80 81 82 83 84 85
                return None
            else:
                return self.options[name]


_current_profiler = None


86 87 88 89 90 91
@deprecated(
    since="2.4.2",
    update_to="paddle.profiler.Profiler",
    level=1,
    reason="Please use new profiler tool, this profiler tool is no longer maintained.",
)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
class Profiler(object):
    def __init__(self, enabled=True, options=None):
        if options is not None:
            self.profiler_options = options
        else:
            self.profiler_options = ProfilerOptions()
        self.batch_id = 0
        self.enabled = enabled

    def __enter__(self):
        # record current profiler
        global _current_profiler
        self.previous_profiler = _current_profiler
        _current_profiler = self

        if self.enabled:
            if self.profiler_options['batch_range'][0] == 0:
                self.start()
        return self

    def __exit__(self, exception_type, exception_value, traceback):
        global _current_profiler
        _current_profiler = self.previous_profiler

        if self.enabled:
            self.stop()

    def start(self):
        if self.enabled:
            try:
                start_profiler(
                    state=self.profiler_options['state'],
124 125
                    tracer_option=self.profiler_options['tracer_level'],
                )
126 127
            except Exception as e:
                warnings.warn(
128 129 130 131
                    "Profiler is not enabled becuase following exception:\n{}".format(
                        e
                    )
                )
132 133 134 135 136 137

    def stop(self):
        if self.enabled:
            try:
                stop_profiler(
                    sorted_key=self.profiler_options['sorted_key'],
138 139
                    profile_path=self.profiler_options['profile_path'],
                )
140 141
            except Exception as e:
                warnings.warn(
142 143 144 145
                    "Profiler is not disabled becuase following exception:\n{}".format(
                        e
                    )
                )
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    def reset(self):
        if self.enabled and core.is_profiler_enabled():
            reset_profiler()

    def record_step(self, change_profiler_status=True):
        if not self.enabled:
            return
        self.batch_id = self.batch_id + 1
        if change_profiler_status:
            if self.batch_id == self.profiler_options['batch_range'][0]:
                if core.is_profiler_enabled():
                    self.reset()
                else:
                    self.start()

            if self.batch_id == self.profiler_options['batch_range'][1]:
                self.stop()


166 167 168 169 170 171
@deprecated(
    since="2.4.2",
    update_to="paddle.profiler.Profiler",
    level=1,
    reason="Please use new profiler tool, this profiler tool is no longer maintained.",
)
172 173 174 175 176
def get_profiler():
    global _current_profiler
    if _current_profiler is None:
        _current_profiler = Profiler()
    return _current_profiler