Skip to content

Commit 67e4450

Browse files
authored
Merge pull request #15485 from luotao1/fc500110-bert_test
add bert analyzer test
2 parents 6000a6e + e31aef9 commit 67e4450

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed

paddle/fluid/inference/tests/api/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ inference_analysis_api_test_with_fake_data(test_analyzer_resnet50
128128
inference_analysis_api_test_with_fake_data(test_analyzer_mobilenet_depthwise_conv
129129
"${INFERENCE_DEMO_INSTALL_DIR}/mobilenet_depthwise_conv" analyzer_resnet50_tester.cc "mobilenet_model.tar.gz" SERIAL)
130130

131+
# bert
132+
set(BERT_INSTALL_DIR "${INFERENCE_DEMO_INSTALL_DIR}/bert")
133+
download_model_and_data(${BERT_INSTALL_DIR} "bert_model.tar.gz" "bert_data.txt.tar.gz")
134+
inference_analysis_api_test(test_analyzer_bert ${BERT_INSTALL_DIR} analyzer_bert_tester.cc)
135+
131136
# anakin
132137
if (WITH_ANAKIN AND WITH_MKL) # only needed in CI
133138
# anakin rnn1
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "paddle/fluid/inference/tests/api/tester_helper.h"
16+
17+
namespace paddle {
18+
namespace inference {
19+
20+
using paddle::PaddleTensor;
21+
using paddle::contrib::AnalysisConfig;
22+
23+
template <typename T>
24+
void GetValueFromStream(std::stringstream *ss, T *t) {
25+
(*ss) >> (*t);
26+
}
27+
28+
template <>
29+
void GetValueFromStream<std::string>(std::stringstream *ss, std::string *t) {
30+
*t = ss->str();
31+
}
32+
33+
// Split string to vector
34+
template <typename T>
35+
void Split(const std::string &line, char sep, std::vector<T> *v) {
36+
std::stringstream ss;
37+
T t;
38+
for (auto c : line) {
39+
if (c != sep) {
40+
ss << c;
41+
} else {
42+
GetValueFromStream<T>(&ss, &t);
43+
v->push_back(std::move(t));
44+
ss.str({});
45+
ss.clear();
46+
}
47+
}
48+
49+
if (!ss.str().empty()) {
50+
GetValueFromStream<T>(&ss, &t);
51+
v->push_back(std::move(t));
52+
ss.str({});
53+
ss.clear();
54+
}
55+
}
56+
57+
template <typename T>
58+
constexpr paddle::PaddleDType GetPaddleDType();
59+
60+
template <>
61+
constexpr paddle::PaddleDType GetPaddleDType<int64_t>() {
62+
return paddle::PaddleDType::INT64;
63+
}
64+
65+
template <>
66+
constexpr paddle::PaddleDType GetPaddleDType<float>() {
67+
return paddle::PaddleDType::FLOAT32;
68+
}
69+
70+
// Parse tensor from string
71+
template <typename T>
72+
bool ParseTensor(const std::string &field, paddle::PaddleTensor *tensor) {
73+
std::vector<std::string> data;
74+
Split(field, ':', &data);
75+
if (data.size() < 2) return false;
76+
77+
std::string shape_str = data[0];
78+
79+
std::vector<int> shape;
80+
Split(shape_str, ' ', &shape);
81+
82+
std::string mat_str = data[1];
83+
84+
std::vector<T> mat;
85+
Split(mat_str, ' ', &mat);
86+
87+
tensor->shape = shape;
88+
auto size =
89+
std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<int>()) *
90+
sizeof(T);
91+
tensor->data.Resize(size);
92+
std::copy(mat.begin(), mat.end(), static_cast<T *>(tensor->data.data()));
93+
tensor->dtype = GetPaddleDType<T>();
94+
95+
return true;
96+
}
97+
98+
// Parse input tensors from string
99+
bool ParseLine(const std::string &line,
100+
std::vector<paddle::PaddleTensor> *tensors) {
101+
std::vector<std::string> fields;
102+
Split(line, ';', &fields);
103+
104+
if (fields.size() < 5) return false;
105+
106+
tensors->clear();
107+
tensors->reserve(5);
108+
109+
int i = 0;
110+
// src_id
111+
paddle::PaddleTensor src_id;
112+
ParseTensor<int64_t>(fields[i++], &src_id);
113+
tensors->push_back(src_id);
114+
115+
// pos_id
116+
paddle::PaddleTensor pos_id;
117+
ParseTensor<int64_t>(fields[i++], &pos_id);
118+
tensors->push_back(pos_id);
119+
120+
// segment_id
121+
paddle::PaddleTensor segment_id;
122+
ParseTensor<int64_t>(fields[i++], &segment_id);
123+
tensors->push_back(segment_id);
124+
125+
// self_attention_bias
126+
paddle::PaddleTensor self_attention_bias;
127+
ParseTensor<float>(fields[i++], &self_attention_bias);
128+
tensors->push_back(self_attention_bias);
129+
130+
// next_segment_index
131+
paddle::PaddleTensor next_segment_index;
132+
ParseTensor<int64_t>(fields[i++], &next_segment_index);
133+
tensors->push_back(next_segment_index);
134+
135+
return true;
136+
}
137+
138+
bool LoadInputData(std::vector<std::vector<paddle::PaddleTensor>> *inputs) {
139+
if (FLAGS_infer_data.empty()) {
140+
LOG(ERROR) << "please set input data path";
141+
return false;
142+
}
143+
144+
std::ifstream fin(FLAGS_infer_data);
145+
std::string line;
146+
int sample = 0;
147+
148+
// The unit-test dataset only have 10 samples, each sample have 5 feeds.
149+
while (std::getline(fin, line)) {
150+
std::vector<paddle::PaddleTensor> feed_data;
151+
ParseLine(line, &feed_data);
152+
inputs->push_back(std::move(feed_data));
153+
sample++;
154+
if (!FLAGS_test_all_data && sample == FLAGS_batch_size) break;
155+
}
156+
LOG(INFO) << "number of samples: " << sample;
157+
158+
return true;
159+
}
160+
161+
void SetConfig(contrib::AnalysisConfig *config) {
162+
config->SetModel(FLAGS_infer_model);
163+
}
164+
165+
void profile(bool use_mkldnn = false) {
166+
contrib::AnalysisConfig config;
167+
SetConfig(&config);
168+
169+
if (use_mkldnn) {
170+
config.EnableMKLDNN();
171+
}
172+
173+
std::vector<PaddleTensor> outputs;
174+
std::vector<std::vector<PaddleTensor>> inputs;
175+
LoadInputData(&inputs);
176+
TestPrediction(reinterpret_cast<const PaddlePredictor::Config *>(&config),
177+
inputs, &outputs, FLAGS_num_threads);
178+
}
179+
180+
TEST(Analyzer_bert, profile) { profile(); }
181+
#ifdef PADDLE_WITH_MKLDNN
182+
TEST(Analyzer_bert, profile_mkldnn) { profile(true); }
183+
#endif
184+
185+
// Check the fuse status
186+
TEST(Analyzer_bert, fuse_statis) {
187+
AnalysisConfig cfg;
188+
SetConfig(&cfg);
189+
int num_ops;
190+
auto predictor = CreatePaddlePredictor<AnalysisConfig>(cfg);
191+
auto fuse_statis = GetFuseStatis(
192+
static_cast<AnalysisPredictor *>(predictor.get()), &num_ops);
193+
LOG(INFO) << "num_ops: " << num_ops;
194+
}
195+
196+
// Compare result of NativeConfig and AnalysisConfig
197+
void compare(bool use_mkldnn = false) {
198+
AnalysisConfig cfg;
199+
SetConfig(&cfg);
200+
if (use_mkldnn) {
201+
cfg.EnableMKLDNN();
202+
}
203+
204+
std::vector<std::vector<PaddleTensor>> inputs;
205+
LoadInputData(&inputs);
206+
CompareNativeAndAnalysis(
207+
reinterpret_cast<const PaddlePredictor::Config *>(&cfg), inputs);
208+
}
209+
210+
TEST(Analyzer_bert, compare) { compare(); }
211+
#ifdef PADDLE_WITH_MKLDNN
212+
TEST(Analyzer_bert, compare_mkldnn) { compare(true /* use_mkldnn */); }
213+
#endif
214+
215+
// Compare Deterministic result
216+
// TODO(luotao): Since each unit-test on CI only have 10 minutes, cancel this to
217+
// decrease the CI time.
218+
// TEST(Analyzer_bert, compare_determine) {
219+
// AnalysisConfig cfg;
220+
// SetConfig(&cfg);
221+
//
222+
// std::vector<std::vector<PaddleTensor>> inputs;
223+
// LoadInputData(&inputs);
224+
// CompareDeterministic(reinterpret_cast<const PaddlePredictor::Config
225+
// *>(&cfg),
226+
// inputs);
227+
// }
228+
} // namespace inference
229+
} // namespace paddle

0 commit comments

Comments
 (0)