Skip to content

Commit f67483b

Browse files
luotao1Superjomn
authored andcommitted
add seq_conv UT (#13517)
* add multi_label UT * rename, fix typo, add fuse_statis check
1 parent c07b2a9 commit f67483b

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ set(TEXT_CLASSIFICATION_INSTALL_DIR "${INFERENCE_DEMO_INSTALL_DIR}/text_classifi
5858
download_model_and_data(${TEXT_CLASSIFICATION_INSTALL_DIR} "text-classification-Senta.tar.gz" "text_classification_data.txt.tar.gz")
5959
inference_analysis_api_test(test_analyzer_text_classification ${TEXT_CLASSIFICATION_INSTALL_DIR} analyzer_text_classification_tester.cc)
6060

61+
# seq_conv1
62+
set(SEQ_CONV1_INSTALL_DIR "${INFERENCE_DEMO_INSTALL_DIR}/seq_conv1")
63+
download_model_and_data(${SEQ_CONV1_INSTALL_DIR} "seq_conv1_model.tar.gz" "seq_conv1_data.txt.tar.gz")
64+
inference_analysis_api_test(test_analyzer_seq_conv1 ${SEQ_CONV1_INSTALL_DIR} analyzer_seq_conv1_tester.cc)
65+
6166
# ocr
6267
set(OCR_INSTALL_DIR "${INFERENCE_DEMO_INSTALL_DIR}/ocr")
6368
if (NOT EXISTS ${OCR_INSTALL_DIR})
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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+
struct DataRecord {
21+
std::vector<std::vector<int64_t>> title1_all, title2_all, title3_all, l1_all;
22+
std::vector<std::vector<int64_t>> title1, title2, title3, l1;
23+
std::vector<size_t> title1_lod, title2_lod, title3_lod, l1_lod;
24+
size_t batch_iter{0};
25+
size_t batch_size{1};
26+
size_t num_samples; // total number of samples
27+
DataRecord() = default;
28+
explicit DataRecord(const std::string &path, int batch_size = 1)
29+
: batch_size(batch_size) {
30+
Load(path);
31+
}
32+
DataRecord NextBatch() {
33+
DataRecord data;
34+
size_t batch_end = batch_iter + batch_size;
35+
// NOTE skip the final batch, if no enough data is provided.
36+
if (batch_end <= title1_all.size()) {
37+
data.title1_all.assign(title1_all.begin() + batch_iter,
38+
title1_all.begin() + batch_end);
39+
data.title2_all.assign(title2_all.begin() + batch_iter,
40+
title2_all.begin() + batch_end);
41+
data.title3_all.assign(title3_all.begin() + batch_iter,
42+
title3_all.begin() + batch_end);
43+
data.l1_all.assign(l1_all.begin() + batch_iter,
44+
l1_all.begin() + batch_end);
45+
// Prepare LoDs
46+
data.title1_lod.push_back(0);
47+
data.title2_lod.push_back(0);
48+
data.title3_lod.push_back(0);
49+
data.l1_lod.push_back(0);
50+
CHECK(!data.title1_all.empty());
51+
CHECK(!data.title2_all.empty());
52+
CHECK(!data.title3_all.empty());
53+
CHECK(!data.l1_all.empty());
54+
CHECK_EQ(data.title1_all.size(), data.title2_all.size());
55+
CHECK_EQ(data.title1_all.size(), data.title3_all.size());
56+
CHECK_EQ(data.title1_all.size(), data.l1_all.size());
57+
for (size_t j = 0; j < data.title1_all.size(); j++) {
58+
data.title1.push_back(data.title1_all[j]);
59+
data.title2.push_back(data.title2_all[j]);
60+
data.title3.push_back(data.title3_all[j]);
61+
data.l1.push_back(data.l1_all[j]);
62+
// calculate lod
63+
data.title1_lod.push_back(data.title1_lod.back() +
64+
data.title1_all[j].size());
65+
data.title2_lod.push_back(data.title2_lod.back() +
66+
data.title2_all[j].size());
67+
data.title3_lod.push_back(data.title3_lod.back() +
68+
data.title3_all[j].size());
69+
data.l1_lod.push_back(data.l1_lod.back() + data.l1_all[j].size());
70+
}
71+
}
72+
batch_iter += batch_size;
73+
return data;
74+
}
75+
void Load(const std::string &path) {
76+
std::ifstream file(path);
77+
std::string line;
78+
int num_lines = 0;
79+
while (std::getline(file, line)) {
80+
num_lines++;
81+
std::vector<std::string> data;
82+
split(line, '\t', &data);
83+
// load title1 data
84+
std::vector<int64_t> title1_data;
85+
split_to_int64(data[0], ' ', &title1_data);
86+
// load title2 data
87+
std::vector<int64_t> title2_data;
88+
split_to_int64(data[1], ' ', &title2_data);
89+
// load title3 data
90+
std::vector<int64_t> title3_data;
91+
split_to_int64(data[2], ' ', &title3_data);
92+
// load l1 data
93+
std::vector<int64_t> l1_data;
94+
split_to_int64(data[3], ' ', &l1_data);
95+
title1_all.push_back(std::move(title1_data));
96+
title2_all.push_back(std::move(title2_data));
97+
title3_all.push_back(std::move(title3_data));
98+
l1_all.push_back(std::move(l1_data));
99+
}
100+
num_samples = num_lines;
101+
}
102+
};
103+
104+
void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
105+
int batch_size) {
106+
PaddleTensor title1_tensor, title2_tensor, title3_tensor, l1_tensor;
107+
title1_tensor.name = "title1";
108+
title2_tensor.name = "title2";
109+
title3_tensor.name = "title3";
110+
l1_tensor.name = "l1";
111+
auto one_batch = data->NextBatch();
112+
int title1_size = one_batch.title1_lod[one_batch.title1_lod.size() - 1];
113+
title1_tensor.shape.assign({title1_size, 1});
114+
title1_tensor.lod.assign({one_batch.title1_lod});
115+
int title2_size = one_batch.title2_lod[one_batch.title2_lod.size() - 1];
116+
title2_tensor.shape.assign({title2_size, 1});
117+
title2_tensor.lod.assign({one_batch.title2_lod});
118+
int title3_size = one_batch.title3_lod[one_batch.title3_lod.size() - 1];
119+
title3_tensor.shape.assign({title3_size, 1});
120+
title3_tensor.lod.assign({one_batch.title3_lod});
121+
int l1_size = one_batch.l1_lod[one_batch.l1_lod.size() - 1];
122+
l1_tensor.shape.assign({l1_size, 1});
123+
l1_tensor.lod.assign({one_batch.l1_lod});
124+
125+
// assign data
126+
TensorAssignData<int64_t>(&title1_tensor, one_batch.title1);
127+
TensorAssignData<int64_t>(&title2_tensor, one_batch.title2);
128+
TensorAssignData<int64_t>(&title3_tensor, one_batch.title3);
129+
TensorAssignData<int64_t>(&l1_tensor, one_batch.l1);
130+
// Set inputs.
131+
input_slots->assign({title1_tensor, title2_tensor, title3_tensor, l1_tensor});
132+
for (auto &tensor : *input_slots) {
133+
tensor.dtype = PaddleDType::INT64;
134+
}
135+
}
136+
137+
void SetConfig(AnalysisConfig *cfg) {
138+
cfg->model_dir = FLAGS_infer_model;
139+
cfg->use_gpu = false;
140+
cfg->device = 0;
141+
cfg->specify_input_name = true;
142+
cfg->enable_ir_optim = true;
143+
}
144+
145+
void SetInput(std::vector<std::vector<PaddleTensor>> *inputs) {
146+
DataRecord data(FLAGS_infer_data, FLAGS_batch_size);
147+
std::vector<PaddleTensor> input_slots;
148+
int epoch = FLAGS_test_all_data ? data.num_samples / FLAGS_batch_size : 1;
149+
LOG(INFO) << "number of samples: " << epoch * FLAGS_batch_size;
150+
for (int bid = 0; bid < epoch; ++bid) {
151+
PrepareInputs(&input_slots, &data, FLAGS_batch_size);
152+
(*inputs).emplace_back(input_slots);
153+
}
154+
}
155+
156+
// Easy for profiling independently.
157+
TEST(Analyzer_seq_conv1, profile) {
158+
AnalysisConfig cfg;
159+
SetConfig(&cfg);
160+
std::vector<PaddleTensor> outputs;
161+
162+
std::vector<std::vector<PaddleTensor>> input_slots_all;
163+
SetInput(&input_slots_all);
164+
TestPrediction(cfg, input_slots_all, &outputs, FLAGS_num_threads);
165+
166+
if (FLAGS_num_threads == 1 && !FLAGS_test_all_data) {
167+
// the first inference result
168+
PADDLE_ENFORCE_EQ(outputs.size(), 1UL);
169+
size_t size = GetSize(outputs[0]);
170+
PADDLE_ENFORCE_GT(size, 0);
171+
float *result = static_cast<float *>(outputs[0].data.data());
172+
// output is probability, which is in (0, 1).
173+
for (size_t i = 0; i < size; i++) {
174+
EXPECT_GT(result[i], 0);
175+
EXPECT_LT(result[i], 1);
176+
}
177+
}
178+
}
179+
180+
// Check the fuse status
181+
TEST(Analyzer_seq_conv1, fuse_statis) {
182+
AnalysisConfig cfg;
183+
SetConfig(&cfg);
184+
int num_ops;
185+
auto fuse_statis = GetFuseStatis(cfg, &num_ops);
186+
}
187+
188+
// Compare result of NativeConfig and AnalysisConfig
189+
TEST(Analyzer_seq_conv1, compare) {
190+
AnalysisConfig cfg;
191+
SetConfig(&cfg);
192+
193+
std::vector<std::vector<PaddleTensor>> input_slots_all;
194+
SetInput(&input_slots_all);
195+
CompareNativeAndAnalysis(cfg, input_slots_all);
196+
}
197+
198+
} // namespace inference
199+
} // namespace paddle

0 commit comments

Comments
 (0)