tester_helper.h 16.2 KB
Newer Older
L
luotao1 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <gtest/gtest.h>
Y
Yan Chunwei 已提交
18

L
luotao1 已提交
19
#include <algorithm>
L
luotao1 已提交
20
#include <memory>
T
Tao Luo 已提交
21
#include <string>
L
luotao1 已提交
22
#include <thread>  // NOLINT
L
luotao1 已提交
23
#include <unordered_map>
L
luotao1 已提交
24
#include <vector>
Y
Yiqun Liu 已提交
25 26 27
#ifdef WITH_GPERFTOOLS
#include <gperftools/profiler.h>
#endif
28

L
luotao1 已提交
29
#include "paddle/fluid/framework/ir/fuse_pass_base.h"
30
#include "paddle/fluid/framework/scope.h"
L
luotao1 已提交
31 32 33
#include "paddle/fluid/inference/analysis/analyzer.h"
#include "paddle/fluid/inference/analysis/ut_helper.h"
#include "paddle/fluid/inference/api/analysis_predictor.h"
34
#include "paddle/fluid/inference/api/helper.h"
Y
Yan Chunwei 已提交
35
#include "paddle/fluid/inference/api/paddle_inference_pass.h"
36
#include "paddle/fluid/inference/tests/api/config_printer.h"
T
Tao Luo 已提交
37
#include "paddle/fluid/inference/tests/test_helper.h"
N
nhzlx 已提交
38
#include "paddle/fluid/inference/utils/benchmark.h"
L
luotao1 已提交
39 40
#include "paddle/fluid/platform/profiler.h"

N
nhzlx 已提交
41
DEFINE_string(model_name, "", "model name");
L
luotao1 已提交
42 43
DEFINE_string(infer_model, "", "model path");
DEFINE_string(infer_data, "", "data file");
T
Tao Luo 已提交
44
DEFINE_string(refer_result, "", "reference result for comparison");
L
luotao1 已提交
45 46 47 48
DEFINE_int32(batch_size, 1, "batch size.");
DEFINE_int32(repeat, 1, "Running the inference program repeat times.");
DEFINE_bool(test_all_data, false, "Test the all dataset in data file.");
DEFINE_int32(num_threads, 1, "Running the inference program in multi-threads.");
T
Tao Luo 已提交
49 50
DEFINE_bool(use_analysis, true,
            "Running the inference program in analysis mode.");
N
nhzlx 已提交
51 52
DEFINE_bool(record_benchmark, false,
            "Record benchmark after profiling the model");
L
luotao1 已提交
53
DEFINE_double(accuracy, 1e-3, "Result Accuracy.");
L
luotao1 已提交
54

55
DECLARE_bool(profile);
L
luotao1 已提交
56
DECLARE_int32(paddle_num_threads);
57

L
luotao1 已提交
58 59 60
namespace paddle {
namespace inference {

61
void PrintConfig(const PaddlePredictor::Config *config, bool use_analysis) {
62
  const auto *analysis_config =
63
      reinterpret_cast<const AnalysisConfig *>(config);
64
  if (use_analysis) {
65
    LOG(INFO) << *analysis_config;
66 67
    return;
  }
68
  LOG(INFO) << analysis_config->ToNativeConfig();
69
}
Y
Yan Chunwei 已提交
70

L
luotao1 已提交
71
void CompareResult(const std::vector<PaddleTensor> &outputs,
T
tensor-tang 已提交
72
                   const std::vector<PaddleTensor> &ref_outputs) {
T
Tao Luo 已提交
73
  EXPECT_GT(outputs.size(), 0UL);
T
tensor-tang 已提交
74
  EXPECT_EQ(outputs.size(), ref_outputs.size());
L
luotao1 已提交
75 76
  for (size_t i = 0; i < outputs.size(); i++) {
    auto &out = outputs[i];
T
tensor-tang 已提交
77
    auto &ref_out = ref_outputs[i];
78 79
    size_t size = VecReduceToInt(out.shape);
    size_t ref_size = VecReduceToInt(ref_out.shape);
80
    EXPECT_GT(size, 0UL);
T
tensor-tang 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    EXPECT_EQ(size, ref_size);
    EXPECT_EQ(out.dtype, ref_out.dtype);
    switch (out.dtype) {
      case PaddleDType::INT64: {
        int64_t *pdata = static_cast<int64_t *>(out.data.data());
        int64_t *pdata_ref = static_cast<int64_t *>(ref_out.data.data());
        for (size_t j = 0; j < size; ++j) {
          EXPECT_EQ(pdata_ref[j], pdata[j]);
        }
        break;
      }
      case PaddleDType::FLOAT32: {
        float *pdata = static_cast<float *>(out.data.data());
        float *pdata_ref = static_cast<float *>(ref_out.data.data());
        for (size_t j = 0; j < size; ++j) {
Y
Yan Chunwei 已提交
96
          CHECK_LE(std::abs(pdata_ref[j] - pdata[j]), FLAGS_accuracy);
T
tensor-tang 已提交
97 98 99
        }
        break;
      }
L
luotao1 已提交
100 101 102 103
    }
  }
}

104
std::unique_ptr<PaddlePredictor> CreateTestPredictor(
105
    const PaddlePredictor::Config *config, bool use_analysis = true) {
106
  const auto *analysis_config =
107
      reinterpret_cast<const AnalysisConfig *>(config);
T
Tao Luo 已提交
108
  if (use_analysis) {
109
    return CreatePaddlePredictor<AnalysisConfig>(*analysis_config);
T
Tao Luo 已提交
110
  }
111 112
  auto native_config = analysis_config->ToNativeConfig();
  return CreatePaddlePredictor<NativeConfig>(native_config);
T
Tao Luo 已提交
113 114
}

115
size_t GetSize(const PaddleTensor &out) { return VecReduceToInt(out.shape); }
T
Tao Luo 已提交
116

117
std::unordered_map<std::string, int> GetFuseStatis(PaddlePredictor *predictor,
T
Tao Luo 已提交
118
                                                   int *num_ops) {
119
  std::unordered_map<std::string, int> res;
120
  auto *analysis_predictor = static_cast<AnalysisPredictor *>(predictor);
121 122 123 124 125 126
  auto *fusion_status =
      analysis_predictor->analysis_argument().fusion_statis_ptr();
  if (!fusion_status) {
    return res;
  }
  for (auto &item : *fusion_status) {
T
Tao Luo 已提交
127 128 129 130
    LOG(INFO) << "fused " << item.first << " " << item.second;
  }
  int num = 0;
  for (auto &node :
131 132
       analysis_predictor->analysis_argument().main_graph().Nodes()) {
    if (node->IsOp()) {
T
Tao Luo 已提交
133 134 135 136
      ++num;
    }
  }
  *num_ops = num;
137
  return *fusion_status;
T
Tao Luo 已提交
138 139
}

T
Tao Luo 已提交
140
void SetFakeImageInput(std::vector<std::vector<PaddleTensor>> *inputs,
141 142
                       const std::string &dirname, bool is_combined = true,
                       std::string model_filename = "model",
T
tensor-tang 已提交
143
                       std::string params_filename = "params",
N
nhzlx 已提交
144 145
                       const std::vector<std::string> *feed_names = nullptr,
                       const int continuous_inuput_index = 0) {
T
Tao Luo 已提交
146 147
  // Set fake_image_data
  PADDLE_ENFORCE_EQ(FLAGS_test_all_data, 0, "Only have single batch of data.");
148 149 150 151 152 153 154 155 156 157 158
  std::vector<std::vector<int64_t>> feed_target_shapes = GetFeedTargetShapes(
      dirname, is_combined, model_filename, params_filename);
  std::ostringstream os;
  for (size_t i = 0; i < feed_target_shapes.size(); ++i) {
    os << "feed target " << i << ": {" << feed_target_shapes[i][0];
    for (size_t j = 1; j < feed_target_shapes[i].size(); ++j) {
      os << ", " << feed_target_shapes[i][j];
    }
    os << "}\n";
  }
  LOG(INFO) << os.str();
T
tensor-tang 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  if (feed_names) {
    PADDLE_ENFORCE_EQ(feed_names->size(), feed_target_shapes.size());
  }
  std::vector<PaddleTensor> input_slots(feed_target_shapes.size());
  for (size_t i = 0; i < feed_target_shapes.size(); ++i) {
    const auto &feed_shape = feed_target_shapes[i];
    auto &input = input_slots[i];
    std::vector<int> shape({FLAGS_batch_size});
    for (size_t s = 1; s < feed_shape.size(); ++s) {
      shape.push_back(static_cast<int>(feed_shape[s]));
    }
    if (feed_names) {
      input.name = (*feed_names)[i];
    }
    input.shape = shape;
    input.dtype = PaddleDType::FLOAT32;
    size_t len = std::accumulate(shape.begin(), shape.end(), 1,
                                 [](int a, int b) { return a * b; });
    input.data.Resize(len * sizeof(float));
    input.lod.assign({{0, static_cast<size_t>(FLAGS_batch_size)}});
    float *input_data = static_cast<float *>(input.data.data());
    // fill input data, for profile easily, do not use random data here.
    for (size_t j = 0; j < len; ++j) {
N
nhzlx 已提交
182 183
      *(input_data + j) =
          static_cast<float>((j + continuous_inuput_index) % len) / len;
T
tensor-tang 已提交
184
    }
T
Tao Luo 已提交
185 186 187 188
  }
  (*inputs).emplace_back(input_slots);
}

189 190 191 192 193 194 195 196 197 198 199 200
void GetInputPerBatch(const std::vector<std::vector<int64_t>> &in,
                      std::vector<std::vector<int64_t>> *out,
                      std::vector<size_t> *lod, size_t batch_iter,
                      size_t batch_end) {
  lod->clear();
  lod->push_back(0);
  for (auto it = in.begin() + batch_iter; it < in.begin() + batch_end; it++) {
    out->push_back(*it);
    lod->push_back(lod->back() + (*it).size());  // calculate lod
  }
}

L
luotao1 已提交
201
void TestOneThreadPrediction(
202
    const PaddlePredictor::Config *config,
203
    const std::vector<std::vector<PaddleTensor>> &inputs,
T
Tao Luo 已提交
204
    std::vector<PaddleTensor> *outputs, bool use_analysis = true) {
L
luotao1 已提交
205 206
  int batch_size = FLAGS_batch_size;
  int num_times = FLAGS_repeat;
207
  auto predictor = CreateTestPredictor(config, use_analysis);
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

  // warmup run
  LOG(INFO) << "Warm up run...";
  {
    Timer warmup_timer;
    warmup_timer.tic();
    predictor->Run(inputs[0], outputs, batch_size);
    PrintTime(batch_size, 1, 1, 0, warmup_timer.toc(), 1);
    if (FLAGS_profile) {
      paddle::platform::ResetProfiler();
    }
  }

  LOG(INFO) << "Run " << num_times << " times...";
  {
    Timer run_timer;
    run_timer.tic();
Y
Yiqun Liu 已提交
225 226 227
#ifdef WITH_GPERFTOOLS
    ProfilerStart("paddle_inference.prof");
#endif
228 229 230 231
    for (int i = 0; i < num_times; i++) {
      for (size_t j = 0; j < inputs.size(); j++) {
        predictor->Run(inputs[j], outputs, batch_size);
      }
L
luotao1 已提交
232
    }
Y
Yiqun Liu 已提交
233 234 235
#ifdef WITH_GPERFTOOLS
    ProfilerStop();
#endif
N
nhzlx 已提交
236

Y
Yiqun Liu 已提交
237
    double latency = run_timer.toc() / (num_times > 1 ? num_times : 1);
N
nhzlx 已提交
238 239 240 241 242 243 244 245
    PrintTime(batch_size, num_times, 1, 0, latency, inputs.size());
    if (FLAGS_record_benchmark) {
      Benchmark benchmark;
      benchmark.SetName(FLAGS_model_name);
      benchmark.SetBatchSize(batch_size);
      benchmark.SetLatency(latency);
      benchmark.PersistToFile("benchmark_record.txt");
    }
L
luotao1 已提交
246 247 248 249
  }
}

void TestMultiThreadPrediction(
250
    const PaddlePredictor::Config *config,
251
    const std::vector<std::vector<PaddleTensor>> &inputs,
T
Tao Luo 已提交
252 253
    std::vector<PaddleTensor> *outputs, int num_threads,
    bool use_analysis = true) {
L
luotao1 已提交
254 255 256
  int batch_size = FLAGS_batch_size;
  int num_times = FLAGS_repeat;
  std::vector<std::thread> threads;
L
luotao1 已提交
257 258 259 260 261
  std::vector<std::unique_ptr<PaddlePredictor>> predictors;
  predictors.emplace_back(CreateTestPredictor(config, use_analysis));
  for (int tid = 1; tid < num_threads; tid++) {
    predictors.emplace_back(predictors.front()->Clone());
  }
262 263

  size_t total_time{0};
L
luotao1 已提交
264 265 266 267 268
  for (int tid = 0; tid < num_threads; ++tid) {
    threads.emplace_back([&, tid]() {
      // Each thread should have local inputs and outputs.
      // The inputs of each thread are all the same.
      std::vector<PaddleTensor> outputs_tid;
L
luotao1 已提交
269
      auto &predictor = predictors[tid];
L
luotao1 已提交
270 271 272
#ifdef PADDLE_WITH_MKLDNN
      if (use_analysis) {
        static_cast<AnalysisPredictor *>(predictor.get())
L
luotao1 已提交
273
            ->SetMkldnnThreadID(static_cast<int>(tid) + 1);
L
luotao1 已提交
274 275
      }
#endif
T
Tao Luo 已提交
276 277 278 279 280 281 282 283 284 285

      // warmup run
      LOG(INFO) << "Running thread " << tid << ", warm up run...";
      {
        Timer warmup_timer;
        warmup_timer.tic();
        predictor->Run(inputs[0], outputs, batch_size);
        PrintTime(batch_size, 1, num_threads, tid, warmup_timer.toc(), 1);
        if (FLAGS_profile) {
          paddle::platform::ResetProfiler();
L
luotao1 已提交
286 287
        }
      }
288

T
Tao Luo 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
      LOG(INFO) << "Thread " << tid << " run " << num_times << " times...";
      {
        Timer timer;
        timer.tic();
        for (int i = 0; i < num_times; i++) {
          for (const auto &input : inputs) {
            ASSERT_TRUE(predictor->Run(input, &outputs_tid));
          }
        }

        auto time = timer.toc();
        total_time += time;
        PrintTime(batch_size, num_times, num_threads, tid, time / num_times,
                  inputs.size());
      }
L
luotao1 已提交
304 305 306 307 308 309 310
    });
  }
  for (int i = 0; i < num_threads; ++i) {
    threads[i].join();
  }
}

311
void TestPrediction(const PaddlePredictor::Config *config,
312
                    const std::vector<std::vector<PaddleTensor>> &inputs,
T
Tao Luo 已提交
313 314
                    std::vector<PaddleTensor> *outputs, int num_threads,
                    bool use_analysis = FLAGS_use_analysis) {
315
  PrintConfig(config, use_analysis);
L
luotao1 已提交
316
  if (num_threads == 1) {
T
Tao Luo 已提交
317
    TestOneThreadPrediction(config, inputs, outputs, use_analysis);
L
luotao1 已提交
318
  } else {
T
Tao Luo 已提交
319 320
    TestMultiThreadPrediction(config, inputs, outputs, num_threads,
                              use_analysis);
L
luotao1 已提交
321 322 323
  }
}

L
luotao1 已提交
324 325 326 327 328 329 330 331 332
void CompareDeterministic(
    const PaddlePredictor::Config *config,
    const std::vector<std::vector<PaddleTensor>> &inputs) {
  int batch_size = FLAGS_batch_size;
  int num_times = FLAGS_repeat;
  auto predictor = CreateTestPredictor(config, FLAGS_use_analysis);

  std::vector<PaddleTensor> warmup_outputs, outputs;
  // run num_times to Compare Deterministic Result.
333 334 335 336
  for (size_t j = 0; j < inputs.size(); j++) {
    // warmup run
    predictor->Run(inputs[j], &warmup_outputs, batch_size);
    for (int i = 0; i < num_times; i++) {
L
luotao1 已提交
337 338 339 340 341 342
      predictor->Run(inputs[j], &outputs, batch_size);
      CompareResult(outputs, warmup_outputs);
    }
  }
}

T
Tao Luo 已提交
343
void CompareNativeAndAnalysis(
344
    const PaddlePredictor::Config *config,
345
    const std::vector<std::vector<PaddleTensor>> &inputs) {
346
  PrintConfig(config, true);
T
Tao Luo 已提交
347
  std::vector<PaddleTensor> native_outputs, analysis_outputs;
348
  TestOneThreadPrediction(config, inputs, &native_outputs, false);
T
Tao Luo 已提交
349 350 351 352
  TestOneThreadPrediction(config, inputs, &analysis_outputs, true);
  CompareResult(analysis_outputs, native_outputs);
}

N
nhzlx 已提交
353 354 355 356 357 358 359 360 361 362
void CompareNativeAndAnalysis(
    PaddlePredictor *native_pred, PaddlePredictor *analysis_pred,
    const std::vector<std::vector<PaddleTensor>> &inputs) {
  int batch_size = FLAGS_batch_size;
  std::vector<PaddleTensor> native_outputs, analysis_outputs;
  native_pred->Run(inputs[0], &native_outputs, batch_size);
  analysis_pred->Run(inputs[0], &analysis_outputs, batch_size);
  CompareResult(analysis_outputs, native_outputs);
}

L
luotao1 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
template <typename T>
std::string LoDTensorSummary(const framework::LoDTensor &tensor) {
  std::stringstream ss;
  ss << "\n---- tensor ---" << '\n';
  ss << "lod: [";
  for (const auto &level : tensor.lod()) {
    ss << "[ ";
    for (auto i : level) {
      ss << i << ", ";
    }
    ss << "]";
  }
  ss << "]\n";

  ss << "shape: [";
  int size = 1;
  for (int i = 0; i < tensor.dims().size(); i++) {
    int dim = tensor.dims()[i];
    ss << dim << ", ";
    size *= dim;
  }
  ss << "]\n";

  ss << "data: ";
  for (int i = 0; i < std::min(20, size); i++) {
    ss << tensor.data<T>()[i] << " ";
  }
  ss << "\n";

  return ss.str();
}

static bool CompareLoD(const framework::LoD &a, const framework::LoD &b) {
  if (a.size() != b.size()) {
    LOG(ERROR) << string::Sprintf("lod size not match %d != %d", a.size(),
                                  b.size());
    return false;
  }
  for (size_t i = 0; i < a.size(); i++) {
    auto &al = a[i];
    auto &bl = b[i];
    if (al.size() != bl.size()) {
      LOG(ERROR) << string::Sprintf("level size %d != %d", al.size(),
                                    bl.size());
      return false;
    }
  }
  return true;
}

static bool CompareShape(const std::vector<int64_t> &a,
                         const std::vector<int64_t> &b) {
  if (a.size() != b.size()) {
    LOG(ERROR) << string::Sprintf("shape size not match %d != %d", a.size(),
                                  b.size());
    return false;
  }
  for (size_t i = 0; i < a.size(); i++) {
    if (a[i] != b[i]) {
      LOG(ERROR) << string::Sprintf("shape %d-th element not match %d != %d", i,
                                    a[i], b[i]);
      return false;
    }
  }
  return true;
}

static bool CompareTensorData(const framework::LoDTensor &a,
                              const framework::LoDTensor &b) {
  auto a_shape = framework::vectorize(a.dims());
  auto b_shape = framework::vectorize(b.dims());
  size_t a_size = std::accumulate(a_shape.begin(), a_shape.end(), 1,
                                  [](int a, int b) { return a * b; });
  size_t b_size = std::accumulate(b_shape.begin(), b_shape.end(), 1,
                                  [](int a, int b) { return a * b; });
  if (a_size != b_size) {
    LOG(ERROR) << string::Sprintf("tensor data size not match, %d != %d",
                                  a_size, b_size);
  }

  for (size_t i = 0; i < a_size; i++) {
Y
Yu Yang 已提交
444
    if (a.type() == framework::proto::VarType::FP32) {
L
luotao1 已提交
445 446 447 448 449 450 451 452
      const auto *a_data = a.data<float>();
      const auto *b_data = b.data<float>();
      if (std::abs(a_data[i] - b_data[i]) > 1e-3) {
        LOG(ERROR) << string::Sprintf(
            "tensor data %d-th element not match, %f != %f", i, a_data[i],
            b_data[i]);
        return false;
      }
Y
Yu Yang 已提交
453
    } else if (a.type() == framework::proto::VarType::INT64) {
L
luotao1 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
      const auto *a_data = a.data<int64_t>();
      const auto *b_data = b.data<int64_t>();
      if (std::abs(a_data[i] - b_data[i]) > 1e-3) {
        LOG(ERROR) << string::Sprintf(
            "tensor data %d-th element not match, %f != %f", i, a_data[i],
            b_data[i]);
        return false;
      }
    }
  }

  return true;
}

static bool CompareTensor(const framework::LoDTensor &a,
                          const framework::LoDTensor &b) {
  if (!CompareLoD(a.lod(), b.lod())) {
    return false;
  }
  if (!CompareShape(framework::vectorize(a.dims()),
                    framework::vectorize(b.dims()))) {
    return false;
  }

  if (!CompareTensorData(a, b)) {
    return false;
  }

  return true;
}

L
luotao1 已提交
485 486
}  // namespace inference
}  // namespace paddle