light_api_impl.cc 3.9 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 20 21
#include "lite/model_parser/model_parser.h"

namespace paddle {
S
sangoly 已提交
22
namespace lite {
23

S
sangoly 已提交
24
void LightPredictorImpl::Init(const lite_api::MobileConfig& config) {
25
  // LightPredictor Only support NaiveBuffer backend in publish lib
26 27 28 29 30 31 32 33 34 35 36
  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 已提交
37 38
  mode_ = config.power_mode();
  threads_ = config.threads();
39 40

#ifdef LITE_WITH_NPU
41 42
  // Store the model-level configuration into scope for kernels, and use
  // exe_scope to store the execution-level configuration
43
  Context<TargetType::kNPU>::SetSubgraphModelCacheDir(
44
      raw_predictor_->scope(), config.subgraph_model_cache_dir());
45
#endif
46 47 48 49 50 51
#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 已提交
52 53
}

S
sangoly 已提交
54 55 56
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 已提交
57 58
}

S
sangoly 已提交
59 60 61 62
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 已提交
63 64
}

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

72 73
std::shared_ptr<lite_api::PaddlePredictor> LightPredictorImpl::Clone() {
  LOG(FATAL) << "The Clone API is not supported in LigthPredictor";
74
  return nullptr;
75 76 77 78 79 80
}

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;
81 82
}

83 84
std::string LightPredictorImpl::GetVersion() const { return lite::version(); }

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

S
sangoly 已提交
96
std::vector<std::string> LightPredictorImpl::GetInputNames() {
97 98 99
  return raw_predictor_->GetInputNames();
}

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

S
sangoly 已提交
104 105 106 107
}  // namespace lite

namespace lite_api {

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

}  // namespace lite_api
}  // namespace paddle