trt_mobilenet_test.cc 3.2 KB
Newer Older
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. */

#include <glog/logging.h>
#include <gtest/gtest.h>

18
#include "gflags/gflags.h"
19 20 21 22 23 24 25
#include "paddle/fluid/inference/tests/api/trt_test_helper.h"

namespace paddle {
namespace inference {

TEST(TensorRT_mobilenet, compare) {
  std::string model_dir = FLAGS_infer_model + "/mobilenet";
26 27 28 29 30
  AnalysisConfig config;
  config.EnableUseGpu(100, 0);
  config.SetModel(model_dir);
  config.DisableGlogInfo();
  auto predictor = CreatePaddlePredictor(config);
31 32 33 34 35 36 37 38 39
  compare(model_dir, /* use_tensorrt */ true);
  // Open it when need.
  // profile(model_dir, /* use_analysis */ true, FLAGS_use_tensorrt);
}

TEST(AnalysisPredictor, use_gpu) {
  std::string model_dir = FLAGS_infer_model + "/" + "mobilenet";
  AnalysisConfig config;
  config.EnableUseGpu(100, 0);
40
  config.EnableCUDNN();
41 42 43 44 45 46 47 48
  config.SetModel(model_dir);
  config.pass_builder()->TurnOnDebug();

  std::vector<std::vector<PaddleTensor>> inputs_all;
  auto predictor = CreatePaddlePredictor(config);
  SetFakeImageInput(&inputs_all, model_dir, false, "__model__", "");

  std::vector<PaddleTensor> outputs;
W
Wilber 已提交
49
  for (auto &input : inputs_all) {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    ASSERT_TRUE(predictor->Run(input, &outputs));
    predictor->ClearIntermediateTensor();
  }
}

TEST(AnalysisPredictor, collect_shape_range) {
  std::string model_dir = FLAGS_infer_model + "/" + "mobilenet";
  AnalysisConfig config;
  config.EnableUseGpu(100, 0);
  config.SetModel(model_dir);
  config.CollectShapeRangeInfo("shape_range.pbtxt");

  std::vector<std::vector<PaddleTensor>> inputs_all;
  auto predictor = CreatePaddlePredictor(config);
  SetFakeImageInput(&inputs_all, model_dir, false, "__model__", "");

  std::vector<PaddleTensor> outputs;
  for (auto &input : inputs_all) {
68
    ASSERT_TRUE(predictor->Run(input, &outputs));
69
    predictor->ClearIntermediateTensor();
70 71 72 73 74
  }
}

}  // namespace inference
}  // namespace paddle
W
Wilber 已提交
75 76 77 78 79 80 81 82

namespace paddle_infer {
TEST(PredictorPool, use_gpu) {
  std::string model_dir = FLAGS_infer_model + "/" + "mobilenet";
  Config config;
  config.EnableUseGpu(100, 0);
  config.SetModel(model_dir);
  config.EnableTensorRtEngine();
83
  config.Exp_DisableTensorRtOPs({"fc"});
84
  config.EnableTensorRtDLA(0);
W
Wilber 已提交
85 86 87 88 89 90
  services::PredictorPool pred_pool(config, 1);

  auto predictor = pred_pool.Retrive(0);
  auto input_names = predictor->GetInputNames();
  auto input_t = predictor->GetInputHandle(input_names[0]);
  std::vector<int> in_shape = {1, 3, 224, 224};
91 92 93 94
  int in_num =
      std::accumulate(in_shape.begin(), in_shape.end(), 1, [](int &a, int &b) {
        return a * b;
      });
W
Wilber 已提交
95 96 97 98 99 100 101 102

  std::vector<float> input(in_num, 0);
  input_t->Reshape(in_shape);
  input_t->CopyFromCpu(input.data());
  predictor->Run();
}

}  // namespace paddle_infer