Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
71b35779
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
337
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
71b35779
编写于
1月 10, 2020
作者:
J
juncaipeng
提交者:
GitHub
1月 10, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update benchmark (#2746)
* update benchmark, test=develop
上级
e5c62f96
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
93 addition
and
47 deletion
+93
-47
lite/api/benchmark.cc
lite/api/benchmark.cc
+69
-38
lite/tools/benchmark.sh
lite/tools/benchmark.sh
+24
-9
未找到文件。
lite/api/benchmark.cc
浏览文件 @
71b35779
...
...
@@ -13,32 +13,61 @@
// limitations under the License.
#include <gflags/gflags.h>
#include <sys/time.h>
#include <time.h>
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <numeric>
#include <string>
#include <vector>
#include "lite/api/paddle_api.h"
#include "lite/api/paddle_use_kernels.h"
#include "lite/api/paddle_use_ops.h"
#include "lite/api/paddle_use_passes.h"
#include "lite/api/test_helper.h"
#include "lite/core/device_info.h"
#include "lite/utils/cp_logging.h"
#include "lite/utils/string.h"
DEFINE_string
(
model_dir
,
""
,
"model dir"
);
DEFINE_string
(
input_shape
,
"1,3,224,224"
,
"input shapes, separated by colon and comma"
);
DEFINE_string
(
result_filename
,
""
,
"save test result"
);
"set input shapes according to the model, "
"separated by colon and comma, "
"such as 1,3,244,244:1,3,300,300."
);
DEFINE_int32
(
warmup
,
0
,
"warmup times"
);
DEFINE_int32
(
repeats
,
1
,
"repeats times"
);
DEFINE_int32
(
power_mode
,
3
,
"arm power mode: "
"0 for big cluster, "
"1 for little cluster, "
"2 for all cores, "
"3 for no bind"
);
DEFINE_int32
(
threads
,
1
,
"threads num"
);
DEFINE_string
(
result_filename
,
"result.txt"
,
"save benchmark "
"result to the file"
);
DEFINE_bool
(
run_model_optimize
,
false
,
"if set true, apply model_optimize_tool to model, use optimized "
"model to test"
);
DEFINE_bool
(
is_quantized_model
,
false
,
"if set true, test the quantized model"
);
"if set true, apply model_optimize_tool to "
"model and use optimized model to test. "
);
DEFINE_bool
(
is_quantized_model
,
false
,
"if set true, "
"test the performance of the quantized model. "
);
namespace
paddle
{
namespace
lite_api
{
inline
double
GetCurrentUS
()
{
struct
timeval
time
;
gettimeofday
(
&
time
,
NULL
);
return
1e+6
*
time
.
tv_sec
+
time
.
tv_usec
;
}
void
OutputOptModel
(
const
std
::
string
&
load_model_dir
,
const
std
::
string
&
save_optimized_model_dir
,
const
std
::
vector
<
std
::
vector
<
int64_t
>>&
input_shapes
)
{
...
...
@@ -58,7 +87,7 @@ void OutputOptModel(const std::string& load_model_dir,
paddle
::
lite
::
string_format
(
"rm -rf %s"
,
save_optimized_model_dir
.
c_str
())
.
c_str
());
if
(
ret
==
0
)
{
LOG
(
INFO
)
<<
"
d
elete old optimized model "
<<
save_optimized_model_dir
;
LOG
(
INFO
)
<<
"
D
elete old optimized model "
<<
save_optimized_model_dir
;
}
predictor
->
SaveOptimizedModel
(
save_optimized_model_dir
,
LiteModelType
::
kNaiveBuffer
);
...
...
@@ -69,23 +98,22 @@ void OutputOptModel(const std::string& load_model_dir,
#ifdef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
void
Run
(
const
std
::
vector
<
std
::
vector
<
int64_t
>>&
input_shapes
,
const
std
::
string
&
model_dir
,
const
int
repeat
,
const
int
thread_num
,
const
int
warmup_times
,
const
std
::
string
model_name
)
{
// set config and create predictor
lite_api
::
MobileConfig
config
;
config
.
set_threads
(
thread_num
);
config
.
set_power_mode
(
LITE_POWER_NO_BIND
);
config
.
set_threads
(
FLAGS_threads
);
config
.
set_power_mode
(
static_cast
<
PowerMode
>
(
FLAGS_power_mode
)
);
config
.
set_model_dir
(
model_dir
);
auto
predictor
=
lite_api
::
CreatePaddlePredictor
(
config
);
// set input
for
(
int
j
=
0
;
j
<
input_shapes
.
size
();
++
j
)
{
auto
input_tensor
=
predictor
->
GetInput
(
j
);
input_tensor
->
Resize
(
input_shapes
[
j
]);
auto
input_data
=
input_tensor
->
mutable_data
<
float
>
();
int
input_num
=
1
;
for
(
in
t
i
=
0
;
i
<
input_shapes
[
j
].
size
();
++
i
)
{
for
(
size_
t
i
=
0
;
i
<
input_shapes
[
j
].
size
();
++
i
)
{
input_num
*=
input_shapes
[
j
][
i
];
}
for
(
int
i
=
0
;
i
<
input_num
;
++
i
)
{
...
...
@@ -93,26 +121,36 @@ void Run(const std::vector<std::vector<int64_t>>& input_shapes,
}
}
for
(
int
i
=
0
;
i
<
warmup_times
;
++
i
)
{
// warmup
for
(
int
i
=
0
;
i
<
FLAGS_warmup
;
++
i
)
{
predictor
->
Run
();
}
auto
start
=
lite
::
GetCurrentUS
();
for
(
int
i
=
0
;
i
<
repeat
;
++
i
)
{
// run
std
::
vector
<
float
>
perf_vct
;
for
(
int
i
=
0
;
i
<
FLAGS_repeats
;
++
i
)
{
auto
start
=
GetCurrentUS
();
predictor
->
Run
();
auto
end
=
GetCurrentUS
();
perf_vct
.
push_back
((
end
-
start
)
/
1000.0
);
}
auto
end
=
lite
::
GetCurrentUS
();
std
::
FILE
*
pf
=
std
::
fopen
(
FLAGS_result_filename
.
c_str
(),
"a"
);
if
(
nullptr
==
pf
)
{
LOG
(
INFO
)
<<
"create result file error"
;
exit
(
0
);
std
::
sort
(
perf_vct
.
begin
(),
perf_vct
.
end
());
float
min_res
=
perf_vct
.
back
();
float
max_res
=
perf_vct
.
front
();
float
total_res
=
accumulate
(
perf_vct
.
begin
(),
perf_vct
.
end
(),
0.0
);
float
avg_res
=
total_res
/
FLAGS_repeats
;
// save result
std
::
ofstream
ofs
(
FLAGS_result_filename
,
std
::
ios
::
app
);
if
(
!
ofs
.
is_open
())
{
LOG
(
FATAL
)
<<
"open result file failed"
;
}
fprintf
(
pf
,
"-- %-18s avg = %5.4f ms
\n
"
,
model_name
.
c_str
(),
(
end
-
start
)
/
repeat
/
1000.0
);
std
::
fclose
(
pf
);
ofs
.
precision
(
5
);
ofs
<<
std
::
setw
(
20
)
<<
std
::
fixed
<<
std
::
left
<<
model_name
;
ofs
<<
"min = "
<<
std
::
setw
(
12
)
<<
min_res
;
ofs
<<
"max = "
<<
std
::
setw
(
12
)
<<
max_res
;
ofs
<<
"average = "
<<
std
::
setw
(
12
)
<<
avg_res
;
ofs
<<
std
::
endl
;
}
#endif
...
...
@@ -122,9 +160,7 @@ void Run(const std::vector<std::vector<int64_t>>& input_shapes,
int
main
(
int
argc
,
char
**
argv
)
{
gflags
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
if
(
FLAGS_model_dir
==
""
||
FLAGS_result_filename
==
""
)
{
LOG
(
INFO
)
<<
"usage: "
<<
"--model_dir /path/to/your/model --result_filename "
"/path/to/resultfile"
;
LOG
(
INFO
)
<<
"please run ./benchmark_bin --help to obtain usage."
;
exit
(
0
);
}
...
...
@@ -166,11 +202,11 @@ int main(int argc, char** argv) {
std
::
vector
<
std
::
string
>
str_input_shapes
=
split_string
(
FLAGS_input_shape
);
std
::
vector
<
std
::
vector
<
int64_t
>>
input_shapes
;
for
(
in
t
i
=
0
;
i
<
str_input_shapes
.
size
();
++
i
)
{
for
(
size_
t
i
=
0
;
i
<
str_input_shapes
.
size
();
++
i
)
{
input_shapes
.
push_back
(
get_shape
(
str_input_shapes
[
i
]));
}
// Output optimized model
// Output optimized model
if needed
if
(
FLAGS_run_model_optimize
)
{
paddle
::
lite_api
::
OutputOptModel
(
FLAGS_model_dir
,
save_optimized_model_dir
,
input_shapes
);
...
...
@@ -180,12 +216,7 @@ int main(int argc, char** argv) {
// Run inference using optimized model
std
::
string
run_model_dir
=
FLAGS_run_model_optimize
?
save_optimized_model_dir
:
FLAGS_model_dir
;
paddle
::
lite_api
::
Run
(
input_shapes
,
run_model_dir
,
FLAGS_repeats
,
FLAGS_threads
,
FLAGS_warmup
,
model_name
);
paddle
::
lite_api
::
Run
(
input_shapes
,
run_model_dir
,
model_name
);
#endif
return
0
;
}
lite/tools/benchmark.sh
浏览文件 @
71b35779
...
...
@@ -2,13 +2,12 @@
set
-e
# Check input
if
[
$#
-lt
3
]
;
if
[
$#
-lt
2
]
;
then
echo
"Input error"
echo
"Usage:"
echo
" sh benchmark.sh <benchmark_bin_path> <benchmark_models_path> <result_filename>"
echo
" sh benchmark.sh <benchmark_bin_path> <benchmark_models_path> <result_filename> <is_run_model_optimize: [true|false]>"
echo
" sh benchmark.sh <benchmark_bin_path> <benchmark_models_path> <result_filename> <is_run_model_optimize: [true|false]> <is_run_quantized_model: [trur|false]>"
echo
" sh benchmark.sh benchmark_bin_path benchmark_models_path <result_filename> <input_shape> <power_mode: [0|1|2|3]> <is_run_model_optimize: [true|false]> <is_run_quantized_model: [trur|false]>"
echo
"
\n
power_mode refer: 0 for big cluster, 1 for little cluster, 2 for all cores, 3 for no bind."
exit
fi
...
...
@@ -16,8 +15,10 @@ fi
ANDROID_DIR
=
/data/local/tmp
BENCHMARK_BIN
=
$1
MODELS_DIR
=
$2
RESULT_FILENAME
=
$3
RESULT_FILENAME
=
result.txt
INPUT_SHAPE
=
1,3,244,244
POWER_MODE
=
3
WARMUP
=
10
REPEATS
=
30
IS_RUN_MODEL_OPTIMIZE
=
false
...
...
@@ -26,13 +27,25 @@ NUM_THREADS_LIST=(1 2 4)
MODELS_LIST
=
$(
ls
$MODELS_DIR
)
# Check input
if
[
$#
-gt
2
]
;
then
RESULT_FILENAME
=
$3
fi
if
[
$#
-gt
3
]
;
then
I
S_RUN_MODEL_OPTIMIZ
E
=
$4
I
NPUT_SHAP
E
=
$4
fi
if
[
$#
-gt
4
]
;
then
IS_RUN_QUANTIZED_MODEL
=
$5
POWER_MODE
=
$5
fi
if
[
$#
-gt
5
]
;
then
IS_RUN_MODEL_OPTIMIZE
=
$6
fi
if
[
$#
-gt
6
]
;
then
IS_RUN_QUANTIZED_MODEL
=
$7
fi
# Adb push benchmark_bin, models
...
...
@@ -41,16 +54,18 @@ adb shell chmod +x $ANDROID_DIR/benchmark_bin
adb push
$MODELS_DIR
$ANDROID_DIR
# Run benchmark
adb shell
"echo 'PaddleLite Benchmark' >
$ANDROID_DIR
/
$RESULT_FILENAME
"
adb shell
"echo 'PaddleLite Benchmark
(in ms)
\n
' >
$ANDROID_DIR
/
$RESULT_FILENAME
"
for
threads
in
${
NUM_THREADS_LIST
[@]
}
;
do
adb shell
"echo
Threads=
$threads
Warmup=
$WARMUP
Repeats=
$REPEATS
>>
$ANDROID_DIR
/
$RESULT_FILENAME
"
adb shell
"echo
threads=
$threads
warmup=
$WARMUP
repeats=
$REPEATS
input_shape=
$INPUT_SHAPE
power_mode=
$POWER_MODE
>>
$ANDROID_DIR
/
$RESULT_FILENAME
"
for
model_name
in
${
MODELS_LIST
[@]
}
;
do
echo
"Model=
$model_name
Threads=
$threads
"
adb shell
"
$ANDROID_DIR
/benchmark_bin
\
--model_dir=
$ANDROID_DIR
/
${
MODELS_DIR
}
/
$model_name
\
--input_shape=
$INPUT_SHAPE
\
--warmup=
$WARMUP
\
--repeats=
$REPEATS
\
--threads=
$threads
\
--power_mode=
$POWER_MODE
\
--result_filename=
$ANDROID_DIR
/
$RESULT_FILENAME
\
--run_model_optimize=
$IS_RUN_MODEL_OPTIMIZE
\
--is_quantized_model=
$IS_RUN_QUANTIZED_MODEL
"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录