From efb7044f91edeb00d9ef04348a9e07d043148473 Mon Sep 17 00:00:00 2001 From: sunsuodong Date: Mon, 2 Aug 2021 15:44:41 +0800 Subject: [PATCH] code review --- .../cpu/nnacl/infer/matmul_infer.c | 3 +++ mindspore/lite/src/common/string_util.cc | 16 ++++++------ .../lite/src/ops/populate/split_populate.cc | 4 +-- .../kernel/arm/base/quant_dtype_cast.cc | 5 +++- .../runtime/kernel/arm/base/softmax_base.cc | 2 ++ .../kernel/arm/base/tensorlist_stack.cc | 12 +++++---- .../kernel/arm/fp16/activation_fp16.cc | 2 ++ .../arm/fp16/arithmetic_compare_fp16.cc | 4 ++- .../kernel/arm/fp16/arithmetic_fp16.cc | 5 +++- .../kernel/arm/fp16/arithmetic_self_fp16.cc | 16 +++++++++--- .../runtime/kernel/arm/fp16/batchnorm_fp16.cc | 6 ++--- .../runtime/kernel/arm/fp16/biasadd_fp16.cc | 12 ++++++--- .../src/runtime/kernel/arm/fp16/cast_fp16.cc | 26 ++++++++++++------- .../runtime/kernel/arm/fp16/concat_fp16.cc | 5 ++++ .../kernel/arm/fp16/convolution_1x1_fp16.cc | 2 ++ .../arm/fp16/convolution_delegate_fp16.cc | 2 ++ .../arm/fp16/convolution_depthwise_fp16.cc | 2 ++ .../convolution_depthwise_slidewindow_fp16.cc | 5 ++++ .../kernel/arm/fp16/convolution_fp16.cc | 2 ++ .../arm/fp16/convolution_winograd_fp16.cc | 2 ++ .../src/runtime/kernel/arm/fp16/crop_fp16.cc | 5 +++- .../arm/fp16/deconvolution_depthwise_fp16.cc | 2 ++ .../kernel/arm/fp16/deconvolution_fp16.cc | 4 +++ .../arm/fp16/deconvolution_winograd_fp16.cc | 2 ++ .../kernel/arm/fp16/fullconnection_fp16.cc | 3 +++ .../kernel/arm/fp16/fused_batchnorm_fp16.cc | 8 +++++- .../runtime/kernel/arm/fp16/gather_fp16.cc | 16 +++++++++--- .../kernel/arm/fp16/group_convolution_fp16.cc | 2 ++ .../src/runtime/kernel/arm/fp16/gru_fp16.cc | 13 +++++++++- .../kernel/arm/fp16/instance_norm_fp16.cc | 8 ++++++ .../src/runtime/kernel/arm/fp16/lstm_fp16.cc | 15 +++++++++-- .../kernel/arm/fp16/matmul_base_fp16.cc | 8 ++++++ .../runtime/kernel/arm/fp16/matmul_fp16.cc | 3 +++ .../src/runtime/kernel/arm/fp16/pad_fp16.cc | 3 ++- .../runtime/kernel/arm/fp16/pooling_fp16.cc | 3 ++- .../src/runtime/kernel/arm/fp16/power_fp16.cc | 13 +++++----- .../kernel/arm/fp16/quant_dtype_cast_fp16.cc | 16 ++++-------- .../runtime/kernel/arm/fp16/reduce_fp16.cc | 9 ++++--- .../src/runtime/kernel/arm/fp16/scale_fp16.cc | 10 ++++--- .../runtime/kernel/arm/fp16/softmax_fp16.cc | 14 +++++----- .../src/runtime/kernel/arm/fp16/stack_fp16.cc | 6 ++++- 41 files changed, 215 insertions(+), 81 deletions(-) diff --git a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/infer/matmul_infer.c b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/infer/matmul_infer.c index 31f169c242d..3589ab496be 100644 --- a/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/infer/matmul_infer.c +++ b/mindspore/ccsrc/backend/kernel_compiler/cpu/nnacl/infer/matmul_infer.c @@ -19,6 +19,9 @@ int CheckMatmulInputShape(int *a_shape, size_t a_shape_size, int *b_shape, size_t b_shape_size, MatMulParameter *param) { + if (a_shape_size < 2 || b_shape_size < 2) { + return NNACL_PARAM_INVALID; + } for (size_t i = 0; i < (a_shape_size - 2) && i < (b_shape_size - 2); ++i) { if (a_shape[i] != b_shape[i]) { return NNACL_INPUT_TENSOR_ERROR; diff --git a/mindspore/lite/src/common/string_util.cc b/mindspore/lite/src/common/string_util.cc index 23a781d2d77..b113bfe49a6 100644 --- a/mindspore/lite/src/common/string_util.cc +++ b/mindspore/lite/src/common/string_util.cc @@ -52,10 +52,10 @@ int WriteStringsToTensor(Tensor *tensor, const std::vector &string_b MS_LOG(ERROR) << "tensor is nullptr."; return RET_ERROR; } - int32_t num = string_buffer.size(); + size_t num = string_buffer.size(); std::vector offset(num + 1); offset[0] = 4 * (num + 2); - for (int i = 0; i < num; i++) { + for (size_t i = 0; i < num; i++) { offset[i + 1] = offset[i] + string_buffer[i].len; } std::vector shape = {offset[num]}; @@ -71,10 +71,10 @@ int WriteStringsToTensor(Tensor *tensor, const std::vector &string_b char *string_data = reinterpret_cast(data); string_info[0] = num; - for (int i = 0; i <= num; i++) { + for (size_t i = 0; i <= num; i++) { string_info[i + 1] = offset[i]; } - for (int i = 0; i < num; i++) { + for (size_t i = 0; i < num; i++) { memcpy(string_data + offset[i], string_buffer[i].data, string_buffer[i].len); } return RET_OK; @@ -85,11 +85,11 @@ int WriteSeperatedStringsToTensor(Tensor *tensor, const std::vector offset(num + 1); offset[0] = 4 * (num + 2); std::vector len(num); - for (int i = 0; i < num; i++) { + for (size_t i = 0; i < num; i++) { len[i] = 0; for (int j = 0; j < static_cast(string_buffer[i].size()); j++) { len[i] += string_buffer[i][j].len; @@ -109,10 +109,10 @@ int WriteSeperatedStringsToTensor(Tensor *tensor, const std::vector(data); string_info[0] = num; - for (int i = 0; i <= num; i++) { + for (size_t i = 0; i <= num; i++) { string_info[i + 1] = offset[i]; } - for (int i = 0; i < num; i++) { + for (size_t i = 0; i < num; i++) { auto *dst = string_data + offset[i]; for (auto string_part : string_buffer[i]) { memcpy(dst, string_part.data, string_part.len); diff --git a/mindspore/lite/src/ops/populate/split_populate.cc b/mindspore/lite/src/ops/populate/split_populate.cc index b2f9b9603c3..5d81afa4363 100644 --- a/mindspore/lite/src/ops/populate/split_populate.cc +++ b/mindspore/lite/src/ops/populate/split_populate.cc @@ -37,8 +37,8 @@ OpParameter *PopulateSplitParameter(const void *prim) { param->op_parameter_.type_ = primitive->value_type(); param->num_split_ = value->output_num(); - if (param->num_split_ > std::numeric_limits::max() / static_cast(sizeof(int)) || param->num_split_ < 0) { - MS_LOG(ERROR) << "The value of param->num_split_ is too big"; + if (param->num_split_ > std::numeric_limits::max() / static_cast(sizeof(int)) || param->num_split_ <= 0) { + MS_LOG(ERROR) << "The value of param->num_split_ is not correct"; free(param); return nullptr; } diff --git a/mindspore/lite/src/runtime/kernel/arm/base/quant_dtype_cast.cc b/mindspore/lite/src/runtime/kernel/arm/base/quant_dtype_cast.cc index cb8cfdb648a..29c0f1066f3 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/quant_dtype_cast.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/quant_dtype_cast.cc @@ -83,7 +83,7 @@ int QuantDTypeCastCPUKernel::QuantDTypeCast(int task_id) { (!out_tensors_.front()->quant_params().empty() && out_tensors_.front()->quant_params().front().inited) ? out_tensors_.front()->quant_params().front() : in_tensors_.front()->quant_params().front(); - int ret = RET_OK; + int ret = RET_ERROR; if (src_dtype == TypeId::kNumberTypeInt8 && dst_dtype == TypeId::kNumberTypeFloat32) { ret = DoDequantizeInt8ToFp32(int8_ptr_ + thread_offset, float32_ptr_ + thread_offset, quant_arg.scale, quant_arg.zeroPoint, num_unit_thread); @@ -195,6 +195,9 @@ int QuantDTypeCastCPUKernel::Run() { if (float32_ptr_ == nullptr || uint8_ptr_ == nullptr) { return RET_NULL_PTR; } + } else { + MS_LOG(ERROR) << "Not support"; + return RET_ERROR; } auto ret = ParallelLaunch(this->ms_context_, QuantDTypeCastRun, this, thread_n_num_); diff --git a/mindspore/lite/src/runtime/kernel/arm/base/softmax_base.cc b/mindspore/lite/src/runtime/kernel/arm/base/softmax_base.cc index a24dbf76dcf..49e9e9e4d52 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/softmax_base.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/softmax_base.cc @@ -29,6 +29,8 @@ using mindspore::lite::RET_OK; namespace mindspore::kernel { int SoftmaxBaseCPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 1); + CHECK_LESS_RETURN(out_tensors_.size(), 1); if (softmax_param_ == nullptr) { MS_LOG(ERROR) << "SoftmaxParameter nullptr"; return RET_NULL_PTR; diff --git a/mindspore/lite/src/runtime/kernel/arm/base/tensorlist_stack.cc b/mindspore/lite/src/runtime/kernel/arm/base/tensorlist_stack.cc index b05be63e1db..be8b2ed404b 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/tensorlist_stack.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/tensorlist_stack.cc @@ -73,6 +73,7 @@ int TensorListStackCPUKernel::MergeElementShape() { return RET_ERROR; } auto ele_shape_data = reinterpret_cast(in_tensors_[1]->data_c()); + MS_ASSERT(ele_shape_data != nullptr); output_shape_.clear(); for (int i = 0; i < in_tensors_[1]->ElementsNum(); ++i) { output_shape_.push_back(ele_shape_data[i]); @@ -140,8 +141,8 @@ int TensorListStackCPUKernel::Run() { MS_LOG(ERROR) << "CheckParam failed!"; return RET_ERROR; } - dtype_ = input0_->tensors_data_type(); - if (output0_->ElementsNum() == 0) { + size_t out_ele_num = output0_->ElementsNum(); + if (out_ele_num == 0) { return RET_OK; } auto ret = MergeElementShape(); @@ -150,14 +151,15 @@ int TensorListStackCPUKernel::Run() { return RET_ERROR; } size_t in_ele_num = num_element_ * TypeUnknownSize; - size_t out_ele_num = output0_->ElementsNum(); if (in_ele_num != out_ele_num) { MS_LOG(ERROR) << "out_tensors_[0]->ElementsNum():" << out_ele_num << "must be equal to in_ele_num:" << in_ele_num; return RET_ERROR; } - auto out_data = reinterpret_cast(output0_->MutableData()); - auto unknown_type_offset = TypeUnknownSize * lite::DataTypeSize(dtype_); + auto out_data = reinterpret_cast(output0_->data_c()); MS_ASSERT(out_data != nullptr); + dtype_ = input0_->tensors_data_type(); + auto unknown_type_offset = TypeUnknownSize * lite::DataTypeSize(dtype_); + for (int i = 0; i < num_element_; ++i) { auto in_ptr = input0_->GetTensor(i); if (in_ptr == nullptr) { diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/activation_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/activation_fp16.cc index 712f936fd4b..9460cd26043 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/activation_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/activation_fp16.cc @@ -35,6 +35,8 @@ using mindspore::schema::PrimitiveType_Activation; namespace mindspore::kernel { int ActivationFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 1); + CHECK_LESS_RETURN(out_tensors_.size(), 1); if (type_ != schema::ActivationType_RELU && type_ != schema::ActivationType_RELU6 && type_ != schema::ActivationType_LEAKY_RELU && type_ != schema::ActivationType_SIGMOID && type_ != schema::ActivationType_TANH && type_ != schema::ActivationType_HSWISH && diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_compare_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_compare_fp16.cc index 72a4f7fa082..d75177920e3 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_compare_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_compare_fp16.cc @@ -66,6 +66,8 @@ ArithmeticCompareOptFuncFp16 GetOptimizedArithmeticCompareFun(int primitive_type } int ArithmeticCompareFP16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); if (!InferShapeDone()) { return RET_OK; } @@ -162,7 +164,7 @@ int ArithmeticCompareFP16CPUKernel::Run() { input0_fp16_ = ConvertInputFp32toFp16(in_tensors_.at(0), static_cast(this->ms_context_)); input1_fp16_ = ConvertInputFp32toFp16(in_tensors_.at(1), static_cast(this->ms_context_)); - output_fp16_ = reinterpret_cast(output_tensor->MutableData()); + output_fp16_ = reinterpret_cast(output_tensor->data_c()); if (input0_fp16_ == nullptr || input1_fp16_ == nullptr || output_fp16_ == nullptr) { MS_LOG(ERROR) << "Memory allocation failed"; FreeTmpBuffer(); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_fp16.cc index a81bbff7638..8cc9c4c1f64 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_fp16.cc @@ -183,8 +183,11 @@ int ArithmeticFP16CPUKernel::Run() { return RET_ERROR; } auto ret = ParallelLaunch(this->ms_context_, ArithmeticsRun, this, op_parameter_->thread_num_); + if (ret != RET_OK) { + MS_LOG(ERROR) << "ArithmeticsRun failed, ret : " << ret; + } if (out_tensors_.at(0)->data_type() == kNumberTypeFloat32) { - Float16ToFloat32(static_cast(output_ptr_), reinterpret_cast(output_tensor->MutableData()), + Float16ToFloat32(static_cast(output_ptr_), reinterpret_cast(output_tensor->data_c()), output_tensor->ElementsNum()); } FreeFp16Buffer(); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_self_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_self_fp16.cc index bcba2c95056..1f75a664e0c 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_self_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_self_fp16.cc @@ -76,18 +76,28 @@ int ArithmeticSelfFp16CPUKernel::DoExecute(int task_id) { int ArithmeticSelfFp16CPUKernel::Run() { auto input_tensor = in_tensors_.at(0); auto output_tensor = out_tensors_.at(0); - + MS_ASSERT(input_tensor != nullptr); + MS_ASSERT(output_tensor != nullptr); if (input_tensor->data_type() == kNumberTypeFloat32) { - input_fp16_ptr_ = ConvertInputFp32toFp16(input_tensor, static_cast(this->ms_context_)); + input_fp16_ptr_ = ConvertInputFp32toFp16(input_tensor, static_cast(ms_context_)); + if (input_fp16_ptr_ == nullptr) { + return RET_ERROR; + } } else { input_fp16_ptr_ = reinterpret_cast(input_tensor->data_c()); + MS_ASSERT(input_fp16_ptr_ != nullptr); } output_fp16_ptr_ = reinterpret_cast(output_tensor->data_c()); + MS_ASSERT(output_fp16_ptr_ != nullptr); - auto ret = ParallelLaunch(this->ms_context_, ArithmeticSelfRun, this, op_parameter_->thread_num_); + auto ret = ParallelLaunch(ms_context_, ArithmeticSelfRun, this, op_parameter_->thread_num_); if (ret != RET_OK) { MS_LOG(ERROR) << "ArithmeticSelfRun error error_code[" << ret << "]"; } + if (input_tensor->data_type() == kNumberTypeFloat32) { + ms_context_->allocator->Free(input_fp16_ptr_); + input_fp16_ptr_ = nullptr; + } return ret; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/batchnorm_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/batchnorm_fp16.cc index 35f526afe38..98d6fd5312c 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/batchnorm_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/batchnorm_fp16.cc @@ -38,9 +38,9 @@ int BatchnormFp16CPUKernel::InitConstTensor() { FreeMeanAndVariance(); return RET_ERROR; } - Float32ToFloat16(reinterpret_cast(mean_fp32->MutableData()), reinterpret_cast(mean_), + Float32ToFloat16(reinterpret_cast(mean_fp32->data_c()), reinterpret_cast(mean_), mean_fp32->ElementsNum()); - Float32ToFloat16(reinterpret_cast(variance_fp32->MutableData()), reinterpret_cast(variance_), + Float32ToFloat16(reinterpret_cast(variance_fp32->data_c()), reinterpret_cast(variance_), variance_fp32->ElementsNum()); } else { auto ret = BatchnormCPUKernel::InitConstTensor(); @@ -68,7 +68,7 @@ int BatchnormFp16CPUKernel::Run() { MS_LOG(ERROR) << "BatchnormRun error error_code[" << ret << "]"; } if (is_output_fp32_) { - Float16ToFloat32(output_, reinterpret_cast(output_tensor->MutableData()), output_tensor->ElementsNum()); + Float16ToFloat32(output_, reinterpret_cast(output_tensor->data_c()), output_tensor->ElementsNum()); } FreeInputAndOutput(); return ret; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/biasadd_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/biasadd_fp16.cc index a8da79ef223..58cb9aaa3f2 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/biasadd_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/biasadd_fp16.cc @@ -58,8 +58,10 @@ int BiasAddCPUFp16Kernel::Run() { is_repack_ = false; } } - auto in = reinterpret_cast(in_tensors_.at(0)->MutableData()); - auto out = reinterpret_cast(out_tensors_.at(0)->MutableData()); + auto in = reinterpret_cast(in_tensors_.at(0)->data_c()); + auto out = reinterpret_cast(out_tensors_.at(0)->data_c()); + MS_ASSERT(in != nullptr); + MS_ASSERT(out != nullptr); size_t data_size = in_tensors_.at(0)->ElementsNum(); MS_ASSERT(ms_context_->allocator != nullptr); auto tile_in = reinterpret_cast(ms_context_->allocator->Malloc(data_size * sizeof(float16_t))); @@ -93,7 +95,7 @@ int BiasAddCPUFp16Kernel::GetBiasData() { return RET_NULL_PTR; } } - auto bias = reinterpret_cast(bias_tensor_->MutableData()); + auto bias = reinterpret_cast(bias_tensor_->data_c()); if (bias == nullptr) { MS_LOG(ERROR) << "bias is nullptr!"; return RET_NULL_PTR; @@ -102,7 +104,7 @@ int BiasAddCPUFp16Kernel::GetBiasData() { bias_data_[i] = static_cast(bias[i]); } } else { - bias_data_ = reinterpret_cast(bias_tensor_->MutableData()); + bias_data_ = reinterpret_cast(bias_tensor_->data_c()); if (bias_data_ == nullptr) { MS_LOG(ERROR) << "bias_data_ is nullptr"; return RET_NULL_PTR; @@ -112,6 +114,8 @@ int BiasAddCPUFp16Kernel::GetBiasData() { } int BiasAddCPUFp16Kernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); bias_tensor_ = in_tensors_.at(1); MS_ASSERT(bias_tensor_ != nullptr); if (!InferShapeDone()) { diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/cast_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/cast_fp16.cc index 0dc3170de08..a17f381f40e 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/cast_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/cast_fp16.cc @@ -37,6 +37,8 @@ int CastFp16Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) { } // namespace int CastFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 1); + CHECK_LESS_RETURN(out_tensors_.size(), 1); if (!InferShapeDone()) { return RET_OK; } @@ -55,6 +57,9 @@ int CastFp16CPUKernel::ReSize() { int CastFp16CPUKernel::DoCast(int thread_id) { auto input = in_tensors_.at(0); + MS_ASSERT(input != nullptr); + auto input_data = input->data_c(); + MS_ASSERT(input_data != nullptr); int data_num = MSMIN(stride_, data_num_ - thread_id * stride_); if (data_num <= 0) { return RET_OK; @@ -63,26 +68,27 @@ int CastFp16CPUKernel::DoCast(int thread_id) { auto offset = thread_id * stride_; auto output = out_tensors_.at(0); auto output_data = output->data_c(); + MS_ASSERT(output_data != nullptr); auto input_data_type = input->data_type(); auto output_data_type = output->data_type(); if (input_data_type == kNumberTypeFloat16) { switch (output_data_type) { case kNumberTypeInt64: - Float16ToInt64(reinterpret_cast(input->data_c()) + offset, + Float16ToInt64(reinterpret_cast(input_data) + offset, reinterpret_cast(output_data) + offset, data_num); break; case kNumberTypeInt32: - Float16ToInt32(reinterpret_cast(input->data_c()) + offset, + Float16ToInt32(reinterpret_cast(input_data) + offset, reinterpret_cast(output_data) + offset, data_num); break; case kNumberTypeFloat32: - Float16ToFloat32(reinterpret_cast(input->MutableData()) + offset, + Float16ToFloat32(reinterpret_cast(input_data) + offset, reinterpret_cast(output_data) + offset, data_num); break; case kNumberTypeFloat16: - memcpy(reinterpret_cast(output_data) + offset, - reinterpret_cast(input->data_c()) + offset, data_num * sizeof(float16_t)); + memcpy(reinterpret_cast(output_data) + offset, reinterpret_cast(input_data) + offset, + data_num * sizeof(float16_t)); break; default: MS_LOG(ERROR) << "Unsupported output data type " << output_data_type; @@ -91,19 +97,19 @@ int CastFp16CPUKernel::DoCast(int thread_id) { } else if (input_data_type == kNumberTypeFloat32) { switch (output_data_type) { case kNumberTypeInt64: - Float32ToInt64(reinterpret_cast(input->data_c()) + offset, + Float32ToInt64(reinterpret_cast(input_data) + offset, reinterpret_cast(output_data) + offset, data_num); break; case kNumberTypeInt32: - Float32ToInt32(reinterpret_cast(input->data_c()) + offset, + Float32ToInt32(reinterpret_cast(input_data) + offset, reinterpret_cast(output_data) + offset, data_num); break; case kNumberTypeFloat32: - memcpy(reinterpret_cast(output_data) + offset, reinterpret_cast(input->data_c()) + offset, + memcpy(reinterpret_cast(output_data) + offset, reinterpret_cast(input_data) + offset, data_num * sizeof(float)); break; case kNumberTypeFloat16: - Float32ToFloat16(reinterpret_cast(input->MutableData()) + offset, + Float32ToFloat16(reinterpret_cast(input_data) + offset, reinterpret_cast(output_data) + offset, data_num); break; default: @@ -113,7 +119,7 @@ int CastFp16CPUKernel::DoCast(int thread_id) { } else if (input_data_type == kNumberTypeInt32) { switch (output_data_type) { case kNumberTypeFloat32: - Int32ToFloat32(static_cast(input->data_c()) + offset, static_cast(output_data) + offset, + Int32ToFloat32(static_cast(input_data) + offset, static_cast(output_data) + offset, data_num); break; default: diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/concat_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/concat_fp16.cc index 00d9bb92c7d..355ad85f5d6 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/concat_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/concat_fp16.cc @@ -24,6 +24,8 @@ using mindspore::schema::PrimitiveType_Concat; namespace mindspore::kernel { int ConcatFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 1); + CHECK_LESS_RETURN(out_tensors_.size(), 1); if (!InferShapeDone()) { return RET_OK; } @@ -98,9 +100,11 @@ int ConcatFp16CPUKernel::Run() { const auto in_tensor = in_tensors_.at(i); if (in_tensor->data_type() == kNumberTypeFloat || in_tensor->data_type() == kNumberTypeFloat32) { auto in_tensor_data = reinterpret_cast(in_tensor->data_c()); + MS_ASSERT(in_tensor_data != nullptr); Float32ToFloat16(in_tensor_data, fp16_inputs_[i], in_tensor->ElementsNum()); } else { fp16_inputs_[i] = reinterpret_cast(in_tensor->data_c()); + MS_ASSERT(fp16_inputs_[i] != nullptr); } shapes.push_back(in_tensors_[i]->shape()); @@ -111,6 +115,7 @@ int ConcatFp16CPUKernel::Run() { auto output_addr = out_tensors_.at(0)->MutableData(); if (out_tensors_.at(0)->data_type() == kNumberTypeFloat16) { fp16_output_ = reinterpret_cast(out_tensors_.at(0)->data_c()); + MS_ASSERT(fp16_output_ != nullptr); } int dtype_len = in_tensors_.at(0)->data_type() == kNumberTypeInt32 ? sizeof(int32_t) : sizeof(float16_t); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_1x1_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_1x1_fp16.cc index f3257b424a7..637d6d8983c 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_1x1_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_1x1_fp16.cc @@ -123,6 +123,8 @@ int Convolution1x1FP16CPUKernel::InitWeightBias() { } int Convolution1x1FP16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); #ifdef ENABLE_ARM64 row_tile_ = C12NUM; col_tile_ = C16NUM; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_delegate_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_delegate_fp16.cc index e6e1dfed963..c10e2d886f0 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_delegate_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_delegate_fp16.cc @@ -65,6 +65,8 @@ void *ConvolutionDelegateFP16CPUKernel::CopyData(lite::Tensor *tensor) { } int ConvolutionDelegateFP16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); if (!InferShapeDone()) { origin_weight_ = CopyData(in_tensors_.at(kWeightIndex)); need_free_ = need_free_ | WEIGHT_NEED_FREE; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_depthwise_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_depthwise_fp16.cc index af240421dee..ca1e46955f9 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_depthwise_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_depthwise_fp16.cc @@ -64,6 +64,8 @@ int ConvolutionDepthwiseFp16CPUKernel::InitWeightBias() { } int ConvolutionDepthwiseFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); auto ret = InitWeightBias(); if (ret != 0) { MS_LOG(ERROR) << "Convolution depthwise fp16 InitWeightBias failed."; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_depthwise_slidewindow_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_depthwise_slidewindow_fp16.cc index dcdcc930b6b..7d3a0769b3e 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_depthwise_slidewindow_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_depthwise_slidewindow_fp16.cc @@ -51,6 +51,7 @@ int ConvolutionDepthwiseSWFp16CPUKernel::InitPackedInputOutput() { if (packed_output_ == nullptr) { MS_LOG(ERROR) << "Malloc buffer failed."; ms_context_->allocator->Free(packed_input_); + packed_input_ = nullptr; return RET_ERROR; } } @@ -94,6 +95,8 @@ int ConvolutionDepthwiseSWFp16CPUKernel::InitWeightBias() { } // namespace mindspore::kernel int ConvolutionDepthwiseSWFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); sliding_ = new (std::nothrow) SlidingWindowParam; if (sliding_ == nullptr) { MS_LOG(ERROR) << "new sliding window param failed."; @@ -151,6 +154,7 @@ int ConvolutionDepthwiseSWFp16CPUKernel::Run() { MS_ASSERT(output_ptr != nullptr); if (input_ptr == nullptr || output_ptr == nullptr) { MS_LOG(ERROR) << "Convolution depthwise Fp16 get null tensor data!"; + FreePackedInputOutput(); return RET_ERROR; } @@ -166,6 +170,7 @@ int ConvolutionDepthwiseSWFp16CPUKernel::Run() { ret = InitWeightBias(); if (ret != 0) { MS_LOG(ERROR) << "Convolution depthwise fp16 repack weight failure"; + FreePackedInputOutput(); return RET_ERROR; } is_repack_ = false; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_fp16.cc index e21fca572a1..79a18ca5c97 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_fp16.cc @@ -85,6 +85,8 @@ int ConvolutionFP16CPUKernel::InitTmpBuffer() { } int ConvolutionFP16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); #ifdef ENABLE_ARM64 row_tile_ = C16NUM; #else diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_winograd_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_winograd_fp16.cc index d213679f02c..c7ed1110aa3 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_winograd_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/convolution_winograd_fp16.cc @@ -143,6 +143,8 @@ int ConvolutionWinogradFP16CPUKernel::ConfigInputOutput() { } int ConvolutionWinogradFP16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); col_tile_ = C8NUM; #ifdef ENABLE_ARM64 row_tile_ = C16NUM; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/crop_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/crop_fp16.cc index 7cce484401a..8193a2e667b 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/crop_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/crop_fp16.cc @@ -24,6 +24,8 @@ using mindspore::schema::PrimitiveType_Crop; namespace mindspore::kernel { int CropFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 1); + CHECK_LESS_RETURN(out_tensors_.size(), 1); if (!InferShapeDone()) { return RET_OK; } @@ -48,7 +50,8 @@ static int CropFp16Run(void *cdata, int task_id, float lhs_scale, float rhs_scal int CropFp16CPUKernel::Run() { auto input_tensor = in_tensors_.at(0); auto output_tensor = out_tensors_.at(0); - + MS_ASSERT(input_tensor != nullptr); + MS_ASSERT(output_tensor != nullptr); input_ptr_ = reinterpret_cast(input_tensor->data_c()); output_ptr_ = reinterpret_cast(output_tensor->data_c()); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_depthwise_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_depthwise_fp16.cc index 79459ad8b74..2dab787eacf 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_depthwise_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_depthwise_fp16.cc @@ -102,6 +102,8 @@ int DeconvolutionDepthwiseFp16CPUKernel::InitWeightBias() { } int DeconvolutionDepthwiseFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); sliding_ = new (std::nothrow) SlidingWindowParam; if (sliding_ == nullptr) { MS_LOG(ERROR) << "new SlidingWindowParam fail!"; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_fp16.cc index 87093a8605d..2d831059790 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_fp16.cc @@ -183,6 +183,8 @@ int DeConvolutionFp16CPUKernel::DoDeconv(int task_id) { } int DeConvolutionFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); matmul_param_ = new (std::nothrow) MatMulParameter(); if (matmul_param_ == nullptr) { MS_LOG(ERROR) << "Memory allocation failed"; @@ -225,6 +227,8 @@ int DeConvolutionFp16CPUKernel::Run() { error_code = ParallelLaunch(this->ms_context_, DeConvFp16Run, this, thread_count_); if (error_code != RET_OK) { MS_LOG(ERROR) << "deconv fp16 run error! error_code[" << error_code << "]"; + FreeRunBuf(); + return error_code; } } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_winograd_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_winograd_fp16.cc index 13fc716af11..3788a316c12 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_winograd_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_winograd_fp16.cc @@ -358,6 +358,8 @@ int DeConvWinogradFp16CPUKernel::ReSize() { } int DeConvWinogradFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); deconv_param_ = new (std::nothrow) DeConvParam(); if (deconv_param_ == nullptr) { MS_LOG(ERROR) << "Memory allocation failed"; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/fullconnection_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/fullconnection_fp16.cc index 47da33433ef..4cfa3edd456 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/fullconnection_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/fullconnection_fp16.cc @@ -18,6 +18,7 @@ #include "src/kernel_registry.h" using mindspore::lite::KernelRegistrar; +using mindspore::lite::RET_ERROR; using mindspore::lite::RET_OK; using mindspore::schema::PrimitiveType_FullConnection; @@ -41,6 +42,8 @@ int FullconnectionFP16CPUKernel::ReSize() { } int FullconnectionFP16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); #ifdef ENABLE_ARM64 row_tile_ = C16NUM; #else diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/fused_batchnorm_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/fused_batchnorm_fp16.cc index 9e544cd6e30..6f4b7232782 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/fused_batchnorm_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/fused_batchnorm_fp16.cc @@ -86,6 +86,11 @@ int FusedBatchnormFp16CPUKernel::DoExecute(int task_id) { ms_context_->allocator->Free(output_fp16); return RET_ERROR; } + MS_ASSERT(input->data_c() != nullptr); + MS_ASSERT(scale->data_c() != nullptr); + MS_ASSERT(offset->data_c() != nullptr); + MS_ASSERT(mean->data_c() != nullptr); + MS_ASSERT(variance->data_c() != nullptr); Float32ToFloat16(reinterpret_cast(input->data_c()), reinterpret_cast(input_fp16), input->ElementsNum()); Float32ToFloat16(reinterpret_cast(scale->data_c()), reinterpret_cast(scale_fp16), @@ -116,7 +121,8 @@ int FusedBatchnormFp16CPUKernel::DoExecute(int task_id) { ms_context_->allocator->Free(output_fp16); return RET_OK; } - + MS_ASSERT(in_tensors_.at(0)->data_c() != nullptr); + MS_ASSERT(out_tensors_.at(0)->data_c() != nullptr); if (IsTrain() && IsTrainable() && in_tensors_.size() >= kMaxInIdx) { CalcMeanVar(static_cast(in_tensors_.at(0)->data_c()), static_cast(in_tensors_.at(kInScaleIdx)->data_c()), diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/gather_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/gather_fp16.cc index e9cbb9d2dd5..1e60c294a6b 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/gather_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/gather_fp16.cc @@ -40,13 +40,17 @@ GatherFp16CPUKernel::~GatherFp16CPUKernel() { } int GatherFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 3); + CHECK_LESS_RETURN(out_tensors_.size(), 1); auto input_tensor = in_tensors_.at(0); + MS_ASSERT(input_tensor != nullptr); if (input_tensor->data_type() == kNumberTypeFloat32 && input_tensor->data_c() != nullptr) { const_input_ = true; input_data_ = reinterpret_cast(ms_context_->allocator->Malloc(input_tensor->ElementsNum() * sizeof(float16_t))); Float32ToFloat16(reinterpret_cast(input_tensor->data_c()), input_data_, input_tensor->ElementsNum()); } + MS_ASSERT(in_tensors_.at(kSecondInput)->data_c() != nullptr); (reinterpret_cast(op_parameter_))->axis_ = *(reinterpret_cast(in_tensors_.at(kSecondInput)->data_c())); if (!InferShapeDone()) { @@ -118,6 +122,8 @@ int GatherFp16CPUKernel::DoGather(int task_id) { return RET_ERROR; } int8_t *int8_out = reinterpret_cast(out_tensor->data_c()); + MS_ASSERT(int8_in != nullptr); + MS_ASSERT(int8_out != nullptr); int data_size = lite::DataTypeSize(kNumberTypeFloat16); int8_in += thread_stride * limit * inner_size * data_size; int8_out += thread_stride * indices_element_size * inner_size * data_size; @@ -156,6 +162,7 @@ int GatherFp16CPUKernel::Run() { } if (!const_input_) { auto input_tensor = in_tensors_.at(0); + MS_ASSERT(input_tensor->data_c() != nullptr); if (input_tensor->data_type() == kNumberTypeFloat32) { input_data_ = reinterpret_cast(ms_context_->allocator->Malloc(input_tensor->ElementsNum() * sizeof(float16_t))); @@ -176,6 +183,7 @@ int GatherFp16CPUKernel::Run() { } int GatherFp16CPUKernel::AssignIndicesData(bool isIndicesInt32, int indices_num, lite::Tensor *indices_tensor) { + MS_ASSERT(indices_tensor->data_c() != nullptr); if (!isIndicesInt32) { if (indices_num >= std::numeric_limits::max() / static_cast(sizeof(int))) { MS_LOG(ERROR) << "Input indices_num is invalid, indices_num: " << indices_num; @@ -188,18 +196,20 @@ int GatherFp16CPUKernel::AssignIndicesData(bool isIndicesInt32, int indices_num, } if (indices_tensor->data_type() == kNumberTypeInt64) { for (int i = 0; i < indices_num; i++) { - indices_data_[i] = reinterpret_cast(indices_tensor->MutableData())[i]; + indices_data_[i] = reinterpret_cast(indices_tensor->data_c())[i]; } } else if (indices_tensor->data_type() == kNumberTypeFloat16) { for (int i = 0; i < indices_num; i++) { - indices_data_[i] = reinterpret_cast(indices_tensor->MutableData())[i]; + indices_data_[i] = reinterpret_cast(indices_tensor->data_c())[i]; } } else { MS_LOG(ERROR) << "The data type of indices tensor is wrong"; + ms_context_->allocator->Free(indices_data_); + indices_data_ = nullptr; return RET_ERROR; } } else { - indices_data_ = reinterpret_cast(indices_tensor->MutableData()); + indices_data_ = reinterpret_cast(indices_tensor->data_c()); } return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/group_convolution_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/group_convolution_fp16.cc index 80c3751f1b0..9a968988640 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/group_convolution_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/group_convolution_fp16.cc @@ -83,6 +83,8 @@ int GroupConvolutionFP16CPUKernel::PostConcat(int group_id) { } int GroupConvolutionFP16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 1); + CHECK_LESS_RETURN(out_tensors_.size(), 1); if (group_conv_creator_ == nullptr) { return lite::RET_ERROR; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/gru_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/gru_fp16.cc index 9aa8e26a7d0..7be43799813 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/gru_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/gru_fp16.cc @@ -89,6 +89,7 @@ int GruFp16CPUKernel::InitInputWeightBias() { // result -- row: seq_len * batch; col: hidden_size auto weight_g = in_tensors_.at(1); MS_ASSERT(weight_g != nullptr); + MS_ASSERT(weight_g->data_c() != nullptr); weight_g_ptr_ = reinterpret_cast( malloc(weight_batch_ * gru_param_->input_col_align_ * gru_param_->input_size_ * sizeof(float16_t))); if (weight_g_ptr_ == nullptr) { @@ -109,6 +110,7 @@ int GruFp16CPUKernel::InitInputWeightBias() { // input bias auto bias = in_tensors_.at(3); MS_ASSERT(bias != nullptr); + MS_ASSERT(bias->data_c() != nullptr); input_bias_ = reinterpret_cast(malloc(weight_batch_ * gru_param_->input_col_align_ * sizeof(float16_t))); if (input_bias_ == nullptr) { MS_LOG(ERROR) << "GruFp16CPUKernel malloc input_bias_ error."; @@ -135,6 +137,7 @@ int GruFp16CPUKernel::InitStateWeightBias() { // result -- row: batch; col: hidden_size auto weight_r = in_tensors_.at(2); MS_ASSERT(weight_r != nullptr); + MS_ASSERT(weight_r->data_c() != nullptr); weight_r_ptr_ = reinterpret_cast( malloc(weight_batch_ * gru_param_->state_col_align_ * gru_param_->hidden_size_ * sizeof(float16_t))); if (weight_r_ptr_ == nullptr) { @@ -167,6 +170,7 @@ int GruFp16CPUKernel::InitStateWeightBias() { // state bias auto bias = in_tensors_.at(3); MS_ASSERT(bias != nullptr); + MS_ASSERT(bias->data_c() != nullptr); state_bias_ = reinterpret_cast(malloc(weight_batch_ * gru_param_->state_col_align_ * sizeof(float16_t))); if (state_bias_ == nullptr) { MS_LOG(ERROR) << "GruFp16CPUKernel malloc state_bias_ error."; @@ -189,6 +193,8 @@ int GruFp16CPUKernel::InitStateWeightBias() { } int GruFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 5); + CHECK_LESS_RETURN(out_tensors_.size(), 2); if (!InferShapeDone()) { return RET_OK; } @@ -267,10 +273,14 @@ int GruFp16CPUKernel::Run() { auto output_ptr = reinterpret_cast(output->data_c()); MS_ASSERT(output_ptr); auto output_hidden_state = out_tensors_[1]; + MS_ASSERT(output_hidden_state->data_c() != nullptr); + MS_ASSERT(hidden_state->data_c() != nullptr); memcpy(output_hidden_state->data_c(), hidden_state->data_c(), hidden_state->ElementsNum() * sizeof(float16_t)); int check_seq_len = gru_param_->seq_len_; if (in_tensors_.size() == 6) { - auto seq_len = reinterpret_cast(in_tensors_.at(5)->data_c()); + MS_ASSERT(in_tensors_.at(5) != nullptr); + int *seq_len = reinterpret_cast(in_tensors_.at(5)->data_c()); + MS_ASSERT(seq_len != nullptr); if (!std::equal(seq_len + 1, seq_len + gru_param_->batch_, seq_len)) { MS_LOG(ERROR) << "different batch seq_len is currently not supported"; return RET_ERROR; @@ -281,6 +291,7 @@ int GruFp16CPUKernel::Run() { auto ret = MallocRunBuffer(); if (ret != RET_OK) { MS_LOG(ERROR) << "GruFp16CPUKernel MallocRunBuffer error."; + FreeRunBuffer(); return RET_ERROR; } MS_ASSERT(weight_g_ptr_ != nullptr); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/instance_norm_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/instance_norm_fp16.cc index 9af3129b128..ad4bd8870cc 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/instance_norm_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/instance_norm_fp16.cc @@ -43,7 +43,11 @@ void InstanceNormFp16CPUKernel::FreeTmpBuffer() { } int InstanceNormFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 3); + CHECK_LESS_RETURN(out_tensors_.size(), 1); auto gamma = in_tensors_[1]; + MS_ASSERT(gamma != nullptr); + MS_ASSERT(gamma->data_c() != nullptr); if (gamma->data_type() == kNumberTypeFloat32) { gamma_data_ = reinterpret_cast(malloc(gamma->ElementsNum() * sizeof(float16_t))); if (gamma_data_ == nullptr) { @@ -59,6 +63,8 @@ int InstanceNormFp16CPUKernel::Init() { } auto beta = in_tensors_[2]; + MS_ASSERT(beta != nullptr); + MS_ASSERT(beta->data_c() != nullptr); if (beta->data_type() == kNumberTypeFloat32) { beta_data_ = reinterpret_cast(malloc(beta->ElementsNum() * sizeof(float16_t))); if (beta_data_ == nullptr) { @@ -108,6 +114,8 @@ int InstanceNormFp16Run(void *cdata, int task_id, float lhs_scale, float rhs_sca int InstanceNormFp16CPUKernel::Run() { src_data_ = reinterpret_cast(in_tensors_[0]->data_c()); dst_data_ = reinterpret_cast(out_tensors_[0]->data_c()); + MS_ASSERT(src_data_ != nullptr); + MS_ASSERT(dst_data_ != nullptr); auto ret = ParallelLaunch(this->ms_context_, InstanceNormFp16Run, this, op_parameter_->thread_num_); if (ret != RET_OK) { MS_LOG(ERROR) << "InstanceNormFp16Run error error_code[" << ret << "]"; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/lstm_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/lstm_fp16.cc index 786765f2914..7ccdb26f8b1 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/lstm_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/lstm_fp16.cc @@ -96,6 +96,7 @@ int LstmFp16CPUKernel::InitInputWeightBias() { // result -- row: seq_len * batch; col: hidden_size auto weight_i = in_tensors_.at(1); MS_ASSERT(weight_i != nullptr); + MS_ASSERT(weight_i->data_c() != nullptr); weight_i_ptr_ = reinterpret_cast( malloc(weight_batch_ * lstm_param_->input_col_align_ * lstm_param_->input_size_ * sizeof(float16_t))); if (weight_i_ptr_ == nullptr) { @@ -116,6 +117,7 @@ int LstmFp16CPUKernel::InitInputWeightBias() { // input bias auto bias = in_tensors_.at(3); MS_ASSERT(bias != nullptr); + MS_ASSERT(bias->data_c() != nullptr); input_bias_ = reinterpret_cast(malloc(weight_batch_ * lstm_param_->input_col_align_ * sizeof(float16_t))); if (input_bias_ == nullptr) { @@ -143,6 +145,7 @@ int LstmFp16CPUKernel::InitStateWeightBias() { // result -- row: batch; col: hidden_size auto weight_h = in_tensors_.at(2); MS_ASSERT(weight_h != nullptr); + MS_ASSERT(weight_h->data_c() != nullptr); weight_h_ptr_ = reinterpret_cast( malloc(weight_batch_ * lstm_param_->state_col_align_ * lstm_param_->hidden_size_ * sizeof(float16_t))); if (weight_h_ptr_ == nullptr) { @@ -175,6 +178,7 @@ int LstmFp16CPUKernel::InitStateWeightBias() { // state bias auto bias = in_tensors_.at(3); MS_ASSERT(bias != nullptr); + MS_ASSERT(bias->data_c() != nullptr); state_bias_ = reinterpret_cast(malloc(weight_batch_ * lstm_param_->state_col_align_ * sizeof(float16_t))); if (state_bias_ == nullptr) { @@ -198,6 +202,8 @@ int LstmFp16CPUKernel::InitStateWeightBias() { } int LstmFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 6); + CHECK_LESS_RETURN(out_tensors_.size(), 3); if (!InferShapeDone()) { return RET_OK; } @@ -286,23 +292,28 @@ int LstmFp16CPUKernel::Run() { MS_ASSERT(input != nullptr); auto hidden_state = in_tensors_.at(4); MS_ASSERT(hidden_state != nullptr); + MS_ASSERT(hidden_state->data_c() != nullptr); auto cell_state = in_tensors_.at(5); MS_ASSERT(cell_state != nullptr); + MS_ASSERT(cell_state->data_c() != nullptr); auto output = out_tensors_.at(0); MS_ASSERT(output != nullptr); auto input_ptr = reinterpret_cast(input->data_c()); - MS_ASSERT(input_ptr); + MS_ASSERT(input_ptr != nullptr); auto output_ptr = reinterpret_cast(output->data_c()); - MS_ASSERT(output_ptr); + MS_ASSERT(output_ptr != nullptr); auto output_hidden_state = out_tensors_[1]; + MS_ASSERT(output_hidden_state->data_c() != nullptr); memcpy(output_hidden_state->data_c(), hidden_state->data_c(), hidden_state->ElementsNum() * sizeof(float16_t)); auto output_cell_state = out_tensors_[2]; + MS_ASSERT(output_cell_state->data_c()); memcpy(output_cell_state->data_c(), cell_state->data_c(), cell_state->ElementsNum() * sizeof(float16_t)); auto ret = MallocRunBuffer(); if (ret != RET_OK) { MS_LOG(ERROR) << "LstmFp16CPUKernel MallocRunBuffer error."; + FreeRunBuffer(); return RET_ERROR; } MS_ASSERT(weight_i_ptr_); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/matmul_base_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/matmul_base_fp16.cc index 03aa5338824..256c598b0be 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/matmul_base_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/matmul_base_fp16.cc @@ -232,11 +232,15 @@ void MatmulBaseFP16CPUKernel::InitMatrixB(void *src_ptr, TypeId src_data_type) { } int MatmulBaseFP16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); ResizeParameter(); if (params_->a_const_ == true) { if (RET_OK != InitBufferA()) { return RET_ERROR; } + MS_ASSERT(in_tensors_[0] != nullptr); + MS_ASSERT(in_tensors_[0]->data_c() != nullptr); InitMatrixA(reinterpret_cast(in_tensors_[0]->data_c())); } @@ -244,6 +248,8 @@ int MatmulBaseFP16CPUKernel::Init() { /* copy origin b data, pack in resize * pack after a infershape done */ auto b_tensor = in_tensors_[1]; + MS_ASSERT(b_tensor != nullptr); + MS_ASSERT(b_tensor->data_c() != nullptr); src_b_ = reinterpret_cast(malloc(params_->batch * params_->col_ * params_->deep_ * sizeof(float16_t))); if (src_b_ == nullptr) { MS_LOG(ERROR) << "Matmul fp16 malloc src_b_ failed"; @@ -302,6 +308,7 @@ int MatmulBaseFP16CPUKernel::Run() { if (RET_OK != InitBufferA()) { return RET_ERROR; } + MS_ASSERT(in_tensors_.at(0)->data_c() != nullptr); InitMatrixA(in_tensors_.at(0)->data_c()); } if ((params_->b_const_ == false) || IsRepack()) { @@ -309,6 +316,7 @@ int MatmulBaseFP16CPUKernel::Run() { FreeResizeBufA(); return RET_ERROR; } + MS_ASSERT(in_tensors_.at(1)->data_c() != nullptr); InitMatrixB(in_tensors_.at(1)->data_c(), in_tensors_.at(1)->data_type()); InitBias(); } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/matmul_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/matmul_fp16.cc index c3bb2461107..69583ccfd6e 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/matmul_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/matmul_fp16.cc @@ -19,6 +19,7 @@ #include "src/kernel_registry.h" using mindspore::lite::KernelRegistrar; +using mindspore::lite::RET_ERROR; using mindspore::lite::RET_OK; using mindspore::schema::PrimitiveType_MatMul; @@ -54,6 +55,8 @@ void MatmulFP16CPUKernel::InitBShape() { } int MatmulFP16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); #ifdef ENABLE_ARM64 row_tile_ = C4NUM; #else diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc index c06b46c0c7c..0d09536eac5 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/pad_fp16.cc @@ -84,7 +84,8 @@ int PadFp16CPUKernel::Run() { auto output_tensor = out_tensors_.at(0); input_ = reinterpret_cast(input_tensor->data_c()); output_ = reinterpret_cast(output_tensor->data_c()); - + MS_ASSERT(input_ != nullptr); + MS_ASSERT(output_ != nullptr); int ret = 0; if (pad_param_->pad_mode_ == static_cast(schema::PaddingMode_CONSTANT)) { if (in_tensors_.size() == kPadMaxInputSize) { diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/pooling_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/pooling_fp16.cc index 50c17f0baaf..0ffff245ca8 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/pooling_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/pooling_fp16.cc @@ -88,7 +88,8 @@ int PoolingFp16CPUKernel::Run() { fp16_input_ = reinterpret_cast(input_tensor->data_c()); fp16_output_ = reinterpret_cast(output_tensor->data_c()); - + MS_ASSERT(fp16_input_ != nullptr); + MS_ASSERT(fp16_output_ != nullptr); int error_code = ParallelLaunch(this->ms_context_, PoolingFp16Impl, this, thread_count_); if (error_code != RET_OK) { MS_LOG(ERROR) << "pooling error error_code[" << error_code << "]"; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/power_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/power_fp16.cc index 691afade3c9..ae159e6b9b4 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/power_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/power_fp16.cc @@ -27,7 +27,8 @@ using mindspore::schema::PrimitiveType_PowFusion; namespace mindspore::kernel { int PowerFp16CPUKernel::Init() { - MS_ASSERT(in_tensors_.size() == 2); + CHECK_LESS_RETURN(in_tensors_.size(), 2); + CHECK_LESS_RETURN(out_tensors_.size(), 1); exp_tensor_ = in_tensors_[1]; MS_ASSERT(exp_tensor_ != nullptr); if (exp_tensor_->IsConst()) { @@ -50,7 +51,7 @@ int PowerFp16CPUKernel::GetExpData() { MS_LOG(ERROR) << "exp_data_ is nullptr"; return RET_NULL_PTR; } - auto exp = reinterpret_cast(exp_tensor_->MutableData()); + auto exp = reinterpret_cast(exp_tensor_->data_c()); if (exp == nullptr) { MS_LOG(ERROR) << "exp is nullptr!"; return RET_NULL_PTR; @@ -59,7 +60,7 @@ int PowerFp16CPUKernel::GetExpData() { exp_data_[i] = (float16_t)(exp[i]); } } else { - exp_data_ = reinterpret_cast(exp_tensor_->MutableData()); + exp_data_ = reinterpret_cast(exp_tensor_->data_c()); if (exp_data_ == nullptr) { MS_LOG(ERROR) << "exp_data_ is nullptr"; return RET_NULL_PTR; @@ -95,10 +96,8 @@ int PowerFp16CPUKernel::Run() { } int PowerFp16CPUKernel::RunImpl(int task_id) { - auto x_addr = reinterpret_cast(in_tensors_.at(0)->MutableData()); - MS_ASSERT(x_addr); - auto output_addr = reinterpret_cast(out_tensors_.at(0)->MutableData()); - MS_ASSERT(output_addr); + auto x_addr = reinterpret_cast(in_tensors_.at(0)->data_c()); + auto output_addr = reinterpret_cast(out_tensors_.at(0)->data_c()); auto size = in_tensors_.at(0)->ElementsNum(); int stride = UP_DIV(size, thread_count_); int len = MSMIN(stride, size - stride * task_id); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/quant_dtype_cast_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/quant_dtype_cast_fp16.cc index a912c60e786..1df7d4486ac 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/quant_dtype_cast_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/quant_dtype_cast_fp16.cc @@ -30,14 +30,8 @@ using mindspore::schema::PrimitiveType_QuantDTypeCast; namespace mindspore::kernel { int QuantDTypeCastFp16CPUKernel::Init() { - if (in_tensors_.size() != 1) { - MS_LOG(ERROR) << "inputs number should be 1, but " << in_tensors_.size() << " is given."; - return RET_PARAM_INVALID; - } - if (out_tensors_.size() != 1) { - MS_LOG(ERROR) << "outputs number should be 1, but " << out_tensors_.size() << " is given."; - return RET_PARAM_INVALID; - } + CHECK_LESS_RETURN(in_tensors_.size(), 1); + CHECK_LESS_RETURN(out_tensors_.size(), 1); auto in_tensor = in_tensors_.front(); auto out_tensor = out_tensors_.front(); auto param = reinterpret_cast(op_parameter_); @@ -102,9 +96,9 @@ int QuantDTypeCastFp16CPUKernel::QuantDTypeCast(int task_id) { auto quant_arg = !out_tensors_.front()->quant_params().empty() ? out_tensors_.front()->quant_params().front() : in_tensors_.front()->quant_params().front(); int ret; - MS_ASSERT(float16_ptr_); + MS_ASSERT(float16_ptr_ != nullptr); if (!is_uint8_) { - MS_ASSERT(int8_ptr_); + MS_ASSERT(int8_ptr_ != nullptr); if (int_to_float_) { ret = DoDequantizeInt8ToFp16(int8_ptr_ + thread_offset, float16_ptr_ + thread_offset, quant_arg.scale, quant_arg.zeroPoint, num_unit_thread); @@ -114,7 +108,7 @@ int QuantDTypeCastFp16CPUKernel::QuantDTypeCast(int task_id) { } } else { // uint8 - MS_ASSERT(uint8_ptr_); + MS_ASSERT(uint8_ptr_ != nullptr); if (int_to_float_) { ret = DoDequantizeUInt8ToFp16(uint8_ptr_ + thread_offset, float16_ptr_ + thread_offset, quant_arg.scale, quant_arg.zeroPoint, num_unit_thread); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/reduce_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/reduce_fp16.cc index 5af2c51d44e..9973a53efab 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/reduce_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/reduce_fp16.cc @@ -63,9 +63,8 @@ int ReduceFp16CPUKernel::Init() { } int ReduceFp16CPUKernel::CallReduceUnit(int task_id) { - auto ret = - reducer_(outer_size_, inner_size_, axis_size_, fp16_src_data_, fp16_dst_data_, task_id, op_parameter_->thread_num_); - return ret; + return reducer_(outer_size_, inner_size_, axis_size_, fp16_src_data_, fp16_dst_data_, task_id, + op_parameter_->thread_num_); } static int ReduceFp16Impl(void *cdata, int task_id, float lhs_scale, float rhs_scale) { @@ -86,7 +85,9 @@ int ReduceFp16CPUKernel::Run() { } auto in_tensor = in_tensors_.at(0); - fp16_src_data_ = reinterpret_cast(in_tensor->MutableData()); + MS_ASSERT(in_tensor != nullptr); + fp16_src_data_ = reinterpret_cast(in_tensor->data_c()); + MS_ASSERT(fp16_src_data_ != nullptr); for (size_t i = 0; i < data_buffers_.size(); ++i) { fp16_dst_data_ = data_buffers_.at(i); outer_size_ = outer_sizes_.at(i); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/scale_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/scale_fp16.cc index 139027072a8..be8d4eb0728 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/scale_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/scale_fp16.cc @@ -48,6 +48,7 @@ int ScaleFp16CPUKernel::Init() { MS_LOG(ERROR) << "inputs to Scale operator should be 2 or 3, but " << in_tensors_.size() << " is given."; return RET_ERROR; } + CHECK_LESS_RETURN(out_tensors_.size(), 1); if (!InferShapeDone()) { return RET_OK; @@ -101,9 +102,12 @@ int ScaleFp16Run(void *cdata, int task_id, float lhs_scale, float rhs_scale) { int ScaleFp16CPUKernel::Run() { auto input_tensor = in_tensors_.at(0); auto output_tensor = out_tensors_.at(0); - input_ = reinterpret_cast(input_tensor->MutableData()); - output_ = reinterpret_cast(output_tensor->MutableData()); - + MS_ASSERT(input_tensor != nullptr); + MS_ASSERT(output_tensor != nullptr); + input_ = reinterpret_cast(input_tensor->data_c()); + output_ = reinterpret_cast(output_tensor->data_c()); + MS_ASSERT(input_ != nullptr); + MS_ASSERT(output_ != nullptr); auto ret = InitScaleOffset(); if (ret != RET_OK) { MS_LOG(ERROR) << "Scale fp16 InitScaleOffset failed."; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/softmax_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/softmax_fp16.cc index 640910814f8..abc10c22e02 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/softmax_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/softmax_fp16.cc @@ -78,8 +78,8 @@ int SoftmaxFp16CPUKernel::DoSoftmaxLastAxis(int task_id) { int end = MSMIN(begin + unit, out_plane_size_); int channel = softmax_param_->input_shape_[softmax_param_->axis_]; int offset = begin * channel; - auto input_ptr = reinterpret_cast(in_tensors_.at(kInputIndex)->MutableData()); - auto output_ptr = reinterpret_cast(out_tensors_.at(kOutputIndex)->MutableData()); + auto input_ptr = reinterpret_cast(in_tensors_.at(kInputIndex)->data_c()); + auto output_ptr = reinterpret_cast(out_tensors_.at(kOutputIndex)->data_c()); SoftmaxLastAxisFp16(input_ptr + offset, output_ptr + offset, end - begin, channel); return RET_OK; } @@ -102,14 +102,14 @@ int SoftmaxFp16CPUKernel::Run() { return ret; } else { auto input_tensor = in_tensors_.at(0); - MS_ASSERT(input_tensor); + MS_ASSERT(input_tensor != nullptr); auto output_tensor = out_tensors_.at(0); - MS_ASSERT(output_tensor); + MS_ASSERT(output_tensor != nullptr); input_fp16_ = reinterpret_cast(input_tensor->data_c()); - MS_ASSERT(input_fp16_); + MS_ASSERT(input_fp16_ != nullptr); output_fp16_ = reinterpret_cast(output_tensor->data_c()); - MS_ASSERT(output_fp16_); - MS_ASSERT(sum_data_); + MS_ASSERT(output_fp16_ != nullptr); + MS_ASSERT(sum_data_ != nullptr); SoftmaxFp16(input_fp16_, output_fp16_, sum_data_, softmax_param_); } return RET_OK; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp16/stack_fp16.cc b/mindspore/lite/src/runtime/kernel/arm/fp16/stack_fp16.cc index 63505d35e6c..e310e07518c 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/stack_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/stack_fp16.cc @@ -73,6 +73,8 @@ void StackFp16CPUKernel::FreeBuffer() { } int StackFp16CPUKernel::Init() { + CHECK_LESS_RETURN(in_tensors_.size(), 1); + CHECK_LESS_RETURN(out_tensors_.size(), 1); data_type_size_ = sizeof(float16_t); if (!InferShapeDone()) { return RET_OK; @@ -114,7 +116,9 @@ int StackFp16CPUKernel::Run() { // if output tensor is fp32, we need to transform if (malloc_out_) { auto out_tensor = out_tensors_.at(0); - Float16ToFloat32(out_buffer_, reinterpret_cast(out_tensor->MutableData()), out_tensor->ElementsNum()); + MS_ASSERT(out_tensor != nullptr); + MS_ASSERT(out_tensor->data_c() != nullptr); + Float16ToFloat32(out_buffer_, reinterpret_cast(out_tensor->data_c()), out_tensor->ElementsNum()); } FreeBuffer(); return RET_OK;