Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
2b633173
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
2b633173
编写于
3月 08, 2019
作者:
T
Tao Luo
提交者:
GitHub
3月 08, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #15601 from luotao1/transformer_ut
add analyzer_transformer_test
上级
6e0311ee
25ca2ca0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
234 addition
and
0 deletion
+234
-0
paddle/fluid/inference/api/helper.h
paddle/fluid/inference/api/helper.h
+7
-0
paddle/fluid/inference/tests/api/CMakeLists.txt
paddle/fluid/inference/tests/api/CMakeLists.txt
+7
-0
paddle/fluid/inference/tests/api/analyzer_transformer_tester.cc
.../fluid/inference/tests/api/analyzer_transformer_tester.cc
+220
-0
未找到文件。
paddle/fluid/inference/api/helper.h
浏览文件 @
2b633173
...
...
@@ -86,6 +86,13 @@ static void split_to_int64(const std::string &str, char sep,
std
::
transform
(
pieces
.
begin
(),
pieces
.
end
(),
std
::
back_inserter
(
*
is
),
[](
const
std
::
string
&
v
)
{
return
std
::
stoi
(
v
);
});
}
static
void
split_to_int
(
const
std
::
string
&
str
,
char
sep
,
std
::
vector
<
int
>
*
is
)
{
std
::
vector
<
std
::
string
>
pieces
;
split
(
str
,
sep
,
&
pieces
);
std
::
transform
(
pieces
.
begin
(),
pieces
.
end
(),
std
::
back_inserter
(
*
is
),
[](
const
std
::
string
&
v
)
{
return
std
::
stoi
(
v
);
});
}
template
<
typename
T
>
std
::
string
to_string
(
const
std
::
vector
<
T
>
&
vec
)
{
std
::
stringstream
ss
;
...
...
paddle/fluid/inference/tests/api/CMakeLists.txt
浏览文件 @
2b633173
...
...
@@ -105,6 +105,13 @@ set(SEQ_CONV1_INSTALL_DIR "${INFERENCE_DEMO_INSTALL_DIR}/seq_conv1")
download_model_and_data
(
${
SEQ_CONV1_INSTALL_DIR
}
"seq_conv1_model.tar.gz"
"seq_conv1_data.txt.tar.gz"
)
inference_analysis_api_test
(
test_analyzer_seq_conv1
${
SEQ_CONV1_INSTALL_DIR
}
analyzer_seq_conv1_tester.cc
)
# transformer, the dataset only works on batch_size=8 now
set
(
TRANSFORMER_INSTALL_DIR
"
${
INFERENCE_DEMO_INSTALL_DIR
}
/transformer"
)
download_model_and_data
(
${
TRANSFORMER_INSTALL_DIR
}
"temp%2Ftransformer_model.tar.gz"
"temp%2Ftransformer_data.txt.tar.gz"
)
inference_analysis_test
(
test_analyzer_transformer SRCS analyzer_transformer_tester.cc
EXTRA_DEPS
${
INFERENCE_EXTRA_DEPS
}
ARGS --infer_model=
${
TRANSFORMER_INSTALL_DIR
}
/model --infer_data=
${
TRANSFORMER_INSTALL_DIR
}
/data.txt --batch_size=8
)
# ocr
set
(
OCR_INSTALL_DIR
"
${
INFERENCE_DEMO_INSTALL_DIR
}
/ocr"
)
if
(
NOT EXISTS
${
OCR_INSTALL_DIR
}
)
...
...
paddle/fluid/inference/tests/api/analyzer_transformer_tester.cc
0 → 100644
浏览文件 @
2b633173
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/fluid/inference/tests/api/tester_helper.h"
namespace
paddle
{
namespace
inference
{
struct
DataRecord
{
std
::
vector
<
std
::
vector
<
int64_t
>>
src_word
,
src_pos
,
trg_word
,
init_idx
;
std
::
vector
<
std
::
vector
<
float
>>
src_slf_attn_bias
,
init_score
,
trg_src_attn_bias
;
std
::
vector
<
std
::
vector
<
int32_t
>>
batch_data_shape
;
std
::
vector
<
std
::
vector
<
size_t
>>
lod
;
size_t
batch_iter
{
0
},
batch_size
{
1
},
num_samples
;
// total number of samples
DataRecord
()
=
default
;
explicit
DataRecord
(
const
std
::
string
&
path
,
int
batch_size
=
1
)
:
batch_size
(
batch_size
)
{
Load
(
path
);
}
DataRecord
NextBatch
()
{
DataRecord
data
;
size_t
batch_end
=
batch_iter
+
batch_size
;
// NOTE skip the final batch, if no enough data is provided.
if
(
batch_end
<=
src_word
.
size
())
{
data
.
src_word
.
assign
(
src_word
.
begin
()
+
batch_iter
,
src_word
.
begin
()
+
batch_end
);
data
.
src_pos
.
assign
(
src_pos
.
begin
()
+
batch_iter
,
src_pos
.
begin
()
+
batch_end
);
data
.
src_slf_attn_bias
.
assign
(
src_slf_attn_bias
.
begin
()
+
batch_iter
,
src_slf_attn_bias
.
begin
()
+
batch_end
);
data
.
trg_word
.
assign
(
trg_word
.
begin
()
+
batch_iter
,
trg_word
.
begin
()
+
batch_end
);
data
.
init_score
.
assign
(
init_score
.
begin
()
+
batch_iter
,
init_score
.
begin
()
+
batch_end
);
data
.
init_idx
.
assign
(
init_idx
.
begin
()
+
batch_iter
,
init_idx
.
begin
()
+
batch_end
);
data
.
trg_src_attn_bias
.
assign
(
trg_src_attn_bias
.
begin
()
+
batch_iter
,
trg_src_attn_bias
.
begin
()
+
batch_end
);
std
::
vector
<
int32_t
>
batch_shape
=
*
(
batch_data_shape
.
begin
()
+
batch_iter
);
data
.
batch_data_shape
.
push_back
(
batch_shape
);
data
.
lod
.
resize
(
2
);
for
(
int
i
=
0
;
i
<
batch_shape
[
0
]
+
1
;
i
++
)
{
data
.
lod
[
0
].
push_back
(
i
);
data
.
lod
[
1
].
push_back
(
i
);
}
}
batch_iter
+=
batch_size
;
return
data
;
}
void
Load
(
const
std
::
string
&
path
)
{
std
::
ifstream
file
(
path
);
std
::
string
line
;
size_t
num_lines
=
0
;
while
(
std
::
getline
(
file
,
line
))
{
num_lines
++
;
std
::
vector
<
std
::
string
>
data
;
split
(
line
,
','
,
&
data
);
CHECK_EQ
(
data
.
size
(),
static_cast
<
size_t
>
(
8
));
// load src_word
std
::
vector
<
int64_t
>
src_word_data
;
split_to_int64
(
data
[
0
],
' '
,
&
src_word_data
);
src_word
.
push_back
(
std
::
move
(
src_word_data
));
// load src_pos
std
::
vector
<
int64_t
>
src_pos_data
;
split_to_int64
(
data
[
1
],
' '
,
&
src_pos_data
);
src_pos
.
push_back
(
std
::
move
(
src_pos_data
));
// load src_slf_attn_bias
std
::
vector
<
float
>
src_slf_attn_bias_data
;
split_to_float
(
data
[
2
],
' '
,
&
src_slf_attn_bias_data
);
src_slf_attn_bias
.
push_back
(
std
::
move
(
src_slf_attn_bias_data
));
// load trg_word
std
::
vector
<
int64_t
>
trg_word_data
;
split_to_int64
(
data
[
3
],
' '
,
&
trg_word_data
);
trg_word
.
push_back
(
std
::
move
(
trg_word_data
));
// load init_score
std
::
vector
<
float
>
init_score_data
;
split_to_float
(
data
[
4
],
' '
,
&
init_score_data
);
init_score
.
push_back
(
std
::
move
(
init_score_data
));
// load init_idx
std
::
vector
<
int64_t
>
init_idx_data
;
split_to_int64
(
data
[
5
],
' '
,
&
init_idx_data
);
init_idx
.
push_back
(
std
::
move
(
init_idx_data
));
// load trg_src_attn_bias
std
::
vector
<
float
>
trg_src_attn_bias_data
;
split_to_float
(
data
[
6
],
' '
,
&
trg_src_attn_bias_data
);
trg_src_attn_bias
.
push_back
(
std
::
move
(
trg_src_attn_bias_data
));
// load shape for variant data shape
std
::
vector
<
int
>
batch_data_shape_data
;
split_to_int
(
data
[
7
],
' '
,
&
batch_data_shape_data
);
batch_data_shape
.
push_back
(
std
::
move
(
batch_data_shape_data
));
}
num_samples
=
num_lines
;
}
};
void
PrepareInputs
(
std
::
vector
<
PaddleTensor
>
*
input_slots
,
DataRecord
*
data
,
int
batch_size
)
{
auto
one_batch
=
data
->
NextBatch
();
batch_size
=
one_batch
.
batch_data_shape
[
0
][
0
];
auto
n_head
=
one_batch
.
batch_data_shape
[
0
][
1
];
auto
trg_seq_len
=
one_batch
.
batch_data_shape
[
0
][
2
];
// 1 for inference
auto
src_seq_len
=
one_batch
.
batch_data_shape
[
0
][
3
];
PaddleTensor
src_word
,
src_pos
,
src_slf_attn_bias
,
trg_word
,
init_score
,
init_idx
,
trg_src_attn_bias
;
src_word
.
name
=
"src_word"
;
src_word
.
shape
.
assign
({
batch_size
,
src_seq_len
,
1
});
src_word
.
dtype
=
PaddleDType
::
INT64
;
TensorAssignData
<
int64_t
>
(
&
src_word
,
one_batch
.
src_word
);
src_pos
.
name
=
"src_pos"
;
src_pos
.
shape
.
assign
({
batch_size
,
src_seq_len
,
1
});
src_pos
.
dtype
=
PaddleDType
::
INT64
;
TensorAssignData
<
int64_t
>
(
&
src_pos
,
one_batch
.
src_pos
);
src_slf_attn_bias
.
name
=
"src_slf_attn_bias"
;
src_slf_attn_bias
.
shape
.
assign
(
{
batch_size
,
n_head
,
src_seq_len
,
src_seq_len
});
src_slf_attn_bias
.
dtype
=
PaddleDType
::
FLOAT32
;
TensorAssignData
<
float
>
(
&
src_slf_attn_bias
,
one_batch
.
src_slf_attn_bias
);
trg_word
.
name
=
"trg_word"
;
trg_word
.
shape
.
assign
({
batch_size
,
1
});
trg_word
.
dtype
=
PaddleDType
::
INT64
;
trg_word
.
lod
.
assign
(
one_batch
.
lod
.
begin
(),
one_batch
.
lod
.
end
());
TensorAssignData
<
int64_t
>
(
&
trg_word
,
one_batch
.
trg_word
);
init_score
.
name
=
"init_score"
;
init_score
.
shape
.
assign
({
batch_size
,
1
});
init_score
.
dtype
=
PaddleDType
::
FLOAT32
;
init_score
.
lod
.
assign
(
one_batch
.
lod
.
begin
(),
one_batch
.
lod
.
end
());
TensorAssignData
<
float
>
(
&
init_score
,
one_batch
.
init_score
);
init_idx
.
name
=
"init_idx"
;
init_idx
.
shape
.
assign
({
batch_size
});
init_idx
.
dtype
=
PaddleDType
::
INT32
;
TensorAssignData
<
int64_t
>
(
&
init_idx
,
one_batch
.
init_idx
);
trg_src_attn_bias
.
name
=
"trg_src_attn_bias"
;
trg_src_attn_bias
.
shape
.
assign
(
{
batch_size
,
n_head
,
trg_seq_len
,
src_seq_len
});
trg_src_attn_bias
.
dtype
=
PaddleDType
::
FLOAT32
;
TensorAssignData
<
float
>
(
&
trg_src_attn_bias
,
one_batch
.
trg_src_attn_bias
);
input_slots
->
assign
({
src_word
,
src_pos
,
src_slf_attn_bias
,
trg_word
,
init_score
,
init_idx
,
trg_src_attn_bias
});
}
void
SetConfig
(
AnalysisConfig
*
cfg
)
{
cfg
->
SetModel
(
FLAGS_infer_model
+
"/model"
,
FLAGS_infer_model
+
"/params"
);
cfg
->
DisableGpu
();
cfg
->
SwitchSpecifyInputNames
();
cfg
->
SwitchIrOptim
();
cfg
->
SetCpuMathLibraryNumThreads
(
FLAGS_paddle_num_threads
);
}
void
SetInput
(
std
::
vector
<
std
::
vector
<
PaddleTensor
>>
*
inputs
)
{
DataRecord
data
(
FLAGS_infer_data
,
FLAGS_batch_size
);
std
::
vector
<
PaddleTensor
>
input_slots
;
int
test_batch_num
=
FLAGS_test_all_data
?
data
.
num_samples
/
FLAGS_batch_size
:
1
;
LOG
(
INFO
)
<<
"The number of samples to be test: "
<<
test_batch_num
*
FLAGS_batch_size
;
for
(
int
bid
=
0
;
bid
<
test_batch_num
;
++
bid
)
{
input_slots
.
clear
();
PrepareInputs
(
&
input_slots
,
&
data
,
FLAGS_batch_size
);
(
*
inputs
).
emplace_back
(
input_slots
);
}
}
// Easy for profiling independently.
TEST
(
Analyzer_Transformer
,
profile
)
{
AnalysisConfig
cfg
;
SetConfig
(
&
cfg
);
std
::
vector
<
PaddleTensor
>
outputs
;
std
::
vector
<
std
::
vector
<
PaddleTensor
>>
input_slots_all
;
SetInput
(
&
input_slots_all
);
TestPrediction
(
reinterpret_cast
<
const
PaddlePredictor
::
Config
*>
(
&
cfg
),
input_slots_all
,
&
outputs
,
FLAGS_num_threads
);
}
// Check the fuse status
TEST
(
Analyzer_Transformer
,
fuse_statis
)
{
AnalysisConfig
cfg
;
SetConfig
(
&
cfg
);
int
num_ops
;
auto
predictor
=
CreatePaddlePredictor
<
AnalysisConfig
>
(
cfg
);
auto
fuse_statis
=
GetFuseStatis
(
static_cast
<
AnalysisPredictor
*>
(
predictor
.
get
()),
&
num_ops
);
}
// Compare result of NativeConfig and AnalysisConfig
TEST
(
Analyzer_Transformer
,
compare
)
{
AnalysisConfig
cfg
;
SetConfig
(
&
cfg
);
std
::
vector
<
std
::
vector
<
PaddleTensor
>>
input_slots_all
;
SetInput
(
&
input_slots_all
);
CompareNativeAndAnalysis
(
reinterpret_cast
<
const
PaddlePredictor
::
Config
*>
(
&
cfg
),
input_slots_all
);
}
}
// namespace inference
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录