Imported Upstream version 1.18.0
[platform/core/ml/nnfw.git] / compiler / luci / pass / src / CircleOptimizer.test.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
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
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "luci/CircleOptimizer.h"
18
19 #include <gtest/gtest.h>
20
21 using namespace luci;
22 using Algorithms = luci::CircleOptimizer::Options::Algorithm;
23 using AlgorithmParameters = luci::CircleOptimizer::Options::AlgorithmParameters;
24
25 TEST(CircleOptimizerTest, optimize_algorithms)
26 {
27   loco::Graph g;
28   luci::CircleOptimizer o;
29
30   auto options = o.options();
31
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);
50
51   o.optimize(&g);
52
53   SUCCEED();
54 }
55
56 TEST(CircleOptimizerTest, sparsify_simple)
57 {
58   loco::Graph g;
59   luci::CircleOptimizer o;
60
61   auto options = o.options();
62
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");
69
70   o.sparsify(&g);
71
72   SUCCEED();
73 }
74
75 TEST(CircleOptimizerTest, quantize_quantdequant_simple)
76 {
77   loco::Graph g;
78   luci::CircleOptimizer o;
79
80   auto options = o.options();
81
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");
86
87   o.quantize(&g);
88
89   SUCCEED();
90 }
91
92 TEST(CircleOptimizerTest, quantize_quantdequant_input_NEG)
93 {
94   loco::Graph g;
95   luci::CircleOptimizer o;
96
97   auto options = o.options();
98
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");
103
104   EXPECT_THROW(o.quantize(&g), std::runtime_error);
105 }
106
107 TEST(CircleOptimizerTest, quantize_quantdequant_output_NEG)
108 {
109   loco::Graph g;
110   luci::CircleOptimizer o;
111
112   auto options = o.options();
113
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");
118
119   EXPECT_THROW(o.quantize(&g), std::runtime_error);
120 }
121
122 TEST(CircleOptimizerTest, quantize_quantdequant_gran_NEG)
123 {
124   loco::Graph g;
125   luci::CircleOptimizer o;
126
127   auto options = o.options();
128
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");
133
134   EXPECT_THROW(o.quantize(&g), std::runtime_error);
135 }
136
137 TEST(CircleOptimizerTest, quantize_minmax_simple)
138 {
139   loco::Graph g;
140   luci::CircleOptimizer o;
141
142   auto options = o.options();
143
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");
148
149   o.quantize(&g);
150
151   SUCCEED();
152 }
153
154 TEST(CircleOptimizerTest, quantize_minmax_input_NEG)
155 {
156   loco::Graph g;
157   luci::CircleOptimizer o;
158
159   auto options = o.options();
160
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");
165
166   EXPECT_THROW(o.quantize(&g), std::runtime_error);
167 }
168
169 TEST(CircleOptimizerTest, quantize_minmax_output_NEG)
170 {
171   loco::Graph g;
172   luci::CircleOptimizer o;
173
174   auto options = o.options();
175
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");
180
181   EXPECT_THROW(o.quantize(&g), std::runtime_error);
182 }
183
184 TEST(CircleOptimizerTest, quantize_minmax_gran_NEG)
185 {
186   loco::Graph g;
187   luci::CircleOptimizer o;
188
189   auto options = o.options();
190
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");
195
196   EXPECT_THROW(o.quantize(&g), std::runtime_error);
197 }
198
199 TEST(CircleOptimizerTest, quantize_requant_simple)
200 {
201   loco::Graph g;
202   luci::CircleOptimizer o;
203
204   auto options = o.options();
205
206   options->enable(Algorithms::Requantize);
207   options->param(AlgorithmParameters::Quantize_input_model_dtype, "int8");
208   options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
209
210   o.quantize(&g);
211
212   SUCCEED();
213 }
214
215 TEST(CircleOptimizerTest, quantize_requant_input_NEG)
216 {
217   loco::Graph g;
218   luci::CircleOptimizer o;
219
220   auto options = o.options();
221
222   options->enable(Algorithms::Requantize);
223   options->param(AlgorithmParameters::Quantize_input_model_dtype, "invalid");
224   options->param(AlgorithmParameters::Quantize_output_model_dtype, "uint8");
225
226   EXPECT_THROW(o.quantize(&g), std::runtime_error);
227 }
228
229 TEST(CircleOptimizerTest, quantize_requant_output_NEG)
230 {
231   loco::Graph g;
232   luci::CircleOptimizer o;
233
234   auto options = o.options();
235
236   options->enable(Algorithms::Requantize);
237   options->param(AlgorithmParameters::Quantize_input_model_dtype, "int8");
238   options->param(AlgorithmParameters::Quantize_output_model_dtype, "invalid");
239
240   EXPECT_THROW(o.quantize(&g), std::runtime_error);
241 }