23
23
24
24
DEFINE_string (infer_ditu_rnn_model, " " , " model path for ditu RNN" );
25
25
DEFINE_string (infer_ditu_rnn_data, " " , " data path for ditu RNN" );
26
+ DEFINE_int32 (batch_size, 10 , " batch size." );
27
+ DEFINE_int32 (repeat, 1 , " Running the inference program repeat times." );
26
28
27
29
namespace paddle {
28
30
namespace inference {
@@ -92,7 +94,7 @@ struct DataRecord {
92
94
size_t batch_iter{0 };
93
95
size_t batch_size{1 };
94
96
DataRecord () = default ;
95
- DataRecord (const std::string &path, int batch_size = 1 )
97
+ explicit DataRecord (const std::string &path, int batch_size = 1 )
96
98
: batch_size(batch_size) {
97
99
Load (path);
98
100
}
@@ -165,7 +167,6 @@ struct DataRecord {
165
167
};
166
168
void PrepareInputs (std::vector<PaddleTensor> *input_slots, DataRecord *data,
167
169
int batch_size) {
168
- // DataRecord data(FLAGS_datapath, batch_size);
169
170
PaddleTensor lod_attention_tensor, init_zero_tensor, lod_tensor_tensor,
170
171
week_tensor, minute_tensor;
171
172
lod_attention_tensor.name = " data_lod_attention" ;
@@ -174,28 +175,33 @@ void PrepareInputs(std::vector<PaddleTensor> *input_slots, DataRecord *data,
174
175
week_tensor.name = " week" ;
175
176
minute_tensor.name = " minute" ;
176
177
auto one_batch = data->NextBatch ();
177
- // clang-format off
178
- std::vector <int > rnn_link_data_shape
179
- ({ static_cast < int >(one_batch. rnn_link_data . size ()), static_cast <int >(one_batch.rnn_link_data .front ().size ())});
178
+ std::vector< int > rnn_link_data_shape (
179
+ { static_cast <int >(one_batch. rnn_link_data . size ()),
180
+ static_cast <int >(one_batch.rnn_link_data .front ().size ())});
180
181
lod_attention_tensor.shape .assign ({1 , 2 });
181
182
lod_attention_tensor.lod .assign ({one_batch.lod1 , one_batch.lod2 });
182
183
init_zero_tensor.shape .assign ({batch_size, 15 });
183
184
init_zero_tensor.lod .assign ({one_batch.lod3 });
184
185
lod_tensor_tensor.shape = rnn_link_data_shape;
185
186
lod_tensor_tensor.lod .assign ({one_batch.lod1 });
186
- week_tensor.shape .assign ({(int ) one_batch.rnn_week_datas .size (), (int ) one_batch.rnn_week_datas .front ().size ()});
187
+ // clang-format off
188
+ week_tensor.shape .assign (
189
+ {static_cast <int >(one_batch.rnn_week_datas .size ()),
190
+ static_cast <int >(one_batch.rnn_week_datas .front ().size ())});
187
191
week_tensor.lod .assign ({one_batch.lod3 });
188
- minute_tensor.shape .assign ({(int ) one_batch.rnn_minute_datas .size (),
189
- (int ) one_batch.rnn_minute_datas .front ().size ()});
192
+ minute_tensor.shape .assign (
193
+ {static_cast <int >(one_batch.rnn_minute_datas .size ()),
194
+ static_cast <int >(one_batch.rnn_minute_datas .front ().size ())});
190
195
minute_tensor.lod .assign ({one_batch.lod3 });
196
+ // clang-format on
191
197
// assign data
192
- TensorAssignData (&lod_attention_tensor, std::vector<std::vector<float >>({{0 , 0 }}));
198
+ TensorAssignData (&lod_attention_tensor,
199
+ std::vector<std::vector<float >>({{0 , 0 }}));
193
200
std::vector<float > tmp_zeros (batch_size * 15 , 0 .);
194
201
TensorAssignData (&init_zero_tensor, {tmp_zeros});
195
202
TensorAssignData (&lod_tensor_tensor, one_batch.rnn_link_data );
196
203
TensorAssignData (&week_tensor, one_batch.rnn_week_datas );
197
204
TensorAssignData (&minute_tensor, one_batch.rnn_minute_datas );
198
- // clang-format on
199
205
// Set inputs.
200
206
auto init_zero_tensor1 = init_zero_tensor;
201
207
init_zero_tensor1.name = " hidden_init" ;
@@ -231,12 +237,9 @@ std::string DescribeTensor(const PaddleTensor &tensor) {
231
237
os << " \n " ;
232
238
os << " - data: " ;
233
239
234
- // clang-format off
235
- int dim = std::accumulate (tensor.shape .begin (),
236
- tensor.shape .end (),
237
- 1 ,
238
- [](int a, int b) { return a * b; }); // clang-format on
239
- for (size_t i = 0 ; i < dim; i++) {
240
+ int dim = std::accumulate (tensor.shape .begin (), tensor.shape .end (), 1 ,
241
+ [](int a, int b) { return a * b; });
242
+ for (int i = 0 ; i < dim; i++) {
240
243
os << static_cast <float *>(tensor.data .data ())[i] << " " ;
241
244
}
242
245
os << ' \n ' ;
@@ -300,13 +303,16 @@ void TestDituRNNPrediction(const std::string &model_path,
300
303
for (int i = 0 ; i < num_times; i++) {
301
304
predictor->Run (input_slots, &outputs);
302
305
}
303
- LOG (INFO) << " time/batch: " << timer.toc () / num_times;
306
+ LOG (INFO) << " ===========profile result===========" ;
307
+ LOG (INFO) << " batch_size: " << batch_size << " , repeat: " << num_times
308
+ << " , latency: " << timer.toc () / num_times << " ms" ;
309
+ LOG (INFO) << " =====================================" ;
304
310
305
311
for (auto &out : outputs) {
306
312
size_t size = std::accumulate (out.shape .begin (), out.shape .end (), 1 ,
307
313
[](int a, int b) { return a * b; });
308
314
float *data = static_cast <float *>(out.data .data ());
309
- for (int i = 0 ;
315
+ for (size_t i = 0 ;
310
316
i < std::min (sizeof (ditu_rnn_target_data) / sizeof (float ), size);
311
317
i++) {
312
318
EXPECT_NEAR (data[i], ditu_rnn_target_data[i], 1e-3 );
@@ -336,22 +342,22 @@ TEST(Analyzer, SupportIRPass) {
336
342
// Directly infer with the original model.
337
343
TEST (Analyzer, DituRNN_without_analysis) {
338
344
TestDituRNNPrediction (FLAGS_infer_ditu_rnn_model, FLAGS_infer_ditu_rnn_data,
339
- 10 , false , false );
345
+ FLAGS_batch_size , false , false , FLAGS_repeat );
340
346
}
341
347
342
348
// Inference with the original model with the analysis turned on, the analysis
343
349
// module will transform the program to a data flow graph.
344
350
TEST (Analyzer, DituRNN_with_analysis) {
345
351
LOG (INFO) << " ditu rnn with analysis" ;
346
352
TestDituRNNPrediction (FLAGS_infer_ditu_rnn_model, FLAGS_infer_ditu_rnn_data,
347
- 10 , true , false , 1 );
353
+ FLAGS_batch_size , true , false , FLAGS_repeat );
348
354
}
349
355
350
356
// Inference with analysis and IR. The IR module will fuse some large kernels.
351
357
TEST (Analyzer, DituRNN_with_analysis_with_IR) {
352
358
LOG (INFO) << " ditu rnn with analysis and IR fuse" ;
353
359
TestDituRNNPrediction (FLAGS_infer_ditu_rnn_model, FLAGS_infer_ditu_rnn_data,
354
- 10 , true , true , 1 );
360
+ FLAGS_batch_size , true , true , FLAGS_repeat );
355
361
}
356
362
357
363
} // namespace analysis
0 commit comments