benchmark.cc 7.6 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2019 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>
J
juncaipeng 已提交
16 17 18
#include <sys/time.h>
#include <time.h>
#include <algorithm>
Y
Yan Chunwei 已提交
19 20
#include <cstdio>
#include <fstream>
J
juncaipeng 已提交
21 22
#include <iomanip>
#include <numeric>
Y
Yan Chunwei 已提交
23 24 25
#include <string>
#include <vector>
#include "lite/api/paddle_api.h"
J
juncaipeng 已提交
26 27 28
#include "lite/api/paddle_use_kernels.h"
#include "lite/api/paddle_use_ops.h"
#include "lite/api/paddle_use_passes.h"
29
#include "lite/core/device_info.h"
Y
Yan Chunwei 已提交
30 31 32
#include "lite/utils/cp_logging.h"
#include "lite/utils/string.h"

33 34 35 36 37 38 39 40 41 42 43 44 45
DEFINE_string(model_dir,
              "",
              "the path of the model, set model_dir when the model is no "
              "combined formate. This option will be ignored if model_file "
              "and param_file are exist.");
DEFINE_string(model_file,
              "",
              "the path of model file, set model_file when the model is "
              "combined formate.");
DEFINE_string(param_file,
              "",
              "the path of param file, set param_file when the model is "
              "combined formate.");
Y
Yan Chunwei 已提交
46 47
DEFINE_string(input_shape,
              "1,3,224,224",
J
juncaipeng 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
              "set input shapes according to the model, "
              "separated by colon and comma, "
              "such as 1,3,244,244:1,3,300,300.");
DEFINE_int32(warmup, 0, "warmup times");
DEFINE_int32(repeats, 1, "repeats times");
DEFINE_int32(power_mode,
             3,
             "arm power mode: "
             "0 for big cluster, "
             "1 for little cluster, "
             "2 for all cores, "
             "3 for no bind");
DEFINE_int32(threads, 1, "threads num");
DEFINE_string(result_filename,
              "result.txt",
              "save benchmark "
              "result to the file");
J
juncaipeng 已提交
65 66
DEFINE_bool(run_model_optimize,
            false,
J
juncaipeng 已提交
67 68 69 70 71 72
            "if set true, apply model_optimize_tool to "
            "model and use optimized model to test. ");
DEFINE_bool(is_quantized_model,
            false,
            "if set true, "
            "test the performance of the quantized model. ");
Y
Yan Chunwei 已提交
73 74 75 76

namespace paddle {
namespace lite_api {

J
juncaipeng 已提交
77 78 79 80 81 82
inline double GetCurrentUS() {
  struct timeval time;
  gettimeofday(&time, NULL);
  return 1e+6 * time.tv_sec + time.tv_usec;
}

83
void OutputOptModel(const std::string& save_optimized_model_dir,
Y
Yan Chunwei 已提交
84 85
                    const std::vector<std::vector<int64_t>>& input_shapes) {
  lite_api::CxxConfig config;
86 87 88
  config.set_model_dir(FLAGS_model_dir);
  config.set_model_file(FLAGS_model_file);
  config.set_param_file(FLAGS_param_file);
89 90 91
  std::vector<Place> vaild_places = {
      Place{TARGET(kARM), PRECISION(kFloat)},
  };
92 93 94 95 96
  if (FLAGS_is_quantized_model) {
    vaild_places.insert(vaild_places.begin(),
                        Place{TARGET(kARM), PRECISION(kInt8)});
  }
  config.set_valid_places(vaild_places);
Y
Yan Chunwei 已提交
97 98 99 100 101 102
  auto predictor = lite_api::CreatePaddlePredictor(config);

  int ret = system(
      paddle::lite::string_format("rm -rf %s", save_optimized_model_dir.c_str())
          .c_str());
  if (ret == 0) {
J
juncaipeng 已提交
103
    LOG(INFO) << "Delete old optimized model " << save_optimized_model_dir;
Y
Yan Chunwei 已提交
104 105 106
  }
  predictor->SaveOptimizedModel(save_optimized_model_dir,
                                LiteModelType::kNaiveBuffer);
107
  LOG(INFO) << "Load model from " << FLAGS_model_dir;
Y
Yan Chunwei 已提交
108 109 110 111 112 113 114
  LOG(INFO) << "Save optimized model to " << save_optimized_model_dir;
}

#ifdef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
void Run(const std::vector<std::vector<int64_t>>& input_shapes,
         const std::string& model_dir,
         const std::string model_name) {
J
juncaipeng 已提交
115
  // set config and create predictor
116
  lite_api::MobileConfig config;
J
juncaipeng 已提交
117 118
  config.set_threads(FLAGS_threads);
  config.set_power_mode(static_cast<PowerMode>(FLAGS_power_mode));
Y
Yan Chunwei 已提交
119 120 121 122
  config.set_model_dir(model_dir);

  auto predictor = lite_api::CreatePaddlePredictor(config);

J
juncaipeng 已提交
123
  // set input
Y
Yan Chunwei 已提交
124 125 126 127 128
  for (int j = 0; j < input_shapes.size(); ++j) {
    auto input_tensor = predictor->GetInput(j);
    input_tensor->Resize(input_shapes[j]);
    auto input_data = input_tensor->mutable_data<float>();
    int input_num = 1;
J
juncaipeng 已提交
129
    for (size_t i = 0; i < input_shapes[j].size(); ++i) {
Y
Yan Chunwei 已提交
130 131 132 133 134 135 136
      input_num *= input_shapes[j][i];
    }
    for (int i = 0; i < input_num; ++i) {
      input_data[i] = 1.f;
    }
  }

J
juncaipeng 已提交
137 138
  // warmup
  for (int i = 0; i < FLAGS_warmup; ++i) {
Y
Yan Chunwei 已提交
139 140 141
    predictor->Run();
  }

J
juncaipeng 已提交
142 143 144 145
  // run
  std::vector<float> perf_vct;
  for (int i = 0; i < FLAGS_repeats; ++i) {
    auto start = GetCurrentUS();
Y
Yan Chunwei 已提交
146
    predictor->Run();
J
juncaipeng 已提交
147 148
    auto end = GetCurrentUS();
    perf_vct.push_back((end - start) / 1000.0);
Y
Yan Chunwei 已提交
149
  }
J
juncaipeng 已提交
150 151 152 153 154 155 156 157 158 159
  std::sort(perf_vct.begin(), perf_vct.end());
  float min_res = perf_vct.back();
  float max_res = perf_vct.front();
  float total_res = accumulate(perf_vct.begin(), perf_vct.end(), 0.0);
  float avg_res = total_res / FLAGS_repeats;

  // save result
  std::ofstream ofs(FLAGS_result_filename, std::ios::app);
  if (!ofs.is_open()) {
    LOG(FATAL) << "open result file failed";
Y
Yan Chunwei 已提交
160
  }
J
juncaipeng 已提交
161
  ofs.precision(5);
162
  ofs << std::setw(30) << std::fixed << std::left << model_name;
J
juncaipeng 已提交
163 164 165 166
  ofs << "min = " << std::setw(12) << min_res;
  ofs << "max = " << std::setw(12) << max_res;
  ofs << "average = " << std::setw(12) << avg_res;
  ofs << std::endl;
J
juncaipeng 已提交
167
  ofs.close();
Y
Yan Chunwei 已提交
168 169 170 171 172 173 174 175 176
}
#endif

}  // namespace lite_api
}  // namespace paddle

int main(int argc, char** argv) {
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  if (FLAGS_model_dir == "" || FLAGS_result_filename == "") {
J
juncaipeng 已提交
177
    LOG(INFO) << "please run ./benchmark_bin --help to obtain usage.";
Y
Yan Chunwei 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    exit(0);
  }

  std::size_t found = FLAGS_model_dir.find_last_of("/");
  std::string model_name = FLAGS_model_dir.substr(found + 1);
  std::string save_optimized_model_dir = FLAGS_model_dir + "opt2";

  auto split_string =
      [](const std::string& str_in) -> std::vector<std::string> {
    std::vector<std::string> str_out;
    std::string tmp_str = str_in;
    while (!tmp_str.empty()) {
      size_t next_offset = tmp_str.find(":");
      str_out.push_back(tmp_str.substr(0, next_offset));
      if (next_offset == std::string::npos) {
        break;
      } else {
        tmp_str = tmp_str.substr(next_offset + 1);
      }
    }
    return str_out;
  };

  auto get_shape = [](const std::string& str_shape) -> std::vector<int64_t> {
    std::vector<int64_t> shape;
    std::string tmp_str = str_shape;
    while (!tmp_str.empty()) {
      int dim = atoi(tmp_str.data());
      shape.push_back(dim);
      size_t next_offset = tmp_str.find(",");
      if (next_offset == std::string::npos) {
        break;
      } else {
        tmp_str = tmp_str.substr(next_offset + 1);
      }
    }
    return shape;
  };

  std::vector<std::string> str_input_shapes = split_string(FLAGS_input_shape);
  std::vector<std::vector<int64_t>> input_shapes;
J
juncaipeng 已提交
219
  for (size_t i = 0; i < str_input_shapes.size(); ++i) {
Y
Yan Chunwei 已提交
220 221 222
    input_shapes.push_back(get_shape(str_input_shapes[i]));
  }

J
juncaipeng 已提交
223
  // Output optimized model if needed
J
juncaipeng 已提交
224
  if (FLAGS_run_model_optimize) {
225
    paddle::lite_api::OutputOptModel(save_optimized_model_dir, input_shapes);
J
juncaipeng 已提交
226
  }
Y
Yan Chunwei 已提交
227 228 229

#ifdef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK
  // Run inference using optimized model
J
juncaipeng 已提交
230 231
  std::string run_model_dir =
      FLAGS_run_model_optimize ? save_optimized_model_dir : FLAGS_model_dir;
J
juncaipeng 已提交
232
  paddle::lite_api::Run(input_shapes, run_model_dir, model_name);
Y
Yan Chunwei 已提交
233 234 235
#endif
  return 0;
}