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_CALIBRATOR_H_ 18 #define MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_CALIBRATOR_H_ 19 #include <string> 20 #include <map> 21 #include <unordered_map> 22 #include <vector> 23 #include <memory> 24 #include "tools/converter/quantizer/quant_params.h" 25 #include "tools/converter/quantizer/quantize_util.h" 26 #include "tools/converter/quantizer/data_distribution.h" 27 28 namespace mindspore::lite::quant { 29 enum CollectType { 30 MIN_MAX, 31 KL_BIN, 32 }; 33 class Calibrator { 34 public: Calibrator(size_t bit_num,int quant_max,int quant_min,ActivationQuantizedMethod activation_quant_method,const preprocess::DataPreProcessParam & data_pre_process_param,bool symmetric)35 Calibrator(size_t bit_num, int quant_max, int quant_min, ActivationQuantizedMethod activation_quant_method, 36 const preprocess::DataPreProcessParam &data_pre_process_param, bool symmetric) 37 : bit_num_(bit_num), 38 quant_max_(quant_max), 39 quant_min_(quant_min), 40 symmetric_(symmetric), 41 activation_quant_method_(activation_quant_method), 42 data_pre_process_param_(data_pre_process_param) {} 43 44 ~Calibrator() = default; 45 46 int GenerateInputData(const std::string &input_name, size_t image_index, mindspore::MSTensor *tensor) const; 47 48 int AddQuantizedOp(const CNodePtr &cnode); 49 50 int RecordMaxMinValue(const std::vector<float> &data, const std::unique_ptr<DataDistribution> &diverg_info); 51 52 int UpdateDivergInterval(); 53 54 int UpdateDataFrequency(const std::vector<float> &data, const std::unique_ptr<DataDistribution> &diverg_info); 55 56 int ComputeThreshold(); 57 GetBatchNum()58 size_t GetBatchNum() const { return data_pre_process_param_.calibrate_size; } 59 GetInputNum()60 size_t GetInputNum() const { return data_pre_process_param_.calibrate_path_vector.size(); } 61 GetInputDivergInfo()62 std::unordered_map<std::string, std::map<int, std::unique_ptr<DataDistribution>>> *GetInputDivergInfo() { 63 return &this->inputs_diverg_info_; 64 } 65 GetOutputDivergInfo()66 std::unordered_map<std::string, std::map<int, std::unique_ptr<DataDistribution>>> *GetOutputDivergInfo() { 67 return &this->outputs_diverg_info_; 68 } 69 70 int CollectDataDistribution( 71 const std::string &node_name, const std::vector<mindspore::MSTensor> &tensors, 72 std::unordered_map<std::string, std::map<int, std::unique_ptr<DataDistribution>>> *diverg_info_map, 73 CollectType collect_type); 74 75 private: 76 // {node_name,{tensor_index,DataDistribution}} 77 std::unordered_map<std::string, std::map<int, std::unique_ptr<DataDistribution>>> inputs_diverg_info_; 78 // {node_name,{tensor_index,DataDistribution}} 79 std::unordered_map<std::string, std::map<int, std::unique_ptr<DataDistribution>>> outputs_diverg_info_; 80 size_t bit_num_; 81 int quant_max_; 82 int quant_min_; 83 bool symmetric_; 84 ActivationQuantizedMethod activation_quant_method_; 85 preprocess::DataPreProcessParam data_pre_process_param_; 86 }; 87 } // namespace mindspore::lite::quant 88 #endif // MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER__CALIBRATOR_H 89