nndeploy C++ API  0.2.0
nndeploy C++ API
inference_param.h
Go to the documentation of this file.
1 
2 #ifndef _NNDEPLOY_INFERENCE_INFERENCE_PARAM_H_
3 #define _NNDEPLOY_INFERENCE_INFERENCE_PARAM_H_
4 
5 #include "nndeploy/base/any.h"
6 #include "nndeploy/base/common.h"
7 #include "nndeploy/base/file.h"
9 #include "nndeploy/base/log.h"
10 #include "nndeploy/base/macro.h"
11 #include "nndeploy/base/object.h"
12 #include "nndeploy/base/param.h"
13 #include "nndeploy/base/status.h"
14 #include "nndeploy/base/string.h"
15 
16 namespace nndeploy {
17 namespace inference {
18 
23  public:
26  virtual ~InferenceParam();
27 
28  InferenceParam(const InferenceParam& param) = default;
29  InferenceParam& operator=(const InferenceParam& param) = default;
30 
33 
34 
41  virtual base::Status set(const std::string& key, base::Any& any);
49  virtual base::Status get(const std::string& key, base::Any& any);
50 
51  base::InferenceType getInferenceType() const;
52  void setInferenceType(base::InferenceType type);
53  base::ModelType getModelType() const;
54  void setModelType(base::ModelType type);
55  bool getIsPath() const;
56  void setIsPath(bool is_path);
57  const std::vector<std::string>& getModelValue() const;
58  void setModelValue(const std::vector<std::string>& model_value);
59  void setModelValue(const std::string& model_value, int i = -1);
60  int getInputNum() const;
61  void setInputNum(int input_num);
62  const std::vector<std::string>& getInputName() const;
63  void setInputName(const std::vector<std::string>& input_name);
64  void setInputName(const std::string& input_name, int i = -1);
65  const std::vector<std::vector<int>>& getInputShape() const;
66  void setInputShape(const std::vector<std::vector<int>>& input_shape);
67  void setInputShape(const std::vector<int>& input_shape, int i = -1);
68  int getOutputNum() const;
69  void setOutputNum(int output_num);
70  const std::vector<std::string>& getOutputName() const;
71  void setOutputName(const std::vector<std::string>& output_name);
72  void setOutputName(const std::string& output_name, int i = -1);
73  base::EncryptType getEncryptType() const;
74  void setEncryptType(base::EncryptType type);
75  const std::string& getLicense() const;
76  void setLicense(const std::string& license);
77  base::DeviceType getDeviceType() const;
78  void setDeviceType(base::DeviceType type);
79  int getNumThread() const;
80  void setNumThread(int num_thread);
81  int getGpuTuneKernel() const;
82  void setGpuTuneKernel(int gpu_tune_kernel);
83  base::ShareMemoryType getShareMemoryMode() const;
84  void setShareMemoryMode(base::ShareMemoryType mode);
86  void setPrecisionType(base::PrecisionType type);
87  base::PowerType getPowerType() const;
88  void setPowerType(base::PowerType type);
89  bool getIsDynamicShape() const;
90  void setIsDynamicShape(bool is_dynamic_shape);
91  const base::ShapeMap& getMinShape() const;
92  void setMinShape(const base::ShapeMap& min_shape);
93  const base::ShapeMap& getOptShape() const;
94  void setOptShape(const base::ShapeMap& opt_shape);
95  const base::ShapeMap& getMaxShape() const;
96  void setMaxShape(const base::ShapeMap& max_shape);
97  const std::vector<std::string>& getCachePath() const;
98  void setCachePath(const std::vector<std::string>& cache_path);
99  const std::vector<std::string>& getLibraryPath() const;
100  void setLibraryPath(const std::vector<std::string>& library_path);
101  void setLibraryPath(const std::string& library_path, int i = -1);
102 
103  base::InferenceType inference_type_ = base::kInferenceTypeNotSupport;
104  base::ModelType model_type_ = base::kModelTypeOnnx; // 模型的类型
105  bool is_path_ = true; // model_value_是否为路径
106  std::vector<std::string> model_value_; // 模型的路径或者内容
107  std::vector<std::string> external_model_data_; // 分离模型权重
108  int input_num_ = 1; // 输入的数量
109  std::vector<std::string> input_name_; // 输入的名称
110  std::vector<std::vector<int>> input_shape_; // 输入的形状
111  int output_num_ = 1; // 输出的数量
112  std::vector<std::string> output_name_; // 输出的名称
113  base::EncryptType encrypt_type_ =
114  base::kEncryptTypeNone; // 模型文件的加解密类型
115  std::string license_; // 模型文件的加解密密钥
116  base::DeviceType device_type_; // 模型推理的设备类型
117  int num_thread_ = 4; // CPU推理的线程数
118  int gpu_tune_kernel_ = 1; // GPU微调的模式
119  base::ShareMemoryType share_memory_mode_ =
120  base::kShareMemoryTypeNoShare; // 推理时的共享内存模式
121  base::PrecisionType precision_type_ =
122  base::kPrecisionTypeFp32; // 推理时的精度类型
123  base::PowerType power_type_ = base::kPowerTypeNormal; // 推理时的功耗类型
124  bool is_dynamic_shape_ = false; // 是否是动态shape
125  base::ShapeMap min_shape_ = base::ShapeMap(); // 当为动态输入时最小shape
126  base::ShapeMap opt_shape_ = base::ShapeMap(); // 当为动态输入时最优shape
127  base::ShapeMap max_shape_ = base::ShapeMap(); // 当为动态输入时最大shape
128  std::vector<std::string> cache_path_ = {""}; // 缓存路径
129  std::vector<std::string> library_path_ = {""}; // 第三方推理框架的动态库路径
131  int worker_num_ = 1;
132 
133  virtual base::Status serialize(rapidjson::Value& json,
134  rapidjson::Document::AllocatorType& allocator);
135  virtual base::Status deserialize(rapidjson::Value& json);
136 };
137 
144  public:
146  // virtual InferenceParam *createInferenceParam(base::InferenceType type) = 0;
147  virtual std::shared_ptr<InferenceParam> createInferenceParam(
148  base::InferenceType type) = 0;
149 };
150 
156 template <typename T>
158  // virtual InferenceParam *createInferenceParam(base::InferenceType type) {
159  // return new T(type);
160  // }
161  virtual std::shared_ptr<InferenceParam> createInferenceParam(
162  base::InferenceType type) {
163  return std::make_shared<T>(type);
164  }
165 };
166 
173 extern NNDEPLOY_CC_API
174  std::map<base::InferenceType, std::shared_ptr<InferenceParamCreator>>&
176 
182 template <typename T>
184  public:
186  getGlobalInferenceParamCreatorMap()[type] = std::shared_ptr<T>(new T());
187  }
188 };
189 
196 // extern NNDEPLOY_CC_API InferenceParam *createInferenceParam(
197 // base::InferenceType type);
198 
199 extern NNDEPLOY_CC_API std::shared_ptr<InferenceParam> createInferenceParam(
200  base::InferenceType type);
201 
202 } // namespace inference
203 } // namespace nndeploy
204 
205 #endif
InferenceParamCreator is the base class of all inference param creator.
virtual std::shared_ptr< InferenceParam > createInferenceParam(base::InferenceType type)=0
InferenceParam is the base class of all inference param.
InferenceParam & operator=(const InferenceParam &param)=default
InferenceParam(base::InferenceType type)
virtual base::Status deserialize(rapidjson::Value &json)
InferenceParam(const InferenceParam &param)=default
virtual base::Status serialize(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator)
TypeInferenceParamCreator is the template class of all inference param.
TypeInferenceParamRegister is the template class of all inference.
TypeInferenceParamRegister(base::InferenceType type)
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
@ kInferenceTypeNotSupport
Definition: common.h:329
@ kPrecisionTypeFp32
Definition: common.h:170
std::map< std::string, std::vector< int > > ShapeMap
Definition: common.h:381
@ kModelTypeOnnx
Definition: common.h:246
@ kPowerTypeNormal
Definition: common.h:179
PrecisionType getPrecisionType(DataType data_type)
@ kParallelTypeSequential
Definition: common.h:355
@ kEncryptTypeNone
Definition: common.h:333
@ kShareMemoryTypeNoShare
Definition: common.h:187
std::map< base::InferenceType, std::shared_ptr< InferenceParamCreator > > & getGlobalInferenceParamCreatorMap()
Get the Global Inference Param Creator Map object.
std::shared_ptr< InferenceParam > createInferenceParam(base::InferenceType type)
Create a Inference Param object.
#define PARAM_COPY_TO(param_type)
Definition: param.h:25
#define PARAM_COPY(param_type)
Definition: param.h:16