From a274d915c3dd5d8ae2f31641e09226b974260ec6 Mon Sep 17 00:00:00 2001 From: yeyunpeng2020 Date: Wed, 8 Sep 2021 17:11:17 +0800 Subject: [PATCH] fix config parse bug && full quant bug --- .../coder/opcoders/nnacl/int8/sigmoid_int8_coder.cc | 12 +++--------- .../lite/src/runtime/kernel/arm/int8/reduce_int8.cc | 13 +++++++++---- .../src/runtime/kernel/arm/int8/sigmoid_int8.cc | 7 +++---- .../quant/ml_face_mnet_image_posttraining.config | 2 +- mindspore/lite/tools/common/parse_config_utils.cc | 3 +++ .../converter/config_parser/quant_param_parser.cc | 7 ++++--- .../lite/tools/converter/preprocess/opencv_utils.cc | 2 +- .../tools/converter/quantizer/config/full_quant.cfg | 2 +- .../lite/tools/converter/quantizer/quantize_util.cc | 11 +++++++---- .../lite/tools/converter/quantizer/quantize_util.h | 2 +- 10 files changed, 33 insertions(+), 28 deletions(-) diff --git a/mindspore/lite/micro/coder/opcoders/nnacl/int8/sigmoid_int8_coder.cc b/mindspore/lite/micro/coder/opcoders/nnacl/int8/sigmoid_int8_coder.cc index 79abcc569b1..11129f00c64 100644 --- a/mindspore/lite/micro/coder/opcoders/nnacl/int8/sigmoid_int8_coder.cc +++ b/mindspore/lite/micro/coder/opcoders/nnacl/int8/sigmoid_int8_coder.cc @@ -25,11 +25,10 @@ namespace mindspore::lite::micro::nnacl { constexpr auto kInt8Range = 256; -void CalculateTableList(int8_t *table, const float input_scale, const int32_t input_zp) { +void CalculateTableList(int8_t *table, const float input_scale, const int32_t input_zp, const float output_scale, + const int32_t output_zp) { constexpr int32_t min_value = std::numeric_limits::min(); constexpr int32_t max_value = std::numeric_limits::max(); - constexpr float output_scale = 1.0f / kInt8Range; - constexpr int32_t output_zp = std::numeric_limits::min(); for (int i = min_value; i < max_value; ++i) { const float real_input_value = input_scale * (i - input_zp); @@ -49,12 +48,7 @@ int SigmodInt8Coder::Prepare(CoderContext *const context) { const int32_t input_zp = input_tensor_->quant_params().at(0).zeroPoint; const float output_scale = output_tensor_->quant_params().at(0).scale; const int32_t output_zp = output_tensor_->quant_params().at(0).zeroPoint; - if (output_scale != (1.0f / kInt8Range) || output_zp != static_cast(std::numeric_limits::min())) { - MS_LOG(ERROR) << "Output scale is : " << output_scale << ", should be 1/kInt8Range. Output zp is : " << output_zp - << ", should be -128."; - return RET_ERROR; - } - CalculateTableList(table_list_, input_scale, input_zp); + CalculateTableList(table_list_, input_scale, input_zp, output_scale, output_zp); return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/reduce_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/reduce_int8.cc index 76cf17a3b7b..f8a891810ca 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/reduce_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/reduce_int8.cc @@ -560,14 +560,19 @@ int ReduceInt8CPUKernel::CallReduceUnit(int task_id) { MS_LOG(ERROR) << "Input data of reduce int8 operator is null."; return RET_NULL_PTR; } - if (dst_data_ == nullptr) { - MS_LOG(ERROR) << "Output data of reduce int8 operator is null."; - return RET_NULL_PTR; - } + if (!is_last_axis_) { + if (dst_data_ == nullptr) { + MS_LOG(ERROR) << "Output data of reduce int8 operator is null."; + return RET_NULL_PTR; + } return reducer_(outer_size_, inner_size_, axis_size_, src_data_, dst_data_, &quant_arg_, task_id, op_parameter_->thread_num_); } else { + if (last_dst_data_ == nullptr) { + MS_LOG(ERROR) << "Output data of reduce int8 operator is null."; + return RET_NULL_PTR; + } return last_reducer_(outer_size_, inner_size_, axis_size_, src_data_, last_dst_data_, &quant_arg_, task_id, op_parameter_->thread_num_); } diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/sigmoid_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/sigmoid_int8.cc index 57efb845dc2..82e7acc1ebe 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/sigmoid_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/sigmoid_int8.cc @@ -30,11 +30,10 @@ using mindspore::lite::RET_OK; using mindspore::schema::ActivationType_SIGMOID; namespace mindspore::kernel { -void CalculateTableList(int8_t *table, const float input_scale, const int32_t input_zp) { +void CalculateTableList(int8_t *table, const float input_scale, const int32_t input_zp, const float output_scale, + const int32_t output_zp) { int32_t min_value = std::numeric_limits::min(); int32_t max_value = std::numeric_limits::max(); - const float output_scale = 1.0f / 256; - const int32_t output_zp = -128; for (int i = min_value; i < max_value; ++i) { const float real_input_value = input_scale * (i - input_zp); @@ -58,7 +57,7 @@ int SigmoidInt8CPUKernel::Init() { << ", should be -128."; return RET_ERROR; } - CalculateTableList(table_list_, input_scale, input_zp); + CalculateTableList(table_list_, input_scale, input_zp, output_scale, output_zp); return RET_OK; } diff --git a/mindspore/lite/test/config/quant/ml_face_mnet_image_posttraining.config b/mindspore/lite/test/config/quant/ml_face_mnet_image_posttraining.config index ac0eb40843f..4116ef98629 100644 --- a/mindspore/lite/test/config/quant/ml_face_mnet_image_posttraining.config +++ b/mindspore/lite/test/config/quant/ml_face_mnet_image_posttraining.config @@ -18,7 +18,7 @@ normalize_std=[1, 1, 1] # Image resize resize_width=640 resize_height=640 -# Resize method supports LINEAR or NEARST or CUBIC +# Resize method supports LINEAR or NEAREST or CUBIC resize_method=LINEAR [full_quant_param] diff --git a/mindspore/lite/tools/common/parse_config_utils.cc b/mindspore/lite/tools/common/parse_config_utils.cc index cd564cd00b6..6d7257d0c0e 100644 --- a/mindspore/lite/tools/common/parse_config_utils.cc +++ b/mindspore/lite/tools/common/parse_config_utils.cc @@ -78,6 +78,9 @@ int SplitLineToMap(std::ifstream *ifs, std::mapquant_type == schema::QuantType_WeightQuant) { if (common_quant->bit_num < 0 || common_quant->bit_num > kQuantBitNumInt16) { - MS_LOG(ERROR) << "INPUT ILLEGAL: bit_num should be greater than zero and less than 16 currently."; + MS_LOG(ERROR) << "INPUT ILLEGAL: bit_num should be [0,16]."; return RET_INPUT_PARAM_INVALID; } } else if (common_quant->quant_type == schema::QuantType_PostTraining) { - if (common_quant->bit_num <= 0 || common_quant->bit_num > kQuantBitNumInt16) { - MS_LOG(ERROR) << "INPUT ILLEGAL: bit_num should be greater or equal to zero and less than 16 currently."; + if (common_quant->bit_num <= 0 || common_quant->bit_num > kQuantBitNumInt8) { + MS_LOG(ERROR) << "INPUT ILLEGAL: bit_num should be [1,8]."; return RET_INPUT_PARAM_INVALID; } } diff --git a/mindspore/lite/tools/converter/preprocess/opencv_utils.cc b/mindspore/lite/tools/converter/preprocess/opencv_utils.cc index e8a49d07ab4..a8c8fc9860f 100644 --- a/mindspore/lite/tools/converter/preprocess/opencv_utils.cc +++ b/mindspore/lite/tools/converter/preprocess/opencv_utils.cc @@ -51,7 +51,7 @@ cv::InterpolationFlags ConvertResizeMethod(const std::string &method) { } else if (method == "CUBIC") { return cv::INTER_CUBIC; } else { - MS_LOG(ERROR) << "Unsupported resize method:" << method; + MS_LOG(ERROR) << "INPUT ILLEGAL: resize_method must be NEAREST|LINEAR|CUBIC."; return cv::INTER_MAX; } } diff --git a/mindspore/lite/tools/converter/quantizer/config/full_quant.cfg b/mindspore/lite/tools/converter/quantizer/config/full_quant.cfg index 3584f4936e9..7c3f2ca42d6 100644 --- a/mindspore/lite/tools/converter/quantizer/config/full_quant.cfg +++ b/mindspore/lite/tools/converter/quantizer/config/full_quant.cfg @@ -29,7 +29,7 @@ normalize_std=[127.5, 127.5, 127.5] # Image resize resize_width=224 resize_height=224 -# Resize method supports LINEAR or NEARST or CUBIC +# Resize method supports LINEAR or NEAREST or CUBIC resize_method=LINEAR # Image center crop center_crop_width=224 diff --git a/mindspore/lite/tools/converter/quantizer/quantize_util.cc b/mindspore/lite/tools/converter/quantizer/quantize_util.cc index d4346db11bb..1d553ffaf8d 100644 --- a/mindspore/lite/tools/converter/quantizer/quantize_util.cc +++ b/mindspore/lite/tools/converter/quantizer/quantize_util.cc @@ -23,6 +23,7 @@ #include #include #include +#include #include "include/version.h" #include "ops/concat.h" #include "ops/crop.h" @@ -348,13 +349,13 @@ static bool SearchLowerBound(const std::vector &data, const size_t &index return false; } if (fabs(max_tmp - *min_tmp) <= 0.0f || fabs(length - *min_idx) <= 0.0f) { - MS_LOG(ERROR) << "divisor cannot be 0"; + MS_LOG(INFO) << "divisor cannot be 0"; return false; } float range_ratio = (data.at(index) - *min_tmp) / (max_tmp - *min_tmp); float index_ratio = static_cast(index - *min_idx) / (length - *min_idx); if (fabs(index_ratio) <= 0.0f) { - MS_LOG(ERROR) << "divisor cannot be 0"; + MS_LOG(INFO) << "divisor cannot be 0"; return false; } if (index_ratio > 0 && range_ratio / index_ratio > ratio) { @@ -371,13 +372,13 @@ static bool SearchUpperBound(const std::vector &data, const size_t &index return false; } if (fabs(*max_tmp - min_tmp) <= 0.0f || fabs(length - *max_idx) <= 0.0f) { - MS_LOG(ERROR) << "divisor cannot be 0"; + MS_LOG(INFO) << "divisor cannot be 0"; return false; } float range_ratio = (*max_tmp - data.at(index)) / (*max_tmp - min_tmp); float index_ratio = static_cast(index - *max_idx) / (length - *max_idx); if (fabs(index_ratio) <= 0.0f) { - MS_LOG(ERROR) << "divisor cannot be 0"; + MS_LOG(INFO) << "divisor cannot be 0"; return false; } if (index_ratio > 0 && range_ratio / index_ratio > ratio) { @@ -393,8 +394,10 @@ static float CalPercentile(const std::vector &data, const int &outlier_pe int index = std::ceil(val); float result; if (index - val > 0) { + MS_ASSERT(index - 1 >= 0); result = data.at(index - 1); } else { + MS_ASSERT(index - 1 >= 0); result = (data.at(index - 1) + data.at(index)) / 2; } return result; diff --git a/mindspore/lite/tools/converter/quantizer/quantize_util.h b/mindspore/lite/tools/converter/quantizer/quantize_util.h index d6e94cfae28..25d5bd0c5bc 100644 --- a/mindspore/lite/tools/converter/quantizer/quantize_util.h +++ b/mindspore/lite/tools/converter/quantizer/quantize_util.h @@ -57,7 +57,7 @@ enum WeightQuantType { constexpr size_t kUint8Quantization = 8; constexpr size_t kMaxBit = 8; constexpr size_t kMaxNum1024 = 1024; -constexpr size_t kPercentBase = 100; +constexpr float kPercentBase = 100.0; constexpr size_t kMillisecondsBase = 10; constexpr size_t kWightIndex = 1; constexpr double kScaleThreashold = 1e-38;