paddle_inference_api.h 5.5 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
namespace paddle_infer {
45

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

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

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

W
Wilber 已提交
94 95 96 97 98
  ///
  /// \brief Get the input names
  ///
  /// \return input names
  ///
W
Wilber 已提交
99
  std::vector<std::string> GetInputNames();
W
Wilber 已提交
100 101 102 103 104 105 106

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

W
Wilber 已提交
109 110 111 112 113
  ///
  /// \brief Run the prediction engine
  ///
  /// \return Whether the function executed successfully
  ///
W
Wilber 已提交
114 115
  bool Run();

W
Wilber 已提交
116 117 118 119 120
  ///
  /// \brief Get the output names
  ///
  /// \return output names
  ///
W
Wilber 已提交
121
  std::vector<std::string> GetOutputNames();
W
Wilber 已提交
122 123 124 125 126 127 128

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

W
Wilber 已提交
131 132 133 134 135
  ///
  /// \brief Clone to get the new predictor. thread safe.
  ///
  /// \return get a new predictor
  ///
136
  std::unique_ptr<Predictor> Clone(void* stream = nullptr);
W
Wilber 已提交
137 138

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

141 142 143 144 145 146 147 148 149 150 151
  ///
  /// \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();

152 153 154 155 156 157 158 159
  ///
  /// \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 已提交
160 161
 private:
  std::unique_ptr<paddle::PaddlePredictor> predictor_;
W
Wilber 已提交
162
  friend class paddle_infer::experimental::InternalUtils;
W
Wilber 已提交
163 164
};

W
Wilber 已提交
165 166 167 168 169 170 171 172 173 174 175
///
/// \brief A factory to help create predictors.
///
/// Usage:
///
/// \code{.cpp}
/// Config config;
/// ... // change the configs.
/// auto predictor = CreatePredictor(config);
/// \endcode
///
W
Wilber 已提交
176 177
PD_INFER_DECL std::shared_ptr<Predictor> CreatePredictor(
    const Config& config);  // NOLINT
W
Wilber 已提交
178

W
Wilber 已提交
179 180 181
PD_INFER_DECL int GetNumBytesOfDataType(DataType dtype);

PD_INFER_DECL std::string GetVersion();
182 183
PD_INFER_DECL std::tuple<int, int, int> GetTrtCompileVersion();
PD_INFER_DECL std::tuple<int, int, int> GetTrtRuntimeVersion();
W
Wilber 已提交
184 185 186
PD_INFER_DECL std::string UpdateDllFlag(const char* name, const char* value);

namespace services {
W
Wilber 已提交
187 188 189 190 191 192 193 194
///
/// \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 已提交
195 196 197 198 199 200
class PD_INFER_DECL PredictorPool {
 public:
  PredictorPool() = delete;
  PredictorPool(const PredictorPool&) = delete;
  PredictorPool& operator=(const PredictorPool&) = delete;

W
Wilber 已提交
201
  /// \brief Construct the predictor pool with \param size predictor instances.
W
Wilber 已提交
202
  explicit PredictorPool(const Config& config, size_t size = 1);
W
Wilber 已提交
203 204

  /// \brief Get \param id-th predictor.
W
Wilber 已提交
205 206 207 208 209 210 211
  Predictor* Retrive(size_t idx);

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

W
Wilber 已提交
213
}  // namespace paddle_infer