Repository navigation
Expand file tree
/
Copy pathprofiler.py
More file actions
194 lines (159 loc) · 6.32 KB
/
Copy pathprofiler.py
File metadata and controls
194 lines (159 loc) · 6.32 KB
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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================
"""TensorFlow 2.0 Profiler for both Eager Mode and Graph Mode.
The profiler has two mode:
- Programmatic Mode: start(), stop() and Profiler class. It will perform
when calling start() or create Profiler class and will stop
when calling stop() or destroying Profiler class.
- On-demand Mode: start_profiler_server(). It will perform profiling when
receive profiling request.
NOTE: Only one active profiler session is allowed. Use of simultaneous
Programmatic Mode and On-demand Mode is undefined and will likely fail.
NOTE: The Keras TensorBoard callback will automatically perform sampled
profiling. Before enabling customized profiling, set the callback flag
"profile_batches=[]" to disable automatic sampled profiling.
customized profiling.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import datetime
import os
import threading
from tensorflow.python import _pywrap_events_writer
from tensorflow.python.eager import context
from tensorflow.python.framework import errors
from tensorflow.python.platform import gfile
from tensorflow.python.platform import tf_logging as logging
from tensorflow.python.profiler.internal import _pywrap_profiler
from tensorflow.python.util import compat
from tensorflow.python.util.deprecation import deprecated
_profiler = None
_profiler_lock = threading.Lock()
_run_num = 0
# This suffix should be kept in sync with kProfileEmptySuffix in
# tensorflow/core/profiler/rpc/client/capture_profile.cc.
_EVENT_FILE_SUFFIX = '.profile-empty'
class ProfilerAlreadyRunningError(Exception):
pass
class ProfilerNotRunningError(Exception):
pass
@deprecated('2020-07-01', 'use `tf.profiler.experimental.start` instead.')
def start():
"""Start profiling.
Raises:
ProfilerAlreadyRunningError: If another profiling session is running.
"""
global _profiler
with _profiler_lock:
if _profiler is not None:
raise ProfilerAlreadyRunningError('Another profiler is running.')
if context.default_execution_mode == context.EAGER_MODE:
context.ensure_initialized()
_profiler = _pywrap_profiler.ProfilerSession()
try:
_profiler.start('', {})
except errors.AlreadyExistsError:
logging.warning('Another profiler session is running which is probably '
'created by profiler server. Please avoid using profiler '
'server and profiler APIs at the same time.')
raise ProfilerAlreadyRunningError('Another profiler is running.')
@deprecated('2020-07-01', 'use `tf.profiler.experimental.stop` instead.')
def stop():
"""Stop current profiling session and return its result.
Returns:
A binary string of tensorflow.tpu.Trace. User can write the string
to file for offline analysis by tensorboard.
Raises:
ProfilerNotRunningError: If there is no active profiling session.
"""
global _profiler
global _run_num
with _profiler_lock:
if _profiler is None:
raise ProfilerNotRunningError(
'Cannot stop profiling. No profiler is running.')
if context.default_execution_mode == context.EAGER_MODE:
context.context().executor.wait()
result = _profiler.stop()
_profiler = None
_run_num += 1
return result
@deprecated(
'2020-07-01',
'`tf.python.eager.profiler` has deprecated, use `tf.profiler` instead.'
)
def maybe_create_event_file(logdir):
"""Create an empty event file if not already exists.
This event file indicates that we have a plugins/profile/ directory in the
current logdir.
Args:
logdir: log directory.
"""
for file_name in gfile.ListDirectory(logdir):
if file_name.endswith(_EVENT_FILE_SUFFIX):
return
# TODO(b/127330388): Use summary_ops_v2.create_file_writer instead.
event_writer = _pywrap_events_writer.EventsWriter(
compat.as_bytes(os.path.join(logdir, 'events')))
event_writer.InitWithSuffix(compat.as_bytes(_EVENT_FILE_SUFFIX))
@deprecated(
'2020-07-01',
'`tf.python.eager.profiler` has deprecated, use `tf.profiler` instead.'
)
def save(logdir, result):
"""Save profile result to TensorBoard logdir.
Args:
logdir: log directory read by TensorBoard.
result: profiling result returned by stop().
"""
plugin_dir = os.path.join(
logdir, 'plugins', 'profile',
datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
gfile.MakeDirs(plugin_dir)
maybe_create_event_file(logdir)
with gfile.Open(os.path.join(plugin_dir, 'local.trace'), 'wb') as f:
f.write(result)
@deprecated('2020-07-01', 'use `tf.profiler.experimental.server.start`.')
def start_profiler_server(port):
"""Start a profiler grpc server that listens to given port.
The profiler server will keep the program running even the training finishes.
Please shutdown the server with CTRL-C. It can be used in both eager mode and
graph mode. The service defined in
tensorflow/core/profiler/profiler_service.proto. Please use
tensorflow/contrib/tpu/profiler/capture_tpu_profile to capture tracable
file following https://cloud.google.com/tpu/docs/cloud-tpu-tools#capture_trace
Args:
port: port profiler server listens to.
"""
if context.default_execution_mode == context.EAGER_MODE:
context.ensure_initialized()
_pywrap_profiler.start_server(port)
@deprecated('2020-07-01', 'use `tf.profiler.experimental.Profile` instead.')
class Profiler(object):
"""Context-manager eager profiler api.
Example usage:
```python
with Profiler("/path/to/logdir"):
# do some work
```
"""
def __init__(self, logdir):
self._logdir = logdir
def __enter__(self):
start()
def __exit__(self, typ, value, tb):
result = stop()
save(self._logdir, result)