Skip to content

Commit f76f42c

Browse files
authored
Merge pull request #13260 from luotao1/all_data
add test_all_data in test_analyzer_ner
2 parents e2d325a + 00c7230 commit f76f42c

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

paddle/fluid/inference/analysis/analyzer_lac_tester.cc

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@ void GetOneBatch(std::vector<PaddleTensor> *input_slots, DataRecord *data,
114114
PADDLE_ENFORCE_EQ(batch_size, static_cast<int>(one_batch.lod.size() - 1));
115115
input_slots->assign({input_tensor});
116116
}
117-
static void PrintTime(const double latency, const int bs, const int repeat) {
118-
LOG(INFO) << "===========profile result===========";
119-
LOG(INFO) << "batch_size: " << bs << ", repeat: " << repeat
120-
<< ", avg latency: " << latency / repeat << "ms";
121-
LOG(INFO) << "=====================================";
122-
}
123117
void BenchAllData(const std::string &model_path, const std::string &data_file,
124118
const int batch_size, const int repeat) {
125119
NativeConfig config;
@@ -145,7 +139,7 @@ void BenchAllData(const std::string &model_path, const std::string &data_file,
145139
sum += timer.toc();
146140
}
147141
}
148-
PrintTime(sum, batch_size, repeat);
142+
PrintTime(batch_size, repeat, 1, 0, sum / repeat);
149143
}
150144
const int64_t lac_ref_data[] = {24, 25, 25, 25, 38, 30, 31, 14, 15, 44, 24, 25,
151145
25, 25, 25, 25, 44, 24, 25, 25, 25, 36, 42, 43,
@@ -176,7 +170,7 @@ void TestLACPrediction(const std::string &model_path,
176170
for (int i = 0; i < repeat; i++) {
177171
predictor->Run(input_slots, &outputs_slots);
178172
}
179-
PrintTime(timer.toc(), batch_size, repeat);
173+
PrintTime(batch_size, repeat, 1, 0, timer.toc() / repeat);
180174
EXPECT_EQ(outputs_slots.size(), 1UL);
181175
auto &out = outputs_slots[0];
182176
size_t size = std::accumulate(out.shape.begin(), out.shape.end(), 1,

paddle/fluid/inference/analysis/analyzer_ner_tester.cc

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ DEFINE_string(infer_model, "", "model path");
2525
DEFINE_string(infer_data, "", "data path");
2626
DEFINE_int32(batch_size, 10, "batch size.");
2727
DEFINE_int32(repeat, 1, "Running the inference program repeat times.");
28+
DEFINE_bool(test_all_data, false, "Test the all dataset in data file.");
2829

2930
namespace paddle {
3031
namespace inference {
@@ -35,6 +36,7 @@ struct DataRecord {
3536
std::vector<size_t> lod; // two inputs have the same lod info.
3637
size_t batch_iter{0};
3738
size_t batch_size{1};
39+
size_t num_samples; // total number of samples
3840
DataRecord() = default;
3941
explicit DataRecord(const std::string &path, int batch_size = 1)
4042
: batch_size(batch_size) {
@@ -81,6 +83,7 @@ struct DataRecord {
8183
word_data_all.push_back(std::move(word_data));
8284
mention_data_all.push_back(std::move(mention_data));
8385
}
86+
num_samples = num_lines;
8487
}
8588
};
8689

@@ -120,21 +123,38 @@ void TestChineseNERPrediction() {
120123
auto predictor =
121124
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative>(config);
122125
std::vector<PaddleTensor> input_slots;
123-
DataRecord data(FLAGS_infer_data, FLAGS_batch_size);
126+
std::vector<PaddleTensor> outputs;
127+
Timer timer;
128+
129+
if (FLAGS_test_all_data) {
130+
LOG(INFO) << "test all data";
131+
double sum = 0;
132+
size_t num_samples;
133+
for (int i = 0; i < FLAGS_repeat; i++) {
134+
DataRecord data(FLAGS_infer_data, FLAGS_batch_size);
135+
num_samples = data.num_samples;
136+
for (size_t bid = 0; bid < num_samples; ++bid) {
137+
PrepareInputs(&input_slots, &data, FLAGS_batch_size);
138+
timer.tic();
139+
predictor->Run(input_slots, &outputs);
140+
sum += timer.toc();
141+
}
142+
}
143+
LOG(INFO) << "total number of samples: " << num_samples;
144+
PrintTime(FLAGS_batch_size, FLAGS_repeat, 1, 0, sum / FLAGS_repeat);
145+
LOG(INFO) << "average latency of each sample: "
146+
<< sum / FLAGS_repeat / num_samples;
147+
return;
148+
}
124149
// Prepare inputs.
150+
DataRecord data(FLAGS_infer_data, FLAGS_batch_size);
125151
PrepareInputs(&input_slots, &data, FLAGS_batch_size);
126-
std::vector<PaddleTensor> outputs;
127152

128-
Timer timer;
129153
timer.tic();
130154
for (int i = 0; i < FLAGS_repeat; i++) {
131155
predictor->Run(input_slots, &outputs);
132156
}
133-
LOG(INFO) << "===========profile result===========";
134-
LOG(INFO) << "batch_size: " << FLAGS_batch_size
135-
<< ", repeat: " << FLAGS_repeat
136-
<< ", latency: " << timer.toc() / FLAGS_repeat << "ms";
137-
LOG(INFO) << "=====================================";
157+
PrintTime(FLAGS_batch_size, FLAGS_repeat, 1, 0, timer.toc() / FLAGS_repeat);
138158

139159
PADDLE_ENFORCE(outputs.size(), 1UL);
140160
auto &out = outputs[0];

paddle/fluid/inference/analysis/analyzer_text_classification_tester.cc

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,11 @@ DEFINE_int32(repeat, 1, "How many times to repeat run.");
3131
DEFINE_int32(topn, -1, "Run top n batches of data to save time");
3232

3333
namespace paddle {
34-
35-
template <typename T>
36-
std::string to_string(const std::vector<T> &vec) {
37-
std::stringstream ss;
38-
for (const auto &c : vec) {
39-
ss << c << " ";
40-
}
41-
return ss.str();
42-
}
43-
44-
void PrintTime(const double latency, const int bs, const int repeat) {
45-
LOG(INFO) << "===========profile result===========";
46-
LOG(INFO) << "batch_size: " << bs << ", repeat: " << repeat
47-
<< ", avg latency: " << latency / repeat << "ms";
48-
LOG(INFO) << "=====================================";
49-
}
34+
namespace inference {
5035

5136
struct DataReader {
52-
DataReader(const std::string &path) : file(new std::ifstream(path)) {}
37+
explicit DataReader(const std::string &path)
38+
: file(new std::ifstream(path)) {}
5339

5440
bool NextBatch(PaddleTensor *tensor, int batch_size) {
5541
PADDLE_ENFORCE_EQ(batch_size, 1);
@@ -107,8 +93,7 @@ void Main(int batch_size) {
10793
++num_batches;
10894
}
10995
}
110-
111-
PrintTime(sum, batch_size, num_batches);
96+
PrintTime(batch_size, FLAGS_repeat, 1, 0, sum / FLAGS_repeat);
11297

11398
// Get output
11499
LOG(INFO) << "get outputs " << output_slots.size();
@@ -129,4 +114,5 @@ void Main(int batch_size) {
129114

130115
TEST(text_classification, basic) { Main(FLAGS_batch_size); }
131116

117+
} // namespace inference
132118
} // namespace paddle

0 commit comments

Comments
 (0)