model_optimize_tool.cc 3.7 KB
Newer Older
Y
Yan Chunwei 已提交
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
// 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
#include "lite/api/paddle_api.h"
#include "lite/api/paddle_use_kernels.h"
#include "lite/api/paddle_use_ops.h"
#include "lite/api/paddle_use_passes.h"
#include "lite/utils/cp_logging.h"
#include "lite/utils/string.h"

26 27 28 29 30 31
DEFINE_string(model_dir,
              "",
              "path of the model. This option will be ignored if model_file "
              "and param_file are exist");
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 已提交
32 33 34 35 36 37 38 39 40
DEFINE_string(
    optimize_out_type,
    "protobuf",
    "store type of the output optimized model. protobuf/naive_buffer");
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");
41
DEFINE_bool(prefer_int8_kernel, false, "Prefer to run model with int8 kernels");
Y
Yan Chunwei 已提交
42 43 44 45 46

namespace paddle {
namespace lite_api {

void Main() {
47 48 49 50 51
  if (!FLAGS_model_file.empty() && !FLAGS_param_file.empty()) {
    LOG(WARNING)
        << "Load combined-param model. Option model_dir will be ignored";
  }

Y
Yan Chunwei 已提交
52 53
  lite_api::CxxConfig config;
  config.set_model_dir(FLAGS_model_dir);
54 55
  config.set_model_file(FLAGS_model_file);
  config.set_param_file(FLAGS_param_file);
Y
Yan Chunwei 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

  std::vector<Place> valid_places;
  auto target_reprs = lite::Split(FLAGS_valid_targets, " ");
  for (auto& target_repr : target_reprs) {
    if (target_repr == "arm") {
      valid_places.emplace_back(TARGET(kARM));
    } else if (target_repr == "opencl") {
      valid_places.emplace_back(TARGET(kOpenCL));
    } else if (target_repr == "x86") {
      valid_places.emplace_back(TARGET(kX86));
    } else {
      LOG(FATAL) << lite::string_format(
          "Wrong target '%s' found, please check the command flag "
          "'valid_targets'",
          target_repr.c_str());
    }
  }
73
  valid_places.emplace_back(TARGET(kHost));
Y
Yan Chunwei 已提交
74 75 76 77

  CHECK(!valid_places.empty())
      << "At least one target should be set, should set the "
         "command argument 'valid_targets'";
78
  if (FLAGS_prefer_int8_kernel) {
Y
Yan Chunwei 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    LOG(WARNING) << "Int8 mode is only support by ARM target";
    valid_places.push_back(Place{TARGET(kARM), PRECISION(kInt8)});
    config.set_preferred_place(Place{TARGET(kARM), PRECISION(kInt8)});
  }
  config.set_valid_places(valid_places);

  auto predictor = lite_api::CreatePaddlePredictor(config);

  LiteModelType model_type;
  if (FLAGS_optimize_out_type == "protobuf") {
    model_type = LiteModelType::kProtobuf;
  } else if (FLAGS_optimize_out_type == "naive_buffer") {
    model_type = LiteModelType::kNaiveBuffer;
  } else {
    LOG(FATAL) << "Unsupported Model type :" << FLAGS_optimize_out_type;
  }

  predictor->SaveOptimizedModel(FLAGS_optimize_out, model_type);
}

}  // namespace lite_api
}  // namespace paddle

int main(int argc, char** argv) {
  google::ParseCommandLineFlags(&argc, &argv, false);
  paddle::lite_api::Main();
  return 0;
}