thread_icnet_test.cc 4.8 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// 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.

#define GOOGLE_GLOG_DLL_DECL

#include <gflags/gflags.h>
#include <glog/logging.h>
//#include <gtest/gtest.h>
#include <chrono>
#include <fstream>
#include <iostream>
#include <thread>  // NOLINT
D
dzhwinter 已提交
24
#include <utility>
D
dzhwinter 已提交
25
#include "paddle/fluid/inference/api/paddle_inference_api.h"
D
dzhwinter 已提交
26 27 28 29 30 31

#define ASSERT_TRUE(x) x
#define ASSERT_EQ(x, y) assert(x == y)

// DEFINE_string(dirname, "./LB_icnet_model",
//               "Directory of the inference model.");
D
dzhwinter 已提交
32
namespace paddle {
D
dzhwinter 已提交
33 34
NativeConfig GetConfig() {
  NativeConfig config;
D
dzhwinter 已提交
35 36
  config.prog_file = "./hs_lb_without_bn_cuda/__model__";
  config.param_file = "./hs_lb_without_bn_cuda/__params__";
D
dzhwinter 已提交
37
  config.fraction_of_gpu_memory = 0.0;
D
dzhwinter 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51
  config.use_gpu = true;
  config.device = 0;
  return config;
}

using Time = decltype(std::chrono::high_resolution_clock::now());
Time time() { return std::chrono::high_resolution_clock::now(); };
double time_diff(Time t1, Time t2) {
  typedef std::chrono::microseconds ms;
  auto diff = t2 - t1;
  ms counter = std::chrono::duration_cast<ms>(diff);
  return counter.count() / 1000.0;
}

D
dzhwinter 已提交
52
void test_naive(int batch_size, std::string model_path) {
D
dzhwinter 已提交
53 54 55 56
  NativeConfig config = GetConfig();
  int height = 449;
  int width = 581;
  std::vector<float> data;
D
dzhwinter 已提交
57
  for (int i = 0; i < 3 * height * width; ++i) {
D
dzhwinter 已提交
58
    data.push_back(0.0);
D
dzhwinter 已提交
59 60
  }

D
dzhwinter 已提交
61 62 63 64 65 66 67 68
  // read data
  // std::ifstream infile("new_file.list");
  // std::string temp_s;
  // std::vector<std::string> all_files;
  // while (!infile.eof()) {
  //   infile >> temp_s;
  //   all_files.push_back(temp_s);
  // }
D
dzhwinter 已提交
69

D
dzhwinter 已提交
70 71 72 73 74 75 76
  // // size_t file_num = all_files.size();
  // infile.close();
  // // =============read file list =============
  // for (size_t f_k = 0; f_k < 1; f_k++) {
  //   std::ifstream in_img(all_files[f_k]);
  //   std::cout << all_files[f_k] << std::endl;
  //   float temp_v;
D
dzhwinter 已提交
77

D
dzhwinter 已提交
78 79 80 81 82 83 84 85 86 87 88
  //   float sum_n = 0.0;
  //   std::vector<float> data;
  //   while (!in_img.eof()) {
  //     in_img >> temp_v;
  //     data.push_back(float(temp_v));

  //     sum_n += temp_v;
  //   }
  //   in_img.close();
  //   std::cout << "sum: " << sum_n << std::endl;

D
dzhwinter 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  PaddleTensor tensor;
  tensor.shape = std::vector<int>({batch_size, 3, height, width});
  tensor.data.Resize(sizeof(float) * batch_size * 3 * height * width);
  std::copy(data.begin(), data.end(), static_cast<float*>(tensor.data.data()));
  tensor.dtype = PaddleDType::FLOAT32;
  std::vector<PaddleTensor> paddle_tensor_feeds(1, tensor);

  constexpr int num_jobs = 5;  // each job run 1 batch
  std::vector<std::thread> threads;
  // using PtrPred = std::vector<std::unique_ptr<PaddlePredictor>>;
  std::vector<std::unique_ptr<PaddlePredictor>> predictors;
  for (int tid = 0; tid < num_jobs; ++tid) {
    auto& pred = CreatePaddlePredictor<NativeConfig>(config);
    predictors.emplace_back(std::move(pred));
  }
D
dzhwinter 已提交
104

D
dzhwinter 已提交
105 106 107 108 109 110 111 112 113 114 115 116
  using namespace std::chrono_literals;
  // std::this_thread::sleep_for(std::chrono::seconds(20));
  std::cout << "before start predict";

  int epoches = 100000;
  for (int tid = 0; tid < num_jobs; ++tid) {
    threads.emplace_back([&, tid]() {
      // auto predictor = CreatePaddlePredictor<NativeConfig>(config);
      auto& predictor = predictors[tid];
      // auto& predictor = predictors[tid];
      // auto predictor = preds[tid];
      // std::this_thread::sleep_for(std::chrono::seconds(20));
D
dzhwinter 已提交
117 118
      PaddleTensor tensor_out;
      std::vector<PaddleTensor> outputs(1, tensor_out);
D
dzhwinter 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
      for (size_t i = 0; i < epoches; i++) {
        ASSERT_TRUE(predictor->Run(paddle_tensor_feeds, &outputs));
        VLOG(0) << "tid : " << tid << " run: " << i << "finished";
        // std::cout <<"tid : " << tid << " run: " << i << "finished" <<
        // std::endl;
        ASSERT_EQ(outputs.size(), 1UL);
        // int64_t* data_o = static_cast<int64_t*>(outputs[0].data.data());
        // int64_t sum_out = 0;
        // for (size_t j = 0; j < outputs[0].data.length() / sizeof(int64_t);
        //      ++j) {
        //   sum_out += data_o[j];
        // }
        // std::cout << "tid : " << tid << "pass : " << i << " " << sum_out
        //           << std::endl;
      }
    });
  }
  for (int i = 0; i < num_jobs; ++i) {
    threads[i].join();
D
dzhwinter 已提交
138
  }
D
dzhwinter 已提交
139
}
D
dzhwinter 已提交
140
// }
D
dzhwinter 已提交
141
}  // namespace paddle
D
dzhwinter 已提交
142

D
dzhwinter 已提交
143 144 145
int main(int argc, char** argv) {
  paddle::test_naive(1 << 0, "");
  return 0;
D
dzhwinter 已提交
146
}