Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
9cba0622
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
9cba0622
编写于
4月 04, 2018
作者:
L
Liu Yiqun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add inferface to change the feed/fetch_holder_name.
上级
e359b24a
变更
12
显示空白变更内容
内联
并排
Showing
12 changed file
with
123 addition
and
55 deletion
+123
-55
paddle/fluid/framework/block_desc.h
paddle/fluid/framework/block_desc.h
+3
-0
paddle/fluid/framework/program_desc.cc
paddle/fluid/framework/program_desc.cc
+42
-4
paddle/fluid/framework/program_desc.h
paddle/fluid/framework/program_desc.h
+4
-0
paddle/fluid/inference/tests/book/test_inference_image_classification.cc
...ference/tests/book/test_inference_image_classification.cc
+1
-1
paddle/fluid/inference/tests/book/test_inference_label_semantic_roles.cc
...ference/tests/book/test_inference_label_semantic_roles.cc
+8
-8
paddle/fluid/inference/tests/book/test_inference_recognize_digits.cc
...d/inference/tests/book/test_inference_recognize_digits.cc
+1
-1
paddle/fluid/inference/tests/book/test_inference_recommender_system.cc
...inference/tests/book/test_inference_recommender_system.cc
+7
-7
paddle/fluid/inference/tests/book/test_inference_rnn_encoder_decoder.cc
...nference/tests/book/test_inference_rnn_encoder_decoder.cc
+2
-2
paddle/fluid/inference/tests/book/test_inference_understand_sentiment.cc
...ference/tests/book/test_inference_understand_sentiment.cc
+1
-1
paddle/fluid/inference/tests/book/test_inference_word2vec.cc
paddle/fluid/inference/tests/book/test_inference_word2vec.cc
+4
-4
paddle/fluid/inference/tests/test_helper.h
paddle/fluid/inference/tests/test_helper.h
+29
-24
paddle/fluid/inference/tests/test_multi_thread_helper.h
paddle/fluid/inference/tests/test_multi_thread_helper.h
+21
-3
未找到文件。
paddle/fluid/framework/block_desc.h
浏览文件 @
9cba0622
...
@@ -17,6 +17,7 @@ limitations under the License. */
...
@@ -17,6 +17,7 @@ limitations under the License. */
#include <deque>
#include <deque>
#include <memory>
#include <memory>
#include <set>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_map>
#include <vector>
#include <vector>
...
@@ -96,6 +97,8 @@ class BlockDesc {
...
@@ -96,6 +97,8 @@ class BlockDesc {
*/
*/
void
RemoveOp
(
size_t
s
,
size_t
e
);
void
RemoveOp
(
size_t
s
,
size_t
e
);
void
RemoveVar
(
const
std
::
string
&
name
)
{
vars_
.
erase
(
name
);
}
std
::
vector
<
OpDesc
*>
AllOps
()
const
;
std
::
vector
<
OpDesc
*>
AllOps
()
const
;
size_t
OpSize
()
const
{
return
ops_
.
size
();
}
size_t
OpSize
()
const
{
return
ops_
.
size
();
}
...
...
paddle/fluid/framework/program_desc.cc
浏览文件 @
9cba0622
...
@@ -85,9 +85,9 @@ ProgramDesc::ProgramDesc(const std::string &binary_str) {
...
@@ -85,9 +85,9 @@ ProgramDesc::ProgramDesc(const std::string &binary_str) {
}
}
const
std
::
vector
<
std
::
string
>
ProgramDesc
::
GetFeedTargetNames
()
{
const
std
::
vector
<
std
::
string
>
ProgramDesc
::
GetFeedTargetNames
()
{
BlockDesc
*
global_block
=
blocks_
[
0
].
get
(
);
auto
&
global_block
=
Block
(
0
);
std
::
vector
<
std
::
string
>
feed_target_names
;
std
::
vector
<
std
::
string
>
feed_target_names
;
for
(
auto
*
op
:
global_block
->
AllOps
())
{
for
(
auto
*
op
:
global_block
.
AllOps
())
{
if
(
op
->
Type
()
==
kFeedOpType
)
{
if
(
op
->
Type
()
==
kFeedOpType
)
{
feed_target_names
.
insert
(
feed_target_names
.
begin
(),
op
->
Output
(
"Out"
)[
0
]);
feed_target_names
.
insert
(
feed_target_names
.
begin
(),
op
->
Output
(
"Out"
)[
0
]);
}
}
...
@@ -96,9 +96,9 @@ const std::vector<std::string> ProgramDesc::GetFeedTargetNames() {
...
@@ -96,9 +96,9 @@ const std::vector<std::string> ProgramDesc::GetFeedTargetNames() {
}
}
const
std
::
vector
<
std
::
string
>
ProgramDesc
::
GetFetchTargetNames
()
{
const
std
::
vector
<
std
::
string
>
ProgramDesc
::
GetFetchTargetNames
()
{
BlockDesc
*
global_block
=
blocks_
[
0
].
get
(
);
auto
&
global_block
=
Block
(
0
);
std
::
vector
<
std
::
string
>
fetch_target_names
;
std
::
vector
<
std
::
string
>
fetch_target_names
;
for
(
auto
*
op
:
global_block
->
AllOps
())
{
for
(
auto
*
op
:
global_block
.
AllOps
())
{
if
(
op
->
Type
()
==
kFetchOpType
)
{
if
(
op
->
Type
()
==
kFetchOpType
)
{
fetch_target_names
.
push_back
(
op
->
Input
(
"X"
)[
0
]);
fetch_target_names
.
push_back
(
op
->
Input
(
"X"
)[
0
]);
}
}
...
@@ -106,5 +106,43 @@ const std::vector<std::string> ProgramDesc::GetFetchTargetNames() {
...
@@ -106,5 +106,43 @@ const std::vector<std::string> ProgramDesc::GetFetchTargetNames() {
return
fetch_target_names
;
return
fetch_target_names
;
}
}
void
ProgramDesc
::
SetFeedHolderName
(
const
std
::
string
&
feed_holder_name
)
{
auto
*
global_block
=
MutableBlock
(
0
);
int
index
=
0
;
for
(
auto
*
op
:
global_block
->
AllOps
())
{
if
(
op
->
Type
()
==
kFeedOpType
)
{
// Unify the input's name of all feed_ops to feed_holder_name
global_block
->
RemoveVar
(
op
->
Input
(
"X"
)[
0
]);
op
->
SetInput
(
"X"
,
{
feed_holder_name
});
op
->
SetAttr
(
"col"
,
{
index
});
op
->
CheckAttrs
();
index
++
;
}
}
auto
*
feed_holder
=
global_block
->
Var
(
feed_holder_name
);
feed_holder
->
SetType
(
proto
::
VarType
::
FEED_MINIBATCH
);
feed_holder
->
SetPersistable
(
true
);
}
void
ProgramDesc
::
SetFetchHolderName
(
const
std
::
string
&
fetch_holder_name
)
{
auto
*
global_block
=
MutableBlock
(
0
);
int
index
=
0
;
for
(
auto
*
op
:
global_block
->
AllOps
())
{
if
(
op
->
Type
()
==
kFetchOpType
)
{
// Unify the output's name of all fetch_ops to fetch_holder_name
global_block
->
RemoveVar
(
op
->
Output
(
"Out"
)[
0
]);
op
->
SetOutput
(
"Out"
,
{
fetch_holder_name
});
op
->
SetAttr
(
"col"
,
{
index
});
op
->
CheckAttrs
();
index
++
;
}
}
auto
*
fetch_holder
=
global_block
->
Var
(
fetch_holder_name
);
fetch_holder
->
SetType
(
proto
::
VarType
::
FETCH_LIST
);
fetch_holder
->
SetPersistable
(
true
);
}
}
// namespace framework
}
// namespace framework
}
// namespace paddle
}
// namespace paddle
paddle/fluid/framework/program_desc.h
浏览文件 @
9cba0622
...
@@ -15,6 +15,7 @@ limitations under the License. */
...
@@ -15,6 +15,7 @@ limitations under the License. */
#pragma once
#pragma once
#include <memory>
#include <memory>
#include <string>
#include <vector>
#include <vector>
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/framework/framework.pb.h"
...
@@ -55,6 +56,9 @@ class ProgramDesc {
...
@@ -55,6 +56,9 @@ class ProgramDesc {
const
std
::
vector
<
std
::
string
>
GetFeedTargetNames
();
const
std
::
vector
<
std
::
string
>
GetFeedTargetNames
();
const
std
::
vector
<
std
::
string
>
GetFetchTargetNames
();
const
std
::
vector
<
std
::
string
>
GetFetchTargetNames
();
void
SetFeedHolderName
(
const
std
::
string
&
feed_holder_name
);
void
SetFetchHolderName
(
const
std
::
string
&
fetch_holder_name
);
private:
private:
proto
::
ProgramDesc
desc_
;
proto
::
ProgramDesc
desc_
;
...
...
paddle/fluid/inference/tests/book/test_inference_image_classification.cc
浏览文件 @
9cba0622
...
@@ -35,7 +35,7 @@ TEST(inference, image_classification) {
...
@@ -35,7 +35,7 @@ TEST(inference, image_classification) {
paddle
::
framework
::
LoDTensor
input
;
paddle
::
framework
::
LoDTensor
input
;
// Use normilized image pixels as input data,
// Use normilized image pixels as input data,
// which should be in the range [0.0, 1.0].
// which should be in the range [0.0, 1.0].
SetupTensor
<
float
>
(
input
,
SetupTensor
<
float
>
(
&
input
,
{
FLAGS_batch_size
,
3
,
32
,
32
},
{
FLAGS_batch_size
,
3
,
32
,
32
},
static_cast
<
float
>
(
0
),
static_cast
<
float
>
(
0
),
static_cast
<
float
>
(
1
));
static_cast
<
float
>
(
1
));
...
...
paddle/fluid/inference/tests/book/test_inference_label_semantic_roles.cc
浏览文件 @
9cba0622
...
@@ -36,35 +36,35 @@ TEST(inference, label_semantic_roles) {
...
@@ -36,35 +36,35 @@ TEST(inference, label_semantic_roles) {
int64_t
predicate_dict_len
=
3162
;
int64_t
predicate_dict_len
=
3162
;
int64_t
mark_dict_len
=
2
;
int64_t
mark_dict_len
=
2
;
SetupLoDTensor
(
word
,
SetupLoDTensor
(
&
word
,
lod
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
SetupLoDTensor
(
predicate
,
SetupLoDTensor
(
&
predicate
,
lod
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
predicate_dict_len
-
1
));
static_cast
<
int64_t
>
(
predicate_dict_len
-
1
));
SetupLoDTensor
(
ctx_n2
,
SetupLoDTensor
(
&
ctx_n2
,
lod
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
SetupLoDTensor
(
ctx_n1
,
SetupLoDTensor
(
&
ctx_n1
,
lod
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
SetupLoDTensor
(
ctx_0
,
SetupLoDTensor
(
&
ctx_0
,
lod
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
SetupLoDTensor
(
ctx_p1
,
SetupLoDTensor
(
&
ctx_p1
,
lod
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
SetupLoDTensor
(
ctx_p2
,
SetupLoDTensor
(
&
ctx_p2
,
lod
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
SetupLoDTensor
(
mark
,
SetupLoDTensor
(
&
mark
,
lod
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
mark_dict_len
-
1
));
static_cast
<
int64_t
>
(
mark_dict_len
-
1
));
...
...
paddle/fluid/inference/tests/book/test_inference_recognize_digits.cc
浏览文件 @
9cba0622
...
@@ -35,7 +35,7 @@ TEST(inference, recognize_digits) {
...
@@ -35,7 +35,7 @@ TEST(inference, recognize_digits) {
paddle
::
framework
::
LoDTensor
input
;
paddle
::
framework
::
LoDTensor
input
;
// Use normilized image pixels as input data,
// Use normilized image pixels as input data,
// which should be in the range [-1.0, 1.0].
// which should be in the range [-1.0, 1.0].
SetupTensor
<
float
>
(
input
,
SetupTensor
<
float
>
(
&
input
,
{
FLAGS_batch_size
,
1
,
28
,
28
},
{
FLAGS_batch_size
,
1
,
28
,
28
},
static_cast
<
float
>
(
-
1
),
static_cast
<
float
>
(
-
1
),
static_cast
<
float
>
(
1
));
static_cast
<
float
>
(
1
));
...
...
paddle/fluid/inference/tests/book/test_inference_recommender_system.cc
浏览文件 @
9cba0622
...
@@ -36,25 +36,25 @@ TEST(inference, recommender_system) {
...
@@ -36,25 +36,25 @@ TEST(inference, recommender_system) {
// Use the first data from paddle.dataset.movielens.test() as input
// Use the first data from paddle.dataset.movielens.test() as input
std
::
vector
<
int64_t
>
user_id_data
=
{
1
};
std
::
vector
<
int64_t
>
user_id_data
=
{
1
};
SetupTensor
<
int64_t
>
(
user_id
,
{
batch_size
,
1
},
user_id_data
);
SetupTensor
<
int64_t
>
(
&
user_id
,
{
batch_size
,
1
},
user_id_data
);
std
::
vector
<
int64_t
>
gender_id_data
=
{
1
};
std
::
vector
<
int64_t
>
gender_id_data
=
{
1
};
SetupTensor
<
int64_t
>
(
gender_id
,
{
batch_size
,
1
},
gender_id_data
);
SetupTensor
<
int64_t
>
(
&
gender_id
,
{
batch_size
,
1
},
gender_id_data
);
std
::
vector
<
int64_t
>
age_id_data
=
{
0
};
std
::
vector
<
int64_t
>
age_id_data
=
{
0
};
SetupTensor
<
int64_t
>
(
age_id
,
{
batch_size
,
1
},
age_id_data
);
SetupTensor
<
int64_t
>
(
&
age_id
,
{
batch_size
,
1
},
age_id_data
);
std
::
vector
<
int64_t
>
job_id_data
=
{
10
};
std
::
vector
<
int64_t
>
job_id_data
=
{
10
};
SetupTensor
<
int64_t
>
(
job_id
,
{
batch_size
,
1
},
job_id_data
);
SetupTensor
<
int64_t
>
(
&
job_id
,
{
batch_size
,
1
},
job_id_data
);
std
::
vector
<
int64_t
>
movie_id_data
=
{
783
};
std
::
vector
<
int64_t
>
movie_id_data
=
{
783
};
SetupTensor
<
int64_t
>
(
movie_id
,
{
batch_size
,
1
},
movie_id_data
);
SetupTensor
<
int64_t
>
(
&
movie_id
,
{
batch_size
,
1
},
movie_id_data
);
std
::
vector
<
int64_t
>
category_id_data
=
{
10
,
8
,
9
};
std
::
vector
<
int64_t
>
category_id_data
=
{
10
,
8
,
9
};
SetupLoDTensor
<
int64_t
>
(
category_id
,
{
3
,
1
},
{{
0
,
3
}},
category_id_data
);
SetupLoDTensor
<
int64_t
>
(
&
category_id
,
{
3
,
1
},
{{
0
,
3
}},
category_id_data
);
std
::
vector
<
int64_t
>
movie_title_data
=
{
1069
,
4140
,
2923
,
710
,
988
};
std
::
vector
<
int64_t
>
movie_title_data
=
{
1069
,
4140
,
2923
,
710
,
988
};
SetupLoDTensor
<
int64_t
>
(
movie_title
,
{
5
,
1
},
{{
0
,
5
}},
movie_title_data
);
SetupLoDTensor
<
int64_t
>
(
&
movie_title
,
{
5
,
1
},
{{
0
,
5
}},
movie_title_data
);
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>
cpu_feeds
;
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>
cpu_feeds
;
cpu_feeds
.
push_back
(
&
user_id
);
cpu_feeds
.
push_back
(
&
user_id
);
...
...
paddle/fluid/inference/tests/book/test_inference_rnn_encoder_decoder.cc
浏览文件 @
9cba0622
...
@@ -33,9 +33,9 @@ TEST(inference, rnn_encoder_decoder) {
...
@@ -33,9 +33,9 @@ TEST(inference, rnn_encoder_decoder) {
paddle
::
framework
::
LoD
lod
{{
0
,
4
,
10
}};
paddle
::
framework
::
LoD
lod
{{
0
,
4
,
10
}};
SetupLoDTensor
(
SetupLoDTensor
(
word_data
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
1
));
&
word_data
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
1
));
SetupLoDTensor
(
SetupLoDTensor
(
trg_word
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
1
));
&
trg_word
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
1
));
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>
cpu_feeds
;
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>
cpu_feeds
;
cpu_feeds
.
push_back
(
&
word_data
);
cpu_feeds
.
push_back
(
&
word_data
);
...
...
paddle/fluid/inference/tests/book/test_inference_understand_sentiment.cc
浏览文件 @
9cba0622
...
@@ -33,7 +33,7 @@ TEST(inference, understand_sentiment) {
...
@@ -33,7 +33,7 @@ TEST(inference, understand_sentiment) {
paddle
::
framework
::
LoD
lod
{{
0
,
4
,
10
}};
paddle
::
framework
::
LoD
lod
{{
0
,
4
,
10
}};
int64_t
word_dict_len
=
5147
;
int64_t
word_dict_len
=
5147
;
SetupLoDTensor
(
words
,
SetupLoDTensor
(
&
words
,
lod
,
lod
,
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
0
),
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
static_cast
<
int64_t
>
(
word_dict_len
-
1
));
...
...
paddle/fluid/inference/tests/book/test_inference_word2vec.cc
浏览文件 @
9cba0622
...
@@ -33,10 +33,10 @@ TEST(inference, word2vec) {
...
@@ -33,10 +33,10 @@ TEST(inference, word2vec) {
paddle
::
framework
::
LoD
lod
{{
0
,
1
}};
paddle
::
framework
::
LoD
lod
{{
0
,
1
}};
int64_t
dict_size
=
2073
;
// The size of dictionary
int64_t
dict_size
=
2073
;
// The size of dictionary
SetupLoDTensor
(
first_word
,
lod
,
static_cast
<
int64_t
>
(
0
),
dict_size
-
1
);
SetupLoDTensor
(
&
first_word
,
lod
,
static_cast
<
int64_t
>
(
0
),
dict_size
-
1
);
SetupLoDTensor
(
second_word
,
lod
,
static_cast
<
int64_t
>
(
0
),
dict_size
-
1
);
SetupLoDTensor
(
&
second_word
,
lod
,
static_cast
<
int64_t
>
(
0
),
dict_size
-
1
);
SetupLoDTensor
(
third_word
,
lod
,
static_cast
<
int64_t
>
(
0
),
dict_size
-
1
);
SetupLoDTensor
(
&
third_word
,
lod
,
static_cast
<
int64_t
>
(
0
),
dict_size
-
1
);
SetupLoDTensor
(
fourth_word
,
lod
,
static_cast
<
int64_t
>
(
0
),
dict_size
-
1
);
SetupLoDTensor
(
&
fourth_word
,
lod
,
static_cast
<
int64_t
>
(
0
),
dict_size
-
1
);
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>
cpu_feeds
;
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>
cpu_feeds
;
cpu_feeds
.
push_back
(
&
first_word
);
cpu_feeds
.
push_back
(
&
first_word
);
...
...
paddle/fluid/inference/tests/test_helper.h
浏览文件 @
9cba0622
...
@@ -12,58 +12,63 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...
@@ -12,58 +12,63 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License. */
limitations under the License. */
#pragma once
#include <time.h>
#include <time.h>
#include <map>
#include <string>
#include <vector>
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/inference/io.h"
#include "paddle/fluid/inference/io.h"
#include "paddle/fluid/platform/profiler.h"
#include "paddle/fluid/platform/profiler.h"
template
<
typename
T
>
template
<
typename
T
>
void
SetupTensor
(
paddle
::
framework
::
LoDTensor
&
input
,
void
SetupTensor
(
paddle
::
framework
::
LoDTensor
*
input
,
paddle
::
framework
::
DDim
dims
,
paddle
::
framework
::
DDim
dims
,
T
lower
,
const
T
lower
,
T
upper
)
{
const
T
upper
)
{
srand
(
time
(
0
));
T
*
input_ptr
=
input
->
mutable_data
<
T
>
(
dims
,
paddle
::
platform
::
CPUPlace
(
));
T
*
input_ptr
=
input
.
mutable_data
<
T
>
(
dims
,
paddle
::
platform
::
CPUPlace
()
);
unsigned
int
seed
=
time
(
NULL
);
for
(
int
i
=
0
;
i
<
input
.
numel
();
++
i
)
{
for
(
int
i
=
0
;
i
<
input
->
numel
();
++
i
)
{
input_ptr
[
i
]
=
input_ptr
[
i
]
=
(
static_cast
<
T
>
(
rand_r
(
&
seed
))
/
static_cast
<
T
>
(
RAND_MAX
))
*
(
static_cast
<
T
>
(
rand
())
/
static_cast
<
T
>
(
RAND_MAX
))
*
(
upper
-
lower
)
+
(
upper
-
lower
)
+
lower
;
lower
;
}
}
}
}
template
<
typename
T
>
template
<
typename
T
>
void
SetupTensor
(
paddle
::
framework
::
LoDTensor
&
input
,
void
SetupTensor
(
paddle
::
framework
::
LoDTensor
*
input
,
paddle
::
framework
::
DDim
dims
,
paddle
::
framework
::
DDim
dims
,
std
::
vector
<
T
>&
data
)
{
const
std
::
vector
<
T
>&
data
)
{
CHECK_EQ
(
paddle
::
framework
::
product
(
dims
),
static_cast
<
int64_t
>
(
data
.
size
()));
CHECK_EQ
(
paddle
::
framework
::
product
(
dims
),
static_cast
<
int64_t
>
(
data
.
size
()));
T
*
input_ptr
=
input
.
mutable_data
<
T
>
(
dims
,
paddle
::
platform
::
CPUPlace
());
T
*
input_ptr
=
input
->
mutable_data
<
T
>
(
dims
,
paddle
::
platform
::
CPUPlace
());
memcpy
(
input_ptr
,
data
.
data
(),
input
.
numel
()
*
sizeof
(
T
));
memcpy
(
input_ptr
,
data
.
data
(),
input
->
numel
()
*
sizeof
(
T
));
}
}
template
<
typename
T
>
template
<
typename
T
>
void
SetupLoDTensor
(
paddle
::
framework
::
LoDTensor
&
input
,
void
SetupLoDTensor
(
paddle
::
framework
::
LoDTensor
*
input
,
paddle
::
framework
::
LoD
&
lod
,
const
paddle
::
framework
::
LoD
&
lod
,
T
lower
,
const
T
lower
,
T
upper
)
{
const
T
upper
)
{
input
.
set_lod
(
lod
);
input
->
set_lod
(
lod
);
int
dim
=
lod
[
0
][
lod
[
0
].
size
()
-
1
];
int
dim
=
lod
[
0
][
lod
[
0
].
size
()
-
1
];
SetupTensor
<
T
>
(
input
,
{
dim
,
1
},
lower
,
upper
);
SetupTensor
<
T
>
(
input
,
{
dim
,
1
},
lower
,
upper
);
}
}
template
<
typename
T
>
template
<
typename
T
>
void
SetupLoDTensor
(
paddle
::
framework
::
LoDTensor
&
input
,
void
SetupLoDTensor
(
paddle
::
framework
::
LoDTensor
*
input
,
paddle
::
framework
::
DDim
dims
,
paddle
::
framework
::
DDim
dims
,
paddle
::
framework
::
LoD
lod
,
paddle
::
framework
::
LoD
lod
,
std
::
vector
<
T
>&
data
)
{
const
std
::
vector
<
T
>&
data
)
{
const
size_t
level
=
lod
.
size
()
-
1
;
const
size_t
level
=
lod
.
size
()
-
1
;
CHECK_EQ
(
dims
[
0
],
static_cast
<
int64_t
>
((
lod
[
level
]).
back
()));
CHECK_EQ
(
dims
[
0
],
static_cast
<
int64_t
>
((
lod
[
level
]).
back
()));
input
.
set_lod
(
lod
);
input
->
set_lod
(
lod
);
SetupTensor
<
T
>
(
input
,
dims
,
data
);
SetupTensor
<
T
>
(
input
,
dims
,
data
);
}
}
template
<
typename
T
>
template
<
typename
T
>
void
CheckError
(
paddle
::
framework
::
LoDTensor
&
output1
,
void
CheckError
(
const
paddle
::
framework
::
LoDTensor
&
output1
,
paddle
::
framework
::
LoDTensor
&
output2
)
{
const
paddle
::
framework
::
LoDTensor
&
output2
)
{
// Check lod information
// Check lod information
EXPECT_EQ
(
output1
.
lod
(),
output2
.
lod
());
EXPECT_EQ
(
output1
.
lod
(),
output2
.
lod
());
...
@@ -91,7 +96,7 @@ void CheckError(paddle::framework::LoDTensor& output1,
...
@@ -91,7 +96,7 @@ void CheckError(paddle::framework::LoDTensor& output1,
template
<
typename
Place
>
template
<
typename
Place
>
void
TestInference
(
const
std
::
string
&
dirname
,
void
TestInference
(
const
std
::
string
&
dirname
,
const
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>&
cpu_feeds
,
const
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>&
cpu_feeds
,
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>&
cpu_fetchs
,
const
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>&
cpu_fetchs
,
const
int
repeat
=
1
,
const
int
repeat
=
1
,
const
bool
is_combined
=
false
)
{
const
bool
is_combined
=
false
)
{
// 1. Define place, executor, scope
// 1. Define place, executor, scope
...
...
paddle/fluid/inference/tests/test_multi_thread_helper.h
浏览文件 @
9cba0622
...
@@ -12,6 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...
@@ -12,6 +12,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License. */
limitations under the License. */
#pragma once
#include <thread>
#include <thread>
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/inference/io.h"
#include "paddle/fluid/inference/io.h"
...
@@ -20,13 +22,23 @@ void ThreadedRunInference(
...
@@ -20,13 +22,23 @@ void ThreadedRunInference(
std
::
unique_ptr
<
paddle
::
framework
::
ProgramDesc
>&
inference_program
,
std
::
unique_ptr
<
paddle
::
framework
::
ProgramDesc
>&
inference_program
,
paddle
::
framework
::
Executor
&
executor
,
paddle
::
framework
::
Executor
&
executor
,
paddle
::
framework
::
Scope
*
scope
,
paddle
::
framework
::
Scope
*
scope
,
const
int
thread_id
,
const
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>&
cpu_feeds
,
const
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>&
cpu_feeds
,
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>&
cpu_fetchs
)
{
std
::
vector
<
paddle
::
framework
::
LoDTensor
*>&
cpu_fetchs
)
{
auto
copy_program
=
std
::
unique_ptr
<
paddle
::
framework
::
ProgramDesc
>
(
new
paddle
::
framework
::
ProgramDesc
(
*
inference_program
));
std
::
string
feed_holder_name
=
"feed_"
+
paddle
::
string
::
to_string
(
thread_id
);
std
::
string
fetch_holder_name
=
"fetch_"
+
paddle
::
string
::
to_string
(
thread_id
);
copy_program
->
SetFeedHolderName
(
feed_holder_name
);
copy_program
->
SetFetchHolderName
(
fetch_holder_name
);
// 3. Get the feed_target_names and fetch_target_names
// 3. Get the feed_target_names and fetch_target_names
const
std
::
vector
<
std
::
string
>&
feed_target_names
=
const
std
::
vector
<
std
::
string
>&
feed_target_names
=
inference
_program
->
GetFeedTargetNames
();
copy
_program
->
GetFeedTargetNames
();
const
std
::
vector
<
std
::
string
>&
fetch_target_names
=
const
std
::
vector
<
std
::
string
>&
fetch_target_names
=
inference
_program
->
GetFetchTargetNames
();
copy
_program
->
GetFetchTargetNames
();
// 4. Prepare inputs: set up maps for feed targets
// 4. Prepare inputs: set up maps for feed targets
std
::
map
<
std
::
string
,
const
paddle
::
framework
::
LoDTensor
*>
feed_targets
;
std
::
map
<
std
::
string
,
const
paddle
::
framework
::
LoDTensor
*>
feed_targets
;
...
@@ -42,7 +54,12 @@ void ThreadedRunInference(
...
@@ -42,7 +54,12 @@ void ThreadedRunInference(
}
}
// 6. Run the inference program
// 6. Run the inference program
executor
.
Run
(
*
inference_program
,
scope
,
feed_targets
,
fetch_targets
);
executor
.
Run
(
*
copy_program
,
scope
,
feed_targets
,
fetch_targets
,
feed_holder_name
,
fetch_holder_name
);
}
}
template
<
typename
Place
>
template
<
typename
Place
>
...
@@ -66,6 +83,7 @@ void TestMultiThreadInference(
...
@@ -66,6 +83,7 @@ void TestMultiThreadInference(
std
::
ref
(
inference_program
),
std
::
ref
(
inference_program
),
std
::
ref
(
executor
),
std
::
ref
(
executor
),
scope
,
scope
,
i
,
std
::
ref
(
cpu_feeds
[
i
]),
std
::
ref
(
cpu_feeds
[
i
]),
std
::
ref
(
cpu_fetchs
[
i
])));
std
::
ref
(
cpu_fetchs
[
i
])));
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录