nndeploy C++ API  0.2.0
nndeploy C++ API
ir.h
Go to the documentation of this file.
1 
2 #ifndef _NNDEPLOY_IR_IR_H_
3 #define _NNDEPLOY_IR_IR_H_
4 
5 #include <memory>
6 
7 #include "nndeploy/base/any.h"
8 #include "nndeploy/base/common.h"
10 #include "nndeploy/base/log.h"
11 #include "nndeploy/base/macro.h"
12 #include "nndeploy/base/object.h"
13 #include "nndeploy/base/param.h"
14 #include "nndeploy/base/status.h"
15 #include "nndeploy/base/string.h"
16 #include "nndeploy/device/tensor.h"
17 #include "nndeploy/ir/op_param.h"
18 
19 #ifdef ENABLE_NNDEPLOY_SAFETENSORS_CPP
20 #include "safetensors.hh"
21 #endif
22 
23 namespace nndeploy {
24 namespace ir {
25 
35  public:
36  OpDesc();
37  OpDesc(OpType op_type);
38  OpDesc(const std::string &name, OpType op_type);
39  OpDesc(const std::string &name, OpType op_type,
40  std::shared_ptr<base::Param> op_param);
41  OpDesc(const std::string &name, OpType op_type,
42  std::initializer_list<std::string> inputs,
43  std::initializer_list<std::string> outputs);
44  OpDesc(const std::string &name, OpType op_type,
45  std::initializer_list<std::string> inputs,
46  std::initializer_list<std::string> outputs,
47  std::shared_ptr<base::Param> op_param);
48  OpDesc(const std::string &name, OpType op_type,
49  std::vector<std::string> &inputs, std::vector<std::string> &outputs);
50  OpDesc(const std::string &name, OpType op_type,
51  std::vector<std::string> &inputs, std::vector<std::string> &outputs,
52  std::shared_ptr<base::Param> op_param);
53 
54  virtual ~OpDesc();
55 
56  // 序列化
57  base::Status serialize(rapidjson::Value &json,
58  rapidjson::Document::AllocatorType &allocator);
59  // 反序列化
60  base::Status deserialize(rapidjson::Value &json);
61 
62  public:
63  // 算子名称
64  std::string name_;
65  // 节点类型
67  // 节点输入 : 包含 input、weight等所有参与计算的数据
68  // [conv.input, conv.weight] -> conv -> [conv.output] -> relu -> [relu.output]
69  std::vector<std::string> inputs_;
70  // 节点输出
71  std::vector<std::string> outputs_;
72  // 算子参数
73  std::shared_ptr<base::Param> op_param_;
74 };
75 
81  public:
83  ValueDesc(const std::string &name);
84  ValueDesc(const std::string &name, base::DataType data_type);
85  ValueDesc(const std::string &name, base::DataType data_type,
87 
88  virtual ~ValueDesc();
89 
90  // 序列化
91  base::Status serialize(rapidjson::Value &json,
92  rapidjson::Document::AllocatorType &allocator);
93  // 反序列化
94  base::Status deserialize(rapidjson::Value &json);
95 
96  public:
97  // 名称
98  std::string name_;
99  // 数据类型
101  // 张量形状
103 };
104 
110  public:
112  virtual ~ModelDesc();
113 
114  base::Status dump(std::ostream &stream);
115 
116  // 序列化模型结构为文本
118  rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator);
120  base::Status serializeStructureToJson(const std::string &path);
121  // 反序列化文本为模型结构
123  rapidjson::Value &json, const std::vector<ValueDesc> &input);
125  const std::string &json_str, const std::vector<ValueDesc> &input);
127  const std::string &path, const std::vector<ValueDesc> &input);
128 
129 #ifdef ENABLE_NNDEPLOY_SAFETENSORS_CPP
130  // 序列化模型权重为safetensors
131  base::Status serializeWeightsToSafetensorsImpl(safetensors::safetensors_t &st,
132  bool serialize_buffer = false);
133  base::Status serializeWeightsToSafetensors(
134  std::shared_ptr<safetensors::safetensors_t> &serialize_st_ptr);
135  base::Status serializeWeightsToSafetensors(
136  const std::string &weight_file_path);
137  // 从safetensors中导入成模型文件
138  base::Status deserializeWeightsFromSafetensors(
139  std::shared_ptr<safetensors::safetensors_t> &st_ptr);
140 #endif
141 
142  public:
143  // 描述模型的名称
144  std::string name_;
145  // 模型元数据
146  std::unordered_map<std::string, std::string> metadata_;
147  // 模型输入
148  std::vector<std::shared_ptr<ValueDesc>> inputs_;
149  // 模型输出
150  std::vector<std::shared_ptr<ValueDesc>> outputs_;
151  // 模型算子列表
152  std::vector<std::shared_ptr<OpDesc>> op_descs_;
153  // 模型权重
154  std::map<std::string, device::Tensor *> weights_;
155  // 模型中间值,一般通常为空,多用于调试
156  std::vector<std::shared_ptr<ValueDesc>> values_;
157 };
158 
159 } // namespace ir
160 } // namespace nndeploy
161 
162 #endif /* _NNDEPLOY_IR_IR_H_ */
参照onnx的格式,描述模型的结构
Definition: ir.h:109
base::Status deserializeStructureFromJsonStr(const std::string &json_str, const std::vector< ValueDesc > &input)
std::vector< std::shared_ptr< ValueDesc > > values_
Definition: ir.h:156
base::Status serializeStructureToJson(const std::string &path)
std::unordered_map< std::string, std::string > metadata_
Definition: ir.h:146
base::Status serializeStructureToJson(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator)
base::Status dump(std::ostream &stream)
base::Status deserializeStructureFromJson(const std::string &path, const std::vector< ValueDesc > &input)
base::Status serializeStructureToJsonStr(std::string &json_str)
std::map< std::string, device::Tensor * > weights_
Definition: ir.h:154
std::vector< std::shared_ptr< ValueDesc > > inputs_
Definition: ir.h:148
base::Status deserializeStructureFromJson(rapidjson::Value &json, const std::vector< ValueDesc > &input)
std::vector< std::shared_ptr< ValueDesc > > outputs_
Definition: ir.h:150
std::vector< std::shared_ptr< OpDesc > > op_descs_
Definition: ir.h:152
std::string name_
Definition: ir.h:144
参照并扩充了onnx的格式,描述算子的基本信息
Definition: ir.h:34
std::vector< std::string > outputs_
Definition: ir.h:71
OpType op_type_
Definition: ir.h:66
std::vector< std::string > inputs_
Definition: ir.h:69
std::shared_ptr< base::Param > op_param_
Definition: ir.h:73
base::Status deserialize(rapidjson::Value &json)
OpDesc(const std::string &name, OpType op_type, std::shared_ptr< base::Param > op_param)
OpDesc(const std::string &name, OpType op_type, std::initializer_list< std::string > inputs, std::initializer_list< std::string > outputs, std::shared_ptr< base::Param > op_param)
OpDesc(OpType op_type)
base::Status serialize(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator)
OpDesc(const std::string &name, OpType op_type, std::initializer_list< std::string > inputs, std::initializer_list< std::string > outputs)
OpDesc(const std::string &name, OpType op_type, std::vector< std::string > &inputs, std::vector< std::string > &outputs)
std::string name_
Definition: ir.h:64
OpDesc(const std::string &name, OpType op_type)
OpDesc(const std::string &name, OpType op_type, std::vector< std::string > &inputs, std::vector< std::string > &outputs, std::shared_ptr< base::Param > op_param)
参照onnx的格式,描述模型或者算子输入输出
Definition: ir.h:80
std::string name_
Definition: ir.h:98
ValueDesc(const std::string &name)
ValueDesc(const std::string &name, base::DataType data_type, base::IntVector shape)
base::IntVector shape_
Definition: ir.h:102
ValueDesc(const std::string &name, base::DataType data_type)
base::DataType data_type_
Definition: ir.h:100
base::Status deserialize(rapidjson::Value &json)
base::Status serialize(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator)
#define NNDEPLOY_CC_API
api
Definition: macro.h:29
std::vector< int > IntVector
Definition: common.h:379
OpType
算子类型 算子分类
Definition: op_param.h:65
base::Status shape(device::Tensor *input, std::shared_ptr< ir::ShapeParam > param, device::Tensor *output)