Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
4a24c238
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
提交
4a24c238
编写于
6月 01, 2018
作者:
T
tensor-tang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine code
上级
a4822ed8
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
42 addition
and
49 deletion
+42
-49
paddle/fluid/inference/io.cc
paddle/fluid/inference/io.cc
+1
-1
paddle/fluid/inference/tests/book/test_inference_nlp.cc
paddle/fluid/inference/tests/book/test_inference_nlp.cc
+41
-45
paddle/fluid/inference/tests/test_helper.h
paddle/fluid/inference/tests/test_helper.h
+0
-3
未找到文件。
paddle/fluid/inference/io.cc
浏览文件 @
4a24c238
...
...
@@ -117,7 +117,7 @@ std::unique_ptr<framework::ProgramDesc> Load(framework::Executor* executor,
std
::
string
program_desc_str
;
VLOG
(
3
)
<<
"loading model from "
<<
model_filename
;
ReadBinaryFile
(
model_filename
,
&
program_desc_str
);
// LOG(INFO) << program_desc_str;
std
::
unique_ptr
<
framework
::
ProgramDesc
>
main_program
(
new
framework
::
ProgramDesc
(
program_desc_str
));
...
...
paddle/fluid/inference/tests/book/test_inference_nlp.cc
浏览文件 @
4a24c238
...
...
@@ -24,22 +24,21 @@ limitations under the License. */
#include <omp.h>
#endif
DEFINE_string
(
dirname
,
""
,
"Directory of the inference model."
);
DEFINE_string
(
modelpath
,
""
,
"Directory of the inference model."
);
DEFINE_string
(
datafile
,
""
,
"File of input index data."
);
DEFINE_int32
(
repeat
,
100
,
"Running the inference program repeat times"
);
DEFINE_bool
(
use_mkldnn
,
false
,
"Use MKLDNN to run inference"
);
DEFINE_bool
(
prepare_vars
,
true
,
"Prepare variables before executor"
);
DEFINE_bool
(
prepare_context
,
true
,
"Prepare Context before executor"
);
DEFINE_int32
(
num_threads
,
1
,
"Number of threads should be used"
);
inline
double
get_current_m
s
()
{
inline
double
GetCurrentM
s
()
{
struct
timeval
time
;
gettimeofday
(
&
time
,
NULL
);
return
1e+3
*
time
.
tv_sec
+
1e-3
*
time
.
tv_usec
;
}
// return size of total words
size_t
read_datasets
(
std
::
vector
<
paddle
::
framework
::
LoDTensor
>*
out
,
size_t
LoadData
(
std
::
vector
<
paddle
::
framework
::
LoDTensor
>*
out
,
const
std
::
string
&
filename
)
{
size_t
sz
=
0
;
std
::
fstream
fin
(
filename
);
...
...
@@ -68,6 +67,23 @@ size_t read_datasets(std::vector<paddle::framework::LoDTensor>* out,
return
sz
;
}
void
SplitData
(
const
std
::
vector
<
paddle
::
framework
::
LoDTensor
>&
datasets
,
std
::
vector
<
std
::
vector
<
const
paddle
::
framework
::
LoDTensor
*>>*
jobs
,
const
int
num_threads
)
{
size_t
s
=
0
;
jobs
->
resize
(
num_threads
);
while
(
s
<
datasets
.
size
())
{
for
(
auto
it
=
jobs
->
begin
();
it
!=
jobs
->
end
();
it
++
)
{
it
->
emplace_back
(
&
datasets
[
s
]);
s
++
;
if
(
s
>=
datasets
.
size
())
{
break
;
}
}
}
}
void
ThreadRunInfer
(
const
int
tid
,
paddle
::
framework
::
Executor
*
executor
,
paddle
::
framework
::
Scope
*
scope
,
...
...
@@ -80,7 +96,6 @@ void ThreadRunInfer(
copy_program
->
SetFeedHolderName
(
feed_holder_name
);
copy_program
->
SetFetchHolderName
(
fetch_holder_name
);
// 3. Get the feed_target_names and fetch_target_names
const
std
::
vector
<
std
::
string
>&
feed_target_names
=
copy_program
->
GetFeedTargetNames
();
const
std
::
vector
<
std
::
string
>&
fetch_target_names
=
...
...
@@ -95,51 +110,32 @@ void ThreadRunInfer(
PADDLE_ENFORCE_EQ
(
feed_target_names
.
size
(),
1UL
);
auto
&
inputs
=
jobs
[
tid
];
auto
start_ms
=
get_current_m
s
();
auto
start_ms
=
GetCurrentM
s
();
for
(
size_t
i
=
0
;
i
<
inputs
.
size
();
++
i
)
{
feed_targets
[
feed_target_names
[
0
]]
=
inputs
[
i
];
executor
->
Run
(
*
copy_program
,
scope
,
&
feed_targets
,
&
fetch_targets
,
true
,
true
,
feed_holder_name
,
fetch_holder_name
);
}
auto
stop_ms
=
get_current_m
s
();
auto
stop_ms
=
GetCurrentM
s
();
LOG
(
INFO
)
<<
"Tid: "
<<
tid
<<
", process "
<<
inputs
.
size
()
<<
" samples, avg time per sample: "
<<
(
stop_ms
-
start_ms
)
/
inputs
.
size
()
<<
" ms"
;
}
void
bcast_datasets
(
const
std
::
vector
<
paddle
::
framework
::
LoDTensor
>&
datasets
,
std
::
vector
<
std
::
vector
<
const
paddle
::
framework
::
LoDTensor
*>>*
jobs
,
const
int
num_threads
)
{
size_t
s
=
0
;
jobs
->
resize
(
num_threads
);
while
(
s
<
datasets
.
size
())
{
for
(
auto
it
=
jobs
->
begin
();
it
!=
jobs
->
end
();
it
++
)
{
it
->
emplace_back
(
&
datasets
[
s
]);
s
++
;
if
(
s
>=
datasets
.
size
())
{
break
;
}
}
}
}
TEST
(
inference
,
nlp
)
{
if
(
FLAGS_dirname
.
empty
())
{
LOG
(
FATAL
)
<<
"Usage: ./example --dirname=path/to/your/model"
;
if
(
FLAGS_modelpath
.
empty
()
||
FLAGS_datafile
.
empty
())
{
LOG
(
FATAL
)
<<
"Usage: ./example --modelpath=path/to/your/model "
<<
"--datafile=path/to/your/data"
;
}
LOG
(
INFO
)
<<
"
FLAGS_dirname: "
<<
FLAGS_dirname
<<
std
::
endl
;
std
::
string
dirname
=
FLAGS_dirnam
e
;
LOG
(
INFO
)
<<
"
Model Path: "
<<
FLAGS_modelpath
;
LOG
(
INFO
)
<<
"Data File: "
<<
FLAGS_datafil
e
;
std
::
vector
<
paddle
::
framework
::
LoDTensor
>
datasets
;
size_t
num_total_words
=
read_datasets
(
&
datasets
,
"/home/tangjian/paddle-tj/out.ids.txt"
);
LOG
(
INFO
)
<<
"Number of dataset samples(seq len<1024): "
<<
datasets
.
size
();
size_t
num_total_words
=
LoadData
(
&
datasets
,
FLAGS_datafile
);
LOG
(
INFO
)
<<
"Number of samples (seq_len<1024): "
<<
datasets
.
size
();
LOG
(
INFO
)
<<
"Total number of words: "
<<
num_total_words
;
const
bool
model_combined
=
false
;
// 0. Call `paddle::framework::InitDevices()` initialize all the devices
// 1. Define place, executor, scope
auto
place
=
paddle
::
platform
::
CPUPlace
();
...
...
@@ -148,13 +144,14 @@ TEST(inference, nlp) {
// 2. Initialize the inference_program and load parameters
std
::
unique_ptr
<
paddle
::
framework
::
ProgramDesc
>
inference_program
;
inference_program
=
InitProgram
(
&
executor
,
scope
,
dirname
,
model_combined
);
inference_program
=
InitProgram
(
&
executor
,
scope
,
FLAGS_modelpath
,
model_combined
);
if
(
FLAGS_use_mkldnn
)
{
EnableMKLDNN
(
inference_program
);
}
#ifdef PADDLE_WITH_MKLML
// only use 1
core per
thread
// only use 1
thread number per std::
thread
omp_set_dynamic
(
0
);
omp_set_num_threads
(
1
);
mkl_set_num_threads
(
1
);
...
...
@@ -163,24 +160,23 @@ TEST(inference, nlp) {
double
start_ms
=
0
,
stop_ms
=
0
;
if
(
FLAGS_num_threads
>
1
)
{
std
::
vector
<
std
::
vector
<
const
paddle
::
framework
::
LoDTensor
*>>
jobs
;
bcast_datasets
(
datasets
,
&
jobs
,
FLAGS_num_threads
);
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
)));
}
start_ms
=
get_current_m
s
();
start_ms
=
GetCurrentM
s
();
for
(
int
i
=
0
;
i
<
FLAGS_num_threads
;
++
i
)
{
threads
[
i
]
->
join
();
}
stop_ms
=
get_current_ms
();
stop_ms
=
GetCurrentMs
();
}
else
{
if
(
FLAGS_prepare_vars
)
{
executor
.
CreateVariables
(
*
inference_program
,
scope
,
0
);
}
// always prepare context
and burning first time
// always prepare context
std
::
unique_ptr
<
paddle
::
framework
::
ExecutorPrepareContext
>
ctx
;
ctx
=
executor
.
Prepare
(
*
inference_program
,
0
);
...
...
@@ -198,14 +194,14 @@ TEST(inference, nlp) {
PADDLE_ENFORCE_EQ
(
feed_target_names
.
size
(),
1UL
);
std
::
map
<
std
::
string
,
const
paddle
::
framework
::
LoDTensor
*>
feed_targets
;
// f
or
data and run
start_ms
=
get_current_m
s
();
// f
eed
data and run
start_ms
=
GetCurrentM
s
();
for
(
size_t
i
=
0
;
i
<
datasets
.
size
();
++
i
)
{
feed_targets
[
feed_target_names
[
0
]]
=
&
(
datasets
[
i
]);
executor
.
RunPreparedContext
(
ctx
.
get
(),
scope
,
&
feed_targets
,
&
fetch_targets
,
!
FLAGS_prepare_vars
);
}
stop_ms
=
get_current_m
s
();
stop_ms
=
GetCurrentM
s
();
}
LOG
(
INFO
)
<<
"Total inference time with "
<<
FLAGS_num_threads
...
...
paddle/fluid/inference/tests/test_helper.h
浏览文件 @
4a24c238
...
...
@@ -182,9 +182,6 @@ void TestInference(const std::string& dirname,
"init_program"
,
paddle
::
platform
::
DeviceContextPool
::
Instance
().
Get
(
place
));
inference_program
=
InitProgram
(
&
executor
,
scope
,
dirname
,
is_combined
);
// std::string binary_str;
// inference_program->Proto()->SerializeToString(&binary_str);
// LOG(INFO) << binary_str;
if
(
use_mkldnn
)
{
EnableMKLDNN
(
inference_program
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录