@@ -25,6 +25,7 @@ DEFINE_string(infer_model, "", "model path");
25
25
DEFINE_string (infer_data, " " , " data path" );
26
26
DEFINE_int32 (batch_size, 10 , " batch size." );
27
27
DEFINE_int32 (repeat, 1 , " Running the inference program repeat times." );
28
+ DEFINE_bool (test_all_data, false , " Test the all dataset in data file." );
28
29
29
30
namespace paddle {
30
31
namespace inference {
@@ -35,6 +36,7 @@ struct DataRecord {
35
36
std::vector<size_t > lod; // two inputs have the same lod info.
36
37
size_t batch_iter{0 };
37
38
size_t batch_size{1 };
39
+ size_t num_samples; // total number of samples
38
40
DataRecord () = default ;
39
41
explicit DataRecord (const std::string &path, int batch_size = 1 )
40
42
: batch_size(batch_size) {
@@ -81,6 +83,7 @@ struct DataRecord {
81
83
word_data_all.push_back (std::move (word_data));
82
84
mention_data_all.push_back (std::move (mention_data));
83
85
}
86
+ num_samples = num_lines;
84
87
}
85
88
};
86
89
@@ -120,21 +123,38 @@ void TestChineseNERPrediction() {
120
123
auto predictor =
121
124
CreatePaddlePredictor<NativeConfig, PaddleEngineKind::kNative >(config);
122
125
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
+ }
124
149
// Prepare inputs.
150
+ DataRecord data (FLAGS_infer_data, FLAGS_batch_size);
125
151
PrepareInputs (&input_slots, &data, FLAGS_batch_size);
126
- std::vector<PaddleTensor> outputs;
127
152
128
- Timer timer;
129
153
timer.tic ();
130
154
for (int i = 0 ; i < FLAGS_repeat; i++) {
131
155
predictor->Run (input_slots, &outputs);
132
156
}
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);
138
158
139
159
PADDLE_ENFORCE (outputs.size (), 1UL );
140
160
auto &out = outputs[0 ];
0 commit comments