trt_models_tester.cc 5.1 KB
Newer Older
N
nhzlx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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.

#include <gflags/gflags.h>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include "paddle/fluid/inference/analysis/analyzer.h"
19
#include "paddle/fluid/inference/api/helper.h"
N
nhzlx 已提交
20
#include "paddle/fluid/inference/api/paddle_inference_api.h"
21 22
#include "paddle/fluid/inference/api/paddle_inference_pass.h"
#include "paddle/fluid/inference/tests/api/tester_helper.h"
N
nhzlx 已提交
23 24

namespace paddle {
25
using paddle::contrib::AnalysisConfig;
N
nhzlx 已提交
26 27 28 29 30 31 32

DEFINE_string(dirname, "", "Directory of the inference model.");

NativeConfig GetConfigNative() {
  NativeConfig config;
  config.model_dir = FLAGS_dirname;
  // LOG(INFO) << "dirname  " << config.model_dir;
33
  config.fraction_of_gpu_memory = 0.15;
N
nhzlx 已提交
34 35 36 37 38
  config.use_gpu = true;
  config.device = 0;
  return config;
}

39 40 41 42 43 44 45
void PrepareTRTConfig(AnalysisConfig *config) {
  config->model_dir = FLAGS_dirname + "/" + "mobilenet";
  config->fraction_of_gpu_memory = 0.15;
  config->EnableTensorRtEngine(1 << 10, 5);
  config->pass_builder()->DeletePass("conv_bn_fuse_pass");
  config->pass_builder()->DeletePass("fc_fuse_pass");
  config->pass_builder()->TurnOnDebug();
N
nhzlx 已提交
46 47
}

48 49 50
void PrepareInputs(std::vector<PaddleTensor> *tensors, int batch_size) {
  PADDLE_ENFORCE_EQ(tensors->size(), 1UL);
  auto &tensor = tensors->front();
N
nhzlx 已提交
51 52 53 54 55 56 57 58 59 60 61 62
  int height = 224;
  int width = 224;
  float *data = new float[batch_size * 3 * height * width];
  memset(data, 0, sizeof(float) * (batch_size * 3 * height * width));
  data[0] = 1.0f;

  // Prepare inputs
  tensor.name = "input_0";
  tensor.shape = std::vector<int>({batch_size, 3, height, width});
  tensor.data = PaddleBuf(static_cast<void *>(data),
                          sizeof(float) * (batch_size * 3 * height * width));
  tensor.dtype = PaddleDType::FLOAT32;
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
}

void CompareTensorRTWithFluid(int batch_size, std::string model_dirname) {
  auto config0 = GetConfigNative();
  config0.model_dir = model_dirname;

  AnalysisConfig config1(true);
  PrepareTRTConfig(&config1);
  config1.model_dir = model_dirname;

  auto predictor0 = CreatePaddlePredictor<NativeConfig>(config0);
  auto predictor1 = CreatePaddlePredictor(config1);

  // Prepare inputs
  std::vector<PaddleTensor> paddle_tensor_feeds(1);
  PrepareInputs(&paddle_tensor_feeds, batch_size);
N
nhzlx 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

  // Prepare outputs
  std::vector<PaddleTensor> outputs0;
  std::vector<PaddleTensor> outputs1;
  CHECK(predictor0->Run(paddle_tensor_feeds, &outputs0));
  CHECK(predictor1->Run(paddle_tensor_feeds, &outputs1, batch_size));

  const size_t num_elements = outputs0.front().data.length() / sizeof(float);
  const size_t num_elements1 = outputs1.front().data.length() / sizeof(float);
  EXPECT_EQ(num_elements, num_elements1);

  auto *data0 = static_cast<float *>(outputs0.front().data.data());
  auto *data1 = static_cast<float *>(outputs1.front().data.data());

  ASSERT_GT(num_elements, 0UL);
  for (size_t i = 0; i < std::min(num_elements, num_elements1); i++) {
    EXPECT_NEAR(data0[i], data1[i], 1e-3);
  }
}

N
nhzlx 已提交
99
TEST(trt_models_test, mobilenet) {
100
  CompareTensorRTWithFluid(1, FLAGS_dirname + "/" + "mobilenet");
N
nhzlx 已提交
101 102
}
TEST(trt_models_test, resnet50) {
103
  CompareTensorRTWithFluid(1, FLAGS_dirname + "/" + "resnet50");
N
nhzlx 已提交
104
}
N
nhzlx 已提交
105
TEST(trt_models_test, resnext50) {
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  CompareTensorRTWithFluid(1, FLAGS_dirname + "/" + "resnext50");
}

TEST(trt_models_test, raw_gpu) {
  std::string model_dir = FLAGS_dirname + "/" + "mobilenet";
  auto config0 = GetConfigNative();
  config0.model_dir = model_dir;
  int batch_size = 2;

  AnalysisConfig config1(true);
  config1.fraction_of_gpu_memory = 0.1;
  config1.enable_ir_optim = true;
  config1.model_dir = model_dir;

  auto predictor0 = CreatePaddlePredictor<NativeConfig>(config0);
  auto predictor1 = CreatePaddlePredictor(config1);

  // Prepare inputs
  std::vector<PaddleTensor> paddle_tensor_feeds(1);
  PrepareInputs(&paddle_tensor_feeds, batch_size);

  // Prepare outputs
  std::vector<PaddleTensor> outputs0;
  std::vector<PaddleTensor> outputs1;
  CHECK(predictor0->Run(paddle_tensor_feeds, &outputs0));
  CHECK(predictor1->Run(paddle_tensor_feeds, &outputs1, batch_size));

  const size_t num_elements = outputs0.front().data.length() / sizeof(float);
  const size_t num_elements1 = outputs1.front().data.length() / sizeof(float);
  EXPECT_EQ(num_elements, num_elements1);

  auto *data0 = static_cast<float *>(outputs0.front().data.data());
  auto *data1 = static_cast<float *>(outputs1.front().data.data());

  ASSERT_GT(num_elements, 0UL);
  for (size_t i = 0; i < std::min(num_elements, num_elements1); i++) {
    EXPECT_NEAR(data0[i], data1[i], 1e-3);
  }
N
nhzlx 已提交
144 145
}

N
nhzlx 已提交
146
}  // namespace paddle
147 148

USE_PASS(tensorrt_subgraph_pass);