智能合约具有以下特点:首先,规范性。智能合约以计算机代码为基础,能够最大限度减少语言的模糊性,通过严密的逻辑结构来呈现。智能合约的内容及其执行过程对所有节点均是透明可见的,后者能够通过用户界面去观察、记录、验证合约状态。
其次,不可逆性。一旦满足条件,合约便自动执行预期计划,在给定的事实输入下,智能合约必然输出正确的结果,并在显示视界中被具象化。
def fuse_bias_add(self):
"""
Fuse Pattern like Conv+Add,ConvTranspose+Add,Gemm+Add
This fusion will require a constant input as bias.
"""
graph=self.graph
for op in[_ for _ in graph.operations.values()]:
if op.type in{'Conv','ConvTranspose','Gemm'}:
#check if current op has only 1 downstream op
channel_dimension=1#NCHW,NCHWD,NCH
if op.type=='Gemm':channel_dimension
if len(graph.get_downstream_operations(op))==1:
down=graph.get_downstream_operations(op)[0]
if down.type=='Add':
if down.num_of_parameter!=1:continue
bias=down.parameters[0]
if op.type not in{'Gemm'}:
#check if it is a bias add
if not bias.value.dim()==op.parameters[0].value.dim():continue
if not bias.value.squeeze().dim()==1:continue
if bias.value.shape[channel_dimension]==1:continue
bias.value=bias.value.squeeze()#conv bias can only be 1d
else:
#Gemm bias can be any shape.
#see https://github.com/onnx/onnx/blob/main/docs/Changelog.md#Gemm-11
pass
#ready for fusion
if op.num_of_input==3:#already has a bias
pass
else:
graph.create_variable(is_parameter=True,value=bias.value,dest_ops=[op])
graph.remove_operation(removing_op=down,keep_coherence=True)