paddle_mobile.cpp 2.9 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 {

19 20 21 22 23 24 25 26
template <typename Dtype, Precision P>
void PaddleMobile<Dtype, P>::SetThreadNum(int num) {
#ifdef _OPENMP
  //  omp_set_dynamic(0);
  omp_set_num_threads(num);
#endif
};

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

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

  return true;
}

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

  if (executor_.get() == nullptr) {
    executor_ = std::make_shared<Executor<Dtype, P>>(
W
wangliu 已提交
58 59
        loader_->Load(model_path, para_path, optimize, quantification),
        batch_size, optimize);
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  } else {
    LOG(kLOG_INFO) << "executor inited";
  }

  return true;
}

template <typename Dtype, Precision P>
std::shared_ptr<framework::Tensor> PaddleMobile<Dtype, P>::Predict(
    const framework::Tensor &t) {
  return executor_->Predict(t);
}

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

86
template <typename Dtype, Precision P>
L
liuruilong 已提交
87
PaddleMobile<Dtype, P>::~PaddleMobile() {
88 89 90 91
  executor_ = nullptr;
  loader_ = nullptr;
}

92
template class PaddleMobile<CPU, Precision::FP32>;
93

94
template class PaddleMobile<FPGA, Precision::FP32>;
95

96 97 98
template class PaddleMobile<GPU_MALI, Precision::FP32>;

}  // namespace paddle_mobile