paddle_inference_api.h 8.3 KB
Newer Older
Y
Yan Chunwei 已提交
1 2
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Y
Yan Chunwei 已提交
3 4 5
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
Y
Yan Chunwei 已提交
6

Y
Yan Chunwei 已提交
7
http://www.apache.org/licenses/LICENSE-2.0
Y
Yan Chunwei 已提交
8

Y
Yan Chunwei 已提交
9 10 11 12 13
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. */
Y
Yan Chunwei 已提交
14

15 16 17
/*
 * This file contains the definition of a simple Inference API for Paddle.
 *
18
 * ATTENTION: It requires some C++11 features, for lower version C++ or C, we
19 20 21
 * might release another API.
 */

Y
Yan Chunwei 已提交
22 23
#pragma once

24
#include <cassert>
W
Wilber 已提交
25
#include <map>
26
#include <memory>
Y
Yan Chunwei 已提交
27
#include <string>
W
Wilber 已提交
28
#include <utility>
Y
Yan Chunwei 已提交
29 30
#include <vector>

31
#include "paddle_analysis_config.h"  // NOLINT
32
#include "paddle_api.h"              // NOLINT
W
Wilber 已提交
33

W
Wilber 已提交
34 35 36 37 38 39 40 41 42 43
///
/// \file paddle_inference_api.h
///
/// \brief Paddle Inference API
///
/// \author paddle-infer@baidu.com
/// \date 2020-09-01
/// \since 2.0.0-beta
///

W
Wilber 已提交
44 45 46 47 48 49
namespace paddle_infer {
using DataType = paddle::PaddleDType;
using PlaceType = paddle::PaddlePlace;
using PrecisionType = paddle::AnalysisConfig::Precision;
using Config = paddle::AnalysisConfig;

W
Wilber 已提交
50 51 52 53 54 55 56 57
///
/// \class Tensor
///
/// \brief Represents an n-dimensional array of values.
/// The Tensor is used to store the input or output of the network.
/// It is obtained through Predictor::GetinputHandle()
/// and Predictor::GetOutputHandle() interface.
///
W
Wilber 已提交
58 59 60 61 62 63 64
class PD_INFER_DECL Tensor {
 public:
  // Can only be created by predictor->GetInputHandle(cosnt std::string& name)
  // or predictor->GetOutputHandle(cosnt std::string& name)
  Tensor() = delete;
  explicit Tensor(std::unique_ptr<paddle::ZeroCopyTensor>&& tensor)
      : tensor_(std::move(tensor)) {}
W
Wilber 已提交
65 66 67 68 69 70 71

  ///
  /// \brief Reset the shape of the tensor.
  /// Generally it's only used for the input tensor.
  /// Reshape must be called before calling mutable_data() or CopyFromCpu()
  /// \param shape The shape to set.
  ///
W
Wilber 已提交
72 73
  void Reshape(const std::vector<int>& shape);

W
Wilber 已提交
74 75 76 77 78
  ///
  /// \brief Copy the host memory to tensor data.
  /// It's usually used to set the input tensor data.
  /// \param data The pointer of the data, from which the tensor will copy.
  ///
W
Wilber 已提交
79 80 81
  template <typename T>
  void CopyFromCpu(const T* data);

W
Wilber 已提交
82 83 84 85 86 87 88
  ///
  /// \brief Get the memory pointer in CPU or GPU with specific data type.
  /// Please Reshape the tensor first before call this.
  /// It's usually used to get input data pointer.
  /// \param place The place of the tensor.
  /// \return The tensor data buffer pointer.
  ///
W
Wilber 已提交
89 90 91
  template <typename T>
  T* mutable_data(PlaceType place);

W
Wilber 已提交
92 93 94 95 96
  ///
  /// \brief Copy the tensor data to the host memory.
  /// It's usually used to get the output tensor data.
  /// \param[out] data The tensor will copy the data to the address.
  ///
W
Wilber 已提交
97 98 99
  template <typename T>
  void CopyToCpu(T* data);

W
Wilber 已提交
100 101 102 103 104 105 106
  ///
  /// \brief Get the memory pointer directly.
  /// It's usually used to get the output data pointer.
  /// \param[out] place To get the device type of the tensor.
  /// \param[out] size To get the data size of the tensor.
  /// \return The tensor data buffer pointer.
  ///
W
Wilber 已提交
107 108 109
  template <typename T>
  T* data(PlaceType* place, int* size) const;

W
Wilber 已提交
110 111 112 113 114 115
  ///
  /// \brief Set lod info of the tensor.
  /// More about LOD can be seen here:
  ///  https://www.paddlepaddle.org.cn/documentation/docs/zh/beginners_guide/basic_concept/lod_tensor.html#lodtensor
  /// \param x the lod info.
  ///
W
Wilber 已提交
116
  void SetLoD(const std::vector<std::vector<size_t>>& x);
W
Wilber 已提交
117 118

  /// \brief Return the lod info of the tensor.
W
Wilber 已提交
119 120
  std::vector<std::vector<size_t>> lod() const;

W
Wilber 已提交
121 122 123
  /// \brief Return the data type of the tensor.
  /// It's usually used to get the output tensor data type.
  /// \return The data type of the tensor.
W
Wilber 已提交
124 125
  DataType type() const;

W
Wilber 已提交
126
  /// \brief Return the shape of the Tensor.
W
Wilber 已提交
127
  std::vector<int> shape() const;
W
Wilber 已提交
128 129

  /// \brief Return the name of the tensor.
W
Wilber 已提交
130 131 132 133 134 135
  const std::string& name() const;

 private:
  std::unique_ptr<paddle::ZeroCopyTensor> tensor_;
};

W
Wilber 已提交
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 162 163 164
///
/// \class Predictor
///
/// \brief Predictor is the interface for model prediction.
///
/// The predictor has the following typical uses:
///
/// Get predictor
/// \code{cpp}
///   auto predictor = CreatePredictor(config);
/// \endcode
///
/// Get input or output names
/// \code{cpp}
///   auto input_names = predictor->GetInputNames();
///   auto output_names = predictor->GetOutputNames();
/// \endcode
///
/// Get input or output handle
/// \code{cpp}
///   auto input_t = predictor->GetInputHandle(input_names[0]);
///   auto output_t = predictor->GetOutputHandle(output_names[0]);
/// \endcode
///
/// Run predictor
/// \code{cpp}
///   predictor->Run();
/// \endcode
///
W
Wilber 已提交
165 166
class PD_INFER_DECL Predictor {
 public:
W
Wilber 已提交
167
  Predictor() = delete;
W
Wilber 已提交
168 169 170 171 172
  ~Predictor() {}
  // Use for clone
  explicit Predictor(std::unique_ptr<paddle::PaddlePredictor>&& pred)
      : predictor_(std::move(pred)) {}

W
Wilber 已提交
173 174 175 176 177
  ///
  /// \brief Construct a new Predictor object
  ///
  /// \param[in] Config config
  ///
W
Wilber 已提交
178 179
  explicit Predictor(const Config& config);

W
Wilber 已提交
180 181 182 183 184
  ///
  /// \brief Get the input names
  ///
  /// \return input names
  ///
W
Wilber 已提交
185
  std::vector<std::string> GetInputNames();
W
Wilber 已提交
186 187 188 189 190 191 192

  ///
  /// \brief Get the Input Tensor object
  ///
  /// \param[in] name input name
  /// \return input tensor
  ///
W
Wilber 已提交
193 194
  std::unique_ptr<Tensor> GetInputHandle(const std::string& name);

W
Wilber 已提交
195 196 197 198 199
  ///
  /// \brief Run the prediction engine
  ///
  /// \return Whether the function executed successfully
  ///
W
Wilber 已提交
200 201
  bool Run();

W
Wilber 已提交
202 203 204 205 206
  ///
  /// \brief Get the output names
  ///
  /// \return output names
  ///
W
Wilber 已提交
207
  std::vector<std::string> GetOutputNames();
W
Wilber 已提交
208 209 210 211 212 213 214

  ///
  /// \brief Get the Output Tensor object
  ///
  /// \param[in] name otuput name
  /// \return output tensor
  ///
W
Wilber 已提交
215 216
  std::unique_ptr<Tensor> GetOutputHandle(const std::string& name);

W
Wilber 已提交
217 218 219 220 221
  ///
  /// \brief Clone to get the new predictor. thread safe.
  ///
  /// \return get a new predictor
  ///
W
Wilber 已提交
222
  std::unique_ptr<Predictor> Clone();
W
Wilber 已提交
223 224

  /// \brief Clear the intermediate tensors of the predictor
W
Wilber 已提交
225 226
  void ClearIntermediateTensor();

227 228 229 230 231 232 233 234 235 236 237
  ///
  /// \brief Release all tmp tensor to compress the size of the memory pool.
  /// The memory pool is considered to be composed of a list of chunks, if
  /// the chunk is not occupied, it can be released.
  ///
  /// \return Number of bytes released. It may be smaller than the actual
  /// released memory, because part of the memory is not managed by the
  /// MemoryPool.
  ///
  uint64_t TryShrinkMemory();

W
Wilber 已提交
238 239 240 241
 private:
  std::unique_ptr<paddle::PaddlePredictor> predictor_;
};

W
Wilber 已提交
242 243 244 245 246 247 248 249 250 251 252
///
/// \brief A factory to help create predictors.
///
/// Usage:
///
/// \code{.cpp}
/// Config config;
/// ... // change the configs.
/// auto predictor = CreatePredictor(config);
/// \endcode
///
W
Wilber 已提交
253 254
PD_INFER_DECL std::shared_ptr<Predictor> CreatePredictor(
    const Config& config);  // NOLINT
W
Wilber 已提交
255

W
Wilber 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
PD_INFER_DECL int GetNumBytesOfDataType(DataType dtype);

PD_INFER_DECL std::string GetVersion();
PD_INFER_DECL std::string UpdateDllFlag(const char* name, const char* value);

template <typename T>
void Tensor::CopyFromCpu(const T* data) {
  tensor_->copy_from_cpu<T>(data);
}

template <typename T>
void Tensor::CopyToCpu(T* data) {
  return tensor_->copy_to_cpu<T>(data);
}

template <typename T>
T* Tensor::mutable_data(PlaceType place) {
  return tensor_->mutable_data<T>(place);
}

template <typename T>
T* Tensor::data(PlaceType* place, int* size) const {
  return tensor_->data<T>(place, size);
}

}  // namespace paddle_infer

namespace paddle_infer {
namespace services {

W
Wilber 已提交
286 287 288 289 290 291 292 293
///
/// \class PredictorPool
///
/// \brief PredictorPool is a simple encapsulation of Predictor, suitable for
/// use in multi-threaded situations. According to the thread id, the
/// corresponding Predictor is taken out from PredictorPool to complete the
/// prediction.
///
W
Wilber 已提交
294 295 296 297 298 299
class PD_INFER_DECL PredictorPool {
 public:
  PredictorPool() = delete;
  PredictorPool(const PredictorPool&) = delete;
  PredictorPool& operator=(const PredictorPool&) = delete;

W
Wilber 已提交
300
  /// \brief Construct the predictor pool with \param size predictor instances.
W
Wilber 已提交
301
  explicit PredictorPool(const Config& config, size_t size = 1);
W
Wilber 已提交
302 303

  /// \brief Get \param id-th predictor.
W
Wilber 已提交
304 305 306 307 308 309 310 311
  Predictor* Retrive(size_t idx);

 private:
  std::shared_ptr<Predictor> main_pred_;
  std::vector<std::unique_ptr<Predictor>> preds_;
};
}  // namespace services
}  // namespace paddle_infer