7ec2e69443f21c39127fa9e51e6465574a3945a3
[platform/core/ml/nnfw.git] / compiler / circle-mpqsolver / src / bisection / BisectionSolver.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_BISECTION_SOLVER_H__
18 #define __MPQSOLVER_BISECTION_SOLVER_H__
19
20 #include "Quantizer.h"
21 #include "Evaluator.h"
22 #include <MPQSolver.h>
23
24 #include <luci/IR/Module.h>
25
26 #include <memory>
27 #include <string>
28
29 namespace mpqsolver
30 {
31 namespace bisection
32 {
33
34 class BisectionSolver final : public MPQSolver
35 {
36 public:
37   /**
38    * @brief Algorithm options for running bisection algorithm
39    */
40   enum Algorithm
41   {
42     Auto,
43     ForceQ16Front,
44     ForceQ16Back,
45   };
46
47 public:
48   /**
49    * @brief construct Solver using input_data_path for .h5 file,
50    * qerror_ratio to set target qerror, and input_quantization/output_quantization to set
51    * quantization type at input/output respectively
52    */
53   BisectionSolver(const std::string &input_data_path, float qerror_ratio,
54                   const std::string &input_quantization, const std::string &output_quantization);
55   BisectionSolver() = delete;
56
57   /**
58    * @brief run bisection for recorded float module at module_path
59    */
60   std::unique_ptr<luci::Module> run(const std::string &module_path) override;
61
62   /**
63    * @brief set used algorithm
64    */
65   void algorithm(Algorithm algorithm);
66
67 private:
68   float evaluate(const DatasetEvaluator &evaluator, const std::string &module_path,
69                  const std::string &def_quant, LayerParams &layers);
70
71 private:
72   float _qerror = 0.f; // quantization error
73   Algorithm _algorithm = Algorithm::ForceQ16Front;
74   std::unique_ptr<Quantizer> _quantizer;
75 };
76
77 } // namespace bisection
78 } // namespace mpqsolver
79
80 #endif //__MPQSOLVER_BISECTION_SOLVER_H__