Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
eaeb76c4
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
eaeb76c4
编写于
6月 01, 2018
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add some comments
上级
9c687a97
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
14 addition
and
11 deletion
+14
-11
paddle/fluid/inference/tests/book/test_inference_nlp.cc
paddle/fluid/inference/tests/book/test_inference_nlp.cc
+14
-11
未找到文件。
paddle/fluid/inference/tests/book/test_inference_nlp.cc
浏览文件 @
eaeb76c4
...
...
@@ -37,7 +37,8 @@ inline double GetCurrentMs() {
return
1e+3
*
time
.
tv_sec
+
1e-3
*
time
.
tv_usec
;
}
// return size of total words
// Load the input word index data from file and save into LodTensor.
// Return the size of words.
size_t
LoadData
(
std
::
vector
<
paddle
::
framework
::
LoDTensor
>*
out
,
const
std
::
string
&
filename
)
{
size_t
sz
=
0
;
...
...
@@ -67,6 +68,8 @@ size_t LoadData(std::vector<paddle::framework::LoDTensor>* out,
return
sz
;
}
// Split input data samples into small pieces jobs as balanced as possible,
// according to the number of threads.
void
SplitData
(
const
std
::
vector
<
paddle
::
framework
::
LoDTensor
>&
datasets
,
std
::
vector
<
std
::
vector
<
const
paddle
::
framework
::
LoDTensor
*>>*
jobs
,
...
...
@@ -116,7 +119,8 @@ void ThreadRunInfer(
for
(
size_t
i
=
0
;
i
<
inputs
.
size
();
++
i
)
{
feed_targets
[
feed_target_names
[
0
]]
=
inputs
[
i
];
executor
->
Run
(
*
copy_program
,
&
sub_scope
,
&
feed_targets
,
&
fetch_targets
,
true
,
true
,
feed_holder_name
,
fetch_holder_name
);
true
/*create_local_scope*/
,
true
/*create_vars*/
,
feed_holder_name
,
fetch_holder_name
);
}
auto
stop_ms
=
GetCurrentMs
();
scope
->
DeleteScope
(
&
sub_scope
);
...
...
@@ -143,12 +147,13 @@ TEST(inference, nlp) {
// 1. Define place, executor, scope
auto
place
=
paddle
::
platform
::
CPUPlace
();
auto
executor
=
paddle
::
framework
::
Executor
(
place
);
auto
*
scope
=
new
paddle
::
framework
::
Scope
();
std
::
unique_ptr
<
paddle
::
framework
::
Scope
>
scope
(
new
paddle
::
framework
::
Scope
());
// 2. Initialize the inference_program and load parameters
std
::
unique_ptr
<
paddle
::
framework
::
ProgramDesc
>
inference_program
;
inference_program
=
InitProgram
(
&
executor
,
scope
,
FLAGS_modelpath
,
model_combined
);
InitProgram
(
&
executor
,
scope
.
get
()
,
FLAGS_modelpath
,
model_combined
);
if
(
FLAGS_use_mkldnn
)
{
EnableMKLDNN
(
inference_program
);
}
...
...
@@ -166,9 +171,9 @@ TEST(inference, nlp) {
SplitData
(
datasets
,
&
jobs
,
FLAGS_num_threads
);
std
::
vector
<
std
::
unique_ptr
<
std
::
thread
>>
threads
;
for
(
int
i
=
0
;
i
<
FLAGS_num_threads
;
++
i
)
{
threads
.
emplace_back
(
new
std
::
thread
(
ThreadRunInfer
,
i
,
&
executor
,
scope
,
std
::
ref
(
inference_program
),
std
::
ref
(
jobs
)));
threads
.
emplace_back
(
new
std
::
thread
(
ThreadRunInfer
,
i
,
&
executor
,
scope
.
get
(
),
std
::
ref
(
inference_program
),
std
::
ref
(
jobs
)));
}
start_ms
=
GetCurrentMs
();
for
(
int
i
=
0
;
i
<
FLAGS_num_threads
;
++
i
)
{
...
...
@@ -177,7 +182,7 @@ TEST(inference, nlp) {
stop_ms
=
GetCurrentMs
();
}
else
{
if
(
FLAGS_prepare_vars
)
{
executor
.
CreateVariables
(
*
inference_program
,
scope
,
0
);
executor
.
CreateVariables
(
*
inference_program
,
scope
.
get
()
,
0
);
}
// always prepare context
std
::
unique_ptr
<
paddle
::
framework
::
ExecutorPrepareContext
>
ctx
;
...
...
@@ -201,7 +206,7 @@ TEST(inference, nlp) {
start_ms
=
GetCurrentMs
();
for
(
size_t
i
=
0
;
i
<
datasets
.
size
();
++
i
)
{
feed_targets
[
feed_target_names
[
0
]]
=
&
(
datasets
[
i
]);
executor
.
RunPreparedContext
(
ctx
.
get
(),
scope
,
&
feed_targets
,
executor
.
RunPreparedContext
(
ctx
.
get
(),
scope
.
get
()
,
&
feed_targets
,
&
fetch_targets
,
!
FLAGS_prepare_vars
);
}
stop_ms
=
GetCurrentMs
();
...
...
@@ -209,9 +214,7 @@ TEST(inference, nlp) {
<<
" samples, avg time per sample: "
<<
(
stop_ms
-
start_ms
)
/
datasets
.
size
()
<<
" ms"
;
}
LOG
(
INFO
)
<<
"Total inference time with "
<<
FLAGS_num_threads
<<
" threads : "
<<
(
stop_ms
-
start_ms
)
/
1000.0
<<
" sec, QPS: "
<<
datasets
.
size
()
/
((
stop_ms
-
start_ms
)
/
1000
);
delete
scope
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录