13
13
# limitations under the License.
14
14
15
15
import sys
16
- import paddle
16
+
17
+ import paddle .profiler as profiler
17
18
18
19
# A global variable to record the number of calling times for profiler
19
20
# functions. It is used to specify the tracing range of training steps.
20
21
_profiler_step_id = 0
21
22
22
23
# A global variable to avoid parsing from string every time.
23
24
_profiler_options = None
25
+ _prof = None
24
26
25
27
26
28
class ProfilerOptions (object ):
@@ -54,6 +56,7 @@ def __init__(self, options_str):
54
56
"tracer_option" : "Default" ,
55
57
"profile_path" : "/tmp/profile" ,
56
58
"exit_on_finished" : True ,
59
+ "timer_only" : True ,
57
60
}
58
61
self ._parse_from_string (options_str )
59
62
@@ -69,6 +72,8 @@ def _parse_from_string(self, options_str):
69
72
self ._options [key ] = value .lower () in ("yes" , "true" , "t" , "1" )
70
73
elif key in ["state" , "sorted_key" , "tracer_option" , "profile_path" ]:
71
74
self ._options [key ] = value
75
+ elif key == "timer_only" :
76
+ self ._options [key ] = value
72
77
73
78
def __getitem__ (self , name ):
74
79
if self ._options .get (name , None ) is None :
@@ -81,24 +86,38 @@ def add_profiler_step(options_str=None):
81
86
Enable the operator-level timing using PaddlePaddle's profiler.
82
87
The profiler uses a independent variable to count the profiler steps.
83
88
One call of this function is treated as a profiler step.
84
-
85
89
Args:
86
90
profiler_options - a string to initialize the ProfilerOptions.
87
91
Default is None, and the profiler is disabled.
88
92
"""
89
93
if options_str is None :
90
94
return
91
95
96
+ global _prof
92
97
global _profiler_step_id
93
98
global _profiler_options
94
99
95
100
if _profiler_options is None :
96
101
_profiler_options = ProfilerOptions (options_str )
97
-
98
- if _profiler_step_id == _profiler_options ["batch_range" ][0 ]:
99
- paddle .utils .profiler .start_profiler (_profiler_options ["state" ], _profiler_options ["tracer_option" ])
100
- elif _profiler_step_id == _profiler_options ["batch_range" ][1 ]:
101
- paddle .utils .profiler .stop_profiler (_profiler_options ["sorted_key" ], _profiler_options ["profile_path" ])
102
+ # profile : https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/performance_improving/profiling_model.html#chakanxingnengshujudetongjibiaodan
103
+ # timer_only = True only the model's throughput and time overhead are displayed
104
+ # timer_only = False calling summary can print a statistical form that presents performance data from different perspectives.
105
+ # timer_only = False the output Timeline information can be found in the profiler_log directory
106
+ if _prof is None :
107
+ _timer_only = str (_profiler_options ["timer_only" ]) == str (True )
108
+ _prof = profiler .Profiler (
109
+ scheduler = (_profiler_options ["batch_range" ][0 ], _profiler_options ["batch_range" ][1 ]),
110
+ on_trace_ready = profiler .export_chrome_tracing ("./profiler_log" ),
111
+ timer_only = _timer_only ,
112
+ )
113
+ _prof .start ()
114
+ else :
115
+ _prof .step ()
116
+
117
+ if _profiler_step_id == _profiler_options ["batch_range" ][1 ]:
118
+ _prof .stop ()
119
+ _prof .summary (op_detail = True , thread_sep = False , time_unit = "ms" )
120
+ _prof = None
102
121
if _profiler_options ["exit_on_finished" ]:
103
122
sys .exit (0 )
104
123
0 commit comments