From eb56ead55807f4a63f645f13738edbffb59a6dee Mon Sep 17 00:00:00 2001 From: albert-yan Date: Tue, 21 Jun 2022 13:42:31 +0800 Subject: [PATCH] fix codex --- include/api/types.h | 5 ++- .../tools/converter/quantizer/cle_pattern.cc | 38 ++++++++----------- .../tools/converter/quantizer/cle_pattern.h | 1 + .../tools/converter/quantizer/cle_strategy.cc | 2 - .../converter/quantizer/data_distribution.cc | 13 ++++--- .../converter/quantizer/data_distribution.h | 4 ++ .../converter/quantizer/debug_info_manager.cc | 4 +- .../converter/quantizer/debug_info_manager.h | 4 +- .../converter/quantizer/fse_bit_stream.cc | 2 +- .../quantizer/full_quant_quantizer.cc | 2 +- .../quantizer/full_quant_quantizer.h | 1 - .../quantizer/insert_quant_node_manager.cc | 2 +- .../quantizer/insert_quant_node_manager.h | 2 +- .../quantizer/mixed_bit_weight_quantizer.h | 1 - .../conv_quant_type_determiner.cc | 1 - .../converter/quantizer/quantize_util.cc | 2 +- .../tools/converter/quantizer/quantize_util.h | 8 ++-- .../tools/converter/quantizer/quantizer.h | 2 - 18 files changed, 43 insertions(+), 51 deletions(-) diff --git a/include/api/types.h b/include/api/types.h index 6cf04523e4c..e0fa0c1d7ce 100644 --- a/include/api/types.h +++ b/include/api/types.h @@ -368,8 +368,9 @@ struct MSCallBackParam { }; /// \brief KernelCallBack defined the function pointer for callBack. -using MSKernelCallBack = std::function &inputs, const std::vector &outputs, - const MSCallBackParam &opInfo)>; +using MSKernelCallBack = + std::function & /* inputs */, const std::vector & /* outputs */, + const MSCallBackParam &opInfo)>; std::vector CharVersion(); inline std::string Version() { return CharToString(CharVersion()); } diff --git a/mindspore/lite/tools/converter/quantizer/cle_pattern.cc b/mindspore/lite/tools/converter/quantizer/cle_pattern.cc index 493aca4a59f..6e365ba9fa6 100644 --- a/mindspore/lite/tools/converter/quantizer/cle_pattern.cc +++ b/mindspore/lite/tools/converter/quantizer/cle_pattern.cc @@ -41,7 +41,7 @@ bool IsLinearActivation(const api::SharedPtr &conv2d) { return false; } -bool IsCommonConvNode(const BaseRef &n) { +bool IsConvNode(const BaseRef &n, ConvNode node) { if (utils::isa(n)) { auto anf_node = utils::cast(n); if (!opt::CheckPrimitiveType(anf_node, prim::kPrimConv2DFusion)) { @@ -57,32 +57,24 @@ bool IsCommonConvNode(const BaseRef &n) { if (conv == nullptr) { return false; } - return conv->get_group() == 1; + + if (node == COMMON_CONV) { + return conv->get_group() == 1; + } else if (node == DEPTHWISE_CONV) { + bool ret = IsLinearActivation(conv) && conv->GetAttr(ops::kIsDepthWise) != nullptr && + GetValue(conv->GetAttr(ops::kIsDepthWise)); + return ret; + } else { + MS_LOG(ERROR) << "Not supported conv node type."; + return false; + } } return false; } -bool IsDepthWiseConvNode(const BaseRef &n) { - if (utils::isa(n)) { - auto anf_node = utils::cast(n); - if (!opt::CheckPrimitiveType(anf_node, prim::kPrimConv2DFusion)) { - return false; - } - api::SharedPtr conv = nullptr; - if (utils::isa(anf_node)) { - auto c_node = anf_node->cast(); - conv = ops::GetOperator(c_node->input(0)); - } else if (utils::isa(anf_node)) { - conv = ops::GetOperator(anf_node); - } - if (conv == nullptr || !IsLinearActivation(conv)) { - return false; - } - auto ret = conv->GetAttr(ops::kIsDepthWise) != nullptr && GetValue(conv->GetAttr(ops::kIsDepthWise)); - return ret; - } - return false; -} +bool IsCommonConvNode(const BaseRef &n) { return IsConvNode(n, COMMON_CONV); } + +bool IsDepthWiseConvNode(const BaseRef &n) { return IsConvNode(n, DEPTHWISE_CONV); } VectorRef CLEPattern::DefineConvWithConvPattern() const { auto is_conv1 = std::make_shared(IsCommonConvNode); diff --git a/mindspore/lite/tools/converter/quantizer/cle_pattern.h b/mindspore/lite/tools/converter/quantizer/cle_pattern.h index 29f26d27edd..5d31e0fdb12 100644 --- a/mindspore/lite/tools/converter/quantizer/cle_pattern.h +++ b/mindspore/lite/tools/converter/quantizer/cle_pattern.h @@ -31,6 +31,7 @@ struct CombinationLayer { }; constexpr size_t kInputsNum2 = 2; constexpr size_t kInputsNum3 = 3; +enum ConvNode { COMMON_CONV, DEPTHWISE_CONV }; class CLEPattern : public opt::MultiplePatternProcessPass { public: explicit CLEPattern(const std::string &name = "CLEPattern", bool multigraph = true) diff --git a/mindspore/lite/tools/converter/quantizer/cle_strategy.cc b/mindspore/lite/tools/converter/quantizer/cle_strategy.cc index 7d67225ab74..2f574431aa5 100644 --- a/mindspore/lite/tools/converter/quantizer/cle_strategy.cc +++ b/mindspore/lite/tools/converter/quantizer/cle_strategy.cc @@ -33,8 +33,6 @@ namespace mindspore::lite::quant { using lite::RET_ERROR; using lite::RET_OK; -static const std::set kSupportCLENode = { - schema::EnumNamePrimitiveType(schema::PrimitiveType_Conv2DFusion)}; static const float kDefaultScale = 1; int CLEStrategy::Run() { MS_LOG(INFO) << "CLE start to find pattern."; diff --git a/mindspore/lite/tools/converter/quantizer/data_distribution.cc b/mindspore/lite/tools/converter/quantizer/data_distribution.cc index c035e1dcc76..6d7ed53a8a7 100644 --- a/mindspore/lite/tools/converter/quantizer/data_distribution.cc +++ b/mindspore/lite/tools/converter/quantizer/data_distribution.cc @@ -112,7 +112,7 @@ void DataDistribution::HandleBinForKL(int quant_bint_nums, int bin_index, std::v float left_scale = 0.0f; if (left_upper > start) { left_scale = left_upper - start; - if (this->histogram_[left_upper - 1] != 0) { + if (!IsZero(this->histogram_[left_upper - 1])) { count += left_scale; } } @@ -120,12 +120,13 @@ void DataDistribution::HandleBinForKL(int quant_bint_nums, int bin_index, std::v double right_scale = 0.0f; if (right_lower < end) { right_scale = end - right_lower; - if (this->histogram_[right_lower] != 0) { + if (!IsZero(this->histogram_[right_lower])) { count += right_scale; } } std::for_each(this->histogram_.begin() + left_upper, this->histogram_.begin() + right_lower, [&count](float item) { - if (item != 0) { + bool is_zero = (item <= kEps && item >= -kEps); + if (!is_zero) { count += 1; } }); @@ -133,14 +134,14 @@ void DataDistribution::HandleBinForKL(int quant_bint_nums, int bin_index, std::v continue; } const float average_num = quantized_histogram->at(i) / count; - if (left_upper > start && this->histogram_[left_upper - 1] != 0) { + if (left_upper > start && !IsZero(this->histogram_[left_upper - 1])) { expanded_histogram->at(left_upper - 1) += average_num * left_scale; } - if (right_lower < end && this->histogram_[right_lower] != 0) { + if (right_lower < end && !IsZero(this->histogram_[right_lower])) { expanded_histogram->at(right_lower) += average_num * right_scale; } for (int k = left_upper; k < right_lower; ++k) { - if (this->histogram_[k] != 0) { + if (!IsZero(this->histogram_[k])) { expanded_histogram->at(k) += average_num; } } diff --git a/mindspore/lite/tools/converter/quantizer/data_distribution.h b/mindspore/lite/tools/converter/quantizer/data_distribution.h index fb2f94fe589..f062afa469f 100644 --- a/mindspore/lite/tools/converter/quantizer/data_distribution.h +++ b/mindspore/lite/tools/converter/quantizer/data_distribution.h @@ -21,6 +21,8 @@ #include "tools/converter/quantizer/quant_params.h" #include "tools/converter/quantizer/quantize_util.h" namespace mindspore::lite::quant { +constexpr float kEps = 1e-8; + class DataDistribution { public: DataDistribution() = default; @@ -78,6 +80,8 @@ class DataDistribution { std::pair CalQuantileMinMax(const std::vector &min_datas, const std::vector &max_datas); + inline bool IsZero(float x) { return (x <= kEps && x >= -kEps); } + private: std::vector histogram_; CNodePtr cnode_; diff --git a/mindspore/lite/tools/converter/quantizer/debug_info_manager.cc b/mindspore/lite/tools/converter/quantizer/debug_info_manager.cc index 4c8054956d5..e88989d49d5 100644 --- a/mindspore/lite/tools/converter/quantizer/debug_info_manager.cc +++ b/mindspore/lite/tools/converter/quantizer/debug_info_manager.cc @@ -94,7 +94,7 @@ std::string DebugInfoManager::ParseInOutTensorToString(InOutFlag in_out_flag) { return "ERROR"; } -std::string DebugInfoManager::ParseDataTypeFlagToString(DataTypeFlag data_type_flag) { +std::string DebugInfoManager::ParseDataTypeFlagToString(DataTypeFlag data_type_flag) const { switch (data_type_flag) { case ORIGIN: return "Origin"; @@ -363,7 +363,7 @@ int DebugInfoManager::AddComparedInfo(const mindspore::MSCallBackParam &call_bac } std::map DebugInfoManager::ParseInputTensors( - const mindspore::lite::LiteModel &model) { + const mindspore::lite::LiteModel &model) const { std::map maps; for (auto &node : model.graph_.all_nodes_) { for (auto &index : node->input_indices_) { diff --git a/mindspore/lite/tools/converter/quantizer/debug_info_manager.h b/mindspore/lite/tools/converter/quantizer/debug_info_manager.h index f2b54c4642e..ce3bde2b68b 100644 --- a/mindspore/lite/tools/converter/quantizer/debug_info_manager.h +++ b/mindspore/lite/tools/converter/quantizer/debug_info_manager.h @@ -112,7 +112,7 @@ class DebugInfoManager { int tensor_index, QuantDebugInfo *quant_debug_info, const mindspore::lite::Tensor &tensor, const quant::DebugMode &debug_mode); - std::string ParseDataTypeFlagToString(DataTypeFlag data_type_flag); + std::string ParseDataTypeFlagToString(DataTypeFlag data_type_flag) const; std::string ParseTensorTypeFlagToString(TensorTypeFlag tensor_type_flag); @@ -122,7 +122,7 @@ class DebugInfoManager { void SaveInfo(std::ofstream &out_file, const QuantDebugInfo &info); - std::map ParseInputTensors(const mindspore::lite::LiteModel &model); + std::map ParseInputTensors(const mindspore::lite::LiteModel &model) const; std::map ParseOutputTensorFromModel(const Model &model); diff --git a/mindspore/lite/tools/converter/quantizer/fse_bit_stream.cc b/mindspore/lite/tools/converter/quantizer/fse_bit_stream.cc index 680e948ac62..eeabfe443d8 100644 --- a/mindspore/lite/tools/converter/quantizer/fse_bit_stream.cc +++ b/mindspore/lite/tools/converter/quantizer/fse_bit_stream.cc @@ -58,7 +58,7 @@ void FSEBitStream::Empty() { int64_t FSEBitStream::Pop(uint8_t bit_count) { MS_ASSERT(curr_bit_count_ <= kCurrentBitCount); - int64_t right = curr_chunk_ >> (kCurrentBitCount - curr_bit_count_); + int64_t right = curr_chunk_ >> static_cast(kCurrentBitCount - curr_bit_count_); int64_t res = right & ((1 << bit_count) - 1); curr_bit_count_ -= static_cast(bit_count); if (curr_bit_count_ > 0) { diff --git a/mindspore/lite/tools/converter/quantizer/full_quant_quantizer.cc b/mindspore/lite/tools/converter/quantizer/full_quant_quantizer.cc index 59829da3982..a212ceaae21 100644 --- a/mindspore/lite/tools/converter/quantizer/full_quant_quantizer.cc +++ b/mindspore/lite/tools/converter/quantizer/full_quant_quantizer.cc @@ -303,7 +303,7 @@ int FullQuantQuantizer::QuantNode(const FuncGraphPtr &func_graph) { auto outputs_diverg_info = calibrator_->GetOutputDivergInfo(); auto cnodes = func_graph->GetOrderedCnodes(); - for (auto &cnode : cnodes) { + for (const auto &cnode : cnodes) { auto op_name = cnode->fullname_with_scope(); auto primitive = GetValueNode(cnode->input(0)); if (primitive == nullptr) { diff --git a/mindspore/lite/tools/converter/quantizer/full_quant_quantizer.h b/mindspore/lite/tools/converter/quantizer/full_quant_quantizer.h index a4840e575d7..9c411e53f0f 100644 --- a/mindspore/lite/tools/converter/quantizer/full_quant_quantizer.h +++ b/mindspore/lite/tools/converter/quantizer/full_quant_quantizer.h @@ -29,7 +29,6 @@ #include "schema/inner/model_generated.h" #include "tools/converter/quantizer/quantizer.h" #include "tools/converter/quantizer/quantize_util.h" -#include "tools/converter/quantizer/quant_params.h" #include "tools/converter/preprocess/preprocess_param.h" #include "tools/converter/quantizer/calibrator.h" #include "tools/converter/quantizer/data_distribution.h" diff --git a/mindspore/lite/tools/converter/quantizer/insert_quant_node_manager.cc b/mindspore/lite/tools/converter/quantizer/insert_quant_node_manager.cc index 1284485ea73..2f42240fd80 100644 --- a/mindspore/lite/tools/converter/quantizer/insert_quant_node_manager.cc +++ b/mindspore/lite/tools/converter/quantizer/insert_quant_node_manager.cc @@ -118,7 +118,7 @@ int InsertQuantNodeManager::InsertCastNode(const FuncGraphPtr &graph, const CNod return RET_OK; } -int InsertQuantNodeManager::CheckDataType(const AnfNodePtr &input_node, TypeId check_type_id) { +int InsertQuantNodeManager::CheckDataType(const AnfNodePtr &input_node, TypeId check_type_id) const { bool is_graph_input = IsGraphInput(input_node); if (!input_node->isa() && !is_graph_input) { return RET_NO_CHANGE; diff --git a/mindspore/lite/tools/converter/quantizer/insert_quant_node_manager.h b/mindspore/lite/tools/converter/quantizer/insert_quant_node_manager.h index 1940417c9cb..3555a42c3ca 100644 --- a/mindspore/lite/tools/converter/quantizer/insert_quant_node_manager.h +++ b/mindspore/lite/tools/converter/quantizer/insert_quant_node_manager.h @@ -43,7 +43,7 @@ class InsertQuantNodeManager { int InsertCastNode(const FuncGraphPtr &graph, const CNodePtr &cnode, size_t input_index, bool is_graph_input); - int CheckDataType(const AnfNodePtr &input_node, TypeId check_type_id); + int CheckDataType(const AnfNodePtr &input_node, TypeId check_type_id) const; int NewDynamicQuantNode(const FuncGraphPtr &graph, const CNodePtr &cnode); diff --git a/mindspore/lite/tools/converter/quantizer/mixed_bit_weight_quantizer.h b/mindspore/lite/tools/converter/quantizer/mixed_bit_weight_quantizer.h index 39d921e477c..1473427bba5 100644 --- a/mindspore/lite/tools/converter/quantizer/mixed_bit_weight_quantizer.h +++ b/mindspore/lite/tools/converter/quantizer/mixed_bit_weight_quantizer.h @@ -22,7 +22,6 @@ #include "schema/inner/model_generated.h" #include "src/common/log_adapter.h" #include "src/common/quant_utils.h" -#include "include/errorcode.h" namespace mindspore::lite::quant { constexpr float kBinarySearchStep = 2.0; diff --git a/mindspore/lite/tools/converter/quantizer/quant_helper/conv_quant_type_determiner.cc b/mindspore/lite/tools/converter/quantizer/quant_helper/conv_quant_type_determiner.cc index e45f4722026..ce378c958d0 100644 --- a/mindspore/lite/tools/converter/quantizer/quant_helper/conv_quant_type_determiner.cc +++ b/mindspore/lite/tools/converter/quantizer/quant_helper/conv_quant_type_determiner.cc @@ -18,7 +18,6 @@ #include "tools/converter/quantizer/quant_helper/conv_quant_type_determiner.h" #include "tools/converter/quantizer/quantize_util.h" #include "src/common/log_adapter.h" -#include "mindspore/core/ir/dtype/type_id.h" namespace mindspore::lite { bool ConvQuantTypeDeterminer::DetermineQuantWeight(const mindspore::schema::MetaGraphT &graph, mindspore::schema::CNodeT *node) { diff --git a/mindspore/lite/tools/converter/quantizer/quantize_util.cc b/mindspore/lite/tools/converter/quantizer/quantize_util.cc index 97a8ac300f6..101be4374aa 100644 --- a/mindspore/lite/tools/converter/quantizer/quantize_util.cc +++ b/mindspore/lite/tools/converter/quantizer/quantize_util.cc @@ -82,7 +82,7 @@ int ComputeBiasDataAndQuantParam(const std::vector &bias_scales, const s quant_params->at(i).scale = bias_scale_tmp; MS_LOG(DEBUG) << "new filter scale: " << filter_scale; } - auto quant_data = (int32_t)std::round(raw_datas[i] / bias_scale_tmp); + auto quant_data = static_cast(std::round(raw_datas[i] / bias_scale_tmp)); quant_datas->at(i) = quant_data; } return RET_OK; diff --git a/mindspore/lite/tools/converter/quantizer/quantize_util.h b/mindspore/lite/tools/converter/quantizer/quantize_util.h index 9cd31dbcade..7ef68369c42 100644 --- a/mindspore/lite/tools/converter/quantizer/quantize_util.h +++ b/mindspore/lite/tools/converter/quantizer/quantize_util.h @@ -215,9 +215,9 @@ bool IndexingCompress(const std::set &quant_data_set, const std::map> (bit_num - i - 1)) & (0x1); } // write the unique value set: each value has bit_num bit signed - for (auto unique_value : quant_data_set) { + for (auto iter = quant_data_set.cbegin(); iter != quant_data_set.cend(); ++iter) { for (size_t i = 0; i < bit_num; i++) { - bits[index++] = ((unique_value + (1 << (bit_num - 1))) >> (bit_num - i - 1)) & (0x1); + bits[index++] = ((*iter + (1 << (bit_num - 1))) >> (bit_num - i - 1)) & (0x1); } } // write the index: each index has unique_value_bit unsigned @@ -375,8 +375,8 @@ bool PackRepetition(size_t bit_num, schema::TensorT *tensor) { } std::map unique_value_index_map; auto index = 0; - for (auto value : quant_data_set) { - unique_value_index_map[value] = index++; + for (auto iter = quant_data_set.cbegin(); iter != quant_data_set.cend(); ++iter) { + unique_value_index_map[*iter] = index++; } auto unique_value_cnt = quant_data_set.size(); diff --git a/mindspore/lite/tools/converter/quantizer/quantizer.h b/mindspore/lite/tools/converter/quantizer/quantizer.h index 797b5a4504a..ba439335a93 100644 --- a/mindspore/lite/tools/converter/quantizer/quantizer.h +++ b/mindspore/lite/tools/converter/quantizer/quantizer.h @@ -16,11 +16,9 @@ #ifndef MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_QUANTIZER_H_ #define MINDSPORE_LITE_TOOLS_CONVERTER_QUANTIZER_QUANTIZER_H_ -#include #include #include #include "schema/inner/model_generated.h" -#include "include/errorcode.h" #include "ir/func_graph.h" #include "ir/anf.h" #include "base/base.h"