paddle_inference_api.h 7.4 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>
28
#include <unordered_set>
W
Wilber 已提交
29
#include <utility>
Y
Yan Chunwei 已提交
30 31
#include <vector>

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

W
Wilber 已提交
35 36 37 38 39 40 41 42 43 44
///
/// \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 已提交
45
namespace paddle_infer {
46

W
Wilber 已提交
47 48
using PrecisionType = paddle::AnalysisConfig::Precision;
using Config = paddle::AnalysisConfig;
49
using DistConfig = paddle::DistConfig;
W
Wilber 已提交
50

W
Wilber 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
///
/// \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 已提交
80 81
class PD_INFER_DECL Predictor {
 public:
W
Wilber 已提交
82
  Predictor() = delete;
W
Wilber 已提交
83 84 85 86 87
  ~Predictor() {}
  // Use for clone
  explicit Predictor(std::unique_ptr<paddle::PaddlePredictor>&& pred)
      : predictor_(std::move(pred)) {}

W
Wilber 已提交
88 89 90 91 92
  ///
  /// \brief Construct a new Predictor object
  ///
  /// \param[in] Config config
  ///
W
Wilber 已提交
93 94
  explicit Predictor(const Config& config);

95 96 97 98 99 100 101
  ///
  /// \brief Get all input names and their corresponding shapes
  ///
  /// \return the map of input names and shape
  ///
  std::map<std::string, std::vector<int64_t>> GetInputTensorShape();

102 103 104 105 106 107 108
  ///
  /// \brief Get all input names and their corresponding type
  ///
  /// \return the map of input names and type
  ///
  std::map<std::string, DataType> GetInputTypes();

W
Wilber 已提交
109 110 111 112 113
  ///
  /// \brief Get the input names
  ///
  /// \return input names
  ///
W
Wilber 已提交
114
  std::vector<std::string> GetInputNames();
W
Wilber 已提交
115 116 117 118 119 120 121

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

W
Wilber 已提交
124 125 126 127 128
  ///
  /// \brief Run the prediction engine
  ///
  /// \return Whether the function executed successfully
  ///
W
Wilber 已提交
129 130
  bool Run();

131 132 133 134 135 136 137 138 139 140 141
  ///
  /// \brief Run the prediction engine (Recommended)
  ///
  /// \param[in] inputs An list of Tensor as the input to the network.
  /// \param[out] outputs Pointer to the tensor list, which holds the output
  /// Tensor
  ///
  /// \return Whether the run is successful
  bool Run(const std::vector<paddle::Tensor>& inputs,
           std::vector<paddle::Tensor>* outputs);

W
Wilber 已提交
142 143 144 145 146
  ///
  /// \brief Get the output names
  ///
  /// \return output names
  ///
W
Wilber 已提交
147
  std::vector<std::string> GetOutputNames();
W
Wilber 已提交
148 149 150 151 152 153 154

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

157 158 159 160 161 162 163 164 165 166 167 168 169 170
  ///
  /// \brief Get all output names and their corresponding shapes
  ///
  /// \return the map of output names and shape
  ///
  std::map<std::string, std::vector<int64_t>> GetOutputTensorShape();

  ///
  /// \brief Get all output names and their corresponding type
  ///
  /// \return the map of output names and type
  ///
  std::map<std::string, DataType> GetOutputTypes();

W
Wilber 已提交
171 172 173 174 175
  ///
  /// \brief Clone to get the new predictor. thread safe.
  ///
  /// \return get a new predictor
  ///
176
  std::unique_ptr<Predictor> Clone(void* stream = nullptr);
W
Wilber 已提交
177 178

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

181 182 183 184 185 186 187 188 189 190 191
  ///
  /// \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();

192 193 194 195 196 197 198 199 200 201
  ///
  /// \brief Register a output hook function to operate the intermediate tensor
  /// of op output. when using this function, memory reuse should be tured off.
  /// The hook function signature is void(const std::string&, const
  /// std::string&, const Tensor&>). Here, the first parameter is op's
  /// type, the second param is output var name of the op, and the third
  /// parameter is output tensor with the var name.
  ///
  void RegisterOutputHook(const Exp_OutputHookFunc& hookfunc);

202 203 204 205 206 207 208 209
  ///
  /// \brief Get the execution stream on devices with a concept of stream,
  /// otherwise returns nullptr.
  ///
  /// \return The execution stream or nullptr (CPU).
  ///
  void* GetExecStream() const;

W
Wilber 已提交
210 211
 private:
  std::unique_ptr<paddle::PaddlePredictor> predictor_;
W
Wilber 已提交
212
  friend class paddle_infer::experimental::InternalUtils;
W
Wilber 已提交
213 214
};

W
Wilber 已提交
215 216 217 218 219 220 221 222 223 224 225
///
/// \brief A factory to help create predictors.
///
/// Usage:
///
/// \code{.cpp}
/// Config config;
/// ... // change the configs.
/// auto predictor = CreatePredictor(config);
/// \endcode
///
W
Wilber 已提交
226 227
PD_INFER_DECL std::shared_ptr<Predictor> CreatePredictor(
    const Config& config);  // NOLINT
W
Wilber 已提交
228

W
Wilber 已提交
229 230 231
PD_INFER_DECL int GetNumBytesOfDataType(DataType dtype);

PD_INFER_DECL std::string GetVersion();
232 233
PD_INFER_DECL std::tuple<int, int, int> GetTrtCompileVersion();
PD_INFER_DECL std::tuple<int, int, int> GetTrtRuntimeVersion();
W
Wilber 已提交
234 235
PD_INFER_DECL std::string UpdateDllFlag(const char* name, const char* value);

236 237 238 239 240 241
PD_INFER_DECL void ConvertToMixedPrecision(
    const std::string& model_file,
    const std::string& params_file,
    const std::string& mixed_model_file,
    const std::string& mixed_params_file,
    PrecisionType mixed_precision,
242
    PlaceType backend,
243 244 245
    bool keep_io_types = true,
    std::unordered_set<std::string> black_list = {});

W
Wilber 已提交
246
namespace services {
W
Wilber 已提交
247 248 249 250 251 252 253 254
///
/// \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 已提交
255 256 257 258 259 260
class PD_INFER_DECL PredictorPool {
 public:
  PredictorPool() = delete;
  PredictorPool(const PredictorPool&) = delete;
  PredictorPool& operator=(const PredictorPool&) = delete;

W
Wilber 已提交
261
  /// \brief Construct the predictor pool with \param size predictor instances.
W
Wilber 已提交
262
  explicit PredictorPool(const Config& config, size_t size = 1);
W
Wilber 已提交
263 264

  /// \brief Get \param id-th predictor.
W
Wilber 已提交
265 266 267 268 269 270 271
  Predictor* Retrive(size_t idx);

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

W
Wilber 已提交
273
}  // namespace paddle_infer