paddle_inference_api.h 6.1 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;
50
using BackendType = paddle::AnalysisConfig::Backend;
W
Wilber 已提交
51

W
Wilber 已提交
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 80
///
/// \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 已提交
81 82
class PD_INFER_DECL Predictor {
 public:
W
Wilber 已提交
83
  Predictor() = delete;
W
Wilber 已提交
84 85 86 87 88
  ~Predictor() {}
  // Use for clone
  explicit Predictor(std::unique_ptr<paddle::PaddlePredictor>&& pred)
      : predictor_(std::move(pred)) {}

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

96 97 98 99 100 101 102
  ///
  /// \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 已提交
103 104 105 106 107
  ///
  /// \brief Get the input names
  ///
  /// \return input names
  ///
W
Wilber 已提交
108
  std::vector<std::string> GetInputNames();
W
Wilber 已提交
109 110 111 112 113 114 115

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

W
Wilber 已提交
118 119 120 121 122
  ///
  /// \brief Run the prediction engine
  ///
  /// \return Whether the function executed successfully
  ///
W
Wilber 已提交
123 124
  bool Run();

W
Wilber 已提交
125 126 127 128 129
  ///
  /// \brief Get the output names
  ///
  /// \return output names
  ///
W
Wilber 已提交
130
  std::vector<std::string> GetOutputNames();
W
Wilber 已提交
131 132 133 134 135 136 137

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

W
Wilber 已提交
140 141 142 143 144
  ///
  /// \brief Clone to get the new predictor. thread safe.
  ///
  /// \return get a new predictor
  ///
145
  std::unique_ptr<Predictor> Clone(void* stream = nullptr);
W
Wilber 已提交
146 147

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

150 151 152 153 154 155 156 157 158 159 160
  ///
  /// \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();

161 162 163 164 165 166 167 168
  ///
  /// \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 已提交
169 170
 private:
  std::unique_ptr<paddle::PaddlePredictor> predictor_;
W
Wilber 已提交
171
  friend class paddle_infer::experimental::InternalUtils;
W
Wilber 已提交
172 173
};

W
Wilber 已提交
174 175 176 177 178 179 180 181 182 183 184
///
/// \brief A factory to help create predictors.
///
/// Usage:
///
/// \code{.cpp}
/// Config config;
/// ... // change the configs.
/// auto predictor = CreatePredictor(config);
/// \endcode
///
W
Wilber 已提交
185 186
PD_INFER_DECL std::shared_ptr<Predictor> CreatePredictor(
    const Config& config);  // NOLINT
W
Wilber 已提交
187

W
Wilber 已提交
188 189 190
PD_INFER_DECL int GetNumBytesOfDataType(DataType dtype);

PD_INFER_DECL std::string GetVersion();
191 192
PD_INFER_DECL std::tuple<int, int, int> GetTrtCompileVersion();
PD_INFER_DECL std::tuple<int, int, int> GetTrtRuntimeVersion();
W
Wilber 已提交
193 194
PD_INFER_DECL std::string UpdateDllFlag(const char* name, const char* value);

195 196 197 198 199 200 201 202 203 204
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,
    BackendType backend,
    bool keep_io_types = true,
    std::unordered_set<std::string> black_list = {});

W
Wilber 已提交
205
namespace services {
W
Wilber 已提交
206 207 208 209 210 211 212 213
///
/// \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 已提交
214 215 216 217 218 219
class PD_INFER_DECL PredictorPool {
 public:
  PredictorPool() = delete;
  PredictorPool(const PredictorPool&) = delete;
  PredictorPool& operator=(const PredictorPool&) = delete;

W
Wilber 已提交
220
  /// \brief Construct the predictor pool with \param size predictor instances.
W
Wilber 已提交
221
  explicit PredictorPool(const Config& config, size_t size = 1);
W
Wilber 已提交
222 223

  /// \brief Get \param id-th predictor.
W
Wilber 已提交
224 225 226 227 228 229 230
  Predictor* Retrive(size_t idx);

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

W
Wilber 已提交
232
}  // namespace paddle_infer