engine.cc 2.3 KB
Newer Older
石晓伟 已提交
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.

#ifdef PADDLE_WITH_CUDA
#define LITE_WITH_CUDA 1
#endif

19
#ifdef LITE_SUBGRAPH_WITH_XPU
20 21
#define LITE_WITH_XPU 1
#endif
石晓伟 已提交
22

W
Wilber 已提交
23 24 25 26
#ifndef PADDLE_WITH_ARM
#define LITE_WITH_X86 1
#endif

27
#include "paddle/fluid/inference/lite/engine.h"
W
Wilber 已提交
28
#include <utility>
29

石晓伟 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42
namespace paddle {
namespace inference {
namespace lite {

bool EngineManager::Empty() const { return engines_.size() == 0; }

bool EngineManager::Has(const std::string& name) const {
  if (engines_.count(name) == 0) {
    return false;
  }
  return engines_.at(name).get() != nullptr;
}

W
Wilber 已提交
43 44
paddle::lite_api::PaddlePredictor* EngineManager::Get(
    const std::string& name) const {
石晓伟 已提交
45 46 47
  return engines_.at(name).get();
}

W
Wilber 已提交
48 49 50 51 52 53 54 55
paddle::lite_api::PaddlePredictor* EngineManager::Create(
    const std::string& name, const EngineConfig& cfg) {
  // config info for predictor.
  paddle::lite_api::CxxConfig lite_cxx_config;
  lite_cxx_config.set_model_buffer(cfg.model.c_str(), cfg.model.size(),
                                   cfg.param.c_str(), cfg.param.size());
  lite_cxx_config.set_valid_places(cfg.valid_places);
#ifdef PADDLE_WITH_ARM
56
  lite_cxx_config.set_threads(cfg.cpu_math_library_num_threads);
W
Wilber 已提交
57 58 59
#else
  lite_cxx_config.set_x86_math_library_num_threads(
      cfg.cpu_math_library_num_threads);
石晓伟 已提交
60
#endif
W
Wilber 已提交
61

62
#ifdef LITE_SUBGRAPH_WITH_XPU
W
Wilber 已提交
63 64
  lite_cxx_config.set_xpu_workspace_l3_size_per_thread(
      cfg.xpu_l3_workspace_size);
65
#endif
W
Wilber 已提交
66 67 68 69 70 71

  // create predictor
  std::shared_ptr<paddle::lite_api::PaddlePredictor> p =
      paddle::lite_api::CreatePaddlePredictor(lite_cxx_config);
  engines_[name] = std::move(p);
  return engines_[name].get();
石晓伟 已提交
72 73 74 75
}

void EngineManager::DeleteAll() {
  for (auto& item : engines_) {
W
Wilber 已提交
76
    item.second.reset();
石晓伟 已提交
77 78 79 80 81 82
  }
}

}  // namespace lite
}  // namespace inference
}  // namespace paddle