2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "luci/CircleOptimizer.h"
19 #include <gtest/gtest.h>
22 using Algorithms = luci::CircleOptimizer::Options::Algorithm;
23 using AlgorithmParameters = luci::CircleOptimizer::Options::AlgorithmParameters;
25 TEST(CircleOptimizerTest, optimize_algorithms)
28 luci::CircleOptimizer o;
30 auto options = o.options();
32 // NOTE these are added to cover the test
33 // TODO add more if needed
34 options->enable(Algorithms::FoldAddV2);
35 options->enable(Algorithms::FoldCast);
36 options->enable(Algorithms::FoldDepthwiseConv2D);
37 options->enable(Algorithms::FoldDequantize);
38 options->enable(Algorithms::FoldSparseToDense);
39 options->enable(Algorithms::FusePreActivationBatchNorm);
40 options->enable(Algorithms::MakeBatchNormGammaPositive);
41 options->enable(Algorithms::ShuffleWeightTo16x1Float32);
42 options->enable(Algorithms::RemoveUnnecessaryReshape);
43 options->enable(Algorithms::RemoveUnnecessarySlice);
44 options->enable(Algorithms::RemoveUnnecessarySplit);
45 options->enable(Algorithms::ReplaceMulAddWithDepthwiseConv);
46 options->enable(Algorithms::SubstituteStridedSliceToReshape);
47 options->enable(Algorithms::SubstituteTransposeToReshape);
48 options->enable(Algorithms::ConvertNCHWToNHWC);
49 options->enable(Algorithms::ExpandBroadcastConst);
56 TEST(CircleOptimizerTest, sparsify_simple)
59 luci::CircleOptimizer o;
61 auto options = o.options();
63 options->enable(Algorithms::SparsifyTensorPass);
64 options->param(AlgorithmParameters::Sparsify_tensor_name, "dummy");
65 options->param(AlgorithmParameters::Sparsify_traversal_order, "dummy");
66 options->param(AlgorithmParameters::Sparsify_format, "ds");
67 options->param(AlgorithmParameters::Sparsify_block_size, "1,1");
68 options->param(AlgorithmParameters::Sparsify_block_map, "1,1");
75 TEST(CircleOptimizerTest, quantize_quantdequant_simple)
78 luci::CircleOptimizer o;
80 auto options = o.options();
82 options->enable(Algorithms::QuantizeDequantizeWeights);
83 options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
84 options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
85 options->param(AlgorithmParameters::Quantize_granularity, "layer");
92 TEST(CircleOptimizerTest, quantize_quantdequant_input_NEG)
95 luci::CircleOptimizer o;
97 auto options = o.options();
99 options->enable(Algorithms::QuantizeDequantizeWeights);
100 options->param(AlgorithmParameters::Quantize_input_model_dtype, "invalid");
101 options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
102 options->param(AlgorithmParameters::Quantize_granularity, "layer");
104 EXPECT_THROW(o.quantize(&g), std::runtime_error);
107 TEST(CircleOptimizerTest, quantize_quantdequant_output_NEG)
110 luci::CircleOptimizer o;
112 auto options = o.options();
114 options->enable(Algorithms::QuantizeDequantizeWeights);
115 options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
116 options->param(AlgorithmParameters::Quantize_output_model_dtype, "invalid");
117 options->param(AlgorithmParameters::Quantize_granularity, "layer");
119 EXPECT_THROW(o.quantize(&g), std::runtime_error);
122 TEST(CircleOptimizerTest, quantize_quantdequant_gran_NEG)
125 luci::CircleOptimizer o;
127 auto options = o.options();
129 options->enable(Algorithms::QuantizeDequantizeWeights);
130 options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
131 options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
132 options->param(AlgorithmParameters::Quantize_granularity, "invalid");
134 EXPECT_THROW(o.quantize(&g), std::runtime_error);
137 TEST(CircleOptimizerTest, quantize_minmax_simple)
140 luci::CircleOptimizer o;
142 auto options = o.options();
144 options->enable(Algorithms::QuantizeWithMinMax);
145 options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
146 options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
147 options->param(AlgorithmParameters::Quantize_granularity, "layer");
154 TEST(CircleOptimizerTest, quantize_minmax_input_NEG)
157 luci::CircleOptimizer o;
159 auto options = o.options();
161 options->enable(Algorithms::QuantizeWithMinMax);
162 options->param(AlgorithmParameters::Quantize_input_model_dtype, "invalid");
163 options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
164 options->param(AlgorithmParameters::Quantize_granularity, "layer");
166 EXPECT_THROW(o.quantize(&g), std::runtime_error);
169 TEST(CircleOptimizerTest, quantize_minmax_output_NEG)
172 luci::CircleOptimizer o;
174 auto options = o.options();
176 options->enable(Algorithms::QuantizeWithMinMax);
177 options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
178 options->param(AlgorithmParameters::Quantize_output_model_dtype, "invalid");
179 options->param(AlgorithmParameters::Quantize_granularity, "layer");
181 EXPECT_THROW(o.quantize(&g), std::runtime_error);
184 TEST(CircleOptimizerTest, quantize_minmax_gran_NEG)
187 luci::CircleOptimizer o;
189 auto options = o.options();
191 options->enable(Algorithms::QuantizeWithMinMax);
192 options->param(AlgorithmParameters::Quantize_input_model_dtype, "float32");
193 options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
194 options->param(AlgorithmParameters::Quantize_granularity, "invalid");
196 EXPECT_THROW(o.quantize(&g), std::runtime_error);
199 TEST(CircleOptimizerTest, quantize_requant_simple)
202 luci::CircleOptimizer o;
204 auto options = o.options();
206 options->enable(Algorithms::Requantize);
207 options->param(AlgorithmParameters::Quantize_input_model_dtype, "int8");
208 options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
215 TEST(CircleOptimizerTest, quantize_requant_input_NEG)
218 luci::CircleOptimizer o;
220 auto options = o.options();
222 options->enable(Algorithms::Requantize);
223 options->param(AlgorithmParameters::Quantize_input_model_dtype, "invalid");
224 options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
226 EXPECT_THROW(o.quantize(&g), std::runtime_error);
229 TEST(CircleOptimizerTest, quantize_requant_output_NEG)
232 luci::CircleOptimizer o;
234 auto options = o.options();
236 options->enable(Algorithms::Requantize);
237 options->param(AlgorithmParameters::Quantize_input_model_dtype, "int8");
238 options->param(AlgorithmParameters::Quantize_output_model_dtype, "invalid");
240 EXPECT_THROW(o.quantize(&g), std::runtime_error);