nndeploy C++ API  0.2.0
nndeploy C++ API
inference.h
Go to the documentation of this file.
1 
2 #ifndef _NNDEPLOY_INFERENCE_INFERENCE_H_
3 #define _NNDEPLOY_INFERENCE_INFERENCE_H_
4 
5 #include "nndeploy/base/any.h"
6 #include "nndeploy/base/common.h"
8 #include "nndeploy/base/log.h"
9 #include "nndeploy/base/macro.h"
10 #include "nndeploy/base/object.h"
11 #include "nndeploy/base/status.h"
12 #include "nndeploy/base/string.h"
13 #include "nndeploy/device/buffer.h"
14 #include "nndeploy/device/device.h"
16 #include "nndeploy/device/tensor.h"
18 
19 namespace nndeploy {
20 namespace inference {
21 
41  public:
43  virtual ~Inference();
44 
46 
54  base::Status setParamSharedPtr(std::shared_ptr<base::Param> param);
55 
62  std::shared_ptr<base::Param> getParamSharedPtr();
63 
64  base::DeviceType getDeviceType() { return inference_param_->device_type_; }
65 
66  void setStream(device::Stream *stream);
68 
74  virtual base::Status init() = 0;
80  virtual base::Status deinit() = 0;
81 
106  virtual base::Status reshape(base::ShapeMap &shape_map) = 0;
107 
113  virtual int64_t getMemorySize();
126  virtual float getGFLOPs();
127 
133  virtual bool isBatch();
134 
140  virtual bool isShareContext();
146  virtual bool isShareStream();
152  virtual bool isInputDynamic();
158  virtual bool isOutputDynamic();
167  virtual bool canOpInput();
176  virtual bool canOpOutput();
177 
183  virtual int getNumOfInputTensor();
189  virtual int getNumOfOutputTensor();
190 
197  virtual std::string getInputName(int i);
204  virtual std::string getOutputName(int i);
210  virtual std::vector<std::string> getAllInputTensorName();
216  virtual std::vector<std::string> getAllOutputTensorName();
217 
224  virtual base::IntVector getInputShape(const std::string &name);
238  virtual device::TensorDesc getInputTensorDesc(const std::string &name);
245  virtual device::TensorDesc getOutputTensorDesc(const std::string &name);
246 
253  virtual device::TensorDesc getInputTensorAlignDesc(const std::string &name);
260  virtual device::TensorDesc getOutputTensorAlignDesc(const std::string &name);
261 
267  virtual std::map<std::string, device::Tensor *> getAllInputTensorMap();
273  virtual std::map<std::string, device::Tensor *> getAllOutputTensorMap();
274 
280  virtual std::vector<device::Tensor *> getAllInputTensorVector();
286  virtual std::vector<device::Tensor *> getAllOutputTensorVector();
287 
294  virtual device::Tensor *getInputTensor(const std::string &name);
301  virtual device::Tensor *getOutputTensor(const std::string &name);
302 
312  virtual base::Status setInputTensor(const std::string &name,
313  device::Tensor *input_tensor);
314 
320  virtual base::Status run() = 0;
321 
329  const std::string &name, base::DeviceType device_type, bool is_copy,
330  base::DataFormat data_format = base::kDataFormatAuto) = 0;
331 
332  protected:
342  std::shared_ptr<InferenceParam> inference_param_;
343 
348  bool is_share_context_ = true;
349 
354  bool is_external_stream_ = false;
355  device::Stream *stream_ = nullptr;
356 
363  std::map<std::string, device::Tensor *> input_tensors_;
370  std::map<std::string, device::Tensor *> output_tensors_;
371 
377  std::map<std::string, device::Tensor *> external_input_tensors_;
378 };
379 
385  public:
386  virtual ~InferenceCreator() {};
387  // virtual Inference *createInference(base::InferenceType type) = 0;
388  virtual std::shared_ptr<Inference> createInference(base::InferenceType type) = 0;
389 };
390 
396 template <typename T>
398  // virtual Inference *createInference(base::InferenceType type) {
399  // return new T(type);
400  // }
401  virtual std::shared_ptr<Inference> createInference(base::InferenceType type) {
402  return std::make_shared<T>(type);
403  }
404 };
405 
411 extern NNDEPLOY_CC_API std::map<base::InferenceType, std::shared_ptr<InferenceCreator>> &
413 
419 template <typename T>
421  public:
423  getGlobalInferenceCreatorMap()[type] = std::shared_ptr<T>(new T());
424  }
425 };
426 
433 // extern NNDEPLOY_CC_API Inference *createInference(base::InferenceType type);
434 
435 extern NNDEPLOY_CC_API std::shared_ptr<Inference> createInference(base::InferenceType type);
436 
437 } // namespace inference
438 } // namespace nndeploy
439 
440 #endif
推理框架的创建类
Definition: inference.h:384
virtual std::shared_ptr< Inference > createInference(base::InferenceType type)=0
virtual std::vector< device::Tensor * > getAllOutputTensorVector()
Get the All Output Tensor Vector object.
virtual std::string getInputName(int i)
Get the Input Name object.
virtual base::Status run()=0
推理
virtual base::Status deinit()=0
反初始化推理
virtual int getNumOfOutputTensor()
Get the Num Of Output Tensor object.
virtual int getNumOfInputTensor()
Get the Num Of Input Tensor object.
virtual base::Status setMemory(device::Buffer *buffer)
设置推理所需的内存(推理内存由外部分配)
virtual bool canOpInput()
是否可以操作推理框架内部分配的输入tensor
base::Param * getParam()
Get the Inference Param(这里使用基类指针)
virtual device::TensorDesc getInputTensorDesc(const std::string &name)
Get the Input Tensor Desc object.
Inference(base::InferenceType type)
device::Stream * getStream()
base::InferenceType type_
推理框架的类型
Definition: inference.h:336
virtual bool isOutputDynamic()
是否为动态输出
virtual base::IntVector getInputShape(const std::string &name)
Get the Input Shape object.
virtual std::string getOutputName(int i)
Get the Output Name object.
base::Status setParam(base::Param *param)
Set the Inference Param(这里使用基类指针)
base::InferenceType getInferenceType()
virtual std::map< std::string, device::Tensor * > getAllOutputTensorMap()
Get the All Output Tensor Map object.
virtual bool isShareStream()
该推理实例是否与nndeploy共享一个stream
virtual device::Tensor * getInputTensor(const std::string &name)
Get the Input Tensor object.
virtual base::Status init()=0
初始化推理
virtual base::Status setInputTensor(const std::string &name, device::Tensor *input_tensor)
Set the Input Tensor object.
virtual bool isShareContext()
该推理实例是否与nndeploy共享一个context
virtual device::TensorDesc getOutputTensorAlignDesc(const std::string &name)
Get the Output Tensor Align Desc object.
virtual base::Status reshape(base::ShapeMap &shape_map)=0
针对动态输入的推理,设置输入tensor的shape
virtual device::Tensor * getOutputTensor(const std::string &name)
Get the Output Tensor object.
std::map< std::string, device::Tensor * > input_tensors_
输入tensor的map
Definition: inference.h:363
virtual bool isInputDynamic()
是否为动态输入
virtual std::vector< device::Tensor * > getAllInputTensorVector()
Get the All Input Tensor Vector object.
virtual device::Tensor * getOutputTensorAfterRun(const std::string &name, base::DeviceType device_type, bool is_copy, base::DataFormat data_format=base::kDataFormatAuto)=0
Get the Output Tensor object.
virtual bool isBatch()
是否为多batch推理
virtual std::vector< std::string > getAllInputTensorName()
Get the All Input Tensor Name object.
virtual device::TensorDesc getInputTensorAlignDesc(const std::string &name)
Get the Input Tensor Align Desc object.
base::Status setParamSharedPtr(std::shared_ptr< base::Param > param)
virtual base::ShapeMap getAllInputShape()
Get the Output Shape object.
virtual float getGFLOPs()
获得推理计算量
base::DeviceType getDeviceType()
Definition: inference.h:64
std::shared_ptr< base::Param > getParamSharedPtr()
base::ShapeMap getOptShape()
针对动态输入的推理,获取输入tensor的opt_shape
base::ShapeMap getMaxShape()
针对动态输入的推理,获取输入tensor的max_shape
void setStream(device::Stream *stream)
virtual std::map< std::string, device::Tensor * > getAllInputTensorMap()
Get the All Input Tensor Map object.
std::map< std::string, device::Tensor * > output_tensors_
输出tensor的map
Definition: inference.h:370
virtual bool canOpOutput()
是否可以操作推理框架内部分配的输出tensor
std::shared_ptr< InferenceParam > inference_param_
推理框架的配置
Definition: inference.h:342
base::ShapeMap getMinShape()
针对动态输入的推理,获取输入tensor的min_shape
std::map< std::string, device::Tensor * > external_input_tensors_
外部输入tensor的map
Definition: inference.h:377
virtual int64_t getMemorySize()
获取推理所需的内存大小
virtual std::vector< std::string > getAllOutputTensorName()
Get the All Output Tensor Name object.
virtual device::TensorDesc getOutputTensorDesc(const std::string &name)
Get the Output Tensor Desc object.
推理框架的创建类模板
Definition: inference.h:397
推理框架的创建类的注册类模板
Definition: inference.h:420
TypeInferenceRegister(base::InferenceType type)
Definition: inference.h:422
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
std::vector< int > IntVector
Definition: common.h:379
std::map< std::string, std::vector< int > > ShapeMap
Definition: common.h:381
@ kDataFormatAuto
Definition: common.h:161
std::shared_ptr< Inference > createInference(base::InferenceType type)
Create a Inference object.
std::map< base::InferenceType, std::shared_ptr< InferenceCreator > > & getGlobalInferenceCreatorMap()
Get the Global Inference Creator Map object.