trt_test_helper.h 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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. */
14 15 16
#pragma once
#include <string>
#include <vector>
N
nhzlx 已提交
17

18 19 20
#include "gflags/gflags.h"
#include "glog/logging.h"
#include "gtest/gtest.h"
21

22
#include "paddle/fluid/inference/tests/api/tester_helper.h"
N
nhzlx 已提交
23 24

namespace paddle {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
namespace inference {

DEFINE_bool(use_tensorrt, true, "Test the performance of TensorRT engine.");
DEFINE_string(prog_filename, "", "Name of model file.");
DEFINE_string(param_filename, "", "Name of parameters file.");

template <typename ConfigType>
void SetConfig(ConfigType* config, std::string model_dir, bool use_gpu,
               bool use_tensorrt = false, int batch_size = -1) {
  if (!FLAGS_prog_filename.empty() && !FLAGS_param_filename.empty()) {
    config->prog_file = model_dir + "/" + FLAGS_prog_filename;
    config->param_file = model_dir + "/" + FLAGS_param_filename;
  } else {
    config->model_dir = model_dir;
  }
  if (use_gpu) {
    config->use_gpu = true;
    config->device = 0;
    config->fraction_of_gpu_memory = 0.15;
  }
N
nhzlx 已提交
45 46
}

47
template <>
48 49 50
void SetConfig<AnalysisConfig>(AnalysisConfig* config, std::string model_dir,
                               bool use_gpu, bool use_tensorrt,
                               int batch_size) {
51
  if (!FLAGS_prog_filename.empty() && !FLAGS_param_filename.empty()) {
52 53
    config->SetModel(model_dir + "/" + FLAGS_prog_filename,
                     model_dir + "/" + FLAGS_param_filename);
54
  } else {
55
    config->SetModel(model_dir);
56 57
  }
  if (use_gpu) {
58
    config->EnableUseGpu(100, 0);
59
    if (use_tensorrt) {
N
nhzlx 已提交
60 61
      config->EnableTensorRtEngine(1 << 10, batch_size, 3,
                                   AnalysisConfig::Precision::kFloat32, false);
62 63 64 65
      config->pass_builder()->DeletePass("conv_bn_fuse_pass");
      config->pass_builder()->DeletePass("fc_fuse_pass");
      config->pass_builder()->TurnOnDebug();
    } else {
66
      config->SwitchIrOptim();
67 68
    }
  }
69 70
}

71 72 73 74 75 76 77
void profile(std::string model_dir, bool use_analysis, bool use_tensorrt) {
  std::vector<std::vector<PaddleTensor>> inputs_all;
  if (!FLAGS_prog_filename.empty() && !FLAGS_param_filename.empty()) {
    SetFakeImageInput(&inputs_all, model_dir, true, FLAGS_prog_filename,
                      FLAGS_param_filename);
  } else {
    SetFakeImageInput(&inputs_all, model_dir, false, "__model__", "");
N
nhzlx 已提交
78 79
  }

80
  std::vector<std::vector<PaddleTensor>> outputs;
81
  if (use_analysis || use_tensorrt) {
82
    AnalysisConfig config;
83
    config.EnableUseGpu(100, 0);
84
    config.pass_builder()->TurnOnDebug();
85 86
    SetConfig<AnalysisConfig>(&config, model_dir, true, use_tensorrt,
                              FLAGS_batch_size);
87 88 89 90 91 92 93 94
    TestPrediction(reinterpret_cast<PaddlePredictor::Config*>(&config),
                   inputs_all, &outputs, FLAGS_num_threads, true);
  } else {
    NativeConfig config;
    SetConfig<NativeConfig>(&config, model_dir, true, false);
    TestPrediction(reinterpret_cast<PaddlePredictor::Config*>(&config),
                   inputs_all, &outputs, FLAGS_num_threads, false);
  }
95 96
}

97 98 99 100 101 102 103 104
void compare(std::string model_dir, bool use_tensorrt) {
  std::vector<std::vector<PaddleTensor>> inputs_all;
  if (!FLAGS_prog_filename.empty() && !FLAGS_param_filename.empty()) {
    SetFakeImageInput(&inputs_all, model_dir, true, FLAGS_prog_filename,
                      FLAGS_param_filename);
  } else {
    SetFakeImageInput(&inputs_all, model_dir, false, "__model__", "");
  }
105

106 107 108
  AnalysisConfig analysis_config;
  SetConfig<AnalysisConfig>(&analysis_config, model_dir, true, use_tensorrt,
                            FLAGS_batch_size);
T
Tao Luo 已提交
109 110 111
  CompareNativeAndAnalysis(
      reinterpret_cast<const PaddlePredictor::Config*>(&analysis_config),
      inputs_all);
112
}
113

N
nhzlx 已提交
114
void compare_continuous_input(std::string model_dir, bool use_tensorrt) {
115 116 117
  AnalysisConfig analysis_config;
  SetConfig<AnalysisConfig>(&analysis_config, model_dir, true, use_tensorrt,
                            FLAGS_batch_size);
N
nhzlx 已提交
118 119 120 121
  auto config =
      reinterpret_cast<const PaddlePredictor::Config*>(&analysis_config);
  auto native_pred = CreateTestPredictor(config, false);
  auto analysis_pred = CreateTestPredictor(config, true);
R
root 已提交
122
  for (int i = 0; i < 20; i++) {
N
nhzlx 已提交
123 124 125
    std::vector<std::vector<PaddleTensor>> inputs_all;
    if (!FLAGS_prog_filename.empty() && !FLAGS_param_filename.empty()) {
      SetFakeImageInput(&inputs_all, model_dir, true, FLAGS_prog_filename,
N
nhzlx 已提交
126
                        FLAGS_param_filename, nullptr, i);
N
nhzlx 已提交
127
    } else {
N
nhzlx 已提交
128 129
      SetFakeImageInput(&inputs_all, model_dir, false, "__model__", "", nullptr,
                        i);
N
nhzlx 已提交
130 131 132 133 134 135
    }
    CompareNativeAndAnalysis(native_pred.get(), analysis_pred.get(),
                             inputs_all);
  }
}

136
}  // namespace inference
N
nhzlx 已提交
137
}  // namespace paddle