With the continuous progress of information technology and communication technology,we have entered the era of intelligent industry.In this era,the application of various intelligent technologies is driving the upgrading and transformation of industry,and new generation information technologies such as artificial intelligence technology,5G technology,and industrial internet technology are continuously promoting the progress and development of the era.
直接调用onnx库load接口,已经把onnx计算图解析了,然后用解析后的原始计算图初始化『量化计算图』BaseGraph
graph=parser.build(file_path)
这个parser方法命名可能是借鉴是onnx库的parse吧:
一共有三种parse:OnnxParser、CaffeParser、NativeImporter,先看看OnnxParser是如何实现的。
这应该是以一个字典key-value格式存储的,这方面具体的细节以后再研究研究~
之前说过在量化的全流程中,所有信息都是由BaseGraph这个类保存和管理的。先用graph_pb.name新建一个量化图:
graph=BaseGraph(name=graph_pb.name,built_from=NetworkFramework.ONNX)
接下来就是向这个新建的graph里面填充我们想要的东西,首先要设置每个opset的属性,这个属性里包括domain和version,注意这个设置是op粒度的。
graph._detail[GRAPH_OPSET_ATTRIB]=self.convert_opsets_to_str(opsets)
graph._detail['ir_version']=model_pb.ir_version
onnx_import_opset=DEFAULT_OPSET_VERSION
for opset in graph._detail[GRAPH_OPSET_ATTRIB]:
if opset['domain']==DEFAULT_OPSET_DOMAIN or opset['domain']=='':
onnx_import_opset=opset['version']
break
然后遍历每个节点,然后进行初始化参数。所谓初始化参数initialize_params()就是把对应节点的is_parameter设置为TRUE,表示这个节点是有参数的:
def initialize_params(self,graph:BaseGraph,initializer:Dict[str,Any])->BaseGraph:
for var in graph.variables.values():
if var.name in initializer:
for dest_op in var.dest_ops:
assert isinstance(dest_op,Operation)
dest_op.parameters.append(var)
var.value=initializer[var.name]
var.is_parameter=True
return graph
之前说过这里主要是载入并初始化量化计算图,还不涉及具体计算图优化。但是会做一步移除无需量化的节点:
self.de_inplace(graph)