paddle_mobile.cpp 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2018 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 "io/paddle_mobile.h"

namespace paddle_mobile {

Y
yangfei 已提交
19
static std::mutex lc;
20 21 22 23 24
template <typename Dtype, Precision P>
void PaddleMobile<Dtype, P>::SetThreadNum(int num) {
#ifdef _OPENMP
  omp_set_num_threads(num);
#endif
25
}
26

27
template <typename Dtype, Precision P>
28
bool PaddleMobile<Dtype, P>::Load(const std::string &dirname, bool optimize,
H
hjchen2 已提交
29 30
                                  bool quantification, int batch_size,
                                  bool loddable) {
31
  if (loader_.get() == nullptr) {
32
    loader_ = std::make_shared<framework::Loader<Dtype, P>>();
33 34 35 36 37
  } else {
    LOG(kLOG_INFO) << "loader inited";
  }

  if (executor_.get() == nullptr) {
38
    executor_ = std::make_shared<framework::Executor<Dtype, P>>(
H
hjchen2 已提交
39 40
        loader_->Load(dirname, optimize, quantification), batch_size, optimize,
        loddable);
41 42 43 44 45 46 47 48 49
  } else {
    LOG(kLOG_INFO) << "executor inited";
  }

  return true;
}

template <typename Dtype, Precision P>
bool PaddleMobile<Dtype, P>::Load(const std::string &model_path,
50
                                  const std::string &para_path, bool optimize,
H
hjchen2 已提交
51 52
                                  bool quantification, int batch_size,
                                  bool loddable) {
53
  if (loader_.get() == nullptr) {
54
    loader_ = std::make_shared<framework::Loader<Dtype, P>>();
55 56 57 58 59
  } else {
    LOG(kLOG_INFO) << "loader inited";
  }

  if (executor_.get() == nullptr) {
60
    executor_ = std::make_shared<framework::Executor<Dtype, P>>(
W
wangliu 已提交
61
        loader_->Load(model_path, para_path, optimize, quantification),
H
hjchen2 已提交
62
        batch_size, optimize, loddable);
63 64 65 66 67 68 69
  } else {
    LOG(kLOG_INFO) << "executor inited";
  }

  return true;
}

70
template <typename Dtype, Precision P>
L
liuruilong 已提交
71 72 73 74
bool PaddleMobile<Dtype, P>::LoadCombinedMemory(size_t model_len,
                                                const uint8_t *model_buf,
                                                size_t combined_params_len,
                                                uint8_t *combined_params_buf) {
H
hjchen2 已提交
75
  int batch_size = 1;
76 77 78 79
  bool optimise = true;
  bool quantification = false;

  if (loader_.get() == nullptr) {
80
    loader_ = std::make_shared<framework::Loader<Dtype, P>>();
81 82 83 84 85
  } else {
    LOG(kLOG_INFO) << "loader inited";
  }

  if (executor_.get() == nullptr) {
86
    executor_ = std::make_shared<framework::Executor<Dtype, P>>(
87 88
        loader_->LoadCombinedMemory(model_len, model_buf, combined_params_len,
                                    combined_params_buf, optimise,
89
                                    quantification),
H
hjchen2 已提交
90
        batch_size, optimise);
91 92 93 94 95 96
  } else {
    LOG(kLOG_INFO) << "executor inited";
  }

  return true;
}
97 98 99 100 101 102
template <typename Dtype, Precision P>
std::shared_ptr<framework::Tensor> PaddleMobile<Dtype, P>::Predict(
    const framework::Tensor &t) {
  return executor_->Predict(t);
}

xiebaiyuan's avatar
xiebaiyuan 已提交
103 104 105 106 107 108
template <typename Dtype, Precision P>
std::shared_ptr<framework::Tensor> PaddleMobile<Dtype, P>::PredictLod(
    const framework::LoDTensor &t) {
  return executor_->PredictLod(t);
}

109 110 111 112 113 114 115 116 117 118 119 120 121
template <typename Dtype, Precision P>
std::vector<typename PaddleMobile<Dtype, P>::Ptype>
PaddleMobile<Dtype, P>::Predict(const std::vector<Ptype> &input,
                                const std::vector<int64_t> &dims) {
  return executor_->Predict(input, dims);
}

template <typename Dtype, Precision P>
void PaddleMobile<Dtype, P>::Clear() {
  executor_ = nullptr;
  loader_ = nullptr;
}

122
template <typename Dtype, Precision P>
L
liuruilong 已提交
123
PaddleMobile<Dtype, P>::~PaddleMobile() {
124 125 126 127
  executor_ = nullptr;
  loader_ = nullptr;
}

128 129 130 131
#ifdef PADDLE_MOBILE_FPGA

template <typename Dtype, Precision P>
void PaddleMobile<Dtype, P>::InjectVariable(const framework::Tensor &t,
H
hjchen2 已提交
132
                                            std::string var_name) {
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  executor_->InjectVariable(t, var_name);
}

template <typename Dtype, Precision P>
void PaddleMobile<Dtype, P>::FeedData(const framework::Tensor &t) {
  executor_->FeedData(t);
}

template <typename Dtype, Precision P>
std::shared_ptr<framework::Tensor> PaddleMobile<Dtype, P>::FetchResult(int id) {
  return executor_->FetchResult(id);
}

template <typename Dtype, Precision P>
void PaddleMobile<Dtype, P>::Predict_From_To(int start, int end) {
  executor_->Predict_From_To(start, end);
}

template <typename Dtype, Precision P>
void PaddleMobile<Dtype, P>::Predict_From(int start) {
  executor_->Predict_From(start);
}

template <typename Dtype, Precision P>
void PaddleMobile<Dtype, P>::Predict_To(int end) {
  executor_->Predict_To(end);
}
#endif

Y
yangfei 已提交
162 163 164
#ifdef PADDLE_MOBILE_CL
template <typename Dtype, Precision P>
void PaddleMobile<Dtype, P>::SetCLPath(std::string path) {
Y
yangfei 已提交
165 166 167 168
  std::lock_guard<std::mutex> lock(lc);
  if (framework::CLEngine::Instance()->GetCLPath() == "") {
    framework::CLEngine::Instance()->setClPath(path);
  }
Y
yangfei 已提交
169 170 171
}
#endif

172 173 174 175
template class PaddleMobile<CPU, Precision::FP32>;
template class PaddleMobile<FPGA, Precision::FP32>;
template class PaddleMobile<GPU_MALI, Precision::FP32>;

Y
yangfei 已提交
176 177
template class PaddleMobile<GPU_CL, Precision::FP32>;

178
}  // namespace paddle_mobile