4e7074d981d144bf9491427fcb145231ea5a04bf
[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     };
49
50     enum AlgorithmParameters
51     {
52       // quantize
53       Quantize_input_model_dtype,
54       Quantize_output_model_dtype,
55       Quantize_granularity, // layer-wise or channel-wise
56       Quantize_tensor_names,
57       Quantize_scales,
58       Quantize_zero_points,
59       Quantize_layer_params,
60
61       // copy_quantparam
62       Quantize_src_tensor_names,
63       Quantize_dst_tensor_names,
64
65       Quantize_input_type,
66       Quantize_output_type,
67       Quantize_TF_style_maxpool,
68     };
69
70     virtual ~Options() = default;
71
72     virtual void enable(Algorithm) = 0;
73     virtual bool query(Algorithm) = 0;
74     virtual void param(AlgorithmParameters, const std::string &) = 0;
75     virtual const std::string param(AlgorithmParameters) const = 0;
76     virtual void params(AlgorithmParameters, std::vector<std::string> &) = 0;
77     virtual std::vector<std::string> params(AlgorithmParameters) const = 0;
78
79     // Quantization parameters for multiple layers
80     virtual void layer_params(AlgorithmParameters, std::vector<std::shared_ptr<LayerParam>> &) = 0;
81     virtual std::vector<std::shared_ptr<LayerParam>> layer_params(AlgorithmParameters) const = 0;
82   };
83
84 public:
85   // TODO maybe caller can provide Options as ctor parameters
86   Options *options(void);
87
88 public:
89   void quantize(loco::Graph *) const;
90
91 private:
92   std::unique_ptr<Options> _options;
93 };
94
95 } // namespace luci
96
97 #endif // __LUCI_CIRCLE_QUANTIZER_H__