test_resnet50_quant.cc 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
// Copyright (c) 2021 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 "test_suite.h"  // NOLINT

DEFINE_string(modeldir, "", "Directory of the inference model.");
DEFINE_string(int8dir, "", "Directory of the quant inference model.");
DEFINE_string(datadir, "", "Directory of the infer data.");

namespace paddle_infer {

paddle::test::Record PrepareInput(int batch_size) {
  // init input data
  int channel = 3;
  int width = 224;
  int height = 224;
  paddle::test::Record image_Record;
  int input_num = batch_size * channel * width * height;

  // load from binary data
  std::ifstream fs(FLAGS_datadir, std::ifstream::binary);
  EXPECT_TRUE(fs.is_open());
  CHECK(fs.is_open());

  float* input = new float[input_num];
  memset(input, 0, input_num * sizeof(float));
  auto input_data_tmp = input;
  for (int i = 0; i < input_num; ++i) {
    fs.read(reinterpret_cast<char*>(input_data_tmp), sizeof(*input_data_tmp));
    input_data_tmp++;
  }
  int label = 0;
  fs.read(reinterpret_cast<char*>(&label), sizeof(label));
  fs.close();

  std::vector<float> input_data{input, input + input_num};
  image_Record.data = input_data;
  image_Record.shape = std::vector<int>{batch_size, channel, width, height};
  image_Record.type = paddle::PaddleDType::FLOAT32;
  image_Record.label = label;
  return image_Record;
}

55
TEST(DISABLED_tensorrt_tester_resnet50_quant, multi_thread4_trt_int8_bz1) {
56 57 58 59 60 61 62 63 64 65 66
  int thread_num = 4;
  // init input data
  std::map<std::string, paddle::test::Record> input_data_map;
  input_data_map["image"] = PrepareInput(1);
  // init output data
  std::map<std::string, paddle::test::Record> infer_output_data;
  // prepare inference config
  paddle_infer::Config config;
  config.SetModel(FLAGS_int8dir);
  config.EnableUseGpu(1000, 0);
  config.EnableTensorRtEngine(1 << 20, 10, 3,
67
                              paddle_infer::PrecisionType::kInt8, false, false);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  // get infer results from multi threads
  std::vector<std::thread> threads;
  services::PredictorPool pred_pool(config, thread_num);
  for (int i = 0; i < thread_num; ++i) {
    threads.emplace_back(paddle::test::SingleThreadPrediction,
                         pred_pool.Retrive(i), &input_data_map,
                         &infer_output_data, 5);
  }

  // thread join & check outputs
  for (int i = 0; i < thread_num; ++i) {
    LOG(INFO) << "join tid : " << i;
    threads[i].join();

    // check outputs
    std::vector<int> index(1000);
    std::iota(index.begin(), index.end(), 0);
    auto out_data =
        infer_output_data["save_infer_model/scale_0.tmp_0"].data.data();
    std::sort(index.begin(), index.end(), [out_data](size_t i1, size_t i2) {
      return out_data[i1] > out_data[i2];
    });
    // compare inference & groundtruth label
    ASSERT_EQ(index[0], input_data_map["image"].label);
  }

  std::cout << "finish test" << std::endl;
}

97
TEST(DISABLED_tensorrt_tester_resnet50_quant, multi_thread_multi_instance) {
98 99 100 101 102 103 104 105 106 107 108 109 110 111
  int thread_num = 4;
  // init input data
  std::map<std::string, paddle::test::Record> input_data_fp32, input_data_quant;
  input_data_quant["image"] = PrepareInput(1);
  input_data_fp32["inputs"] = PrepareInput(1);

  // init output data
  std::map<std::string, paddle::test::Record> infer_output_data;
  // prepare inference config
  paddle_infer::Config config_fp32, config_quant;
  config_fp32.SetModel(FLAGS_modeldir + "/inference.pdmodel",
                       FLAGS_modeldir + "/inference.pdiparams");
  config_fp32.EnableUseGpu(1000, 0);
  config_fp32.EnableTensorRtEngine(
112
      1 << 20, 10, 3, paddle_infer::PrecisionType::kFloat32, false, false);
113 114 115 116

  config_quant.SetModel(FLAGS_int8dir);
  config_quant.EnableUseGpu(1000, 0);
  config_quant.EnableTensorRtEngine(
117
      1 << 20, 10, 3, paddle_infer::PrecisionType::kInt8, false, false);
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

  // get infer results from multi threads
  std::vector<std::thread> threads;
  services::PredictorPool pred_pool_fp32(config_fp32, thread_num);
  services::PredictorPool pred_pool_quant(config_quant, thread_num);
  for (int i = 0; i < thread_num; ++i) {
    if (i % 2 == 0) {
      threads.emplace_back(paddle::test::SingleThreadPrediction,
                           pred_pool_fp32.Retrive(i), &input_data_fp32,
                           &infer_output_data, 5);
    } else {
      threads.emplace_back(paddle::test::SingleThreadPrediction,
                           pred_pool_quant.Retrive(i), &input_data_quant,
                           &infer_output_data, 5);
    }
  }

  // thread join & check outputs
  for (int i = 0; i < thread_num; ++i) {
    LOG(INFO) << "join tid : " << i;
    std::vector<int> index(1000);
    threads[i].join();
    if (i % 2 == 0) {
      // check outputs
      std::iota(index.begin(), index.end(), 0);
      auto out_data =
          infer_output_data["save_infer_model/scale_0.tmp_0"].data.data();
      std::sort(index.begin(), index.end(), [out_data](size_t i1, size_t i2) {
        return out_data[i1] > out_data[i2];
      });
      // compare inference & groundtruth label
      ASSERT_EQ(index[0], input_data_fp32["inputs"].label);
    } else {
      // check outputs
      std::iota(index.begin(), index.end(), 0);
      auto out_data =
          infer_output_data["save_infer_model/scale_0.tmp_0"].data.data();
      std::sort(index.begin(), index.end(), [out_data](size_t i1, size_t i2) {
        return out_data[i1] > out_data[i2];
      });
      // compare inference & groundtruth label
      ASSERT_EQ(index[0], input_data_quant["image"].label);
    }
  }
}

}  // namespace paddle_infer

int main(int argc, char** argv) {
  ::testing::InitGoogleTest(&argc, argv);
  ::google::ParseCommandLineFlags(&argc, &argv, true);
  return RUN_ALL_TESTS();
}