opt.cc 17.5 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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>
#ifdef PADDLE_WITH_TESTING
#include <gtest/gtest.h>
#endif
19
// "supported_kernel_op_info.h", "all_kernel_faked.cc" and "kernel_src_map.h"
20
// are created automatically during opt's compiling period
21
#include <iomanip>
22
#include "all_kernel_faked.cc"  // NOLINT
23
#include "kernel_src_map.h"     // NOLINT
24
#include "lite/api/cxx_api.h"
Y
Yan Chunwei 已提交
25 26 27
#include "lite/api/paddle_api.h"
#include "lite/api/paddle_use_ops.h"
#include "lite/api/paddle_use_passes.h"
28
#include "lite/core/op_registry.h"
29
#include "lite/core/version.h"
30 31
#include "lite/model_parser/compatible_pb.h"
#include "lite/model_parser/pb/program_desc.h"
Y
Yan Chunwei 已提交
32
#include "lite/utils/cp_logging.h"
33
#include "lite/utils/io.h"
Y
Yan Chunwei 已提交
34
#include "lite/utils/string.h"
35
#include "supported_kernel_op_info.h"  // NOLINT
Y
Yan Chunwei 已提交
36

37 38 39 40
DEFINE_string(model_dir,
              "",
              "path of the model. This option will be ignored if model_file "
              "and param_file are exist");
41 42 43 44 45 46 47 48 49 50 51 52
DEFINE_string(model_filename,
              "",
              "model topo filename of the model in models set. This option"
              " will be used to specific tailoring");
DEFINE_string(param_filename,
              "",
              "model param filename of the model in models set. This option"
              " will be used to specific tailoring");
DEFINE_string(model_set_dir,
              "",
              "path of the models set. This option will be used to specific"
              " tailoring");
53 54
DEFINE_string(model_file, "", "model file path of the combined-param model");
DEFINE_string(param_file, "", "param file path of the combined-param model");
Y
Yan Chunwei 已提交
55 56 57 58
DEFINE_string(
    optimize_out_type,
    "protobuf",
    "store type of the output optimized model. protobuf/naive_buffer");
59
DEFINE_bool(display_kernels, false, "Display kernel information");
60 61 62 63 64
DEFINE_bool(record_tailoring_info,
            false,
            "Record kernels and operators information of the optimized model "
            "for tailoring compiling, information are stored into optimized "
            "model path as hidden files");
Y
Yan Chunwei 已提交
65 66 67 68 69
DEFINE_string(optimize_out, "", "path of the output optimized model");
DEFINE_string(valid_targets,
              "arm",
              "The targets this model optimized for, should be one of (arm, "
              "opencl, x86), splitted by space");
70
DEFINE_bool(prefer_int8_kernel, false, "Prefer to run model with int8 kernels");
71 72 73 74 75 76 77
DEFINE_bool(print_supported_ops,
            false,
            "Print supported operators on the inputed target");
DEFINE_bool(print_all_ops,
            false,
            "Print all the valid operators of Paddle-Lite");
DEFINE_bool(print_model_ops, false, "Print operators in the input model");
Y
Yan Chunwei 已提交
78 79 80

namespace paddle {
namespace lite_api {
81 82 83 84 85
//! Display the kernel information.
void DisplayKernels() {
  LOG(INFO) << ::paddle::lite::KernelRegistry::Global().DebugString();
}

86
std::vector<Place> ParserValidPlaces() {
Y
Yan Chunwei 已提交
87
  std::vector<Place> valid_places;
88
  auto target_reprs = lite::Split(FLAGS_valid_targets, ",");
Y
Yan Chunwei 已提交
89 90
  for (auto& target_repr : target_reprs) {
    if (target_repr == "arm") {
91 92 93 94
      valid_places.emplace_back(
          Place{TARGET(kARM), PRECISION(kFloat), DATALAYOUT(kNCHW)});
      valid_places.emplace_back(
          Place{TARGET(kARM), PRECISION(kInt32), DATALAYOUT(kNCHW)});
Y
Yan Chunwei 已提交
95
    } else if (target_repr == "opencl") {
96
      valid_places.emplace_back(
97
          Place{TARGET(kOpenCL), PRECISION(kFP16), DATALAYOUT(kImageDefault)});
98 99 100
      valid_places.emplace_back(
          Place{TARGET(kOpenCL), PRECISION(kFloat), DATALAYOUT(kNCHW)});
      valid_places.emplace_back(
101 102 103
          Place{TARGET(kOpenCL), PRECISION(kAny), DATALAYOUT(kImageDefault)});
      valid_places.emplace_back(
          Place{TARGET(kOpenCL), PRECISION(kAny), DATALAYOUT(kNCHW)});
104 105
      valid_places.emplace_back(
          TARGET(kARM));  // enable kARM CPU kernel when no opencl kernel
Y
Yan Chunwei 已提交
106 107
    } else if (target_repr == "x86") {
      valid_places.emplace_back(TARGET(kX86));
108 109 110 111
    } else if (target_repr == "npu") {
      valid_places.emplace_back(TARGET(kNPU));
    } else if (target_repr == "xpu") {
      valid_places.emplace_back(TARGET(kXPU));
Y
Yan Chunwei 已提交
112 113 114 115 116 117 118 119 120 121 122
    } else {
      LOG(FATAL) << lite::string_format(
          "Wrong target '%s' found, please check the command flag "
          "'valid_targets'",
          target_repr.c_str());
    }
  }

  CHECK(!valid_places.empty())
      << "At least one target should be set, should set the "
         "command argument 'valid_targets'";
123

124
  if (FLAGS_prefer_int8_kernel) {
Y
Yan Chunwei 已提交
125
    LOG(WARNING) << "Int8 mode is only support by ARM target";
126 127
    valid_places.insert(valid_places.begin(),
                        Place{TARGET(kARM), PRECISION(kInt8)});
Y
Yan Chunwei 已提交
128
  }
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  return valid_places;
}

void RunOptimize(const std::string& model_dir,
                 const std::string& model_file,
                 const std::string& param_file,
                 const std::string& optimize_out,
                 const std::string& optimize_out_type,
                 const std::vector<Place>& valid_places,
                 bool record_tailoring_info) {
  if (!model_file.empty() && !param_file.empty()) {
    LOG(WARNING)
        << "Load combined-param model. Option model_dir will be ignored";
  }

  lite_api::CxxConfig config;
  config.set_model_dir(model_dir);
  config.set_model_file(model_file);
  config.set_param_file(param_file);
Y
Yan Chunwei 已提交
148 149 150 151
  config.set_valid_places(valid_places);
  auto predictor = lite_api::CreatePaddlePredictor(config);

  LiteModelType model_type;
152
  if (optimize_out_type == "protobuf") {
Y
Yan Chunwei 已提交
153
    model_type = LiteModelType::kProtobuf;
154
  } else if (optimize_out_type == "naive_buffer") {
Y
Yan Chunwei 已提交
155 156
    model_type = LiteModelType::kNaiveBuffer;
  } else {
157
    LOG(FATAL) << "Unsupported Model type :" << optimize_out_type;
Y
Yan Chunwei 已提交
158 159
  }

160
  OpKernelInfoCollector::Global().SetKernel2path(kernel2path_map);
161
  predictor->SaveOptimizedModel(
162 163
      optimize_out, model_type, record_tailoring_info);
  if (record_tailoring_info) {
164
    LOG(INFO) << "Record the information of tailored model into :"
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
              << optimize_out;
  }
}

void CollectModelMetaInfo(const std::string& output_dir,
                          const std::vector<std::string>& models,
                          const std::string& filename) {
  std::set<std::string> total;
  for (const auto& name : models) {
    std::string model_path =
        lite::Join<std::string>({output_dir, name, filename}, "/");
    auto lines = lite::ReadLines(model_path);
    total.insert(lines.begin(), lines.end());
  }
  std::string output_path =
      lite::Join<std::string>({output_dir, filename}, "/");
  lite::WriteLines(std::vector<std::string>(total.begin(), total.end()),
                   output_path);
}
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
void PrintOpsInfo(std::set<std::string> valid_ops = {}) {
  std::vector<std::string> targets = {"kHost",
                                      "kX86",
                                      "kCUDA",
                                      "kARM",
                                      "kOpenCL",
                                      "kFPGA",
                                      "kNPU",
                                      "kXPU",
                                      "kAny",
                                      "kUnk"};
  int maximum_optype_length = 0;
  for (auto it = supported_ops.begin(); it != supported_ops.end(); it++) {
    maximum_optype_length = it->first.size() > maximum_optype_length
                                ? it->first.size()
                                : maximum_optype_length;
  }
  std::cout << std::setiosflags(std::ios::internal);
  std::cout << std::setw(maximum_optype_length) << "OP_name";
  for (int i = 0; i < targets.size(); i++) {
    std::cout << std::setw(10) << targets[i].substr(1);
  }
  std::cout << std::endl;
  if (valid_ops.empty()) {
    for (auto it = supported_ops.begin(); it != supported_ops.end(); it++) {
      std::cout << std::setw(maximum_optype_length) << it->first;
      auto ops_valid_places = it->second;
      for (int i = 0; i < targets.size(); i++) {
        if (std::find(ops_valid_places.begin(),
                      ops_valid_places.end(),
                      targets[i]) != ops_valid_places.end()) {
          std::cout << std::setw(10) << "Y";
        } else {
          std::cout << std::setw(10) << " ";
        }
      }
      std::cout << std::endl;
    }
  } else {
    for (auto op = valid_ops.begin(); op != valid_ops.end(); op++) {
      std::cout << std::setw(maximum_optype_length) << *op;
      // Check: If this kernel doesn't match any operator, we will skip it.
      if (supported_ops.find(*op) == supported_ops.end()) {
        continue;
      }
      // Print OP info.
      auto ops_valid_places = supported_ops.at(*op);
      for (int i = 0; i < targets.size(); i++) {
        if (std::find(ops_valid_places.begin(),
                      ops_valid_places.end(),
                      targets[i]) != ops_valid_places.end()) {
          std::cout << std::setw(10) << "Y";
        } else {
          std::cout << std::setw(10) << " ";
        }
      }
      std::cout << std::endl;
    }
  }
}
/// Print help information
void PrintHelpInfo() {
  // at least one argument should be inputed
247
  const std::string opt_version = lite::version();
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  const char help_info[] =
      "At least one argument should be inputed. Valid arguments are listed "
      "below:\n"
      "  Arguments of model optimization:\n"
      "        `--model_dir=<model_param_dir>`\n"
      "        `--model_file=<model_path>`\n"
      "        `--param_file=<param_path>`\n"
      "        `--optimize_out_type=(protobuf|naive_buffer)`\n"
      "        `--optimize_out=<output_optimize_model_dir>`\n"
      "        `--valid_targets=(arm|opencl|x86|npu|xpu)`\n"
      "        `--prefer_int8_kernel=(true|false)`\n"
      "        `--record_tailoring_info=(true|false)`\n"
      "  Arguments of model checking and ops information:\n"
      "        `--print_all_ops=true`   Display all the valid operators of "
      "Paddle-Lite\n"
      "        `--print_supported_ops=true  "
      "--valid_targets=(arm|opencl|x86|npu|xpu)`"
      "  Display valid operators of input targets\n"
      "        `--print_model_ops=true  --model_dir=<model_param_dir> "
      "--valid_targets=(arm|opencl|x86|npu|xpu)`"
      "  Display operators in the input model\n";
269 270
  std::cout << "opt version:" << opt_version << std::endl
            << help_info << std::endl;
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
  exit(1);
}

// Parse Input command
void ParseInputCommand() {
  if (FLAGS_print_all_ops) {
    std::cout << "All OPs supported by Paddle-Lite: " << supported_ops.size()
              << " ops in total." << std::endl;
    PrintOpsInfo();
    exit(1);
  } else if (FLAGS_print_supported_ops) {
    auto valid_places = paddle::lite_api::ParserValidPlaces();
    // get valid_targets string
    std::vector<TargetType> target_types = {};
    for (int i = 0; i < valid_places.size(); i++) {
      target_types.push_back(valid_places[i].target);
    }
    std::string targets_str = TargetToStr(target_types[0]);
    for (int i = 1; i < target_types.size(); i++) {
      targets_str = targets_str + TargetToStr(target_types[i]);
    }

    std::cout << "Supported OPs on '" << targets_str << "': " << std::endl;
    target_types.push_back(TARGET(kHost));
    target_types.push_back(TARGET(kUnk));

    std::set<std::string> valid_ops;
    for (int i = 0; i < target_types.size(); i++) {
      auto ops = supported_ops_target[static_cast<int>(target_types[i])];
      valid_ops.insert(ops.begin(), ops.end());
    }
    PrintOpsInfo(valid_ops);
    exit(1);
  }
}
// test whether this model is supported
void CheckIfModelSupported() {
  // 1. parse valid places and valid targets
  auto valid_places = paddle::lite_api::ParserValidPlaces();
  // set valid_ops
  auto valid_ops = supported_ops_target[static_cast<int>(TARGET(kHost))];
  auto valid_unktype_ops = supported_ops_target[static_cast<int>(TARGET(kUnk))];
  valid_ops.insert(
      valid_ops.end(), valid_unktype_ops.begin(), valid_unktype_ops.end());
  for (int i = 0; i < valid_places.size(); i++) {
    auto target = valid_places[i].target;
    auto ops = supported_ops_target[static_cast<int>(target)];
    valid_ops.insert(valid_ops.end(), ops.begin(), ops.end());
  }
  // get valid ops
  std::set<std::string> valid_ops_set(valid_ops.begin(), valid_ops.end());

  // 2.Load model into program to get ops in model
  std::string prog_path = FLAGS_model_dir + "/__model__";
  if (!FLAGS_model_file.empty() && !FLAGS_param_file.empty()) {
    prog_path = FLAGS_model_file;
  }
  lite::cpp::ProgramDesc cpp_prog;
  framework::proto::ProgramDesc pb_proto_prog =
      *lite::LoadProgram(prog_path, false);
  lite::pb::ProgramDesc pb_prog(&pb_proto_prog);
  // Transform to cpp::ProgramDesc
  lite::TransformProgramDescAnyToCpp(pb_prog, &cpp_prog);

  std::set<std::string> unsupported_ops;
  std::set<std::string> input_model_ops;
  for (int index = 0; index < cpp_prog.BlocksSize(); index++) {
    auto current_block = cpp_prog.GetBlock<lite::cpp::BlockDesc>(index);
    for (size_t i = 0; i < current_block->OpsSize(); ++i) {
      auto& op_desc = *current_block->GetOp<lite::cpp::OpDesc>(i);
      auto op_type = op_desc.Type();
      input_model_ops.insert(op_type);
      if (valid_ops_set.count(op_type) == 0) {
        unsupported_ops.insert(op_type);
      }
    }
  }
  // 3. Print ops_info of input model and check if this model is supported
  if (FLAGS_print_model_ops) {
    std::cout << "OPs in the input model include:\n";
    PrintOpsInfo(input_model_ops);
  }
  if (!unsupported_ops.empty()) {
    std::string unsupported_ops_str = *unsupported_ops.begin();
    for (auto op_str = ++unsupported_ops.begin();
         op_str != unsupported_ops.end();
         op_str++) {
      unsupported_ops_str = unsupported_ops_str + ", " + *op_str;
    }
    std::vector<TargetType> targets = {};
    for (int i = 0; i < valid_places.size(); i++) {
      targets.push_back(valid_places[i].target);
    }
    std::sort(targets.begin(), targets.end());
    targets.erase(unique(targets.begin(), targets.end()), targets.end());
    std::string targets_str = TargetToStr(targets[0]);
    for (int i = 1; i < targets.size(); i++) {
      targets_str = targets_str + "," + TargetToStr(targets[i]);
    }

    LOG(ERROR) << "Error: This model is not supported, because "
               << unsupported_ops.size() << " ops are not supported on '"
               << targets_str << "'. These unsupported ops are: '"
               << unsupported_ops_str << "'.";
    exit(1);
  }
  if (FLAGS_print_model_ops) {
    std::cout << "Paddle-Lite supports this model!" << std::endl;
    exit(1);
  }
}
382 383 384 385 386

void Main() {
  if (FLAGS_display_kernels) {
    DisplayKernels();
    exit(0);
387
  }
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406

  auto valid_places = ParserValidPlaces();
  if (FLAGS_model_set_dir == "") {
    RunOptimize(FLAGS_model_dir,
                FLAGS_model_file,
                FLAGS_param_file,
                FLAGS_optimize_out,
                FLAGS_optimize_out_type,
                valid_places,
                FLAGS_record_tailoring_info);
    return;
  }

  if (!FLAGS_record_tailoring_info) {
    LOG(WARNING) << "--model_set_dir option only be used with "
                    "--record_tailoring_info=true together";
    return;
  }

407
  lite::MkDirRecur(FLAGS_optimize_out);
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
  auto model_dirs = lite::ListDir(FLAGS_model_set_dir, true);
  if (model_dirs.size() == 0) {
    LOG(FATAL) << "[" << FLAGS_model_set_dir << "] does not contain any model";
  }
  // Optimize models in FLAGS_model_set_dir
  for (const auto& name : model_dirs) {
    std::string input_model_dir =
        lite::Join<std::string>({FLAGS_model_set_dir, name}, "/");
    std::string output_model_dir =
        lite::Join<std::string>({FLAGS_optimize_out, name}, "/");

    std::string model_file = "";
    std::string param_file = "";

    if (FLAGS_model_filename != "" && FLAGS_param_filename != "") {
      model_file =
          lite::Join<std::string>({input_model_dir, FLAGS_model_filename}, "/");
      param_file =
          lite::Join<std::string>({input_model_dir, FLAGS_param_filename}, "/");
    }

    LOG(INFO) << "Start optimize model: " << input_model_dir;
    RunOptimize(input_model_dir,
                model_file,
                param_file,
                output_model_dir,
                FLAGS_optimize_out_type,
                valid_places,
                FLAGS_record_tailoring_info);
    LOG(INFO) << "Optimize done. ";
  }

  // Collect all models information
  CollectModelMetaInfo(
      FLAGS_optimize_out, model_dirs, lite::TAILORD_OPS_SOURCE_LIST_FILENAME);
  CollectModelMetaInfo(
      FLAGS_optimize_out, model_dirs, lite::TAILORD_OPS_LIST_NAME);
  CollectModelMetaInfo(FLAGS_optimize_out,
                       model_dirs,
                       lite::TAILORD_KERNELS_SOURCE_LIST_FILENAME);
  CollectModelMetaInfo(
      FLAGS_optimize_out, model_dirs, lite::TAILORD_KERNELS_LIST_NAME);
Y
Yan Chunwei 已提交
450 451 452 453 454 455
}

}  // namespace lite_api
}  // namespace paddle

int main(int argc, char** argv) {
456 457 458 459
  // If there is none input argument, print help info.
  if (argc < 2) {
    paddle::lite_api::PrintHelpInfo();
  }
Y
Yan Chunwei 已提交
460
  google::ParseCommandLineFlags(&argc, &argv, false);
461
  paddle::lite_api::ParseInputCommand();
462 463 464
  if (FLAGS_model_set_dir == "") {
    paddle::lite_api::CheckIfModelSupported();
  }
Y
Yan Chunwei 已提交
465 466 467
  paddle::lite_api::Main();
  return 0;
}