未验证 提交 d3f95e5a 编写于 作者: C chenjian 提交者: GitHub

reduce performance influence by RecordEvent in Python (#41822)

* reduce performance influence

* add unit test

* fix
上级 c9f4fcf3
...@@ -20,6 +20,7 @@ import tempfile ...@@ -20,6 +20,7 @@ import tempfile
import paddle import paddle
import paddle.profiler as profiler import paddle.profiler as profiler
import paddle.profiler.utils as utils
import paddle.nn as nn import paddle.nn as nn
import paddle.nn.functional as F import paddle.nn.functional as F
from paddle.io import Dataset, DataLoader from paddle.io import Dataset, DataLoader
...@@ -40,11 +41,17 @@ class TestProfiler(unittest.TestCase): ...@@ -40,11 +41,17 @@ class TestProfiler(unittest.TestCase):
with profiler.Profiler(targets=[profiler.ProfilerTarget.CPU], ) as prof: with profiler.Profiler(targets=[profiler.ProfilerTarget.CPU], ) as prof:
y = x / 2.0 y = x / 2.0
prof = None prof = None
self.assertEqual(utils._is_profiler_used, False)
with profiler.RecordEvent(name='test'):
y = x / 2.0
with profiler.Profiler( with profiler.Profiler(
targets=[profiler.ProfilerTarget.CPU], targets=[profiler.ProfilerTarget.CPU],
scheduler=(1, 2)) as prof: scheduler=(1, 2)) as prof:
self.assertEqual(utils._is_profiler_used, True)
with profiler.RecordEvent(name='test'): with profiler.RecordEvent(name='test'):
y = x / 2.0 y = x / 2.0
prof = None prof = None
with profiler.Profiler( with profiler.Profiler(
targets=[profiler.ProfilerTarget.CPU], targets=[profiler.ProfilerTarget.CPU],
......
...@@ -27,6 +27,7 @@ from paddle.fluid.core import (_Profiler, _ProfilerResult, ProfilerOptions, ...@@ -27,6 +27,7 @@ from paddle.fluid.core import (_Profiler, _ProfilerResult, ProfilerOptions,
from .utils import RecordEvent, wrap_optimizers from .utils import RecordEvent, wrap_optimizers
from .profiler_statistic import StatisticData, _build_table, SortedKeys from .profiler_statistic import StatisticData, _build_table, SortedKeys
from paddle.profiler import utils
from .timer import benchmark from .timer import benchmark
...@@ -482,6 +483,7 @@ class Profiler: ...@@ -482,6 +483,7 @@ class Profiler:
if self.timer_only: if self.timer_only:
return return
# CLOSED -> self.current_state # CLOSED -> self.current_state
utils._is_profiler_used = True
if self.current_state == ProfilerState.READY: if self.current_state == ProfilerState.READY:
self.profiler.prepare() self.profiler.prepare()
elif self.current_state == ProfilerState.RECORD: elif self.current_state == ProfilerState.RECORD:
...@@ -534,6 +536,7 @@ class Profiler: ...@@ -534,6 +536,7 @@ class Profiler:
self.profiler_result = self.profiler.stop() self.profiler_result = self.profiler.stop()
if self.on_trace_ready: if self.on_trace_ready:
self.on_trace_ready(self) self.on_trace_ready(self)
utils._is_profiler_used = False
def step(self, num_samples: Optional[int]=None): def step(self, num_samples: Optional[int]=None):
r""" r"""
......
...@@ -20,6 +20,8 @@ from contextlib import ContextDecorator ...@@ -20,6 +20,8 @@ from contextlib import ContextDecorator
from paddle.fluid import core from paddle.fluid import core
from paddle.fluid.core import (_RecordEvent, TracerEventType) from paddle.fluid.core import (_RecordEvent, TracerEventType)
_is_profiler_used = False
_AllowedEventTypeList = [ _AllowedEventTypeList = [
TracerEventType.Dataloader, TracerEventType.ProfileStep, TracerEventType.Dataloader, TracerEventType.ProfileStep,
TracerEventType.UserDefined, TracerEventType.Forward, TracerEventType.UserDefined, TracerEventType.Forward,
...@@ -91,6 +93,8 @@ class RecordEvent(ContextDecorator): ...@@ -91,6 +93,8 @@ class RecordEvent(ContextDecorator):
result = data1 - data2 result = data1 - data2
record_event.end() record_event.end()
""" """
if not _is_profiler_used:
return
if self.event_type not in _AllowedEventTypeList: if self.event_type not in _AllowedEventTypeList:
warn("Only TracerEvent Type in [{}, {}, {}, {}, {}, {},{}]\ warn("Only TracerEvent Type in [{}, {}, {}, {}, {}, {},{}]\
can be recorded.".format(*_AllowedEventTypeList)) can be recorded.".format(*_AllowedEventTypeList))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册