Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / circle-mpqsolver / src / core / Quantizer.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 __MPQSOLVER_CORE_QUANTIZER_H__
18 #define __MPQSOLVER_CORE_QUANTIZER_H__
19
20 #include <luci/IR/Module.h>
21 #include <luci/CircleQuantizer.h>
22
23 #include <string>
24 #include <vector>
25 #include <memory>
26
27 namespace mpqsolver
28 {
29 namespace core
30 {
31
32 using LayerParam = luci::CircleQuantizer::Options::LayerParam;
33 using LayerParams = std::vector<std::shared_ptr<LayerParam>>;
34
35 struct QuantizerHook
36 {
37   /**
38    * @brief called on successfull quantization
39    * @param module quantized module
40    */
41   virtual void on_quantized(luci::Module *module) const = 0;
42 };
43
44 class Quantizer
45 {
46 public:
47   Quantizer(const std::string &input_dtype, const std::string &output_type);
48
49   /**
50    * @brief set hook on the end of quantization event
51    */
52   void set_hook(const QuantizerHook *callback);
53
54   /**
55    * @brief quantize recorded module (min/max initialized) with specified parameters
56    * returns true on success
57    */
58   bool quantize(luci::Module *module, const std::string &quant_dtype, LayerParams &layer_params);
59
60   /**
61    * @brief fake_quantize recorded module (min/max initialized) with specified parameters
62    * returns true on success
63    */
64   bool fake_quantize(luci::Module *module, const std::string &quant_dtype,
65                      LayerParams &layer_params);
66
67 private:
68   std::string _input_dtype = "uint8";
69   std::string _output_dtype = "uint8";
70   const QuantizerHook *_hook = nullptr;
71 };
72
73 } // namespace core
74 } // namespace mpqsolver
75
76 #endif //__MPQSOLVER_CORE_QUANTIZER_H__