!23089 fix config parse bug && full quant bug

Merge pull request !23089 from yeyunpeng2020/quant
This commit is contained in:
i-robot 2021-09-08 12:26:57 +00:00 committed by Gitee
commit a0829c11e5
10 changed files with 33 additions and 28 deletions

View File

@ -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<int8_t>::min();
constexpr int32_t max_value = std::numeric_limits<int8_t>::max();
constexpr float output_scale = 1.0f / kInt8Range;
constexpr int32_t output_zp = std::numeric_limits<int8_t>::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<int32_t>(std::numeric_limits<int8_t>::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;
}

View File

@ -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 (!is_last_axis_) {
if (dst_data_ == nullptr) {
MS_LOG(ERROR) << "Output data of reduce int8 operator is null.";
return RET_NULL_PTR;
}
if (!is_last_axis_) {
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_);
}

View File

@ -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<int8_t>::min();
int32_t max_value = std::numeric_limits<int8_t>::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;
}

View File

@ -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]

View File

@ -78,6 +78,9 @@ int SplitLineToMap(std::ifstream *ifs, std::map<std::string, std::map<std::strin
return RET_ERROR;
}
if (raw_line.empty()) {
continue;
}
if (raw_line.at(0) == '[') {
section = raw_line.substr(1, raw_line.size() - 2);
continue;

View File

@ -22,6 +22,7 @@ namespace mindspore {
namespace lite {
namespace {
constexpr int kQuantBitNumInt16 = 16;
constexpr int kQuantBitNumInt8 = 8;
} // namespace
int QuantParamParser::ParseCommonQuant(const CommonQuantString &common_quant_string,
quant::CommonQuantParam *common_quant) {
@ -39,12 +40,12 @@ int QuantParamParser::ParseCommonQuant(const CommonQuantString &common_quant_str
}
if (common_quant->quant_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;
}
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -23,6 +23,7 @@
#include <memory>
#include <vector>
#include <set>
#include <functional>
#include "include/version.h"
#include "ops/concat.h"
#include "ops/crop.h"
@ -339,13 +340,13 @@ static bool SearchLowerBound(const std::vector<float> &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<float>(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) {
@ -362,13 +363,13 @@ static bool SearchUpperBound(const std::vector<float> &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<float>(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) {
@ -384,8 +385,10 @@ static float CalPercentile(const std::vector<float> &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;

View File

@ -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;