3 # Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 __slots__ = () # This prevents access via __dict__.
21 # Basic optimization passes
22 # These passes do not change the execution result of the model
30 'fold_fully_connected',
32 'fold_sparse_to_dense',
35 'fuse_add_with_tconv',
36 'fuse_add_with_fully_connected',
37 'fuse_batchnorm_with_conv',
38 'fuse_batchnorm_with_dwconv',
39 'fuse_batchnorm_with_tconv',
40 'fuse_activation_function',
44 'fuse_mean_with_mean',
45 'fuse_transpose_with_mean',
46 'transform_min_max_to_relu6',
47 'transform_min_relu_to_relu6',
49 # Remove redundant operators
50 'remove_redundant_reshape',
51 'remove_redundant_transpose',
52 'remove_unnecessary_reshape',
53 'remove_unnecessary_slice',
54 'remove_unnecessary_strided_slice',
55 'remove_unnecessary_split',
58 # (passes to help further optimization)
59 'resolve_customop_add',
60 'resolve_customop_batchmatmul',
61 'resolve_customop_matmul',
62 'resolve_customop_max_pool_with_argmax',
63 'resolve_customop_splitv',
64 'substitute_pack_to_reshape',
65 'substitute_padv2_to_pad',
66 'substitute_splitv_to_split',
67 'substitute_squeeze_to_reshape',
68 'substitute_strided_slice_to_reshape',
69 'substitute_transpose_to_reshape',
70 'forward_reshape_to_unaryop',
71 'forward_transpose_op',
72 'replace_non_const_fc_with_batch_matmul', # For quantization
76 # (OPTION_NAME, HELP_MESSAGE)
77 ('convert_nchw_to_nhwc',
78 'Experimental: This will convert NCHW operators to NHWC under the assumption that input model is NCHW.'
80 ('expand_broadcast_const', 'expand broadcastable constant node inputs'),
81 ('nchw_to_nhwc_input_shape',
82 'convert the input shape of the model (argument for convert_nchw_to_nhwc)'),
83 ('nchw_to_nhwc_output_shape',
84 'convert the output shape of the model (argument for convert_nchw_to_nhwc)'),
85 ('fold_add_v2', 'fold AddV2 op with constant inputs'),
86 ('fold_cast', 'fold Cast op with constant input'),
87 ('fold_densify', 'fold Densify op with sparse constant input'),
88 ('fold_dequantize', 'fold Dequantize op'),
89 ('fold_dwconv', 'fold Depthwise Convolution op with constant inputs'),
90 ('fold_fully_connected', 'fold FullyConnected op with constant inputs'),
91 ('fold_gather', 'fold Gather op'),
92 ('fold_sparse_to_dense', 'fold SparseToDense op'),
93 ('forward_reshape_to_unaryop', 'Forward Reshape op'),
94 ('forward_transpose_op', 'Forward Transpose op'),
95 ('fuse_add_with_tconv', 'fuse Add op to Transposed'),
96 ('fuse_add_with_fully_connected', 'fuse Add op to FullyConnected op'),
97 ('fuse_batchnorm_with_conv', 'fuse BatchNorm op to Convolution op'),
98 ('fuse_batchnorm_with_dwconv', 'fuse BatchNorm op to Depthwise Convolution op'),
99 ('fuse_batchnorm_with_tconv', 'fuse BatchNorm op to Transposed Convolution op'),
100 ('fuse_bcq', 'apply Binary Coded Quantization'),
101 ('fuse_preactivation_batchnorm',
102 'fuse BatchNorm operators of pre-activations to Convolution op'),
103 ('fuse_mean_with_mean', 'fuse two consecutive Mean ops'),
104 ('fuse_transpose_with_mean',
105 'fuse Mean with a preceding Transpose under certain conditions'),
106 ('make_batchnorm_gamma_positive',
107 'make negative gamma of BatchNorm to a small positive value (1e-10).'
108 ' Note that this pass can change the execution result of the model.'
109 ' So, use it only when the impact is known to be acceptable.'),
110 ('fuse_activation_function', 'fuse Activation function to a preceding operator'),
111 ('fuse_instnorm', 'fuse ops to InstanceNorm operator'),
112 ('fuse_prelu', 'fuse ops to PReLU operator'),
113 ('fuse_gelu', 'fuse ops to GeLU operator'),
114 ('replace_cw_mul_add_with_depthwise_conv',
115 'replace channel-wise Mul/Add with DepthwiseConv2D'),
116 ('remove_fakequant', 'remove FakeQuant ops'),
117 ('remove_quantdequant', 'remove Quantize-Dequantize sequence'),
118 ('remove_redundant_quantize', 'remove redundant Quantize ops'),
119 ('remove_redundant_reshape', 'fuse or remove subsequent Reshape ops'),
120 ('remove_redundant_transpose', 'fuse or remove subsequent Transpose ops'),
121 ('remove_unnecessary_reshape', 'remove unnecessary reshape ops'),
122 ('remove_unnecessary_slice', 'remove unnecessary slice ops'),
123 ('remove_unnecessary_strided_slice', 'remove unnecessary strided slice ops'),
124 ('remove_unnecessary_split', 'remove unnecessary split ops'),
125 ('replace_non_const_fc_with_batch_matmul',
126 'replace FullyConnected op with non-const weights to BatchMatMul op'),
127 ('replace_sub_with_add', 'replace Sub op with Add op'),
128 ('resolve_customop_add', 'convert Custom(Add) op to Add op'),
129 ('resolve_customop_batchmatmul',
130 'convert Custom(BatchMatmul) op to BatchMatmul op'),
131 ('resolve_customop_matmul', 'convert Custom(Matmul) op to Matmul op'),
132 ('resolve_customop_max_pool_with_argmax',
133 'convert Custom(MaxPoolWithArgmax) to net of builtin operators'),
134 ('resolve_customop_splitv', 'convert Custom(SplitV) op to SplitV op'),
135 ('shuffle_weight_to_16x1float32',
136 'convert weight format of FullyConnected op to SHUFFLED16x1FLOAT32.'
137 ' Note that it only converts weights whose row is a multiple of 16'),
138 ('substitute_pack_to_reshape', 'convert single input Pack op to Reshape op'),
139 ('substitute_padv2_to_pad', 'convert certain condition PadV2 to Pad'),
140 ('substitute_splitv_to_split', 'convert certain condition SplitV to Split'),
141 ('substitute_squeeze_to_reshape', 'convert certain condition Squeeze to Reshape'),
142 ('substitute_strided_slice_to_reshape',
143 'convert certain condition StridedSlice to Reshape'),
144 ('substitute_transpose_to_reshape',
145 'convert certain condition Transpose to Reshape'),
146 ('transform_min_max_to_relu6', 'transform Minimum-Maximum pattern to Relu6 op'),
147 ('transform_min_relu_to_relu6', 'transform Minimum(6)-Relu pattern to Relu6 op'),
148 ('decompose_hardswish', 'decompose the HardSwish op to Add, Mul and Relu6 ops'),
149 ('unroll_unidirseqlstm', 'unroll UnidirectionalSequenceLSTM op'),
150 ('dynamic_batch_to_single_batch',
151 'convert dynamic batch size (first dimension) of inputs to 1'))
154 CONSTANT = CONSTANT()