paddle_inference_api.h 5.9 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);

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

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

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

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

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

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

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

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

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

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

W
Wilber 已提交
181 182 183
PD_INFER_DECL int GetNumBytesOfDataType(DataType dtype);

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

188 189 190 191 192 193 194 195 196 197
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 已提交
198
namespace services {
W
Wilber 已提交
199 200 201 202 203 204 205 206
///
/// \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 已提交
207 208 209 210 211 212
class PD_INFER_DECL PredictorPool {
 public:
  PredictorPool() = delete;
  PredictorPool(const PredictorPool&) = delete;
  PredictorPool& operator=(const PredictorPool&) = delete;

W
Wilber 已提交
213
  /// \brief Construct the predictor pool with \param size predictor instances.
W
Wilber 已提交
214
  explicit PredictorPool(const Config& config, size_t size = 1);
W
Wilber 已提交
215 216

  /// \brief Get \param id-th predictor.
W
Wilber 已提交
217 218 219 220 221 222 223
  Predictor* Retrive(size_t idx);

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

W
Wilber 已提交
225
}  // namespace paddle_infer