Skip to content

Commit e274c44

Browse files
mmglovezhengya01mmglove
authored
【benchmark】fix new profiler (#6089)
* fix benchmark * fix static gpt benchmark * fix benchmark,add run_mode * fix benchmark * add benchmark model rpoch * add benchmark model epoch * fix benchmark_log * fix benchmark log * fix static benchmark * fix benchmark * fix benchmark * update benchmark model name * fix profiler * fix --------- Co-authored-by: zhengya01 <[email protected]> Co-authored-by: mmglove <[email protected]>
1 parent eef98b6 commit e274c44

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

paddlenlp/utils/profiler.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
# limitations under the License.
1414

1515
import sys
16-
import paddle
16+
17+
import paddle.profiler as profiler
1718

1819
# A global variable to record the number of calling times for profiler
1920
# functions. It is used to specify the tracing range of training steps.
2021
_profiler_step_id = 0
2122

2223
# A global variable to avoid parsing from string every time.
2324
_profiler_options = None
25+
_prof = None
2426

2527

2628
class ProfilerOptions(object):
@@ -54,6 +56,7 @@ def __init__(self, options_str):
5456
"tracer_option": "Default",
5557
"profile_path": "/tmp/profile",
5658
"exit_on_finished": True,
59+
"timer_only": True,
5760
}
5861
self._parse_from_string(options_str)
5962

@@ -69,6 +72,8 @@ def _parse_from_string(self, options_str):
6972
self._options[key] = value.lower() in ("yes", "true", "t", "1")
7073
elif key in ["state", "sorted_key", "tracer_option", "profile_path"]:
7174
self._options[key] = value
75+
elif key == "timer_only":
76+
self._options[key] = value
7277

7378
def __getitem__(self, name):
7479
if self._options.get(name, None) is None:
@@ -81,24 +86,38 @@ def add_profiler_step(options_str=None):
8186
Enable the operator-level timing using PaddlePaddle's profiler.
8287
The profiler uses a independent variable to count the profiler steps.
8388
One call of this function is treated as a profiler step.
84-
8589
Args:
8690
profiler_options - a string to initialize the ProfilerOptions.
8791
Default is None, and the profiler is disabled.
8892
"""
8993
if options_str is None:
9094
return
9195

96+
global _prof
9297
global _profiler_step_id
9398
global _profiler_options
9499

95100
if _profiler_options is None:
96101
_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
102121
if _profiler_options["exit_on_finished"]:
103122
sys.exit(0)
104123

tests/test_tipc/benchmark_train.sh

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ repo_name=$(get_repo_name )
159159
SAVE_LOG=${BENCHMARK_LOG_DIR:-$(pwd)} # */benchmark_log
160160
mkdir -p "${SAVE_LOG}/benchmark_log/"
161161
status_log="${SAVE_LOG}/benchmark_log/results.log"
162+
# get benchmark profiling params : PROFILING_TIMER_ONLY=no|True|False
163+
PROFILING_TIMER_ONLY=${PROFILING_TIMER_ONLY:-"True"}
162164

163165
# The number of lines in which train params can be replaced.
164166
line_python=3
@@ -243,19 +245,26 @@ for batch_size in ${batch_size_list[*]}; do
243245
fi
244246

245247
if [ ${#gpu_id} -le 1 ];then
246-
log_path="$SAVE_LOG/profiling_log"
247-
mkdir -p $log_path
248-
log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_${to_static}profiling"
249-
func_sed_params "$FILENAME" "${line_gpuid}" "0" # sed used gpu_id
250-
# set profile_option params
251-
tmp=`sed -i "${line_profile}s/.*/${profile_option}/" "${FILENAME}"`
252-
253-
# run test_train_inference_python.sh
254-
cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} benchmark_train > ${log_path}/${log_name} 2>&1 "
255-
echo $cmd
256-
eval $cmd
257-
eval "cat ${log_path}/${log_name}"
258-
248+
func_sed_params "$FILENAME" "${line_gpuid}" "0" # sed used gpu_id
249+
if [[ ${PROFILING_TIMER_ONLY} != "no" ]];then
250+
echo "run profile"
251+
# The default value of profile_option's timer_only parameter is True
252+
if [[ ${PROFILING_TIMER_ONLY} = "False" ]];then
253+
profile_option="${profile_option};timer_only=False"
254+
fi
255+
log_path="$SAVE_LOG/profiling_log"
256+
mkdir -p $log_path
257+
log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_${to_static}profiling"
258+
# set profile_option params
259+
tmp=`sed -i "${line_profile}s/.*/\"${profile_option}\"/" "${FILENAME}"`
260+
261+
# run test_train_inference_python.sh
262+
cmd="timeout 5m bash test_tipc/test_train_inference_python.sh ${FILENAME} benchmark_train > ${log_path}/${log_name} 2>&1 "
263+
echo $cmd
264+
eval ${cmd}
265+
eval "cat ${log_path}/${log_name}"
266+
fi
267+
echo "run without profile"
259268
# without profile
260269
log_path="$SAVE_LOG/train_log"
261270
speed_log_path="$SAVE_LOG/index"

0 commit comments

Comments
 (0)