Skip to content

Commit 1a373fb

Browse files
committed
add result check for multi-thread UT
1 parent 2dc23ff commit 1a373fb

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

paddle/fluid/inference/analysis/analyzer_tester.cc

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,26 @@ const float ditu_rnn_target_data[] = {
234234
10.7286, 12.0595, 10.6672, 0, 0, 0, 0, 0,
235235
93.5771, 3.84641, 0, 0, 0, 0, 0, 0,
236236
169.426, 0, 0, 0, 0, 0, 0, 0};
237+
void CompareResult(const std::vector<PaddleTensor> &outputs,
238+
const std::vector<PaddleTensor> &base_outputs) {
239+
PADDLE_ENFORCE_GT(outputs.size(), 0);
240+
PADDLE_ENFORCE_EQ(outputs.size(), base_outputs.size());
241+
for (size_t i = 0; i < outputs.size(); i++) {
242+
auto &out = outputs[i];
243+
auto &base_out = base_outputs[i];
244+
size_t size = std::accumulate(out.shape.begin(), out.shape.end(), 1,
245+
[](int a, int b) { return a * b; });
246+
size_t size1 = std::accumulate(base_out.shape.begin(), base_out.shape.end(),
247+
1, [](int a, int b) { return a * b; });
248+
PADDLE_ENFORCE_EQ(size, size1);
249+
PADDLE_ENFORCE_GT(size, 0);
250+
float *data = static_cast<float *>(out.data.data());
251+
float *base_data = static_cast<float *>(base_out.data.data());
252+
for (size_t i = 0; i < size; i++) {
253+
EXPECT_NEAR(data[i], base_data[i], 1e-3);
254+
}
255+
}
256+
}
237257
// Test with a really complicate model.
238258
void TestDituRNNPrediction(bool use_analysis_and_activate_ir = false,
239259
int num_threads = FLAGS_num_threads) {
@@ -266,7 +286,8 @@ void TestDituRNNPrediction(bool use_analysis_and_activate_ir = false,
266286
for (int i = 0; i < num_times; i++) {
267287
predictor->Run(input_slots, &outputs);
268288
}
269-
print_time(batch_size, num_times, 1, 0, timer.toc() / num_times);
289+
PrintTime(batch_size, num_times, 1, 0, timer.toc() / num_times);
290+
CompareResult(outputs, base_outputs);
270291
} else {
271292
std::vector<std::thread> threads;
272293
std::vector<std::unique_ptr<PaddlePredictor>> predictors;
@@ -279,13 +300,19 @@ void TestDituRNNPrediction(bool use_analysis_and_activate_ir = false,
279300
}
280301
for (int tid = 0; tid < num_threads; ++tid) {
281302
threads.emplace_back([&, tid]() {
303+
// Each thread should have local input_slots and outputs.
304+
std::vector<PaddleTensor> input_slots;
305+
DataRecord data(FLAGS_infer_ditu_rnn_data, batch_size);
306+
PrepareInputs(&input_slots, &data, batch_size);
307+
std::vector<PaddleTensor> outputs;
282308
Timer timer;
283309
timer.tic();
284310
for (int i = 0; i < num_times; i++) {
285311
predictors[tid]->Run(input_slots, &outputs);
286312
}
287-
print_time(batch_size, num_times, num_threads, tid,
288-
timer.toc() / num_times);
313+
PrintTime(batch_size, num_times, num_threads, tid,
314+
timer.toc() / num_times);
315+
CompareResult(outputs, base_outputs);
289316
});
290317
}
291318
for (int i = 0; i < num_threads; ++i) {
@@ -294,27 +321,6 @@ void TestDituRNNPrediction(bool use_analysis_and_activate_ir = false,
294321
}
295322
LOG(INFO) << "=====================================";
296323

297-
if (num_threads == 1) {
298-
PADDLE_ENFORCE_GT(outputs.size(), 0);
299-
PADDLE_ENFORCE_EQ(outputs.size(), base_outputs.size());
300-
for (size_t i = 0; i < outputs.size(); i++) {
301-
auto &out = outputs[i];
302-
auto &base_out = base_outputs[i];
303-
size_t size = std::accumulate(out.shape.begin(), out.shape.end(), 1,
304-
[](int a, int b) { return a * b; });
305-
size_t size1 =
306-
std::accumulate(base_out.shape.begin(), base_out.shape.end(), 1,
307-
[](int a, int b) { return a * b; });
308-
PADDLE_ENFORCE_EQ(size, size1);
309-
PADDLE_ENFORCE_GT(size, 0);
310-
float *data = static_cast<float *>(out.data.data());
311-
float *base_data = static_cast<float *>(base_out.data.data());
312-
for (size_t i = 0; i < size; i++) {
313-
EXPECT_NEAR(data[i], base_data[i], 1e-3);
314-
}
315-
}
316-
}
317-
318324
if (use_analysis_and_activate_ir) {
319325
AnalysisPredictor *analysis_predictor =
320326
dynamic_cast<AnalysisPredictor *>(predictor.get());
@@ -342,13 +348,13 @@ void TestDituRNNPrediction(bool use_analysis_and_activate_ir = false,
342348
}
343349
}
344350

345-
TEST(Analyzer, DituRNN) {
346-
// default FLAGS_num_threads = 1
347-
TestDituRNNPrediction(false, FLAGS_num_threads);
348-
TestDituRNNPrediction(true, FLAGS_num_threads);
349-
}
351+
// basic unit-test of DituRNN, easy for profiling independently.
352+
TEST(Analyzer, DituRNN) { TestDituRNNPrediction(false, FLAGS_num_threads); }
350353

354+
// advance unit-test of DituRNN, test use_analysis_and_activate_ir and
355+
// multi-threads.
351356
TEST(Analyzer, DituRNN_multi_thread) {
357+
TestDituRNNPrediction(true, 1);
352358
TestDituRNNPrediction(false, 4);
353359
TestDituRNNPrediction(true, 4);
354360
}

paddle/fluid/inference/api/helper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ std::string DescribeTensor(const PaddleTensor &tensor) {
122122
return os.str();
123123
}
124124

125-
void print_time(int batch_size, int repeat, int num_threads, int tid,
126-
double latency) {
125+
void PrintTime(int batch_size, int repeat, int num_threads, int tid,
126+
double latency) {
127127
LOG(INFO) << "batch_size: " << batch_size << ", repeat: " << repeat
128128
<< ", threads: " << num_threads << ", thread id: " << tid
129129
<< ", latency: " << latency << "ms";

0 commit comments

Comments
 (0)