ONE - On-device Neural Engine
Loading...
Searching...
No Matches
constant.py
Go to the documentation of this file.
1#!/usr/bin/env python
2
3# Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
4#
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
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
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.
16
17
19 __slots__ = () # This prevents access via __dict__.
20
21 # Basic optimization passes
22 # These passes do not change the execution result of the model
23 O1 = (
24 # Constant folding
25 'fold_add_v2',
26 'fold_cast',
27 'fold_densify',
28 'fold_dequantize',
29 'fold_dwconv',
30 'fold_fully_connected',
31 'fold_gather',
32 'fold_mul',
33 'fold_reshape',
34 'fold_shape',
35 'fold_sparse_to_dense',
36 'fold_squeeze',
37
38 # Operator fusion
39 'fuse_add_to_fullyconnected_bias',
40 'fuse_add_with_conv',
41 'fuse_add_with_tconv',
42 'fuse_add_with_fully_connected',
43 'fuse_batchnorm_with_conv',
44 'fuse_batchnorm_with_dwconv',
45 'fuse_batchnorm_with_tconv',
46 'fuse_activation_function',
47 'fuse_mul_to_fullyconnected_weights',
48 'fuse_instnorm',
49 'fuse_prelu',
50 'fuse_gelu',
51 'fuse_rsqrt',
52 'fuse_mean_with_mean',
53 'fuse_mul_with_conv',
54 'fuse_mul_with_div',
55 'fuse_mul_with_fullyconnected',
56 'fuse_transpose_with_mean',
57 'fuse_slice_with_tconv',
58 'fuse_horizontal_fc_layers',
59 'transform_min_max_to_relu6',
60 'transform_min_relu_to_relu6',
61
62 # Remove redundant operators
63 'remove_redundant_reshape',
64 'remove_redundant_transpose',
65 'remove_unnecessary_add',
66 'remove_unnecessary_cast',
67 'remove_unnecessary_reshape',
68 'remove_unnecessary_slice',
69 'remove_unnecessary_strided_slice',
70 'remove_unnecessary_split',
71 'remove_unnecessary_transpose',
72 'common_subexpression_elimination',
73
74 # Canonicalization
75 # (passes to help further optimization)
76 'resolve_customop_add',
77 'resolve_customop_batchmatmul',
78 'resolve_customop_matmul',
79 'resolve_customop_max_pool_with_argmax',
80 'resolve_customop_splitv',
81 'substitute_expand_dims_to_reshape',
82 'substitute_pack_to_reshape',
83 'substitute_padv2_to_pad',
84 'substitute_splitv_to_split',
85 'substitute_squeeze_to_reshape',
86 'substitute_strided_slice_to_reshape',
87 'substitute_transpose_to_reshape',
88 'forward_reshape_to_unaryop',
89 'forward_transpose_op',
90 'replace_non_const_fc_with_batch_matmul', # For quantization
91 'replace_with_fc_gelu_fc',
92 'transform_sqrt_div_to_rsqrt_mul',
93 )
94
95 OPTIMIZATION_OPTS = (
96 # (OPTION_NAME, HELP_MESSAGE)
97 ('convert_nchw_to_nhwc',
98 'Experimental: This will convert NCHW operators to NHWC under the assumption that input model is NCHW.'
99 ),
100 ('common_subexpression_elimination', 'perform common subexpression elimination'),
101 ('expand_broadcast_const', 'expand broadcastable constant node inputs'),
102 ('nchw_to_nhwc_input_shape',
103 'convert the input shape of the model (argument for convert_nchw_to_nhwc)'),
104 ('nchw_to_nhwc_output_shape',
105 'convert the output shape of the model (argument for convert_nchw_to_nhwc)'),
106 ('fold_add_v2', 'fold AddV2 op with constant inputs'),
107 ('fold_cast', 'fold Cast op with constant input'),
108 ('fold_densify', 'fold Densify op with sparse constant input'),
109 ('fold_dequantize', 'fold Dequantize op'),
110 ('fold_dwconv', 'fold Depthwise Convolution op with constant inputs'),
111 ('fold_fully_connected', 'fold FullyConnected op with constant inputs'),
112 ('fold_gather', 'fold Gather op'),
113 ('fold_mul', 'fold Mul Op'),
114 ('fold_reshape', 'fold Reshape op'),
115 ('fold_shape', 'fold Shape op'),
116 ('fold_sparse_to_dense', 'fold SparseToDense op'),
117 ('fold_squeeze', 'fold Squeeze op'),
118 ('forward_reshape_to_unaryop', 'Forward Reshape op'),
119 ('forward_transpose_op', 'Forward Transpose op'),
120 ('fuse_add_to_fullyconnected_bias',
121 'Fuse Add op to following FullyConnected op bias'),
122 ('fuse_add_with_conv', 'fuse Add op to Convolution op'),
123 ('fuse_add_with_tconv', 'fuse Add op to Transposed'),
124 ('fuse_add_with_fully_connected', 'fuse Add op to FullyConnected op'),
125 ('fuse_batchnorm_with_conv', 'fuse BatchNorm op to Convolution op'),
126 ('fuse_batchnorm_with_dwconv', 'fuse BatchNorm op to Depthwise Convolution op'),
127 ('fuse_batchnorm_with_tconv', 'fuse BatchNorm op to Transposed Convolution op'),
128 ('fuse_mul_to_fullyconnected_weights',
129 'fuse Mul op to following FullyConnected op weights'),
130 ('fuse_slice_with_tconv', 'fuse Slice op to Transposed Convolution op'),
131 ('fuse_bcq', 'apply Binary Coded Quantization'),
132 ('fuse_preactivation_batchnorm',
133 'fuse BatchNorm operators of pre-activations to Convolution op'),
134 ('fuse_mean_with_mean', 'fuse two consecutive Mean ops'),
135 ('fuse_mul_with_conv', 'fuse Mul op to Convolution op'),
136 ('fuse_mul_with_div', 'fuse Mul with Div as Div'),
137 ('fuse_mul_with_fullyconnected', 'fuse Mul op to FullyConnected op'),
138 ('fuse_transpose_with_mean',
139 'fuse Mean with a preceding Transpose under certain conditions'),
140 ('fuse_horizontal_fc_layers',
141 'fuse horizontal FullyConnected layers under certain conditions'),
142 ('make_batchnorm_gamma_positive',
143 'make negative gamma of BatchNorm to a small positive value (1e-10).'
144 ' Note that this pass can change the execution result of the model.'
145 ' So, use it only when the impact is known to be acceptable.'),
146 ('fuse_activation_function', 'fuse Activation function to a preceding operator'),
147 ('fuse_instnorm', 'fuse ops to InstanceNorm operator'),
148 ('fuse_prelu', 'fuse ops to PReLU operator'),
149 ('fuse_gelu', 'fuse ops to GeLU operator'),
150 ('fuse_rmsnorm', 'fuse ops to RmsNorm operator'),
151 ('fuse_rsqrt', 'fuse ops to Rsqrt operator'),
152 ('replace_cw_mul_add_with_depthwise_conv',
153 'replace channel-wise Mul/Add with DepthwiseConv2D'),
154 ('remove_fakequant', 'remove FakeQuant ops'),
155 ('remove_gather_guard',
156 'remove Add/FloorMod guards of Gather indices with certain conditions. '
157 'CAUTION: user must guarantee that indices are all non-negative values.'),
158 ('remove_quantdequant', 'remove Quantize-Dequantize sequence'),
159 ('remove_redundant_quantize', 'remove redundant Quantize ops'),
160 ('remove_redundant_reshape', 'fuse or remove subsequent Reshape ops'),
161 ('remove_redundant_transpose', 'fuse or remove subsequent Transpose ops'),
162 ('remove_unnecessary_add', 'remove unnecessary add ops'),
163 ('remove_unnecessary_cast', 'remove unnecessary cast ops'),
164 ('remove_unnecessary_reshape', 'remove unnecessary reshape ops'),
165 ('remove_unnecessary_slice', 'remove unnecessary slice ops'),
166 ('remove_unnecessary_strided_slice', 'remove unnecessary strided slice ops'),
167 ('remove_unnecessary_split', 'remove unnecessary split ops'),
168 ('remove_unnecessary_transpose', 'remove unnecessary transpose ops'),
169 ('replace_non_const_fc_with_batch_matmul',
170 'replace FullyConnected op with non-const weights to BatchMatMul op'),
171 ('replace_sub_with_add', 'replace Sub op with Add op'),
172 ('replace_with_fc_gelu_fc', 'replace a certain pattern with FC-Gelu-FC ops'),
173 ('resolve_customop_add', 'convert Custom(Add) op to Add op'),
174 ('resolve_customop_batchmatmul',
175 'convert Custom(BatchMatmul) op to BatchMatmul op'),
176 ('resolve_customop_matmul', 'convert Custom(Matmul) op to Matmul op'),
177 ('resolve_customop_max_pool_with_argmax',
178 'convert Custom(MaxPoolWithArgmax) to net of builtin operators'),
179 ('resolve_customop_splitv', 'convert Custom(SplitV) op to SplitV op'),
180 ('shuffle_weight_to_16x1float32',
181 'convert weight format of FullyConnected op to SHUFFLED16x1FLOAT32.'
182 ' Note that it only converts weights whose row is a multiple of 16'),
183 ('substitute_expand_dims_to_reshape',
184 'convert ExpandDims with constant axis to Reshape op'),
185 ('substitute_pack_to_reshape', 'convert single input Pack op to Reshape op'),
186 ('substitute_padv2_to_pad', 'convert certain condition PadV2 to Pad'),
187 ('substitute_splitv_to_split', 'convert certain condition SplitV to Split'),
188 ('substitute_squeeze_to_reshape', 'convert certain condition Squeeze to Reshape'),
189 ('substitute_strided_slice_to_reshape',
190 'convert certain condition StridedSlice to Reshape'),
191 ('substitute_transpose_to_reshape',
192 'convert certain condition Transpose to Reshape'),
193 ('transform_min_max_to_relu6', 'transform Minimum-Maximum pattern to Relu6 op'),
194 ('transform_min_relu_to_relu6', 'transform Minimum(6)-Relu pattern to Relu6 op'),
195 ('transform_sqrt_div_to_rsqrt_mul',
196 'transform Sqrt-Div pattern to Rsqrt-Mul ops'),
197 ('decompose_hardswish', 'decompose the HardSwish op to Add, Mul and Relu6 ops'),
198 ('decompose_softmax',
199 'decompose the Softmax op to Max, Sub, Exp, Sum, Div and optionally Mul ops'),
200 ('unroll_unidirseqlstm', 'unroll UnidirectionalSequenceLSTM op'),
201 ('dynamic_batch_to_single_batch',
202 'convert dynamic batch size (first dimension) of inputs to 1'))
203
204
205CONSTANT = CONSTANT()