6fe1d560b2a811ce967c7688a4e7c1ef2ef112da
[platform/core/ml/nnfw.git] / compiler / circle-mpqsolver / src / bisection / Quantizer.cpp
1 /*
2  * Copyright (c) 2022 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 "Quantizer.h"
18 #include <luci/Service/Validate.h>
19
20 #include <iostream>
21
22 using namespace mpqsolver::bisection;
23 using AlgorithmParameters = luci::CircleQuantizer::Options::AlgorithmParameters;
24 using Algorithms = luci::CircleQuantizer::Options::Algorithm;
25
26 namespace
27 {
28
29 bool make_model_fake_quantized(luci::Module *module)
30 {
31   luci::CircleQuantizer quantizer;
32
33   auto options = quantizer.options();
34   options->enable(Algorithms::ConvertToFakeQuantizedModel);
35
36   for (size_t idx = 0; idx < module->size(); ++idx)
37   {
38     auto graph = module->graph(idx);
39     // quantize the graph
40     quantizer.quantize(graph);
41     if (!luci::validate(graph))
42     {
43       return false;
44     }
45   }
46
47   return true;
48 }
49
50 } // namespace
51
52 Quantizer::Quantizer(const std::string &input_dtype, const std::string &output_dtype)
53   : _input_dtype(input_dtype), _output_dtype(output_dtype)
54 {
55 }
56
57 /**
58  * @brief quantize recorded module (min/max initialized) with specified parameters
59  * returns true on success
60  */
61 bool Quantizer::quantize(luci::Module *module, const std::string &quant_dtype,
62                          LayerParams &layer_params)
63 {
64   if (!module)
65     return false;
66
67   static const std::string default_dtype = "float32";
68   static const std::string granularity_type = "channel";
69
70   luci::CircleQuantizer quantizer;
71
72   auto options = quantizer.options();
73   options->enable(Algorithms::QuantizeWithMinMax);
74
75   options->param(AlgorithmParameters::Quantize_input_model_dtype, default_dtype);
76   options->param(AlgorithmParameters::Quantize_output_model_dtype, quant_dtype);
77   options->param(AlgorithmParameters::Quantize_granularity, granularity_type);
78   options->param(AlgorithmParameters::Quantize_input_type, _input_dtype);
79   options->param(AlgorithmParameters::Quantize_output_type, _output_dtype);
80   options->param(AlgorithmParameters::Quantize_TF_style_maxpool, "False");
81
82   if (!layer_params.empty())
83   {
84     try
85     {
86       options->layer_params(AlgorithmParameters::Quantize_layer_params, layer_params);
87     }
88     catch (const std::runtime_error &e)
89     {
90       std::cerr << e.what() << '\n';
91       return false;
92     }
93   }
94
95   for (size_t idx = 0; idx < module->size(); ++idx)
96   {
97     auto graph = module->graph(idx);
98     // quantize the graph
99     quantizer.quantize(graph);
100     if (!luci::validate(graph))
101     {
102       std::cerr << "ERROR: Quantized graph is invalid" << std::endl;
103       return false;
104     }
105   }
106
107   return true;
108 }
109
110 /**
111  * @brief fake_quantize recorded module (min/max initialized) with specified parameters
112  * returns true on success
113  */
114 bool Quantizer::fake_quantize(luci::Module *module, const std::string &quant_dtype,
115                               LayerParams &layer_params)
116 {
117   if (!quantize(module, quant_dtype, layer_params))
118     return false;
119
120   if (!make_model_fake_quantized(module))
121     return false;
122
123   return true;
124 }