!21146 [MS][LITE][CPU] 高性能模式和安全模型的宏定义 div0 数组越界等

Merge pull request !21146 from liuzhongkai/div_00
This commit is contained in:
i-robot 2021-08-02 01:58:17 +00:00 committed by Gitee
commit bfaf780c99
20 changed files with 94 additions and 58 deletions

View File

@ -28,7 +28,7 @@ int ElementFloorMod(const float *in0, const float *in1, float *out, int size) {
int ElementFloorModInt(const int *in0, const int *in1, int *out, int size) {
for (int i = 0; i < size; i++) {
NNACL_ASSERT(in1[i] != 0);
NNACL_CHECK_ZERO_RETURN_ERR(in1[i]);
int remainder = in0[i] - (in0[i] / in1[i]) * in1[i];
out[i] = (remainder != 0) && ((in0[i] > 0) != (in1[i] > 0)) ? remainder + in1[i] : remainder;
}
@ -37,14 +37,15 @@ int ElementFloorModInt(const int *in0, const int *in1, int *out, int size) {
int ElementMod(const float *in0, const float *in1, float *out, int size) {
for (int i = 0; i < size; i++) {
out[i] = fmod(in0[i], in1[i]);
out[i] = fmodf(in0[i], in1[i]);
}
return NNACL_OK;
}
int ElementModInt(const int *in0, const int *in1, int *out, int size) {
for (int i = 0; i < size; i++) {
out[i] = fmod(in0[i], in1[i]);
NNACL_CHECK_ZERO_RETURN_ERR(in1[i]);
out[i] = in0[i] % in1[i];
}
return NNACL_OK;
}
@ -52,11 +53,11 @@ int ElementModInt(const int *in0, const int *in1, int *out, int size) {
int ElementOptMod(const float *in0, const float *in1, float *out, int size, const ArithmeticParameter *param) {
if (param->in_elements_num0_ == 1) {
for (int index = 0; index < size; index++) {
out[index] = fmod(in0[0], in1[index]);
out[index] = fmodf(in0[0], in1[index]);
}
} else {
for (int index = 0; index < size; index++) {
out[index] = fmod(in0[index], in1[0]);
out[index] = fmodf(in0[index], in1[0]);
}
}
return NNACL_OK;
@ -65,11 +66,13 @@ int ElementOptMod(const float *in0, const float *in1, float *out, int size, cons
int ElementOptModInt(const int *in0, const int *in1, int *out, int size, const ArithmeticParameter *param) {
if (param->in_elements_num0_ == 1) {
for (int index = 0; index < size; index++) {
out[index] = fmod(in0[0], in1[index]);
NNACL_CHECK_ZERO_RETURN_ERR(in1[index]);
out[index] = in0[0] % in1[index];
}
} else {
NNACL_CHECK_ZERO_RETURN_ERR(in1[0]);
for (int index = 0; index < size; index++) {
out[index] = fmod(in0[index], in1[0]);
out[index] = in0[index] % in1[0];
}
}
return NNACL_OK;
@ -84,7 +87,7 @@ int ElementFloorDiv(const float *in0, const float *in1, float *out, int size) {
int ElementFloorDivInt(const int *in0, const int *in1, int *out, int size) {
for (int i = 0; i < size; i++) {
NNACL_ASSERT(in1[i] != 0);
NNACL_CHECK_ZERO_RETURN_ERR(in1[i]);
out[i] = in0[i] / in1[i];
}
return NNACL_OK;

View File

@ -102,7 +102,7 @@ int ElementLogicalNotBool(const bool *input, bool *output, const int element_siz
// round:
int ElementRound(const float *input, float *output, const int element_size) {
for (int i = 0; i < element_size; i++) {
output[i] = round(input[i]);
output[i] = roundf(input[i]);
}
return NNACL_OK;
}
@ -117,7 +117,7 @@ int ElementFloor(const float *input, float *output, const int element_size) {
int ElementCeil(const float *input, float *output, const int number) {
for (int i = 0; i < number; ++i) {
output[i] = ceil(input[i]);
output[i] = ceilf(input[i]);
}
return NNACL_OK;
}

View File

@ -31,7 +31,7 @@ void BatchNormFp32(const void *input, const void *mean, const void *variance, co
for (int i = 0; i < cur_unit; i++) {
for (int c = 0; c < param->channel_; c++) {
float variance_sqrt = sqrt(((const float *)variance)[c] + param->epsilon_);
float variance_sqrt = sqrtf(((const float *)variance)[c] + param->epsilon_);
((float *)output)[cur_offset + c] =
(((const float *)input)[cur_offset + c] - ((const float *)mean)[c]) / variance_sqrt;
}
@ -51,7 +51,7 @@ void FusedBatchNormFp32(const void *input, const void *scale, const void *offset
for (int i = 0; i < cur_unit; i++) {
for (int c = 0; c < param->channel_; c++) {
float variance_sqrt = sqrt(((const float *)variance)[c] + param->epsilon_);
float variance_sqrt = sqrtf(((const float *)variance)[c] + param->epsilon_);
float norm_val = (((const float *)input)[cur_offset + c] - ((const float *)mean)[c]) / variance_sqrt;
((float *)output)[cur_offset + c] = norm_val * ((const float *)scale)[c] + ((const float *)offset)[c];
}

View File

@ -215,7 +215,7 @@ int DetectionPostProcessRegular(const int num_boxes, const int num_classes_with_
}
for (int i = 0; i < param->max_detections_ * param->max_classes_per_detection_; ++i) {
if (i < all_classes_output_num) {
NNACL_ASSERT(num_classes_with_bg != 0);
NNACL_CHECK_ZERO_RETURN_ERR(num_classes_with_bg);
const int box_index = all_indexes[i] / num_classes_with_bg;
const int class_index = all_indexes[i] % num_classes_with_bg - first_class_index;
*((BboxCorner *)(output_boxes) + i) = *((BboxCorner *)(decoded_boxes) + box_index);

View File

@ -65,12 +65,11 @@ int ElementOptDivRelu6(const float *in0, const float *in1, float *out, int size,
int ElementOptDivInt(const int *in0, const int *in1, int *out, int size, const ArithmeticParameter *param) {
if (param->in_elements_num0_ == 1) {
for (int index = 0; index < size; index++) {
NNACL_CHECK_ZERO_RETURN_ERR(in1[index] != 0);
out[index] = in0[0] / in1[index];
}
} else {
if (in1[0] == 0) {
return NNACL_ERRCODE_DIVISOR_ZERO;
}
NNACL_CHECK_ZERO_RETURN_ERR(in1[0] != 0);
for (int index = 0; index < size; index++) {
out[index] = in0[index] / in1[0];
}

View File

@ -62,7 +62,7 @@ int ThreadTrailingAxis(const float *input_ptr, float *output_ptr, const L2NormPa
const float val = input_ptr[i * c + j];
square_sum += val * val;
}
float sqrt_sum = sqrt(square_sum > param->epsilon_ ? square_sum : param->epsilon_);
float sqrt_sum = sqrtf(square_sum > param->epsilon_ ? square_sum : param->epsilon_);
for (j = 0; j < c; ++j) {
float tmp = input_ptr[i * c + j] / sqrt_sum;
if (is_relu) {

View File

@ -73,6 +73,8 @@ int LayerNorm(const float *src_data, const float *gamma_data, const float *beta_
out_deno == NULL) {
return NNACL_NULL_PTR;
}
NNACL_CHECK_ZERO_RETURN_ERR(param->params_inner_size_);
NNACL_CHECK_ZERO_RETURN_ERR(param->params_outer_size_);
int step = UP_DIV(param->norm_outer_size_, param->op_parameter_.thread_num_);
int thread_end = MSMIN((task_id + 1) * step, param->norm_outer_size_);
for (int i = task_id * step; i < thread_end; i++) {

View File

@ -37,7 +37,7 @@ void LogSoftmaxLastAxis(const float *src, float *dst, float *exp_data, int batch
sum += exp_data[cur_batch_offset + j];
}
for (int k = 0; k < channel; k++) {
dst[cur_batch_offset + k] = dst[cur_batch_offset + k] - log(sum);
dst[cur_batch_offset + k] = dst[cur_batch_offset + k] - logf(sum);
}
}
}
@ -70,7 +70,7 @@ void LogSoftmax(const float *input_ptr, float *output_ptr, float *sum_data, cons
for (int j = 0; j < input_shape[axis]; j++) {
int axis_offset = inner_offset + j * inner_size;
output_ptr[axis_offset] = input_ptr[axis_offset] - max_data;
sum_data[k + sum_outter_offset] += exp(output_ptr[axis_offset]);
sum_data[k + sum_outter_offset] += expf(output_ptr[axis_offset]);
}
}
}
@ -81,7 +81,7 @@ void LogSoftmax(const float *input_ptr, float *output_ptr, float *sum_data, cons
int axis_offset = outter_offset + j * inner_size;
for (int k = 0; k < inner_size; k++) {
int inner_offset = axis_offset + k;
output_ptr[inner_offset] = output_ptr[inner_offset] - log(sum_data[k + sum_outter_offset]);
output_ptr[inner_offset] = output_ptr[inner_offset] - logf(sum_data[k + sum_outter_offset]);
}
}
}

View File

@ -16,6 +16,7 @@
#include "nnacl/fp32/pad_fp32.h"
#include "nnacl/common_func.h"
#include "nnacl/errorcode.h"
void Pad(const float *input_data, float *output_data, const int *input_shape, const int *output_shape,
const int *paddings, int tid, int thread_num) {
@ -59,6 +60,7 @@ int GetInputFlattenIndex(int out_flatten_index, const int *input_shape, const Pa
int i;
for (i = 0; i < COMM_SHAPE_SIZE; ++i) {
int left_pad = pad_param->paddings_[i * 2];
NNACL_CHECK_ZERO_RETURN_ERR(pad_param->out_strides[i])
int out_dim_index = out_flatten_index / pad_param->out_strides[i];
out_flatten_index %= pad_param->out_strides[i];
int in_dim_index = TransOut2InputDimIndex(out_dim_index, left_pad, input_shape[i], pad_param->mirror_offset_);

View File

@ -30,9 +30,7 @@ int AvgPooling(const float *input_ptr, float *output_ptr, const PoolingParameter
int output_h = pooling_param->output_h_;
int out_plane = output_w * output_h;
int out_tile_count = UP_DIV(out_plane, TILE_NUM);
if (output_w == 0) {
return NNACL_ERR;
}
NNACL_CHECK_ZERO_RETURN_ERR(output_w);
#ifdef ENABLE_AVX
int c8 = channel / C8NUM * C8NUM;
MS_FLOAT32X8 min_value_8 = MS_MOV256_F32(minf);
@ -147,9 +145,7 @@ int MaxPooling(const float *input_ptr, float *output_ptr, const PoolingParameter
int output_batch = pooling_param->output_batch_;
int out_plane = output_w * output_h;
int out_tile_count = UP_DIV(out_plane, TILE_NUM);
if (output_w == 0) {
return NNACL_ERR;
}
NNACL_CHECK_ZERO_RETURN_ERR(output_w);
#ifdef ENABLE_AVX
int c8 = channel / C8NUM * C8NUM;
MS_FLOAT32X8 min_value_8 = MS_MOV256_F32(minf);

View File

@ -59,6 +59,7 @@ int IntReduceMean(int outer_size, int inner_size, int axis_size, const int *src_
if (thread_num == 0) {
return NNACL_PARAM_INVALID;
}
NNACL_CHECK_ZERO_RETURN_ERR(axis_size);
int i, j;
#ifdef ENABLE_NEON
int block_mod = inner_size % C4NUM;

View File

@ -125,7 +125,8 @@ int PrepareCropAndResizeBilinear(const int *input_shape, const float *boxes, con
int new_width = output_shape[2];
float actual_x;
float actual_y;
NNACL_CHECK_ZERO_RETURN_ERR(new_height - 1);
NNACL_CHECK_ZERO_RETURN_ERR(new_width - 1);
for (int b = 0; b < batch; b++) {
const float *box = boxes + b * 4;
float start_h = box[0];
@ -404,6 +405,8 @@ int RewriteExtrapolationValue(const float *input_data, float *output_data, const
int new_channel = output_shape[3];
int input_h = input_shape[1];
int input_w = input_shape[2];
NNACL_CHECK_ZERO_RETURN_ERR(new_height - 1);
NNACL_CHECK_ZERO_RETURN_ERR(new_width - 1);
for (int b = 0; b < batch; b++) {
float *output = output_data + b * new_height * new_width * new_channel;

View File

@ -128,7 +128,7 @@ void Softmax(const float *input_ptr, float *output_ptr, float *sum_data, const S
}
for (int j = 0; j < input_shape[axis]; j++) {
int axis_offset = inner_offset + j * inner_size;
output_ptr[axis_offset] = exp(input_ptr[axis_offset] - max_data);
output_ptr[axis_offset] = expf(input_ptr[axis_offset] - max_data);
sum_data[k + sum_outter_offset] += output_ptr[axis_offset];
}
}

View File

@ -193,6 +193,7 @@ void TransposeDimsFp32(const float *in_data, float *out_data, const int *output_
int output_idx = 0;
int input_idx = 0;
for (int i = 0; i < num_axes; ++i) {
NNACL_CHECK_ZERO_RETURN(*(out_strides + i));
int position = pos / *(out_strides + i);
int out_stride = i < num_axes - 1 ? out_strides[i] : 1;
output_idx += (position * out_stride);
@ -211,7 +212,7 @@ int DoTransposeFp32(const float *in_data, float *out_data, const int *output_sha
int *perm = (int *)(transpose_param->perm_);
int *strides = (int *)(transpose_param->strides_);
int *out_strides = (int *)(transpose_param->out_strides_);
int data_size = transpose_param->data_num_ * sizeof(float);
int data_size = transpose_param->data_num_ * (int)(sizeof(float));
int num_axes = transpose_param->num_axes_;
// check if transpose is needed

View File

@ -29,9 +29,7 @@ void WinogradInputTransform(const float *input_data, float *trans_input, float *
int pad_w = conv_param->pad_l_;
int input_h = conv_param->input_h_;
int input_w = conv_param->input_w_;
if (out_w_block_num == 0) {
return;
}
NNACL_CHECK_ZERO_RETURN(out_w_block_num);
for (int c = 0; c < cal_num; c++) { // actual tiled number
int src_x_s = (out_tile_index % out_w_block_num) * output_unit - pad_w;
@ -107,9 +105,8 @@ void WinogradOutputTransform(const float *gemm_out, float *out_data, const float
int oc4 = UP_DIV(output_channel, C4NUM);
int oc8 = UP_DIV(output_channel, C8NUM);
int input_unit = conv_param->input_unit_;
if (output_unit_num == 0) {
return;
}
NNACL_CHECK_ZERO_RETURN(output_unit_num);
for (int i = 0; i < cal_num; i++) {
int dst_x_s = out_tile_index % output_unit_num;
int dst_y_s = out_tile_index / output_unit_num;

View File

@ -75,6 +75,44 @@
#define MAX_LEN 256
#define FLT16_MAX 65504
#ifndef ENABLE_HIGH_PERFORMANCE
#define CHECK_NULL_RETURN(ptr) \
do { \
if ((ptr) == nullptr) { \
MS_LOG(ERROR) << #ptr << " must not be null!"; \
return RET_NULL_PTR; \
} \
} while (0);
#define CHECK_LESS_RETURN(size1, size2) \
do { \
if ((size1) < (size2)) { \
MS_LOG(ERROR) << #size1 << " must not less than " << #size2; \
return RET_ERROR; \
} \
} while (0);
#define NNACL_CHECK_ZERO_RETURN_ERR(val) \
do { \
if ((val) == 0) { \
return NNACL_ERR; \
} \
} while (0);
#define NNACL_CHECK_ZERO_RETURN(val) \
do { \
if ((val) == 0) { \
return; \
} \
} while (0);
#else
#define CHECK_NULL_RETURN(ptr)
#define CHECK_LESS_RETURN(size1, size2)
#define NNACL_CHECK_ZERO_RETURN_ERR(val)
#define NNACL_CHECK_ZERO_RETURN(val)
#endif
typedef enum LiteDataType {
kDataTypeFloat,
kDataTypeFloat16,

View File

@ -32,6 +32,7 @@ option(MSLITE_ENABLE_TOOLS "enable tools" on)
option(MSLITE_ENABLE_TESTCASES "enable testcase" off)
option(MSLITE_ENABLE_NNIE "enable NNIE" off)
option(MSLITE_COMPILE_NNIE "compile NNIE" off)
option(MSLITE_ENABLE_HIGH_PERFORMANCE "enable high performance" on)
# Option that can be configured through manually
option(ENABLE_VERBOSE "" off)
@ -78,6 +79,9 @@ endif()
if(DEFINED ENV{MSLITE_COMPILE_NNIE})
set(MSLITE_COMPILE_NNIE $ENV{MSLITE_COMPILE_NNIE})
endif()
if(DEFINED ENV{MSLITE_ENABLE_HIGH_PERFORMANCE})
set(MSLITE_ENABLE_HIGH_PERFORMANCE $ENV{MSLITE_ENABLE_HIGH_PERFORMANCE})
endif()
if(PLATFORM_ARM64)
if(MSLITE_GPU_BACKEND STREQUAL "")
@ -148,6 +152,11 @@ message(STATUS "\tMSLITE_ENABLE_AVX = \t${MSLITE_ENABLE_AVX}")
message(STATUS "\tMSLITE_ENABLE_CONVERTER = \t${MSLITE_ENABLE_CONVERTER}")
message(STATUS "\tMSLITE_ENABLE_TOOLS = \t${MSLITE_ENABLE_TOOLS}")
message(STATUS "\tMSLITE_ENABLE_TESTCASES = \t${MSLITE_ENABLE_TESTCASES}")
message(STATUS "\tMSLITE_ENABLE_HIGH_PERFORMANCE = \t${MSLITE_ENABLE_HIGH_PERFORMANCE}")
if(MSLITE_ENABLE_HIGH_PERFORMANCE)
add_compile_definitions(ENABLE_HIGH_PERFORMANCE)
endif()
if(ENABLE_ASAN)
add_definitions(-fsanitize=address -fno-omit-frame-pointer)

View File

@ -154,11 +154,11 @@ void ArithmeticFP16CPUKernel::TileConstTensor(const void *in_data, void *out_dat
int ArithmeticFP16CPUKernel::Execute(const void *input0, const void *input1, void *output, int size, bool is_opt) {
int ret = RET_OK;
if (is_opt) {
CHECK_NULL_RETURN(arithmetic_opt_func_, RET_ERROR);
CHECK_NULL_RETURN(arithmetic_opt_func_);
ret = arithmetic_opt_func_(reinterpret_cast<const float16_t *>(input0), reinterpret_cast<const float16_t *>(input1),
reinterpret_cast<float16_t *>(output), size, param_);
} else {
CHECK_NULL_RETURN(arithmetic_func_, RET_ERROR);
CHECK_NULL_RETURN(arithmetic_func_);
ret = arithmetic_func_(reinterpret_cast<const float16_t *>(input0), reinterpret_cast<const float16_t *>(input1),
reinterpret_cast<float16_t *>(output), size);
}

View File

@ -19,6 +19,7 @@
using mindspore::kernel::KERNEL_ARCH;
using mindspore::lite::KernelRegistrar;
using mindspore::lite::RET_ERROR;
using mindspore::lite::RET_NULL_PTR;
using mindspore::lite::RET_OK;
using mindspore::schema::PrimitiveType_Eltwise;
@ -249,25 +250,25 @@ int ArithmeticCPUKernel::Execute(const void *input0, const void *input1, void *o
int ret = RET_OK;
if (in_tensors_[0]->data_type() == kNumberTypeFloat32) {
if (is_opt) {
CHECK_NULL_RETURN(arithmetic_opt_run_, RET_ERROR);
CHECK_NULL_RETURN(arithmetic_opt_run_);
ret = arithmetic_opt_run_(reinterpret_cast<const float *>(input0), reinterpret_cast<const float *>(input1),
reinterpret_cast<float *>(output), size, param_);
} else {
CHECK_NULL_RETURN(arithmetic_run_, RET_ERROR);
CHECK_NULL_RETURN(arithmetic_run_);
ret = arithmetic_run_(reinterpret_cast<const float *>(input0), reinterpret_cast<const float *>(input1),
reinterpret_cast<float *>(output), size);
}
} else if (in_tensors_[0]->data_type() == kNumberTypeBool) {
CHECK_NULL_RETURN(arithmetic_run_bool_, RET_ERROR);
CHECK_NULL_RETURN(arithmetic_run_bool_);
ret = arithmetic_run_bool_(reinterpret_cast<const bool *>(input0), reinterpret_cast<const bool *>(input1),
reinterpret_cast<bool *>(output), size);
} else {
if (is_opt) {
CHECK_NULL_RETURN(arithmetic_opt_run_int_, RET_ERROR);
CHECK_NULL_RETURN(arithmetic_opt_run_int_);
ret = arithmetic_opt_run_int_(reinterpret_cast<const int *>(input0), reinterpret_cast<const int *>(input1),
reinterpret_cast<int *>(output), size, param_);
} else {
CHECK_NULL_RETURN(arithmetic_run_int_, RET_ERROR);
CHECK_NULL_RETURN(arithmetic_run_int_);
ret = arithmetic_run_int_(reinterpret_cast<const int *>(input0), reinterpret_cast<const int *>(input1),
reinterpret_cast<int *>(output), size);
}

View File

@ -40,22 +40,6 @@ using mindspore::schema::PrimitiveType_RealDiv;
using mindspore::schema::PrimitiveType_SquaredDifference;
using mindspore::schema::PrimitiveType_SubFusion;
#define CHECK_NULL_RETURN(ptr, errcode) \
do { \
if (ptr == nullptr) { \
MS_LOG(ERROR) << "ptr must not be null."; \
return errcode; \
} \
} while (0);
#define CHECK_NULL_RETURN(ptr, errcode) \
do { \
if (ptr == nullptr) { \
MS_LOG(ERROR) << "ptr must not be null."; \
return errcode; \
} \
} while (0);
namespace mindspore::kernel {
class ArithmeticCPUKernel : public InnerKernel {
typedef int (*ArithmeticRun)(const float *input0, const float *input1, float *output, const int element_size);