[Onnx简化库深度剖析] OnnxSimplifier和OnnxOptimizer解读-(3)

发布时间:2024年01月12日

[Onnx简化库深度剖析] OnnxSimplifier和OnnxOptimizer解读-(3)

简介

补充剩下的所有Pass的特性

具体的Pass实现和细节罗列(补充

PassPassNamePassTypePassEfficiencyPassOptimizationType描述
EliminateNopMonotoneArgmaxeliminate_nop_monotone_argmaxNopPartialCompute消除掉那些正相关的激活函数到argmax函数上,减少计算
EliminateNopPadeliminate_nop_padNopCompleteCompute消除pads=0的Pad算子
EliminateNopConcateliminate_nop_concatNopCompleteMemory消除输入个数为1的Concat算子
EliminateNopSpliteliminate_nop_splitNopCompleteMemory消除输出个数为1、input_dim[axis]=split[0]的Split算子
EliminateNopExpandeliminate_nop_expandNopCompleteCompute消除expand_dim可以广播到input_dim的Expand算子
EliminateShapeGathereliminate_shape_gatherFuseCompleteCompute融合掉indices=[indices_val,]、前面的节点是Shape的Gather算子
EliminateSliceAfterShapeeliminate_slice_after_shapeFuseCompleteCompute融合掉前面的节点是Shape的Slice算子
EliminateNopTransposeeliminate_nop_transposeNopCompleteCompute消除掉不起作用的transpose算子
FuseAddBiasIntoConvfuse_add_bias_into_convFuseCompleteCompute融合掉前面的节点是Conv2d、且input[1]是常量可配合dim的Add算子
FuseBNIntoConvfuse_bn_into_convFuseCompleteCompute融合BN算子到Conv2d上,同时修改Conv2d的权重
FuseConsecutiveConcatsfuse_consecutive_concatsFusePartialCompute融合前面的axis相同的Concat到该Concat节点上
FuseConsecutiveLogSoftmaxfuse_consecutive_log_softmaxFuseCompleteCompute融合softmax+log成为LogSoftmax算子
FuseConsecutiveReduceUnsqueezefuse_consecutive_reduce_unsqueezeFuseCompleteCompute当前面的Reduce算子的axes=Unsqueeze_axes、keepdims=0时,融合Unsqueeze算子到Reduce算子上
FuseConsecutiveSqueezesfuse_consecutive_squeezesFuseCompleteCompute合并多个连续的Squeeze算子成为一个Squeeze算子
FuseConsecutiveTransposesfuse_consecutive_transposesFuseCompleteCompute合并多个连续的Transpose算子成为一个Transpose算子
FuseMatMulAddBiasIntoGemmfuse_matmul_add_bias_into_gemmFuseCompleteCompute合并MatMul+Add成为一个Gemm算子
FusePadIntoConvfuse_pad_into_convFuseCompleteCompute合并Pad+Conv成为一个Conv算子,Pad操作合并到了Conv上
FusePadIntoPoolfuse_pad_into_poolFuseCompleteCompute合并Pad+AveragePool/MaxPool成为一个Pool算子
FuseTransposeIntoGemmfuse_transpose_into_gemmFuseCompleteCompute融合前面的Transpose操作反转Gemm的transA/transB参数,从而融合掉transpose算子
ReplaceEinsumWithMatmulreplace_einsum_with_matmulReplaceCompleteCompute满足条件的einsum变成matmul操作:"bhij,bhjd->bhid"变成matmul; "bhid,bhjd->bhij"变成transpose+matmul操作
LiftLexicalReferenceslift_lexical_referencesSeparateCompleteMemory待说明
SplitInitsplit_initSeparateCompleteMemory待说明
SplitPredictsplit_predictSeparateCompleteMemory待说明
FuseConcatIntoReshapefuse_concat_into_reshapeFuseCompleteCompute融合reshape的shape输入的concat/cast算子,变成constant shape输入
EliminateNopReshapeeliminate_nop_reshapeNopCompleteCompute消除掉reshape dim == input_dim的reshape算子
EliminateOpWithUniteliminate_nop_with_unitNopCompleteCompute消除掉同0并的And、同1乘的Mul、同0或的Or、同0加的Add、减0的Sub、除1的Div、方1的Pow、 无效的Concat
EliminateCommonSubexpressioneliminate_common_subexpressionNopCompleteCompute消除掉那些属性/输入一致的节点,也就是公共子表达式
FuseQKVfuse_qkvFuseCompleteCompute合并qkv计算的三个matmul为只有一个matmul: A = matmul(X, Q), B = matmul(X, K), C = matmul(X, V) ==> A,B,C = split(matmul(X, concat(Q,K,V)))
FuseConsecutiveUnsqueezesfuse_consecutive_unsqueezesFuseCompleteCompute融合连续的Unsqueezes算子
EliminateDeadEndeliminate_deadendNopCompleteCompute移除掉output没有连接到其他节点的node
EliminateIdentityeliminate_identityNopCompleteCompute移除掉Identity node
EliminateShapeOpeliminate_shape_opFuseCompleteCompute融合掉可以直接获取input_shape的Shape算子
FuseConsecutiveSlicesfuse_consecutive_slicesFuseCompleteMemory融合掉axes没有交集的连续的slice算子,合并为一个Slice
EliminateUnusedInitializereliminate_unused_initializerNopCompleteMemory移除掉不被使用的initializer
EliminateDuplicateInitializereliminate_duplicate_initializerNopCompleteMemory移除掉重复的initializer
AdjustSliceAndMatmuladjust_slice_and_matmulReplaceCompleteCompute调整slice和matmul之间的顺序,以便优化:Y = Matmul(Slice(data, start, end, axes) ,rhs) ==> Y = Slice(Matmul(data, rhs), start, end, axes)
RewriteInputDtyperewrite_input_dtypeOtherCompleteNone重写input的类型从int64变成int32,从A=node1(INT64_INPUT) ==> A=node1(Cast(INT32_INPUT, INT64))

总结

基本上目前所有的Pass已经被罗列出来了,后续会用实际的效果去加深大家的印象
文章来源:https://blog.csdn.net/Pengcode/article/details/135555311
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。