Skip to content

Commit b758875

Browse files
luotao1Superjomn
authored andcommitted
Refine infer api test (#13472)
* refine analyzer_nlp_tester * refine analyzer_rnn/vis_tester
1 parent d4570f0 commit b758875

File tree

7 files changed

+383
-420
lines changed

7 files changed

+383
-420
lines changed

paddle/fluid/inference/tests/api/analyzer_lac_tester.cc

Lines changed: 57 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -103,108 +103,74 @@ void GetOneBatch(std::vector<PaddleTensor> *input_slots, DataRecord *data,
103103
input_slots->assign({input_tensor});
104104
}
105105

106-
const int64_t lac_ref_data[] = {24, 25, 25, 25, 38, 30, 31, 14, 15, 44, 24, 25,
107-
25, 25, 25, 25, 44, 24, 25, 25, 25, 36, 42, 43,
108-
44, 14, 15, 44, 14, 15, 44, 14, 15, 44, 38, 39,
109-
14, 15, 44, 22, 23, 23, 23, 23, 23, 23, 23};
110-
111-
void TestLACPrediction(const std::string &model_path,
112-
const std::string &data_file, const int batch_size,
113-
const int repeat, bool use_analysis = false) {
114-
AnalysisConfig cfg;
115-
cfg.model_dir = model_path;
116-
cfg.use_gpu = false;
117-
cfg.device = 0;
118-
cfg.specify_input_name = true;
119-
cfg.enable_ir_optim = true;
106+
void SetConfig(AnalysisConfig *cfg) {
107+
cfg->model_dir = FLAGS_infer_model;
108+
cfg->use_gpu = false;
109+
cfg->device = 0;
110+
cfg->specify_input_name = true;
111+
cfg->enable_ir_optim = true;
112+
}
120113

121-
std::vector<PaddleTensor> input_slots, outputs_slots;
122-
DataRecord data(data_file, batch_size);
123-
GetOneBatch(&input_slots, &data, batch_size);
124-
std::unique_ptr<PaddlePredictor> predictor;
125-
if (use_analysis) {
126-
predictor =
127-
CreatePaddlePredictor<AnalysisConfig, PaddleEngineKind::kAnalysis>(cfg);
128-
} else {
129-
predictor =
130-
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(cfg);
131-
}
132-
for (int i = 0; i < FLAGS_burning; i++) {
133-
predictor->Run(input_slots, &outputs_slots);
114+
void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) {
115+
DataRecord data(FLAGS_infer_data, FLAGS_batch_size);
116+
std::vector<PaddleTensor> input_slots;
117+
int epoch = FLAGS_test_all_data ? data.batched_datas.size() : 1;
118+
LOG(INFO) << "number of samples: " << epoch;
119+
for (int bid = 0; bid < epoch; ++bid) {
120+
GetOneBatch(&input_slots, &data, FLAGS_batch_size);
121+
(*inputs).emplace_back(input_slots);
134122
}
135-
Timer timer;
136-
if (FLAGS_test_all_data) {
137-
LOG(INFO) << "test all data";
138-
std::vector<std::vector<PaddleTensor>> input_slots_all;
139-
for (size_t bid = 0; bid < data.batched_datas.size(); ++bid) {
140-
GetOneBatch(&input_slots, &data, batch_size);
141-
input_slots_all.emplace_back(input_slots);
142-
}
143-
LOG(INFO) << "total number of samples: " << data.datasets.size();
144-
TestPrediction(cfg, input_slots_all, &outputs_slots, FLAGS_num_threads);
145-
return;
146-
}
147-
timer.tic();
148-
for (int i = 0; i < repeat; i++) {
149-
predictor->Run(input_slots, &outputs_slots);
150-
}
151-
PrintTime(batch_size, repeat, 1, 0, timer.toc() / repeat);
123+
}
152124

153-
// check result
154-
EXPECT_EQ(outputs_slots.size(), 1UL);
155-
auto &out = outputs_slots[0];
156-
size_t size = std::accumulate(out.shape.begin(), out.shape.end(), 1,
157-
[](int a, int b) { return a * b; });
158-
size_t batch1_size = sizeof(lac_ref_data) / sizeof(int64_t);
159-
PADDLE_ENFORCE_GT(size, 0);
160-
EXPECT_GE(size, batch1_size);
161-
int64_t *pdata = static_cast<int64_t *>(out.data.data());
162-
for (size_t i = 0; i < batch1_size; ++i) {
163-
EXPECT_EQ(pdata[i], lac_ref_data[i]);
164-
}
125+
// Easy for profiling independently.
126+
TEST(Analyzer_LAC, profile) {
127+
AnalysisConfig cfg;
128+
SetConfig(&cfg);
129+
std::vector<PaddleTensor> outputs;
165130

166-
if (use_analysis) {
167-
// run once for comparion as reference
168-
auto ref_predictor =
169-
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(cfg);
170-
std::vector<PaddleTensor> ref_outputs_slots;
171-
ref_predictor->Run(input_slots, &ref_outputs_slots);
172-
CompareResult(ref_outputs_slots, outputs_slots);
131+
std::vector<std::vector<PaddleTensor>> input_slots_all;
132+
SetInput(&input_slots_all);
133+
TestPrediction(cfg, input_slots_all, &outputs, FLAGS_num_threads);
173134

174-
AnalysisPredictor *analysis_predictor =
175-
dynamic_cast<AnalysisPredictor *>(predictor.get());
176-
auto &fuse_statis = analysis_predictor->analysis_argument()
177-
.Get<std::unordered_map<std::string, int>>(
178-
framework::ir::kFuseStatisAttr);
179-
for (auto &item : fuse_statis) {
180-
LOG(INFO) << "fused " << item.first << " " << item.second;
181-
}
182-
int num_ops = 0;
183-
for (auto &node :
184-
analysis_predictor->analysis_argument().main_dfg->nodes.nodes()) {
185-
if (node->IsFunction()) {
186-
++num_ops;
187-
}
135+
if (FLAGS_num_threads == 1 && !FLAGS_test_all_data) {
136+
// the first inference result
137+
const int64_t lac_ref_data[] = {
138+
24, 25, 25, 25, 38, 30, 31, 14, 15, 44, 24, 25, 25, 25, 25, 25,
139+
44, 24, 25, 25, 25, 36, 42, 43, 44, 14, 15, 44, 14, 15, 44, 14,
140+
15, 44, 38, 39, 14, 15, 44, 22, 23, 23, 23, 23, 23, 23, 23};
141+
PADDLE_ENFORCE_EQ(outputs.size(), 1UL);
142+
size_t size = GetSize(outputs[0]);
143+
size_t batch1_size = sizeof(lac_ref_data) / sizeof(int64_t);
144+
PADDLE_ENFORCE_GE(size, batch1_size);
145+
int64_t *pdata = static_cast<int64_t *>(outputs[0].data.data());
146+
for (size_t i = 0; i < batch1_size; ++i) {
147+
EXPECT_EQ(pdata[i], lac_ref_data[i]);
188148
}
189-
LOG(INFO) << "has num ops: " << num_ops;
190-
ASSERT_TRUE(fuse_statis.count("fc_fuse"));
191-
ASSERT_TRUE(fuse_statis.count("fc_gru_fuse"));
192-
EXPECT_EQ(fuse_statis.at("fc_fuse"), 1);
193-
EXPECT_EQ(fuse_statis.at("fc_gru_fuse"), 4);
194-
EXPECT_EQ(num_ops, 11);
195149
}
196150
}
197151

198-
TEST(Analyzer_LAC, native) {
199-
LOG(INFO) << "LAC with native";
200-
TestLACPrediction(FLAGS_infer_model, FLAGS_infer_data, FLAGS_batch_size,
201-
FLAGS_repeat);
152+
// Check the fuse status
153+
TEST(Analyzer_LAC, fuse_statis) {
154+
AnalysisConfig cfg;
155+
SetConfig(&cfg);
156+
157+
int num_ops;
158+
auto fuse_statis = GetFuseStatis(cfg, &num_ops);
159+
ASSERT_TRUE(fuse_statis.count("fc_fuse"));
160+
ASSERT_TRUE(fuse_statis.count("fc_gru_fuse"));
161+
EXPECT_EQ(fuse_statis.at("fc_fuse"), 1);
162+
EXPECT_EQ(fuse_statis.at("fc_gru_fuse"), 4);
163+
EXPECT_EQ(num_ops, 11);
202164
}
203165

204-
TEST(Analyzer_LAC, analysis) {
205-
LOG(INFO) << "LAC with analysis";
206-
TestLACPrediction(FLAGS_infer_model, FLAGS_infer_data, FLAGS_batch_size,
207-
FLAGS_repeat, true);
166+
// Compare result of NativeConfig and AnalysisConfig
167+
TEST(Analyzer_LAC, compare) {
168+
AnalysisConfig cfg;
169+
SetConfig(&cfg);
170+
171+
std::vector<std::vector<PaddleTensor>> input_slots_all;
172+
SetInput(&input_slots_all);
173+
CompareNativeAndAnalysis(cfg, input_slots_all);
208174
}
209175

210176
} // namespace analysis

paddle/fluid/inference/tests/api/analyzer_ner_tester.cc

Lines changed: 56 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -95,97 +95,73 @@ void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
9595
}
9696
}
9797

98-
// the first inference result
99-
const int chinese_ner_result_data[] = {30, 45, 41, 48, 17, 26,
100-
48, 39, 38, 16, 25};
101-
102-
void TestChineseNERPrediction(bool use_analysis) {
103-
AnalysisConfig cfg;
104-
cfg.prog_file = FLAGS_infer_model + "/__model__";
105-
cfg.param_file = FLAGS_infer_model + "/param";
106-
cfg.use_gpu = false;
107-
cfg.device = 0;
108-
cfg.specify_input_name = true;
109-
cfg.enable_ir_optim = true;
110-
111-
std::vector<PaddleTensor> input_slots, outputs;
112-
std::unique_ptr<PaddlePredictor> predictor;
113-
Timer timer;
114-
if (use_analysis) {
115-
predictor =
116-
CreatePaddlePredictor<AnalysisConfig, PaddleEngineKind::kAnalysis>(cfg);
117-
} else {
118-
predictor =
119-
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(cfg);
120-
}
98+
void SetConfig(AnalysisConfig *cfg) {
99+
cfg->prog_file = FLAGS_infer_model + "/__model__";
100+
cfg->param_file = FLAGS_infer_model + "/param";
101+
cfg->use_gpu = false;
102+
cfg->device = 0;
103+
cfg->specify_input_name = true;
104+
cfg->enable_ir_optim = true;
105+
}
121106

122-
if (FLAGS_test_all_data) {
123-
LOG(INFO) << "test all data";
124-
DataRecord data(FLAGS_infer_data, FLAGS_batch_size);
125-
std::vector<std::vector<PaddleTensor>> input_slots_all;
126-
for (size_t bid = 0; bid < data.num_samples / FLAGS_batch_size; ++bid) {
127-
PrepareInputs(&input_slots, &data, FLAGS_batch_size);
128-
input_slots_all.emplace_back(input_slots);
129-
}
130-
LOG(INFO) << "total number of samples: " << data.num_samples;
131-
TestPrediction(cfg, input_slots_all, &outputs, FLAGS_num_threads);
132-
return;
133-
}
134-
// Prepare inputs.
107+
void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) {
135108
DataRecord data(FLAGS_infer_data, FLAGS_batch_size);
136-
PrepareInputs(&input_slots, &data, FLAGS_batch_size);
137-
138-
timer.tic();
139-
for (int i = 0; i < FLAGS_repeat; i++) {
140-
predictor->Run(input_slots, &outputs);
109+
std::vector<PaddleTensor> input_slots;
110+
int epoch = FLAGS_test_all_data ? data.num_samples / FLAGS_batch_size : 1;
111+
LOG(INFO) << "number of samples: " << epoch * FLAGS_batch_size;
112+
for (int bid = 0; bid < epoch; ++bid) {
113+
PrepareInputs(&input_slots, &data, FLAGS_batch_size);
114+
(*inputs).emplace_back(input_slots);
141115
}
142-
PrintTime(FLAGS_batch_size, FLAGS_repeat, 1, 0, timer.toc() / FLAGS_repeat);
116+
}
143117

144-
PADDLE_ENFORCE(outputs.size(), 1UL);
145-
auto &out = outputs[0];
146-
size_t size = std::accumulate(out.shape.begin(), out.shape.end(), 1,
147-
[](int a, int b) { return a * b; });
148-
PADDLE_ENFORCE_GT(size, 0);
149-
int64_t *result = static_cast<int64_t *>(out.data.data());
150-
for (size_t i = 0; i < std::min(11UL, size); i++) {
151-
PADDLE_ENFORCE(result[i], chinese_ner_result_data[i]);
152-
}
118+
// Easy for profiling independently.
119+
TEST(Analyzer_Chinese_ner, profile) {
120+
AnalysisConfig cfg;
121+
SetConfig(&cfg);
122+
std::vector<PaddleTensor> outputs;
153123

154-
if (use_analysis) {
155-
// run once for comparion as reference
156-
auto ref_predictor =
157-
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(cfg);
158-
std::vector<PaddleTensor> ref_outputs_slots;
159-
ref_predictor->Run(input_slots, &ref_outputs_slots);
160-
CompareResult(ref_outputs_slots, outputs);
124+
std::vector<std::vector<PaddleTensor>> input_slots_all;
125+
SetInput(&input_slots_all);
126+
TestPrediction(cfg, input_slots_all, &outputs, FLAGS_num_threads);
161127

162-
AnalysisPredictor *analysis_predictor =
163-
dynamic_cast<AnalysisPredictor *>(predictor.get());
164-
auto &fuse_statis = analysis_predictor->analysis_argument()
165-
.Get<std::unordered_map<std::string, int>>(
166-
framework::ir::kFuseStatisAttr);
167-
for (auto &item : fuse_statis) {
168-
LOG(INFO) << "fused " << item.first << " " << item.second;
169-
}
170-
int num_ops = 0;
171-
for (auto &node :
172-
analysis_predictor->analysis_argument().main_dfg->nodes.nodes()) {
173-
if (node->IsFunction()) {
174-
++num_ops;
175-
}
128+
if (FLAGS_num_threads == 1 && !FLAGS_test_all_data) {
129+
// the first inference result
130+
const int chinese_ner_result_data[] = {30, 45, 41, 48, 17, 26,
131+
48, 39, 38, 16, 25};
132+
PADDLE_ENFORCE_EQ(outputs.size(), 1UL);
133+
size_t size = GetSize(outputs[0]);
134+
PADDLE_ENFORCE_GT(size, 0);
135+
int64_t *result = static_cast<int64_t *>(outputs[0].data.data());
136+
for (size_t i = 0; i < std::min(11UL, size); i++) {
137+
EXPECT_EQ(result[i], chinese_ner_result_data[i]);
176138
}
177-
LOG(INFO) << "has num ops: " << num_ops;
178-
ASSERT_TRUE(fuse_statis.count("fc_fuse"));
179-
ASSERT_TRUE(fuse_statis.count("fc_gru_fuse"));
180-
EXPECT_EQ(fuse_statis.at("fc_fuse"), 1);
181-
EXPECT_EQ(fuse_statis.at("fc_gru_fuse"), 2);
182-
EXPECT_EQ(num_ops, 14);
183139
}
184140
}
185141

186-
TEST(Analyzer_Chinese_ner, native) { TestChineseNERPrediction(false); }
142+
// Check the fuse status
143+
TEST(Analyzer_Chinese_ner, fuse_statis) {
144+
AnalysisConfig cfg;
145+
SetConfig(&cfg);
187146

188-
TEST(Analyzer_Chinese_ner, analysis) { TestChineseNERPrediction(true); }
147+
int num_ops;
148+
auto fuse_statis = GetFuseStatis(cfg, &num_ops);
149+
ASSERT_TRUE(fuse_statis.count("fc_fuse"));
150+
ASSERT_TRUE(fuse_statis.count("fc_gru_fuse"));
151+
EXPECT_EQ(fuse_statis.at("fc_fuse"), 1);
152+
EXPECT_EQ(fuse_statis.at("fc_gru_fuse"), 2);
153+
EXPECT_EQ(num_ops, 14);
154+
}
155+
156+
// Compare result of NativeConfig and AnalysisConfig
157+
TEST(Analyzer_Chinese_ner, compare) {
158+
AnalysisConfig cfg;
159+
SetConfig(&cfg);
160+
161+
std::vector<std::vector<PaddleTensor>> input_slots_all;
162+
SetInput(&input_slots_all);
163+
CompareNativeAndAnalysis(cfg, input_slots_all);
164+
}
189165

190166
} // namespace inference
191167
} // namespace paddle

0 commit comments

Comments
 (0)