paddle_inference_api.h 6.3 KB
Newer Older
N
nhzlx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* 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. */

/*
 * This file contains the definition of a simple Inference API for Paddle.
 *
 * ATTENTION: It requires some C++ features, for lower version C++ or C, we
 * might release another API.
 */

#pragma once

#include <cassert>
#include <memory>
#include <string>
#include <vector>

namespace paddle_mobile {

Z
zhangyang0701 已提交
31
#ifdef PADDLE_MOBILE_FPGA
32

Z
zhangyang0701 已提交
33 34
namespace fpga {
int open_device();
35
int close_device();
36 37
void* fpga_malloc(size_t size);
void fpga_free(void* ptr);
Z
zhangyang0701 已提交
38 39 40 41

//  Usage:
//  auto version = fpga::paddle_mobile_version();
//  std::cout << "0X0" << std::hex << version << std::endl;
42
uint32_t paddle_mobile_version();
43
}  // namespace fpga
Z
zhangyang0701 已提交
44 45
#endif

N
nhzlx 已提交
46 47
enum PaddleDType {
  FLOAT32,
48
  FLOAT16,
N
nhzlx 已提交
49
  INT64,
50
  INT8,
51
  UINT8,
52 53 54 55 56
};

enum LayoutType {
  LAYOUT_CHW = 1,
  LAYOUT_HWC = 0,
N
nhzlx 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69
};

class PaddleBuf {
 public:
  PaddleBuf() = default;
  PaddleBuf(PaddleBuf&& other);
  // Copy only available when memory is managed externally.
  explicit PaddleBuf(const PaddleBuf&);
  PaddleBuf& operator=(const PaddleBuf&);
  // Do not own the memory.
  PaddleBuf(void* data, size_t length)
      : data_(data), length_(length), memory_owned_{false} {}
  // Own memory.
L
liuruilong 已提交
70
  explicit PaddleBuf(size_t length)
N
nhzlx 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
      : data_(new char[length]), length_(length), memory_owned_(true) {}
  // Resize to `length` bytes.
  void Resize(size_t length);
  // Reset to external memory.
  void Reset(void* data, size_t length);
  bool empty() const { return length_ == 0; }
  void* data() const { return data_; }
  size_t length() const { return length_; }

  ~PaddleBuf() { Free(); }

 private:
  void Free();
  void* data_{nullptr};  // pointer to the data memory.
  size_t length_{0};     // number of memory bytes.
  bool memory_owned_{true};
};

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
typedef enum {
  paddle_void = 0,
  paddle_float,
  paddle_int,
  paddle_uint16_t,
  paddle_double,
  paddle_int64_t,
  paddle_size_t,
  paddle_int16_t,
  paddle_int8_t,
  paddle_uint8_t,
  paddle_bool,
  paddle_string,
  paddle_floats = 100,
  paddle_ints,
  paddle_int64_ts,
  paddle_size_ts,
  paddle_bools,
  paddle_strings,
  paddle_const_float = 200,
  paddle_const_int,
  paddle_block = 300,
  paddle_tensor,
  paddle_lod_tensor,
  paddle_blocks,
  paddle_tensors,
  paddle_lod_tensors,
  paddle_p_block = 400,
  paddle_p_tensor,
  paddle_p_lod_tensor,
  paddle_p_blocks,
  paddle_p_tensors,
  paddle_p_lod_tensors,
  paddle_scopes = 500,
  paddle_selected_rows,
  paddle_dim0 = 600,
  paddle_dim1,
  paddle_dim2,
  paddle_dim3,
  paddle_dim4,
  paddle_dim5,
  paddle_dim6,
  paddle_dim7,
  paddle_dim8,
  paddle_dim9,
#ifdef PADDLE_MOBILE_CL
  paddle_cl_image,
#endif
} PaddlekTypeId_t;

N
nhzlx 已提交
139 140 141 142
struct PaddleTensor {
  PaddleTensor() = default;
  std::string name;  // variable name.
  std::vector<int> shape;
143
  std::vector<int> lod;
N
nhzlx 已提交
144 145
  PaddleBuf data;  // blob of data.
  PaddleDType dtype;
146
  PaddlekTypeId_t dtypeid;
147
  LayoutType layout;
N
nhzlx 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
};

enum class PaddleEngineKind {
  kPaddleMobile,
  // TODO(Superjomn) support following engines latter.
  // kTensorRT,           // Use TensorRT for inference.
  // kAutoMixedAnakin,    // Automatically mix Fluid with Anakin.
  // kAutoMixedTensorRT,  // Automatically mix Fluid with TensorRT.
};

/*
 * A simple Inference API for Paddle. Currently this API can be used by
 * non-sequence scenerios.
 */
class PaddlePredictor {
 public:
  struct Config;
  PaddlePredictor(const PaddlePredictor&) = delete;
  PaddlePredictor& operator=(const PaddlePredictor&) = delete;

  // Predict an record.
  // The caller should be responsible for allocating and releasing the memory of
  // `inputs`. `inputs` should be available until Run returns. Caller should be
  // responsible for the output tensor's buffer, either allocated or passed from
  // outside.
Z
zhangyang0701 已提交
173

N
nhzlx 已提交
174 175 176
  virtual bool Run(const std::vector<PaddleTensor>& inputs,
                   std::vector<PaddleTensor>* output_data,
                   int batch_size = -1) = 0;
177
  virtual std::string GetExceptionMsg() { return ""; }
N
nhzlx 已提交
178 179 180 181 182 183
  // Destroy the Predictor.
  virtual ~PaddlePredictor() = default;

  // The common configs for all the predictors.
  struct Config {
    std::string model_dir;  // path to the model directory.
184 185
    std::string prog_file;
    std::string param_file;
N
nhzlx 已提交
186
  };
Z
zhangyang0701 已提交
187
#ifdef PADDLE_MOBILE_FPGA
188 189 190
  virtual void Predict_From_To(int start, int end) = 0;
  virtual void FeedPaddleTensors(const std::vector<PaddleTensor>& inputs) = 0;
  virtual void FetchPaddleTensors(std::vector<PaddleTensor>* outputs) = 0;
J
jameswu2014 已提交
191
  virtual void FetchPaddleTensors(PaddleTensor* outputs, int id) = 0;
192 193
  virtual void GetPaddleTensor(const std::string& name,
                               PaddleTensor* output) = 0;
194 195 196 197
#else
  virtual void Feed(const std::string& var_name, const PaddleTensor& input) = 0;
  virtual void Fetch(const std::string& var_name, PaddleTensor* output) = 0;
  virtual bool Run() = 0;
Z
zhangyang0701 已提交
198
#endif
L
liuruilong 已提交
199 200 201

 protected:
  PaddlePredictor() = default;
N
nhzlx 已提交
202 203
};

xiebaiyuan's avatar
xiebaiyuan 已提交
204 205 206 207 208 209 210 211
struct PaddleModelMemoryPack {
  bool from_memory = false;
  size_t model_size = 0;
  uint8_t* model_buf = nullptr;
  size_t combined_params_size = 0;
  uint8_t* combined_params_buf = nullptr;
};

N
nhzlx 已提交
212 213
struct PaddleMobileConfig : public PaddlePredictor::Config {
  enum Precision { FP32 = 0 };
L
liuruilong 已提交
214
  enum Device { kCPU = 0, kFPGA = 1, kGPU_MALI = 2, kGPU_CL = 3 };
215
  enum PrePostType { NONE_PRE_POST = 0, UINT8_255 = 1 };
N
nhzlx 已提交
216 217 218

  enum Precision precision;
  enum Device device;
219
  enum PrePostType pre_post_type;
N
nhzlx 已提交
220 221 222 223

  int batch_size = 1;
  bool optimize = true;
  bool quantification = false;
224
  int quantification_fold = 1;
225
  bool lod_mode = false;
N
nhzlx 已提交
226
  int thread_num = 1;
227
  bool load_when_predict = false;
228
  bool mem_opt = true;
Y
yangfei 已提交
229
  std::string cl_path;
xiebaiyuan's avatar
xiebaiyuan 已提交
230
  struct PaddleModelMemoryPack memory_pack;
N
nhzlx 已提交
231 232 233 234 235 236 237 238
};

// A factory to help create different predictors.
template <typename ConfigT,
          PaddleEngineKind engine = PaddleEngineKind::kPaddleMobile>
std::unique_ptr<PaddlePredictor> CreatePaddlePredictor(const ConfigT& config);

}  // namespace paddle_mobile