nndeploy C++ API  0.2.0
nndeploy C++ API
net.h
Go to the documentation of this file.
1 
2 #ifndef _NNDEPLOY_NET_NET_H_
3 #define _NNDEPLOY_NET_NET_H_
4 
6 #include "nndeploy/ir/ir.h"
8 #include "nndeploy/net/runtime.h"
9 #include "nndeploy/net/util.h"
10 #include "nndeploy/op/op.h"
11 
12 namespace nndeploy {
13 namespace net {
14 
15 class NNDEPLOY_CC_API Net : public op::Op {
16  public:
17  Net();
18  virtual ~Net();
19 
20  // 在这个函数之前调用setDeviceType
23  base::Status setDynamicShape(bool is_dynamic_shape, base::ShapeMap &min_shape,
24  base::ShapeMap &opt_shape,
25  base::ShapeMap &max_shape);
27  base::Status setTensorPoolMemory(bool is_external);
28 
29  TensorWrapper *createTensor(const std::string &name, bool is_weight = false);
30  TensorWrapper *addTensor(device::Tensor *tensor, bool is_external = true,
31  bool is_weight = false);
32  device::Tensor *getTensor(const std::string &name);
33 
34  bool isWeight(const std::string &name);
35  // 有转移所有权属性
36  device::Tensor *getWeight(const std::string &weight);
37 
38  op::Op *createOp(base::DeviceType device_type, const std::string &name,
39  ir::OpType op_type,
40  std::initializer_list<std::string> inputs,
41  std::initializer_list<std::string> outputs);
42  op::Op *createOp(base::DeviceType device_type, const std::string &name,
43  ir::OpType op_type, std::vector<std::string> &inputs,
44  std::vector<std::string> &outputs);
45  base::Status addNet(Net *net, bool is_external);
46 
47  base::Status setOpParam(const std::string &op_name,
48  std::shared_ptr<base::Param> param);
49  std::shared_ptr<base::Param> getOpParam(const std::string &op_name);
50 
51  virtual base::Status init();
52  virtual base::Status deinit();
53 
59  virtual int64_t getMemorySize();
67 
71 
73  virtual base::Status reshape(base::ShapeMap &shape_map);
74 
75  virtual base::Status preRun();
76  virtual uint64_t getFlops();
77 
78  virtual base::Status run();
79  virtual base::Status postRun();
80 
81  base::Status dump(std::ostream &oss);
82 
87  base::Status enableOpt(bool flag);
88 
92  base::Status setEnablePass(std::set<OptPassType>);
93 
97  base::Status setDisablePass(std::set<OptPassType>);
98 
105  base::Status setWorkers(int worker_num,
106  std::vector<base::DeviceType> device_types =
107  std::vector<base::DeviceType>());
108 
116 
126  device::Tensor *getOutputTensorAfterRun(const std::string &name,
127  base::DeviceType device_type,
128  bool is_copy,
129  base::DataFormat data_format);
130 
131  protected:
133  // NNDEPLOY_LOGI("1. Optimizer Graph V1!\n");
135  // NNDEPLOY_LOGI("##############\n");
136  // NNDEPLOY_LOGI("runtime init\n");
137  // NNDEPLOY_LOGI("#. Optimizer Graph V2!\n");
138  // NNDEPLOY_LOGI("#. Memory Allocation Phase!\n");
139  // NNDEPLOY_LOGI("#. Cost Calculations!\n");
140  // NNDEPLOY_LOGI("##############\n");
142 
143  protected:
145 
146  std::vector<TensorWrapper *> tensor_repository_;
147  std::vector<OpWrapper *> op_repository_;
148 
149  bool is_dynamic_shape_ = false; // 是否是动态shape
150  base::ShapeMap min_shape_ = base::ShapeMap(); // 当为动态输入时最小shape
151  base::ShapeMap opt_shape_ = base::ShapeMap(); // 当为动态输入时最优shape
152  base::ShapeMap max_shape_ = base::ShapeMap(); // 当为动态输入时最大shape
153  TensorPoolType tensor_pool_type_ =
155  bool tensor_pool_memory_is_external_ = false;
156  Runtime *runtime_ = nullptr;
157 
158  bool net_opt_flag_ = true; // 默认开启图优化
159  std::set<OptPassType> enable_pass_; // 仅使用这些pass,如果为空则启用全部pass
160  // 禁用这些pass,如果为空则启用全部pass;
161  // 如果同时设置了enable_pass_,则只有enable_pass_生效
162  std::set<OptPassType> disable_pass_;
163 
164  // 推理并行数量
165  int worker_num_ = 1;
166  // 推理设备类型
167  std::vector<base::DeviceType> device_types_;
168 };
169 
170 Net *createNet(ir::ModelDesc *model_desc, base::DeviceType device_type,
171  base::PrecisionType precision_type);
172 
173 } // namespace net
174 } // namespace nndeploy
175 
176 #endif
参照onnx的格式,描述模型的结构
Definition: ir.h:109
virtual base::Status inferShape()
形状推理
TensorWrapper * createTensor(const std::string &name, bool is_weight=false)
base::Status copyToInputTensor(device::Tensor *tensor)
将输入tensor复制到输入tensor
std::set< OptPassType > disable_pass_
Definition: net.h:162
virtual int64_t getMemorySize()
获取推理所需的内存大小
std::vector< base::DeviceType > device_types_
Definition: net.h:167
device::Tensor * getWeight(const std::string &weight)
bool isWeight(const std::string &name)
op::Op * createOp(base::DeviceType device_type, const std::string &name, ir::OpType op_type, std::initializer_list< std::string > inputs, std::initializer_list< std::string > outputs)
base::Status setWorkers(int worker_num, std::vector< base::DeviceType > device_types=std::vector< base::DeviceType >())
设置推理并行数量
virtual base::Status reshape(base::ShapeMap &shape_map)
重新推理形状,通常在初始化之后、preRun前调用
virtual uint64_t getFlops()
得到op的flops
device::Tensor * getTensor(const std::string &name)
virtual base::Status inferDataFormat()
数据格式推理
base::Status setTensorPoolType(TensorPoolType tensor_pool_type)
op::Op * createOp(base::DeviceType device_type, const std::string &name, ir::OpType op_type, std::vector< std::string > &inputs, std::vector< std::string > &outputs)
base::Status setDynamicShape(bool is_dynamic_shape, base::ShapeMap &min_shape, base::ShapeMap &opt_shape, base::ShapeMap &max_shape)
ir::ModelDesc * model_desc_
Definition: net.h:144
virtual base::Status preRun()
base::Status setInterpret(ir::Interpret *interpret)
base::Status setModelDesc(ir::ModelDesc *model_desc)
std::shared_ptr< base::Param > getOpParam(const std::string &op_name)
TensorWrapper * addTensor(device::Tensor *tensor, bool is_external=true, bool is_weight=false)
virtual base::Status runtime()
base::Status addNet(Net *net, bool is_external)
base::Status setOpParam(const std::string &op_name, std::shared_ptr< base::Param > param)
virtual base::Status deinit()
virtual base::Status init()
初始化
virtual base::Status postRun()
base::Status setEnablePass(std::set< OptPassType >)
在图优化时仅启用这些pass,如果为空则启用全部pass
std::vector< OpWrapper * > op_repository_
Definition: net.h:147
base::Status optimizer()
base::Status setTensorPoolMemory(bool is_external)
std::vector< TensorWrapper * > tensor_repository_
Definition: net.h:146
virtual base::Status setMemory(device::Buffer *buffer)
设置推理所需的内存(推理内存由外部分配)
virtual base::Status construct()
base::Status setDisablePass(std::set< OptPassType >)
在图优化时禁用这些pass,如果为空则启用全部pass
virtual base::Status run()
std::set< OptPassType > enable_pass_
Definition: net.h:159
virtual base::Status inferDataType()
类型推理
base::Status enableOpt(bool flag)
设置开启图优化的开关 flag: true 启用图优化 false:关闭图优化
device::Tensor * getOutputTensorAfterRun(const std::string &name, base::DeviceType device_type, bool is_copy, base::DataFormat data_format)
获取推理后的输出tensor
base::Status dump(std::ostream &oss)
Op的基类
Definition: op.h:42
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
std::map< std::string, std::vector< int > > ShapeMap
Definition: common.h:381
OpType
算子类型 算子分类
Definition: op_param.h:65
@ kTensorPool1DSharedObjectTypeGreedyBySizeImprove
Definition: tensor_pool.h:31
Net * createNet(ir::ModelDesc *model_desc, base::DeviceType device_type, base::PrecisionType precision_type)