Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / luci / pass / include / luci / CircleQuantizer.h
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 #ifndef __LUCI_CIRCLE_QUANTIZER_H__
18 #define __LUCI_CIRCLE_QUANTIZER_H__
19
20 #include <loco.h>
21
22 #include <string>
23 #include <vector>
24
25 namespace luci
26 {
27
28 class CircleQuantizer final
29 {
30 public:
31   struct Options
32   {
33     struct LayerParam
34     {
35       std::string name;
36       std::string dtype;
37       std::string granularity;
38     };
39
40     enum Algorithm
41     {
42       QuantizeDequantizeWeights,
43       QuantizeWithMinMax,
44       Requantize,
45       CopyQuantParam,
46       ForceQuantParam,
47       ConvertToFakeQuantizedModel,
48       QuantizeWeights,
49     };
50
51     enum AlgorithmParameters
52     {
53       // quantize
54       Quantize_input_model_dtype,
55       Quantize_output_model_dtype,
56       Quantize_granularity, // layer-wise or channel-wise
57       Quantize_tensor_names,
58       Quantize_scales,
59       Quantize_zero_points,
60       Quantize_layer_params,
61
62       // copy_quantparam
63       Quantize_src_tensor_names,
64       Quantize_dst_tensor_names,
65
66       Quantize_input_type,
67       Quantize_output_type,
68       Quantize_TF_style_maxpool,
69     };
70
71     virtual ~Options() = default;
72
73     virtual void enable(Algorithm) = 0;
74     virtual bool query(Algorithm) = 0;
75     virtual void param(AlgorithmParameters, const std::string &) = 0;
76     virtual const std::string param(AlgorithmParameters) const = 0;
77     virtual void params(AlgorithmParameters, std::vector<std::string> &) = 0;
78     virtual std::vector<std::string> params(AlgorithmParameters) const = 0;
79
80     // Quantization parameters for multiple layers
81     virtual void layer_params(AlgorithmParameters, std::vector<std::shared_ptr<LayerParam>> &) = 0;
82     virtual std::vector<std::shared_ptr<LayerParam>> layer_params(AlgorithmParameters) const = 0;
83   };
84
85 public:
86   // TODO maybe caller can provide Options as ctor parameters
87   Options *options(void);
88
89 public:
90   void quantize(loco::Graph *) const;
91
92 private:
93   std::unique_ptr<Options> _options;
94 };
95
96 } // namespace luci
97
98 #endif // __LUCI_CIRCLE_QUANTIZER_H__