paddle_mobile.h 4.0 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. */

#pragma once

#include <memory>
#include <string>
19
#include <utility>
20 21
#include <vector>
#include "common/types.h"
22
#include "framework/executor.h"
H
hjchen2 已提交
23
#include "framework/load_ops.h"
24
#include "framework/loader.h"
25
#include "framework/tensor.h"
26
#include "io/paddle_inference_api.h"
Y
yangfei 已提交
27 28 29
#ifdef PADDLE_MOBILE_CL
#include "framework/cl/cl_engine.h"
#endif
30 31 32

namespace paddle_mobile {

33
template <typename Device, typename T = float>
34 35
class PaddleMobile {
 public:
xiebaiyuan's avatar
xiebaiyuan 已提交
36
  PaddleMobile(PaddleMobileConfigInternal config) : config_(config) {
L
liuruilong 已提交
37 38 39 40 41 42
#ifndef PADDLE_MOBILE_CL
    bool is_gpu = std::is_same<DeviceType<kGPU_CL>, Device>::value;
    PADDLE_MOBILE_ENFORCE(!is_gpu, "Please recompile with GPU_CL is on");
#endif
  }

43 44
  PaddleMobile() {
#ifndef PADDLE_MOBILE_CL
45 46
    bool is_gpu = std::is_same<DeviceType<kGPU_CL>, Device>::value;
    PADDLE_MOBILE_ENFORCE(!is_gpu, "Please recompile with GPU_CL is on");
47 48
#endif
  }
49 50 51 52
  ~PaddleMobile() {}

  PMStatus Load(const std::string &dirname, const bool optimize = false,
                const bool quantification = false, const int batch_size = 1,
53
                const bool lod_mode = false);
54 55
  PMStatus Load(const std::string &model_path, const std::string &para_path,
                const bool optimize = false, const bool quantification = false,
56 57 58
                const int batch_size = 1, const bool lod_mode = false);

  PMStatus Load(const PaddleMobileConfig &config);
59 60 61

  PMStatus Predict(const framework::Tensor &input);
  PMStatus Predict(const framework::LoDTensor &input);
62

63 64 65 66
  PMStatus Predict(
      const std::vector<std::pair<std::string, framework::Tensor>> &inputs);
  PMStatus Predict(
      const std::vector<std::pair<std::string, framework::LoDTensor>> &inputs);
67

68 69 70
  std::vector<T> Predict(const std::vector<T> &input,
                         const std::vector<int64_t> &dims);
  PMStatus Predict();
71

72 73
  void Feed(const framework::LoDTensor &input, const std::string &var_name);
  void Feed(const framework::Tensor &input, const std::string &var_name);
xiebaiyuan's avatar
xiebaiyuan 已提交
74

75 76 77 78
  typedef std::shared_ptr<framework::LoDTensor> LoDTensorPtr;
  LoDTensorPtr Fetch(const std::string &var_name);

  LoDTensorPtr Fetch() { return Fetch("fetch"); }
79

80 81
  bool LoadCombinedMemory(size_t model_len, const uint8_t *model_buf,
                          size_t combined_params_len,
L
liuruilong 已提交
82 83
                          uint8_t *combined_params_buf, bool optimize = false,
                          bool quantification = false, int batch_size = 1,
84
                          bool lod_mode = false);
85

86
  void SetThreadNum(int count);
87
  void Clear();
88
  double GetPredictTime();
89

90
#ifdef PADDLE_MOBILE_FPGA
H
hjchen2 已提交
91
  void InjectVariable(const framework::Tensor &t, std::string var_name);
92
  void FeedData(const framework::Tensor &t);
93
  void FeedData(const std::vector<void *> &v);
94
  void FeedTensorData(const std::vector<framework::Tensor> &v);
95

96
  void GetResults(std::vector<void *> *v);
97
  void GetTensorResults(std::vector<framework::Tensor *> *v);
98

99 100 101 102 103 104
  std::shared_ptr<framework::Tensor> FetchResult(int id = -1);
  void Predict_From_To(int start = 0, int end = -1);
  void Predict_From(int start);
  void Predict_To(int end);
#endif

Y
yangfei 已提交
105
#ifdef PADDLE_MOBILE_CL
106
 public:  // NOLINT
Y
yangfei 已提交
107
  void SetCLPath(std::string cl_path);
108 109
  int readText(const char *kernelPath,
               char **pcode);  // 读取文本文件放入 pcode,返回字符串长度
Y
yangfei 已提交
110 111
#endif

112
 private:
113 114
  std::shared_ptr<framework::Loader<Device, T>> loader_;
  std::shared_ptr<framework::Executor<Device, T>> executor_;
L
liuruilong 已提交
115
  PaddleMobileConfigInternal config_;
116 117 118
};

}  // namespace paddle_mobile