• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
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 MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_DATA_DISTRIBUTION_H_
18 #define MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_DATA_DISTRIBUTION_H_
19 
20 #include <vector>
21 #include <utility>
22 #include <limits>
23 #include "tools/converter/quantizer/quant_params.h"
24 #include "tools/converter/quantizer/quantize_util.h"
25 #include "src/common/quant_utils.h"
26 
27 namespace mindspore::lite::quant {
28 constexpr float kEps = 1e-8;
29 
30 class DataDistribution {
31  public:
32   DataDistribution() = default;
33 
DataDistribution(CNodePtr cnode,int bins,size_t bits,int quant_max,int quant_min,ActivationQuantizedMethod activation_quant_method,bool symmetric)34   DataDistribution(CNodePtr cnode, int bins, size_t bits, int quant_max, int quant_min,
35                    ActivationQuantizedMethod activation_quant_method, bool symmetric) {
36     this->activation_quant_method_ = activation_quant_method;
37     this->cnode_ = std::move(cnode);
38     this->bin_num_ = bins;
39     this->bit_num_ = bits;
40     histogram_.resize(bin_num_);
41     real_max_ = -FLT_MAX;
42     real_min_ = FLT_MAX;
43     this->quant_max_ = quant_max;
44     this->quant_min_ = quant_min;
45     std::fill(histogram_.begin(), histogram_.end(), 1.0e-7);
46     if (this->activation_quant_method_ == KL) {
47       symmetric_ = true;
48     } else {
49       symmetric_ = symmetric;
50     }
51   }
52 
53   int RecordMaxMinValueArray(const std::vector<float> &data);
54 
55   void UpdateInterval();
56 
57   int UpdateHistogram(const std::vector<float> &data);
58 
59   void DumpHistogram();
60 
61   void HandleBinForKL(int quant_bint_nums, int bin_index, std::vector<float> *quantized_histogram,
62                       std::vector<float> *expanded_histogram);
63 
64   int ComputeThreshold();
65 
66   double GetScale();
67 
68   int32_t GetZeroPoint();
69 
GetRealMax()70   float GetRealMax() { return this->real_max_; }
71 
GetRealMin()72   float GetRealMin() { return this->real_min_; }
73 
GetEncodeMin()74   float GetEncodeMin() { return this->encode_min_; }
75 
GetEncodeMax()76   float GetEncodeMax() { return this->encode_max_; }
77 
GetCNode()78   CNodePtr GetCNode() { return this->cnode_; }
79 
80  private:
81   double CalculateMinMaxScale();
82 
83   double CalculateRemovalOutlierScale();
84 
85   double CalculateKLScale();
86 
87   double CalculateScale(float min_value, float max_value);
88 
89   std::pair<float, float> CalQuantileMinMax(const std::vector<float> &min_datas, const std::vector<float> &max_datas);
90 
91  private:
92   std::vector<float> histogram_;
93   CNodePtr cnode_;
94   int bin_num_ = 0;
95   float interval_ = 0;
96   float real_max_ = -FLT_MAX;
97   float real_min_ = FLT_MAX;
98   float best_T_ = 0.0f;
99   size_t bit_num_ = 0;
100   float encode_min_ = 0.0f;
101   float encode_max_ = 0.0f;
102   int quant_max_ = 255;
103   int quant_min_ = 0;
104   ActivationQuantizedMethod activation_quant_method_ = MAX_MIN;
105   std::vector<float> min_datas_;
106   std::vector<float> max_datas_;
107   std::pair<float, float> percent_result_{0.0, 0.0};
108   double scale_ = 0;
109   int zero_point_ = 0;
110   bool symmetric_ = true;
111 };
112 }  // namespace mindspore::lite::quant
113 #endif  // MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_DATA_DISTRIBUTION_H_
114