| @@ -15,6 +15,7 @@ | |||||
| */ | */ | ||||
| #include "src/runtime/kernel/arm/int8/softmax_int8.h" | #include "src/runtime/kernel/arm/int8/softmax_int8.h" | ||||
| #include <limits> | |||||
| #include "src/runtime/kernel/arm/nnacl/int8/softmax_int8.h" | #include "src/runtime/kernel/arm/nnacl/int8/softmax_int8.h" | ||||
| #include "schema/model_generated.h" | #include "schema/model_generated.h" | ||||
| #include "src/runtime/runtime_api.h" | #include "src/runtime/runtime_api.h" | ||||
| @@ -44,6 +45,8 @@ int SoftmaxInt8CPUKernel::Init() { | |||||
| auto out_quant_args = out_tensor->GetQuantParams(); | auto out_quant_args = out_tensor->GetQuantParams(); | ||||
| quant_params_.out_quant_arg_.scale_ = out_quant_args.front().scale; | quant_params_.out_quant_arg_.scale_ = out_quant_args.front().scale; | ||||
| quant_params_.out_quant_arg_.zp_ = out_quant_args.front().zeroPoint; | quant_params_.out_quant_arg_.zp_ = out_quant_args.front().zeroPoint; | ||||
| quant_params_.output_activation_min_ = std::numeric_limits<int8_t>::min(); | |||||
| quant_params_.output_activation_max_ = std::numeric_limits<int8_t>::max(); | |||||
| if (!InferShapeDone()) { | if (!InferShapeDone()) { | ||||
| return RET_OK; | return RET_OK; | ||||
| @@ -95,12 +98,10 @@ int SoftmaxInt8CPUKernel::DoSoftmax(int task_id) { | |||||
| int stride = UP_DIV(outter_size, thread_count_); | int stride = UP_DIV(outter_size, thread_count_); | ||||
| int count = MSMIN(stride, outter_size - stride * task_id); | int count = MSMIN(stride, outter_size - stride * task_id); | ||||
| int stride_size = stride * task_id * inner_size; | |||||
| input_ptr += stride * task_id * inner_size; | |||||
| output_ptr += stride * task_id * inner_size; | |||||
| exp_data_ += stride * task_id * inner_size; | |||||
| auto error_code = Int8Softmax(input_ptr, output_ptr, count, exp_data_, sum_data_, quant_params_, softmax_param_); | |||||
| auto error_code = SoftmaxInt8(input_ptr + stride_size, output_ptr + stride_size, count, exp_data_ + stride_size, | |||||
| sum_data_, quant_params_, softmax_param_); | |||||
| if (error_code != RET_OK) { | if (error_code != RET_OK) { | ||||
| MS_LOG(ERROR) << "DoSoftmax error task_id[" << task_id << "] error_code[" << error_code << "]"; | MS_LOG(ERROR) << "DoSoftmax error task_id[" << task_id << "] error_code[" << error_code << "]"; | ||||
| return RET_ERROR; | return RET_ERROR; | ||||
| @@ -37,8 +37,8 @@ class SoftmaxInt8CPUKernel : public SoftmaxBaseCPUKernel { | |||||
| private: | private: | ||||
| void FreeTmpBuffer(); | void FreeTmpBuffer(); | ||||
| float *sum_data_; | |||||
| float *exp_data_; | |||||
| float *sum_data_ = nullptr; | |||||
| float *exp_data_ = nullptr; | |||||
| SoftmaxQuantArg quant_params_; | SoftmaxQuantArg quant_params_; | ||||
| }; | }; | ||||
| } // namespace mindspore::kernel | } // namespace mindspore::kernel | ||||
| @@ -17,7 +17,7 @@ | |||||
| #include "nnacl/int8/softmax_int8.h" | #include "nnacl/int8/softmax_int8.h" | ||||
| #include <math.h> | #include <math.h> | ||||
| int Int8Softmax(const int8_t *input_ptr, int8_t *output_ptr, int count, float *exp_data, float *sum_data, | |||||
| int SoftmaxInt8(const int8_t *input_ptr, int8_t *output_ptr, int count, float *exp_data, float *sum_data, | |||||
| SoftmaxQuantArg quant_param, SoftmaxParameter *parameter) { | SoftmaxQuantArg quant_param, SoftmaxParameter *parameter) { | ||||
| int32_t axis = parameter->axis_; | int32_t axis = parameter->axis_; | ||||
| int n_dim = parameter->n_dim_; | int n_dim = parameter->n_dim_; | ||||
| @@ -48,7 +48,8 @@ int Int8Softmax(const int8_t *input_ptr, int8_t *output_ptr, int count, float *e | |||||
| int inner_offset = axis_offset + i; | int inner_offset = axis_offset + i; | ||||
| float real_output = exp_data[inner_offset] / sum_data[i]; | float real_output = exp_data[inner_offset] / sum_data[i]; | ||||
| int32_t output_scaled = round(real_output / output_scale) + output_zp; | int32_t output_scaled = round(real_output / output_scale) + output_zp; | ||||
| output_ptr[inner_offset] = MSMAX(CHAR_MIN, MSMIN(CHAR_MAX, output_scaled)); | |||||
| output_ptr[inner_offset] = | |||||
| MSMAX(quant_param.output_activation_min_, MSMIN(quant_param.output_activation_max_, output_scaled)); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -24,7 +24,7 @@ | |||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||
| extern "C" { | extern "C" { | ||||
| #endif | #endif | ||||
| int Int8Softmax(const int8_t *input_ptr, int8_t *output_ptr, int count, float *exp_data, float *sum_data, | |||||
| int SoftmaxInt8(const int8_t *input_ptr, int8_t *output_ptr, int count, float *exp_data, float *sum_data, | |||||
| SoftmaxQuantArg quant_param, SoftmaxParameter *parameter); | SoftmaxQuantArg quant_param, SoftmaxParameter *parameter); | ||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||
| } | } | ||||
| @@ -169,6 +169,11 @@ typedef struct SplitQuantArg { | |||||
| typedef struct SoftmaxQuantArg { | typedef struct SoftmaxQuantArg { | ||||
| QuantArg in_quant_args_; | QuantArg in_quant_args_; | ||||
| QuantArg out_quant_arg_; | QuantArg out_quant_arg_; | ||||
| int output_activation_min_; | |||||
| int output_activation_max_; | |||||
| int output_multiplier_; | |||||
| int shift_left_; | |||||
| int shift_right_; | |||||
| } SoftmaxQuantArg; | } SoftmaxQuantArg; | ||||
| typedef struct ReshapeQuantArg { | typedef struct ReshapeQuantArg { | ||||