Skip to content

Commit fa5036a

Browse files
committed
add test_all_data in test_analyzer_ner
1 parent b4fa3db commit fa5036a

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

paddle/fluid/inference/analysis/analyzer_ner_tester.cc

Lines changed: 27 additions & 3 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,12 +123,33 @@ 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);

0 commit comments

Comments
 (0)