Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / circle-mpqsolver / src / core / Dumper.h
1 /*
2  * Copyright (c) 2023 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_DUMPER_H__
18 #define __MPQSOLVER_DUMPER_H__
19
20 #include <luci/IR/Module.h>
21 #include <luci/CircleQuantizer.h>
22
23 #include <string>
24
25 namespace mpqsolver
26 {
27 namespace core
28 {
29
30 using LayerParam = luci::CircleQuantizer::Options::LayerParam;
31 using LayerParams = std::vector<std::shared_ptr<LayerParam>>;
32
33 class Dumper final
34 {
35 public:
36   Dumper() = default;
37   Dumper(const std::string &dir_path);
38
39   /**
40    * @brief sets model path for further usage
41    */
42   void set_model_path(const std::string &model_path);
43
44   /**
45    * @brief dumps mpq configuration
46    * @param layers specific quantization parameters
47    * @param def_dtype default quantization data type
48    * @param step id of mpq configuration
49    */
50   void dump_MPQ_configuration(const LayerParams &layers, const std::string &def_dtype,
51                               int step) const;
52
53   /**
54    * @brief dumps final mpq configuration
55    * @param layers specific quantization parameters
56    * @param def_dtype default quantization data type
57    */
58   void dump_final_MPQ(const LayerParams &layers, const std::string &def_dtype) const;
59
60   /**
61    * @brief dumps quantized module
62    * @param layers specific quantization parameters
63    * @param step id of quantized module
64    */
65   void dump_quantized(luci::Module *module, uint32_t step) const;
66
67   /**
68    * @brief create file for error dumping
69    */
70   void prepare_for_error_dumping() const;
71
72   /**
73    * @brief append error of Q8 quantization
74    */
75   void dump_Q8_error(float error) const;
76
77   /**
78    * @brief append error of Q16 quantization
79    */
80   void dump_Q16_error(float error) const;
81
82   /**
83    * @brief append error of mpq quantization
84    * @param error error of quantization
85    * @param step id of error
86    */
87   void dump_MPQ_error(float error, uint32_t step) const;
88
89   /**
90    * @brief dump final error
91    * @param error final error of quantization
92    */
93   void dump_MPQ_error(float error) const;
94
95 private:
96   void write_data_to_file(const std::string &path, const std::string &data) const;
97   void dump_MPQ_configuration(const LayerParams &layers, const std::string &def_dtype,
98                               const std::string &path) const;
99   void prepare_directory(const std::string &dir_path) const;
100   void save_circle(luci::Module *module, std::string &path) const;
101   void dump_error(float error, const std::string &tag, const std::string &path) const;
102   std::string get_error_path() const { return _dir_path + "/errors" + ".mpq.txt"; }
103
104 private:
105   std::string _dir_path;
106   std::string _model_path;
107
108 }; // Dumper
109
110 } // namespace core
111 } // namespace mpqsolver
112
113 #endif //__MPQSOLVER_DUMPER_H__