paddle_inference_api.h 5.0 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 48
using PrecisionType = paddle::AnalysisConfig::Precision;
using Config = paddle::AnalysisConfig;

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

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

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

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

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

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

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

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

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

140 141 142 143 144 145 146 147 148 149 150
  ///
  /// \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 已提交
151 152 153 154
 private:
  std::unique_ptr<paddle::PaddlePredictor> predictor_;
};

W
Wilber 已提交
155 156 157 158 159 160 161 162 163 164 165
///
/// \brief A factory to help create predictors.
///
/// Usage:
///
/// \code{.cpp}
/// Config config;
/// ... // change the configs.
/// auto predictor = CreatePredictor(config);
/// \endcode
///
W
Wilber 已提交
166 167
PD_INFER_DECL std::shared_ptr<Predictor> CreatePredictor(
    const Config& config);  // NOLINT
W
Wilber 已提交
168

W
Wilber 已提交
169 170 171 172 173 174
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);

namespace services {
W
Wilber 已提交
175 176 177 178 179 180 181 182
///
/// \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 已提交
183 184 185 186 187 188
class PD_INFER_DECL PredictorPool {
 public:
  PredictorPool() = delete;
  PredictorPool(const PredictorPool&) = delete;
  PredictorPool& operator=(const PredictorPool&) = delete;

W
Wilber 已提交
189
  /// \brief Construct the predictor pool with \param size predictor instances.
W
Wilber 已提交
190
  explicit PredictorPool(const Config& config, size_t size = 1);
W
Wilber 已提交
191 192

  /// \brief Get \param id-th predictor.
W
Wilber 已提交
193 194 195 196 197 198 199
  Predictor* Retrive(size_t idx);

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

W
Wilber 已提交
201
}  // namespace paddle_infer