Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / compiler / record-minmax / include / MinMaxComputer.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 __RECORD_MINMAX_MINMAXCOMPUTER_H__
18 #define __RECORD_MINMAX_MINMAXCOMPUTER_H__
19
20 #include "MinMaxVectors.h"
21
22 #include <luci/IR/CircleNode.h>
23
24 #include <unordered_map>
25 #include <memory>
26
27 namespace record_minmax
28 {
29
30 class MinMaxComputer
31 {
32 public:
33   MinMaxComputer()
34   {
35     // Do nothing
36   }
37
38   virtual ~MinMaxComputer() = default;
39
40   // Child class must implement this
41   virtual void
42   update_qparam(const std::unordered_map<const luci::CircleNode *, MinMaxVectors> *minmax_map) = 0;
43 };
44
45 class PercentileComputer : public MinMaxComputer
46 {
47 public:
48   PercentileComputer(float min_percentile, float max_percentile)
49     : _min_percentile(min_percentile), _max_percentile(max_percentile)
50   {
51   }
52
53   virtual void
54   update_qparam(const std::unordered_map<const luci::CircleNode *, MinMaxVectors> *minmax_map);
55
56 private:
57   float _min_percentile = 0.0;
58   float _max_percentile = 0.0;
59 };
60
61 class MovingAvgComputer : public MinMaxComputer
62 {
63 public:
64   MovingAvgComputer(uint32_t batch_size, float update_const)
65     : _batch_size(batch_size), _update_const(update_const)
66   {
67   }
68
69   virtual void
70   update_qparam(const std::unordered_map<const luci::CircleNode *, MinMaxVectors> *minmax_map);
71
72 private:
73   uint32_t _batch_size = 0;
74   float _update_const = 0.0;
75 };
76
77 std::unique_ptr<MinMaxComputer> make_percentile_computer(float min_percentile,
78                                                          float max_percentile);
79
80 std::unique_ptr<MinMaxComputer> make_moving_avg_computer(uint32_t batch_size,
81                                                          float moving_avg_const);
82
83 } // namespace record_minmax
84
85 #endif // __RECORD_MINMAX_MINMAXCOMPUTER_H__