profiler.py 4.6 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
__all__ = [     #noqa
           'Profiler',
           'get_profiler',
           'ProfilerOptions',
           'cuda_profiler',
           'start_profiler',
           'profiler',
           'stop_profiler',
           'reset_profiler'
]
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 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148


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',
            'op_summary_path': 'none'
        }
        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(
                "ProfilerOptions does not have an option named %s." % name)
        else:
            if isinstance(self.options[name],
                          str) and self.options[name] == 'none':
                return None
            else:
                return self.options[name]


_current_profiler = None


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'],
                    tracer_option=self.profiler_options['tracer_level'])
            except Exception as e:
                warnings.warn(
                    "Profiler is not enabled becuase following exception:\n{}".
                    format(e))

    def stop(self):
        if self.enabled:
            try:
                stop_profiler(
                    sorted_key=self.profiler_options['sorted_key'],
                    profile_path=self.profiler_options['profile_path'])
            except Exception as e:
                warnings.warn(
                    "Profiler is not disabled becuase following exception:\n{}".
                    format(e))

    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()


def get_profiler():
    global _current_profiler
    if _current_profiler is None:
        _current_profiler = Profiler()
    return _current_profiler