light_api_impl.cc 4.0 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 "lite/api/light_api.h"
16
#include <string>
Y
Yan Chunwei 已提交
17
#include "lite/api/paddle_api.h"
18
#include "lite/core/version.h"
Y
Yan Chunwei 已提交
19
#include "lite/model_parser/model_parser.h"
20 21 22 23
#ifndef LITE_ON_TINY_PUBLISH
#include "lite/api/paddle_use_kernels.h"
#include "lite/api/paddle_use_ops.h"
#endif
Y
Yan Chunwei 已提交
24 25

namespace paddle {
S
sangoly 已提交
26
namespace lite {
27

S
sangoly 已提交
28
void LightPredictorImpl::Init(const lite_api::MobileConfig& config) {
29
  // LightPredictor Only support NaiveBuffer backend in publish lib
30 31 32 33 34 35 36 37 38 39 40
  if (config.lite_model_file().empty()) {
    raw_predictor_.reset(
        new LightPredictor(config.model_dir(),
                           config.model_buffer(),
                           config.param_buffer(),
                           config.model_from_memory(),
                           lite_api::LiteModelType::kNaiveBuffer));
  } else {
    raw_predictor_.reset(new LightPredictor(config.lite_model_file(),
                                            config.model_from_memory()));
  }
T
TianXiaogang 已提交
41 42
  mode_ = config.power_mode();
  threads_ = config.threads();
43 44

#ifdef LITE_WITH_NPU
45 46
  // Store the model-level configuration into scope for kernels, and use
  // exe_scope to store the execution-level configuration
47
  Context<TargetType::kNPU>::SetSubgraphModelCacheDir(
48
      raw_predictor_->scope(), config.subgraph_model_cache_dir());
49
#endif
50 51 52 53 54 55
#ifdef LITE_WITH_HUAWEI_ASCEND_NPU
  Context<TargetType::kHuaweiAscendNPU>::SetHuaweiAscendDeviceID(
      config.get_device_id());
  Context<TargetType::kHuaweiAscendNPU>::SetSubgraphModelCacheDir(
      config.subgraph_model_cache_dir());
#endif
Y
Yan Chunwei 已提交
56 57
}

S
sangoly 已提交
58 59 60
std::unique_ptr<lite_api::Tensor> LightPredictorImpl::GetInput(int i) {
  return std::unique_ptr<lite_api::Tensor>(
      new lite_api::Tensor(raw_predictor_->GetInput(i)));
Y
Yan Chunwei 已提交
61 62
}

S
sangoly 已提交
63 64 65 66
std::unique_ptr<const lite_api::Tensor> LightPredictorImpl::GetOutput(
    int i) const {
  return std::unique_ptr<lite_api::Tensor>(
      new lite_api::Tensor(raw_predictor_->GetOutput(i)));
Y
Yan Chunwei 已提交
67 68
}

T
TianXiaogang 已提交
69 70 71 72 73 74
void LightPredictorImpl::Run() {
#ifdef LITE_WITH_ARM
  lite::DeviceInfo::Global().SetRunMode(mode_, threads_);
#endif
  raw_predictor_->Run();
}
Y
Yan Chunwei 已提交
75

76 77
std::shared_ptr<lite_api::PaddlePredictor> LightPredictorImpl::Clone() {
  LOG(FATAL) << "The Clone API is not supported in LigthPredictor";
78
  return nullptr;
79 80 81 82 83 84
}

std::shared_ptr<lite_api::PaddlePredictor> LightPredictorImpl::Clone(
    const std::vector<std::string>& var_names) {
  LOG(FATAL) << "The Clone API is not supported in LigthPredictor";
  return nullptr;
85 86
}

87 88
std::string LightPredictorImpl::GetVersion() const { return lite::version(); }

S
sangoly 已提交
89
std::unique_ptr<const lite_api::Tensor> LightPredictorImpl::GetTensor(
Y
Yan Chunwei 已提交
90
    const std::string& name) const {
S
sangoly 已提交
91 92
  return std::unique_ptr<const lite_api::Tensor>(
      new lite_api::Tensor(raw_predictor_->GetTensor(name)));
Y
Yan Chunwei 已提交
93
}
S
sangoly 已提交
94
std::unique_ptr<lite_api::Tensor> LightPredictorImpl::GetInputByName(
95
    const std::string& name) {
S
sangoly 已提交
96 97
  return std::unique_ptr<lite_api::Tensor>(
      new lite_api::Tensor(raw_predictor_->GetInputByName(name)));
98 99
}

S
sangoly 已提交
100
std::vector<std::string> LightPredictorImpl::GetInputNames() {
101 102 103
  return raw_predictor_->GetInputNames();
}

S
sangoly 已提交
104
std::vector<std::string> LightPredictorImpl::GetOutputNames() {
105 106
  return raw_predictor_->GetOutputNames();
}
Y
Yan Chunwei 已提交
107

S
sangoly 已提交
108 109 110 111
}  // namespace lite

namespace lite_api {

Y
Yan Chunwei 已提交
112 113 114
template <>
std::shared_ptr<PaddlePredictor> CreatePaddlePredictor(
    const MobileConfig& config) {
S
sangoly 已提交
115
  auto x = std::make_shared<lite::LightPredictorImpl>();
Y
Yan Chunwei 已提交
116 117 118 119 120 121
  x->Init(config);
  return x;
}

}  // namespace lite_api
}  // namespace paddle