From dcd6f88b876d3b835f9298ab97867c7de9ffb7c8 Mon Sep 17 00:00:00 2001 From: sunsuodong Date: Wed, 2 Dec 2020 16:28:38 +0800 Subject: [PATCH] fix code review --- .../lite/nnacl/depth_to_space_parameter.h | 3 ++ mindspore/lite/src/ops/assign.cc | 6 +-- mindspore/lite/src/ops/assign_add.cc | 8 +-- .../lite/src/ops/binary_cross_entropy.cc | 4 +- mindspore/lite/src/ops/conv2d.cc | 52 +++++++++---------- mindspore/lite/src/ops/deconv2d.cc | 48 ++++++++--------- mindspore/lite/src/ops/depthwise_conv2d.cc | 24 ++++----- .../populate/constant_of_shape_populate.cc | 2 +- mindspore/lite/src/ops/strided_slice.cc | 2 +- .../kernel/arm/base/arg_min_max_base.cc | 2 +- .../kernel/arm/base/batch_to_space_base.cc | 4 +- .../kernel/arm/base/depth_to_space_base.cc | 8 +-- .../src/runtime/kernel/arm/base/dequant.h | 4 +- .../arm/base/detection_post_process_base.cc | 4 +- .../arm/fp16/arithmetic_compare_fp16.cc | 6 +-- .../kernel/arm/fp16/arithmetic_fp16.cc | 6 +-- .../kernel/arm/fp16/arithmetic_self_fp16.cc | 2 +- .../src/runtime/kernel/arm/fp16/cast_fp16.cc | 2 +- .../runtime/kernel/arm/fp16/concat_fp16.cc | 2 +- .../arm/fp16/convolution_depthwise_fp16.cc | 2 +- .../convolution_depthwise_slidewindow_fp16.cc | 2 +- .../arm/fp16/deconvolution_depthwise_fp16.cc | 2 +- .../kernel/arm/fp16/deconvolution_fp16.cc | 4 +- .../arm/fp16/deconvolution_winograd_fp16.cc | 2 +- .../arm/fp32/arithmetic_compare_fp32.cc | 1 - .../kernel/arm/fp32/arithmetic_self_fp32.cc | 2 +- .../runtime/kernel/arm/fp32/batchnorm_fp32.cc | 10 ++-- .../src/runtime/kernel/arm/fp32/bias_fp32.cc | 7 ++- .../kernel/arm/fp32/broadcast_to_fp32.cc | 4 +- .../src/runtime/kernel/arm/fp32/cast_fp32.cc | 2 +- .../arm/fp32/convolution_depthwise_fp32.cc | 7 ++- .../convolution_depthwise_slidewindow_fp32.cc | 4 +- .../arm/fp32/deconvolution_depthwise_fp32.cc | 2 +- .../kernel/arm/fp32/deconvolution_fp32.cc | 4 +- .../runtime/kernel/arm/fp32/gatherNd_fp32.cc | 5 +- .../runtime/kernel/arm/fp32/gather_fp32.cc | 4 ++ .../kernel/arm/fp32/sparse_to_dense_fp32.cc | 8 ++- .../runtime/kernel/arm/int8/batchnorm_int8.cc | 24 ++++----- .../runtime/kernel/arm/int8/bias_add_int8.cc | 9 +++- .../kernel/arm/int8/convolution_1x1_int8.cc | 4 +- .../int8/convolution_depthwise_3x3_int8.cc | 2 +- .../arm/int8/convolution_depthwise_int8.cc | 5 +- .../convolution_depthwise_slidewindow_int8.cc | 2 +- .../arm/int8/deconvolution_depthwise_int8.cc | 2 +- .../kernel/arm/int8/deconvolution_int8.cc | 4 +- .../runtime/kernel/arm/int8/gatherNd_int8.cc | 5 +- .../converter/parser/onnx/onnx_conv_parser.cc | 4 +- 47 files changed, 175 insertions(+), 147 deletions(-) diff --git a/mindspore/lite/nnacl/depth_to_space_parameter.h b/mindspore/lite/nnacl/depth_to_space_parameter.h index b95c88012a..0c9dfe9ba4 100644 --- a/mindspore/lite/nnacl/depth_to_space_parameter.h +++ b/mindspore/lite/nnacl/depth_to_space_parameter.h @@ -19,13 +19,16 @@ typedef struct DepthToSpaceParameter { OpParameter op_parameter_; + // primitive parameter int32_t block_size_; + // shape correlative int32_t in_stride_dim0_; int32_t in_stride_dim1_; int32_t in_stride_dim2_; int32_t out_stride_dim0_; int32_t out_stride_dim1_; int32_t out_stride_dim2_; + // other parameter uint8_t data_type_size_; } DepthToSpaceParameter; diff --git a/mindspore/lite/src/ops/assign.cc b/mindspore/lite/src/ops/assign.cc index 8a9c2ff405..9facccd921 100644 --- a/mindspore/lite/src/ops/assign.cc +++ b/mindspore/lite/src/ops/assign.cc @@ -71,7 +71,7 @@ int Assign::InferShape(std::vector inputs, std::vectorElementsNum() != inputs[1]->ElementsNum()) { + if (inputs.at(0)->ElementsNum() != inputs.at(1)->ElementsNum()) { MS_LOG(ERROR) << "error input data size!"; return RET_ERROR; } @@ -79,8 +79,8 @@ int Assign::InferShape(std::vector inputs, std::vectorset_data_type(inputs[0]->data_type()); - out->set_format(inputs[0]->format()); + out->set_data_type(inputs.at(0)->data_type()); + out->set_format(inputs.at(0)->format()); out->set_shape({1}); } return RET_OK; diff --git a/mindspore/lite/src/ops/assign_add.cc b/mindspore/lite/src/ops/assign_add.cc index 488e221901..6d77708ad1 100644 --- a/mindspore/lite/src/ops/assign_add.cc +++ b/mindspore/lite/src/ops/assign_add.cc @@ -73,16 +73,16 @@ Registry AssignAddRegistry(schema::PrimitiveType_AssignAdd, AssignAddCreator); #endif int AssignAdd::InferShape(std::vector inputs_, std::vector outputs_) { - Tensor *x = inputs_[0]; - Tensor *y = inputs_[1]; - Tensor *out = outputs_[0]; + Tensor *x = inputs_.at(0); + Tensor *y = inputs_.at(1); + Tensor *out = outputs_.at(0); std::vector x_shape = x->shape(); if (x->data_type() != y->data_type()) { MS_LOG(ERROR) << "no matched shape of x and y"; return RET_ERROR; } std::vector output_shape(x_shape.size()); - for (int i = 0; i < static_cast(x_shape.size()); i++) { + for (size_t i = 0; i < x_shape.size(); i++) { output_shape[i] = x_shape[i]; } out->set_shape(output_shape); diff --git a/mindspore/lite/src/ops/binary_cross_entropy.cc b/mindspore/lite/src/ops/binary_cross_entropy.cc index adb344c102..da06fff538 100644 --- a/mindspore/lite/src/ops/binary_cross_entropy.cc +++ b/mindspore/lite/src/ops/binary_cross_entropy.cc @@ -100,8 +100,8 @@ PrimitiveC *BinaryCrossEntropyCreator(const schema::Primitive *primitive) { Registry BinaryCrossEntropyRegistry(schema::PrimitiveType_BinaryCrossEntropy, BinaryCrossEntropyCreator); #endif int BinaryCrossEntropy::InferShape(std::vector inputs_, std::vector outputs_) { - Tensor *x = inputs_[0]; - Tensor *out = outputs_[0]; + Tensor *x = inputs_.at(0); + Tensor *out = outputs_.at(0); out->set_format(x->format()); out->set_data_type(x->data_type()); int reduction = GetReduction(); diff --git a/mindspore/lite/src/ops/conv2d.cc b/mindspore/lite/src/ops/conv2d.cc index 08be441b0c..5d7155a403 100644 --- a/mindspore/lite/src/ops/conv2d.cc +++ b/mindspore/lite/src/ops/conv2d.cc @@ -89,10 +89,10 @@ void ConvertConvWeight(const ParameterPtr ¶m_node) { return; } - size_t filter_k = weight->tensor_shape()[0]; - size_t filter_c = weight->tensor_shape()[1]; - size_t filter_h = weight->tensor_shape()[2]; - size_t filter_w = weight->tensor_shape()[3]; + size_t filter_k = weight->tensor_shape().at(0); + size_t filter_c = weight->tensor_shape().at(1); + size_t filter_h = weight->tensor_shape().at(2); + size_t filter_w = weight->tensor_shape().at(3); T *p1Buff = nullptr; T *p2Buff = nullptr; for (size_t k = 0; k < filter_k; ++k) { @@ -145,26 +145,26 @@ void Conv2D::PopulaterConv2DMultiGroup(const Primitive &prim, schema::PrimitiveT attr->format = schema::Format::Format_NUM_OF_FORMAT; } auto pad_list = CastToInt(prim.GetAttr("pad_list")); - attr->padUp = pad_list[0]; - attr->padDown = pad_list[1]; - attr->padLeft = pad_list[2]; - attr->padRight = pad_list[3]; + attr->padUp = pad_list.at(0); + attr->padDown = pad_list.at(1); + attr->padLeft = pad_list.at(2); + attr->padRight = pad_list.at(3); auto dilation = CastToInt(prim.GetAttr("dilation")); #ifdef SUPPORT_TRAIN - attr->dilateH = dilation[2]; - attr->dilateW = dilation[3]; + attr->dilateH = dilation.at(2); + attr->dilateW = dilation.at(3); #else - attr->dilateH = dilation[0]; - attr->dilateW = dilation[1]; + attr->dilateH = dilation.at(0); + attr->dilateW = dilation.at(1); #endif auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size[0]; - attr->kernelW = kernel_size[1]; + attr->kernelH = kernel_size.at(0); + attr->kernelW = kernel_size.at(1); auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride[2]; - attr->strideW = stride[3]; + attr->strideH = stride.at(2); + attr->strideW = stride.at(3); auto pad_mode = GetValue(prim.GetAttr("pad_mode")); if (pad_mode == "valid") { @@ -229,22 +229,22 @@ void Conv2D::PopulaterConv2DSingleGroup(const Primitive &prim, schema::Primitive attr->format = schema::Format::Format_NUM_OF_FORMAT; } auto pad_list = CastToInt(prim.GetAttr("pad_list")); - attr->padUp = pad_list[0]; - attr->padDown = pad_list[1]; - attr->padLeft = pad_list[2]; - attr->padRight = pad_list[3]; + attr->padUp = pad_list.at(0); + attr->padDown = pad_list.at(1); + attr->padLeft = pad_list.at(2); + attr->padRight = pad_list.at(3); auto dilation = CastToInt(prim.GetAttr("dilation")); - attr->dilateH = dilation[2]; - attr->dilateW = dilation[3]; + attr->dilateH = dilation.at(2); + attr->dilateW = dilation.at(3); auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size[0]; - attr->kernelW = kernel_size[1]; + attr->kernelH = kernel_size.at(0); + attr->kernelW = kernel_size.at(1); auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride[2]; - attr->strideW = stride[3]; + attr->strideH = stride.at(2); + attr->strideW = stride.at(3); attr->channelOut = CastToInt(prim.GetAttr("out_channel")).front(); diff --git a/mindspore/lite/src/ops/deconv2d.cc b/mindspore/lite/src/ops/deconv2d.cc index 4d638e0e0b..7f42b5f3e4 100644 --- a/mindspore/lite/src/ops/deconv2d.cc +++ b/mindspore/lite/src/ops/deconv2d.cc @@ -82,10 +82,10 @@ void ConvertConvWeight(const ParameterPtr ¶m_node) { return; } - size_t filter_k = weight->tensor_shape()[0]; - size_t filter_c = weight->tensor_shape()[1]; - size_t filter_h = weight->tensor_shape()[2]; - size_t filter_w = weight->tensor_shape()[3]; + size_t filter_k = weight->tensor_shape().at(0); + size_t filter_c = weight->tensor_shape().at(1); + size_t filter_h = weight->tensor_shape().at(2); + size_t filter_w = weight->tensor_shape().at(3); T *p1Buff = nullptr; T *p2Buff = nullptr; for (size_t k = 0; k < filter_k; ++k) { @@ -137,22 +137,22 @@ void DeConv2D::PopulaterConv2DMultiGroup(const Primitive &prim, schema::Primitiv attr->format = schema::Format::Format_NUM_OF_FORMAT; } auto pad_list = CastToInt(prim.GetAttr("pad_list")); - attr->padUp = pad_list[0]; - attr->padDown = pad_list[1]; - attr->padLeft = pad_list[2]; - attr->padRight = pad_list[3]; + attr->padUp = pad_list.at(0); + attr->padDown = pad_list.at(1); + attr->padLeft = pad_list.at(2); + attr->padRight = pad_list.at(3); auto dilation = CastToInt(prim.GetAttr("dilation")); - attr->dilateH = dilation[0]; - attr->dilateW = dilation[1]; + attr->dilateH = dilation.at(0); + attr->dilateW = dilation.at(1); auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size[0]; - attr->kernelW = kernel_size[1]; + attr->kernelH = kernel_size.at(0); + attr->kernelW = kernel_size.at(1); auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride[0]; - attr->strideW = stride[1]; + attr->strideH = stride.at(0); + attr->strideW = stride.at(1); auto pad_mode = GetValue(prim.GetAttr("pad_mode")); if (pad_mode == "valid") { @@ -204,22 +204,22 @@ void DeConv2D::PopulaterDeConv2DSingleGroup(const Primitive &prim, schema::Primi attr->format = schema::Format_NUM_OF_FORMAT; } auto pad_list = CastToInt(prim.GetAttr("pad_list")); - attr->padUp = pad_list[0]; - attr->padDown = pad_list[1]; - attr->padLeft = pad_list[2]; - attr->padRight = pad_list[3]; + attr->padUp = pad_list.at(0); + attr->padDown = pad_list.at(1); + attr->padLeft = pad_list.at(2); + attr->padRight = pad_list.at(3); auto dilation = CastToInt(prim.GetAttr("dilation")); - attr->dilateH = dilation[0]; - attr->dilateW = dilation[1]; + attr->dilateH = dilation.at(0); + attr->dilateW = dilation.at(1); auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size[0]; - attr->kernelW = kernel_size[1]; + attr->kernelH = kernel_size.at(0); + attr->kernelW = kernel_size.at(1); auto stride = CastToInt(prim.GetAttr("stride")); - attr->strideH = stride[0]; - attr->strideW = stride[1]; + attr->strideH = stride.at(0); + attr->strideW = stride.at(1); attr->channelOut = CastToInt(prim.GetAttr("out_channel")).front(); diff --git a/mindspore/lite/src/ops/depthwise_conv2d.cc b/mindspore/lite/src/ops/depthwise_conv2d.cc index 5ab48bfb4f..f9a44a32c6 100644 --- a/mindspore/lite/src/ops/depthwise_conv2d.cc +++ b/mindspore/lite/src/ops/depthwise_conv2d.cc @@ -87,19 +87,19 @@ int DepthwiseConv2D::UnPackAttr(const Primitive &prim, const std::vectorformat = schema::Format::Format_NUM_OF_FORMAT; } auto pad_list = CastToInt(prim.GetAttr("pads")); - attr->padUp = pad_list[0]; - attr->padDown = pad_list[1]; - attr->padLeft = pad_list[2]; - attr->padRight = pad_list[3]; + attr->padUp = pad_list.at(0); + attr->padDown = pad_list.at(1); + attr->padLeft = pad_list.at(2); + attr->padRight = pad_list.at(3); auto dilation = CastToInt(prim.GetAttr("dilation")); - attr->dilateH = dilation[0]; - attr->dilateW = dilation[1]; + attr->dilateH = dilation.at(0); + attr->dilateW = dilation.at(1); if (utils::isa(prim.GetAttr("kernel_size"))) { auto kernel_size = CastToInt(prim.GetAttr("kernel_size")); - attr->kernelH = kernel_size[0]; - attr->kernelW = kernel_size[1]; + attr->kernelH = kernel_size.at(0); + attr->kernelW = kernel_size.at(1); } else { auto kernel_size = CastToInt(prim.GetAttr("kernel_size")).front(); attr->kernelH = kernel_size; @@ -107,8 +107,8 @@ int DepthwiseConv2D::UnPackAttr(const Primitive &prim, const std::vectorstrideH = stride[2]; - attr->strideW = stride[3]; + attr->strideH = stride.at(2); + attr->strideW = stride.at(3); auto pad_mode = GetValue(prim.GetAttr("pad_mode")); if (pad_mode == "valid") { @@ -252,11 +252,11 @@ int DepthwiseConv2D::InferShape(std::vector inputs_, std::vector std::vector out_shape{input->shape()}; out_shape.at(1) = output_h; out_shape.at(2) = output_w; - if (GetChannelMultiplier() * input_channel != weight->shape()[0]) { + if (GetChannelMultiplier() * input_channel != weight->shape().at(0)) { MS_LOG(ERROR) << "Conv depthwise only support group equals output channel."; return 1; } - out_shape.at(3) = weight->shape()[0] * weight->shape()[3]; // in_channel * out_channel + out_shape.at(3) = weight->shape().at(0) * weight->shape().at(3); // in_channel * out_channel output->set_shape(out_shape); return 0; diff --git a/mindspore/lite/src/ops/populate/constant_of_shape_populate.cc b/mindspore/lite/src/ops/populate/constant_of_shape_populate.cc index 9286c2cc3b..2ead683888 100644 --- a/mindspore/lite/src/ops/populate/constant_of_shape_populate.cc +++ b/mindspore/lite/src/ops/populate/constant_of_shape_populate.cc @@ -38,7 +38,7 @@ OpParameter *PopulateConstantOfShapeParameter(const mindspore::lite::PrimitiveC if (value.empty() || value.size() > 1) { MS_LOG(ERROR) << "The value of constant of shape is empty or more than 1."; } else { - param->value_ = attr->GetValue()[0]; + param->value_ = attr->GetValue().at(0); } param->data_type_ = attr->GetDataType(); return reinterpret_cast(param); diff --git a/mindspore/lite/src/ops/strided_slice.cc b/mindspore/lite/src/ops/strided_slice.cc index fc1628ec6c..db5eaa5564 100644 --- a/mindspore/lite/src/ops/strided_slice.cc +++ b/mindspore/lite/src/ops/strided_slice.cc @@ -304,7 +304,7 @@ int StridedSlice::HandleAxesInputExist(const std::vector &inputs std::vector axes; if (axes_data == nullptr) { for (int i = 0; i < begin_ndim; ++i) { - axes[i] = i; + axes.push_back(i); } } else { axes.assign(axes_data, axes_data + begin_ndim); diff --git a/mindspore/lite/src/runtime/kernel/arm/base/arg_min_max_base.cc b/mindspore/lite/src/runtime/kernel/arm/base/arg_min_max_base.cc index 1c03a63510..0ca1734c7e 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/arg_min_max_base.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/arg_min_max_base.cc @@ -59,7 +59,7 @@ int ArgMinMaxBaseCPUKernel::ReSize() { MS_LOG(ERROR) << "Invalid topk " << param->topk_; return RET_PARAM_INVALID; } - param->topk_ = MSMIN(param->topk_, in_shape[axis]); + param->topk_ = MSMIN(param->topk_, in_shape.at(axis)); ComputeStrides(in_shape.data(), param->in_strides_, in_shape.size()); auto out_shape = out_tensors_.at(0)->shape(); ComputeStrides(out_shape.data(), param->out_strides_, out_shape.size()); diff --git a/mindspore/lite/src/runtime/kernel/arm/base/batch_to_space_base.cc b/mindspore/lite/src/runtime/kernel/arm/base/batch_to_space_base.cc index 55c0e339c8..9e1c80475d 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/batch_to_space_base.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/batch_to_space_base.cc @@ -30,7 +30,7 @@ using mindspore::schema::PrimitiveType_BatchToSpaceND; namespace mindspore::kernel { int BatchToSpaceBaseCPUKernel::Init() { - if (in_tensors_[0]->format() != schema::Format::Format_NHWC) { + if (in_tensors_.at(0)->format() != schema::Format::Format_NHWC) { MS_LOG(ERROR) << "batch_to_space only support NHWC now!"; return RET_FORMAT_ERR; } @@ -44,7 +44,7 @@ int BatchToSpaceBaseCPUKernel::Init() { } int BatchToSpaceBaseCPUKernel::ReSize() { - auto shape = in_tensors_[0]->shape(); + auto shape = in_tensors_.at(0)->shape(); if (shape.size() != 4) { MS_LOG(ERROR) << "Unsupport shape size: " << shape.size(); return RET_ERROR; diff --git a/mindspore/lite/src/runtime/kernel/arm/base/depth_to_space_base.cc b/mindspore/lite/src/runtime/kernel/arm/base/depth_to_space_base.cc index 63278f7d41..3b4d5b2311 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/depth_to_space_base.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/depth_to_space_base.cc @@ -33,7 +33,7 @@ namespace mindspore::kernel { int DepthToSpaceBaseCPUKernel::Init() { return RET_OK; } int DepthToSpaceBaseCPUKernel::ReSize() { - if (in_tensors_[0]->format() != schema::Format::Format_NHWC) { + if (in_tensors_.at(0)->format() != schema::Format::Format_NHWC) { MS_LOG(ERROR) << "depth_to_space only support NHWC now!"; return RET_FORMAT_ERR; } @@ -42,18 +42,18 @@ int DepthToSpaceBaseCPUKernel::ReSize() { MS_LOG(ERROR) << "Input block_size should > 0!"; return RET_PARAM_INVALID; } - auto shape_size = in_tensors_[0]->shape().size(); + auto shape_size = in_tensors_.at(0)->shape().size(); if (shape_size != DIMENSION_4D) { MS_LOG(ERROR) << "Input shape size should be " << DIMENSION_4D; return RET_PARAM_INVALID; } int32_t in_strides[DIMENSION_4D]; - ComputeStrides(const_cast(in_tensors_[0]->shape().data()), in_strides, shape_size); + ComputeStrides(const_cast(in_tensors_.at(0)->shape().data()), in_strides, shape_size); param->in_stride_dim0_ = in_strides[0]; param->in_stride_dim1_ = in_strides[1]; param->in_stride_dim2_ = in_strides[2]; int32_t out_strides[DIMENSION_4D]; - ComputeStrides(const_cast(out_tensors_[0]->shape().data()), out_strides, shape_size); + ComputeStrides(const_cast(out_tensors_.at(0)->shape().data()), out_strides, shape_size); param->out_stride_dim0_ = out_strides[0]; param->out_stride_dim1_ = out_strides[1]; param->out_stride_dim2_ = out_strides[2]; diff --git a/mindspore/lite/src/runtime/kernel/arm/base/dequant.h b/mindspore/lite/src/runtime/kernel/arm/base/dequant.h index 9b46736258..6aaa8f2ca4 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/dequant.h +++ b/mindspore/lite/src/runtime/kernel/arm/base/dequant.h @@ -44,8 +44,8 @@ class DequantUtil { return nullptr; } if (input_tensor->shape().size() == kPerBatch && - input_tensor->quant_params().size() == static_cast(input_tensor->shape()[0])) { // per batch matmul - auto per_batch_size = input_tensor->shape()[0]; + input_tensor->quant_params().size() == static_cast(input_tensor->shape().at(0))) { // per batch matmul + auto per_batch_size = input_tensor->shape().at(0); auto quant_param = input_tensor->quant_params(); for (int i = 0; i < per_batch_size; i++) { auto param = quant_param.at(i); diff --git a/mindspore/lite/src/runtime/kernel/arm/base/detection_post_process_base.cc b/mindspore/lite/src/runtime/kernel/arm/base/detection_post_process_base.cc index 1b0f1bc6d6..afea852cb7 100644 --- a/mindspore/lite/src/runtime/kernel/arm/base/detection_post_process_base.cc +++ b/mindspore/lite/src/runtime/kernel/arm/base/detection_post_process_base.cc @@ -156,8 +156,8 @@ int DetectionPostProcessBaseCPUKernel::Run() { auto output_scores = reinterpret_cast(out_tensors_.at(2)->MutableData()); auto output_num = reinterpret_cast(out_tensors_.at(3)->MutableData()); - num_boxes_ = in_tensors_.at(0)->shape()[1]; - num_classes_with_bg_ = in_tensors_.at(1)->shape()[2]; + num_boxes_ = in_tensors_.at(0)->shape().at(1); + num_classes_with_bg_ = in_tensors_.at(1)->shape().at(2); params_->decoded_boxes_ = context_->allocator->Malloc(num_boxes_ * 4 * sizeof(float)); if (params_->decoded_boxes_ == nullptr) { MS_LOG(ERROR) << "malloc params->decoded_boxes_ failed."; 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 26ec7f7a97..c4cfb7b137 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 @@ -74,9 +74,9 @@ int ArithmeticCompareFP16CPUKernel::Init() { } int ArithmeticCompareFP16CPUKernel::ReSize() { - param_->in_elements_num0_ = in_tensors_[0]->ElementsNum(); - param_->in_elements_num1_ = in_tensors_[1]->ElementsNum(); - param_->out_elements_num_ = out_tensors_[0]->ElementsNum(); + param_->in_elements_num0_ = in_tensors_.at(0)->ElementsNum(); + param_->in_elements_num1_ = in_tensors_.at(1)->ElementsNum(); + param_->out_elements_num_ = out_tensors_.at(0)->ElementsNum(); if (param_->in_elements_num0_ == 1 || param_->in_elements_num1_ == 1) { param_->broadcasting_ = false; 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 fe979f0377..de05d3e105 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/arithmetic_fp16.cc @@ -135,9 +135,9 @@ int ArithmeticFP16CPUKernel::PreProcess() { } int ArithmeticFP16CPUKernel::ReSize() { - param_->in_elements_num0_ = in_tensors_[0]->ElementsNum(); - param_->in_elements_num1_ = in_tensors_[1]->ElementsNum(); - param_->out_elements_num_ = out_tensors_[0]->ElementsNum(); + param_->in_elements_num0_ = in_tensors_.at(0)->ElementsNum(); + param_->in_elements_num1_ = in_tensors_.at(1)->ElementsNum(); + param_->out_elements_num_ = out_tensors_.at(0)->ElementsNum(); if (param_->in_elements_num0_ == 1 || param_->in_elements_num1_ == 1) { param_->broadcasting_ = false; 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 5ac00097f0..567f863fd5 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 @@ -44,7 +44,7 @@ ArithmeticSelfFp16Func ArithmeticSelfFp16CPUKernel::GetArithmeticSelfFp16Fun(int {mindspore::schema::PrimitiveType_Ceil, ElementCeilFp16}, {mindspore::schema::PrimitiveType_Round, ElementRoundFp16}, {mindspore::schema::PrimitiveType_Neg, ElementNegativeFp16}}; - for (size_t i = 0; i < sizeof(type_func_table); i++) { + for (size_t i = 0; i < sizeof(type_func_table) / sizeof(TYPE_FUNC_INFO); i++) { if (type_func_table[i].primitive_type_ == primitive_type) { return type_func_table[i].func_; } 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 c41a8d0b76..061f578b4e 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/cast_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/cast_fp16.cc @@ -48,7 +48,7 @@ int CastFp16CPUKernel::Init() { } int CastFp16CPUKernel::ReSize() { - data_num_ = in_tensors_[0]->ElementsNum(); + data_num_ = in_tensors_.at(0)->ElementsNum(); if (data_num_ == 0) { return RET_OK; } 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 95801be836..72519e5045 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/concat_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/concat_fp16.cc @@ -102,7 +102,7 @@ int ConcatFp16CPUKernel::Run() { std::vector> shapes; for (size_t i = 0; i < input_num; ++i) { - const auto in_tensor = in_tensors_[i]; + 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->MutableData()); Float32ToFloat16(in_tensor_data, fp16_inputs_[i], in_tensor->ElementsNum()); 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 39e24d130c..623ebfffe5 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 @@ -42,7 +42,7 @@ int ConvolutionDepthwiseFp16CPUKernel::InitWeightBias() { // init weight: o, h, w, i; o == group, i == 1 ConvolutionBaseFP16CPUKernel::GetExecuteFilter(); - auto weight_tensor = in_tensors_[kWeightIndex]; + auto weight_tensor = in_tensors_.at(kWeightIndex); int channel = weight_tensor->Batch(); int pack_weight_size = channel * weight_tensor->Height() * weight_tensor->Width(); 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 0de96878ec..07caca2f2a 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 @@ -64,7 +64,7 @@ int ConvolutionDepthwiseSWFp16CPUKernel::InitPackedInputOutput() { int ConvolutionDepthwiseSWFp16CPUKernel::InitWeightBias() { // init weight: o, h, w, i; o == group, i == 1 - auto weight_tensor = in_tensors_[kWeightIndex]; + auto weight_tensor = in_tensors_.at(kWeightIndex); int OC8 = UP_DIV(weight_tensor->Batch(), C8NUM); auto origin_weight = reinterpret_cast(weight_tensor->MutableData()); int pack_weight_size = C8NUM * OC8 * weight_tensor->Height() * weight_tensor->Width(); 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 eadbd553cf..3aa167aa76 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 @@ -77,7 +77,7 @@ int DeconvolutionDepthwiseFp16CPUKernel::InitPackedInputOutput() { int DeconvolutionDepthwiseFp16CPUKernel::InitWeightBias() { // init weight: o, h, w, i; o == group, i == 1 - auto weight_tensor = in_tensors_[kWeightIndex]; + auto weight_tensor = in_tensors_.at(kWeightIndex); int OC8 = UP_DIV(weight_tensor->Batch(), C8NUM); auto origin_weight = reinterpret_cast(weight_tensor->MutableData()); int pack_weight_size = C8NUM * OC8 * weight_tensor->Height() * weight_tensor->Width(); 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 1b59471413..264d5e8dc0 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_fp16.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp16/deconvolution_fp16.cc @@ -64,7 +64,7 @@ int DeConvolutionFp16CPUKernel::InitWeightBias() { } memset(bias_data_, 0, UP_ROUND(output_channel, C4NUM) * sizeof(float16_t)); if (in_tensors_.size() == 3) { - Float32ToFloat16(reinterpret_cast(in_tensors_[2]->MutableData()), + Float32ToFloat16(reinterpret_cast(in_tensors_.at(2)->MutableData()), reinterpret_cast(bias_data_), output_channel); } @@ -75,7 +75,7 @@ int DeConvolutionFp16CPUKernel::InitWeightBias() { return RET_ERROR; } memset(execute_weight_, 0, weight_pack_size); - PackNHWCFp32ToC8HWN8Fp16(reinterpret_cast(in_tensors_[1]->MutableData()), execute_weight_, input_channel, + PackNHWCFp32ToC8HWN8Fp16(reinterpret_cast(in_tensors_.at(1)->MutableData()), execute_weight_, input_channel, kernel_w * kernel_h, output_channel); return RET_OK; } 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 c2d8d8daa9..1387e6b8d7 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 @@ -239,7 +239,7 @@ int DeConvWgPostFp16Run(void *cdata, int task_id) { } int DeConvWinogradFp16CPUKernel::InitComputeParam() { - auto weight_tensor = in_tensors_[1]; + auto weight_tensor = in_tensors_.at(1); conv_param_->input_channel_ = weight_tensor->Batch(); conv_param_->output_channel_ = weight_tensor->Channel(); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_compare_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_compare_fp32.cc index 840d76c5a8..d3b7238c9a 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_compare_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_compare_fp32.cc @@ -28,7 +28,6 @@ using mindspore::schema::PrimitiveType_LessEqual; using mindspore::schema::PrimitiveType_NotEqual; namespace mindspore::kernel { - int ArithmeticCompareCPUKernel::BroadcastRun(void *input0, void *input1, void *output, int dim, int out_count, int out_thread_stride) { if (dim > break_pos_) { diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_self_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_self_fp32.cc index a9d978a4a8..5d77e788be 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_self_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/arithmetic_self_fp32.cc @@ -42,7 +42,7 @@ ArithmeticSelfFunc ArithmeticSelfCPUKernel::GetArithmeticSelfFun(int primitive_t {mindspore::schema::PrimitiveType_Ceil, ElementCeil}, {mindspore::schema::PrimitiveType_Round, ElementRound}, {mindspore::schema::PrimitiveType_Neg, ElementNegative}}; - for (size_t i = 0; i < sizeof(type_func_table); i++) { + for (size_t i = 0; i < sizeof(type_func_table) / sizeof(TYPE_FUNC_INFO); i++) { if (type_func_table[i].primitive_type_ == primitive_type) { return type_func_table[i].func_; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/batchnorm_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/batchnorm_fp32.cc index 79bdd050ac..cfec5dfd06 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/batchnorm_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/batchnorm_fp32.cc @@ -48,7 +48,7 @@ void BatchnormCPUKernel::FreeMeanAndVariance() { } void BatchnormCPUKernel::FillParam() { - auto input_shapes = in_tensors_[0]->shape(); + auto input_shapes = in_tensors_.at(0)->shape(); auto n_dim = input_shapes.size(); auto param = reinterpret_cast(op_parameter_); param->channel_ = input_shapes[n_dim - 1]; @@ -59,15 +59,15 @@ void BatchnormCPUKernel::FillParam() { } int BatchnormCPUKernel::InitConstTensor() { - mean_ = malloc(in_tensors_[1]->Size()); - variance_ = malloc(in_tensors_[2]->Size()); + mean_ = malloc(in_tensors_.at(1)->Size()); + variance_ = malloc(in_tensors_.at(2)->Size()); if (mean_ == nullptr || variance_ == nullptr) { MS_LOG(ERROR) << "Memory allocation failed"; FreeMeanAndVariance(); return RET_ERROR; } - memcpy(mean_, in_tensors_[1]->MutableData(), in_tensors_[1]->Size()); - memcpy(variance_, in_tensors_[2]->MutableData(), in_tensors_[2]->Size()); + memcpy(mean_, in_tensors_.at(1)->MutableData(), in_tensors_.at(1)->Size()); + memcpy(variance_, in_tensors_.at(2)->MutableData(), in_tensors_.at(2)->Size()); return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/bias_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/bias_fp32.cc index 5353d47376..2573ff2101 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/bias_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/bias_fp32.cc @@ -29,9 +29,12 @@ using mindspore::schema::PrimitiveType_BiasAdd; namespace mindspore::kernel { int BiasCPUKernel::ReSize() { - auto dims = in_tensors_[0]->shape(); - MS_ASSERT(dims.size() <= 5); + auto dims = in_tensors_.at(0)->shape(); bias_param_->ndim_ = dims.size(); + if (bias_param_->ndim_ < 1 || bias_param_->ndim_ > 5) { + MS_LOG(ERROR) << "input shape is invalid"; + return RET_ERROR; + } for (size_t i = 0; i < bias_param_->ndim_; i++) { bias_param_->in_shape0_[i] = dims[i]; bias_param_->in_shape1_[i] = 1; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/broadcast_to_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/broadcast_to_fp32.cc index 3ddc28d9f1..5656777fb1 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/broadcast_to_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/broadcast_to_fp32.cc @@ -26,13 +26,13 @@ using mindspore::schema::PrimitiveType_BroadcastTo; namespace mindspore::kernel { int BroadcastToCPUKernel::ReSize() { - auto input_shape = in_tensors_[0]->shape(); + auto input_shape = in_tensors_.at(0)->shape(); for (size_t i = 0; i < input_shape.size(); ++i) { shape_info_.input_shape_[i] = input_shape[i]; } shape_info_.input_shape_size_ = static_cast(input_shape.size()); - auto output_shape = out_tensors_[0]->shape(); + auto output_shape = out_tensors_.at(0)->shape(); for (size_t i = 0; i < output_shape.size(); ++i) { shape_info_.output_shape_[i] = output_shape[i]; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/cast_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/cast_fp32.cc index ea93db34b4..af2fae877f 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/cast_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/cast_fp32.cc @@ -49,7 +49,7 @@ int CastCPUKernel::Init() { } int CastCPUKernel::ReSize() { - data_num_ = in_tensors_[0]->ElementsNum(); + data_num_ = in_tensors_.at(0)->ElementsNum(); if (data_num_ == 0) { return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_depthwise_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_depthwise_fp32.cc index 4b46c6ede1..af91ace56d 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_depthwise_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_depthwise_fp32.cc @@ -40,11 +40,14 @@ ConvolutionDepthwiseCPUKernel::~ConvolutionDepthwiseCPUKernel() { int ConvolutionDepthwiseCPUKernel::InitWeightBias() { // init weight: k, h, w, c; k == group == output_channel, c == 1 - auto weight_tensor = in_tensors_[kWeightIndex]; + auto weight_tensor = in_tensors_.at(kWeightIndex); auto origin_weight = reinterpret_cast(weight_tensor->MutableData()); int channel = weight_tensor->Batch(); int pack_weight_size = weight_tensor->Batch() * weight_tensor->Height() * weight_tensor->Width(); - + if (pack_weight_size >= std::numeric_limits::max() / static_cast(sizeof(float))) { + MS_LOG(ERROR) << "pack_weight_size is invalid, pack_weight_size: " << pack_weight_size; + return RET_ERROR; + } packed_weight_ = reinterpret_cast(malloc(pack_weight_size * sizeof(float))); if (packed_weight_ == nullptr) { MS_LOG(ERROR) << "Malloc buffer failed."; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_depthwise_slidewindow_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_depthwise_slidewindow_fp32.cc index 5edd619b64..45d0ea6750 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_depthwise_slidewindow_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/convolution_depthwise_slidewindow_fp32.cc @@ -40,7 +40,7 @@ ConvolutionDepthwiseSWCPUKernel::~ConvolutionDepthwiseSWCPUKernel() { int ConvolutionDepthwiseSWCPUKernel::InitWeightBias() { // init weight: o, h, w, i; o == group, i == 1 - auto weight_tensor = in_tensors_[kWeightIndex]; + auto weight_tensor = in_tensors_.at(kWeightIndex); auto origin_weight = reinterpret_cast(weight_tensor->MutableData()); int OC4 = UP_DIV(weight_tensor->Batch(), C4NUM); int pack_weight_size = C4NUM * OC4 * weight_tensor->Height() * weight_tensor->Width(); @@ -66,7 +66,7 @@ int ConvolutionDepthwiseSWCPUKernel::InitWeightBias() { memset(bias_data_, 0, malloc_size * sizeof(float)); if (in_tensors_.size() == kInputSize2) { - auto bias_tensor = in_tensors_[kBiasIndex]; + auto bias_tensor = in_tensors_.at(kBiasIndex); auto ori_bias = reinterpret_cast(bias_tensor->MutableData()); memcpy(bias_data_, ori_bias, bias_tensor->ElementsNum() * sizeof(float)); } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/deconvolution_depthwise_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/deconvolution_depthwise_fp32.cc index 7edb7037cb..486b2b0fcf 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/deconvolution_depthwise_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/deconvolution_depthwise_fp32.cc @@ -54,7 +54,7 @@ int DeconvolutionDepthwiseCPUKernel::InitSlideParam() { int DeconvolutionDepthwiseCPUKernel::InitWeightBias() { // init weight: o, h, w, i; o == group, i == 1 - auto weight_tensor = in_tensors_[kWeightIndex]; + auto weight_tensor = in_tensors_.at(kWeightIndex); auto origin_weight = reinterpret_cast(weight_tensor->MutableData()); int OC4 = UP_DIV(weight_tensor->Batch(), C4NUM); int pack_weight_size = C4NUM * OC4 * weight_tensor->Height() * weight_tensor->Width(); diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/deconvolution_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/deconvolution_fp32.cc index fa5f929986..533d889182 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/deconvolution_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/deconvolution_fp32.cc @@ -63,7 +63,7 @@ int DeConvolutionCPUKernel::InitWeightBias() { } memset(bias_data_, 0, UP_ROUND(output_channel, C4NUM) * sizeof(float)); if (in_tensors_.size() == 3) { - memcpy(bias_data_, in_tensors_[2]->MutableData(), output_channel * sizeof(float)); + memcpy(bias_data_, in_tensors_.at(2)->MutableData(), output_channel * sizeof(float)); } size_t weight_pack_size = input_channel * kernel_w_ * kernel_h_ * UP_ROUND(output_channel, C8NUM) * sizeof(float); @@ -73,7 +73,7 @@ int DeConvolutionCPUKernel::InitWeightBias() { return RET_ERROR; } memset(weight_ptr_, 0, weight_pack_size); - PackNHWCToC8HWN8Fp32(reinterpret_cast(in_tensors_[1]->MutableData()), weight_ptr_, input_channel, + PackNHWCToC8HWN8Fp32(reinterpret_cast(in_tensors_.at(1)->MutableData()), weight_ptr_, input_channel, kernel_w_ * kernel_h_, output_channel); return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/gatherNd_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/gatherNd_fp32.cc index 8834aa7dc4..1759a7817a 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/gatherNd_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/gatherNd_fp32.cc @@ -56,7 +56,10 @@ int GatherNdCPUKernel::ReSize() { for (int i = 0; i < indices_rank - 1; ++i) { count_ *= indices_shape[i]; } - + if (count_ >= std::numeric_limits::max() / static_cast(sizeof(int))) { + MS_LOG(ERROR) << "count_ is invalid, count_: " << count_; + return RET_ERROR; + } in_offset_ = reinterpret_cast(malloc(count_ * sizeof(int))); if (in_offset_ == nullptr) { MS_LOG(ERROR) << "GatherNd Malloc in_offset_ error!"; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/gather_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/gather_fp32.cc index 2e23dddd83..5162b061b0 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/gather_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/gather_fp32.cc @@ -113,6 +113,10 @@ int GatherCPUKernel::Run() { int GatherCPUKernel::AssignIndicesData(bool isIndicesInt32, int indices_num, lite::Tensor *indices_tensor) { 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; + return RET_ERROR; + } indices_data_ = reinterpret_cast(context_->allocator->Malloc(sizeof(int32_t) * indices_num)); if (indices_data_ == nullptr) { MS_LOG(ERROR) << "Memory allocation failed"; diff --git a/mindspore/lite/src/runtime/kernel/arm/fp32/sparse_to_dense_fp32.cc b/mindspore/lite/src/runtime/kernel/arm/fp32/sparse_to_dense_fp32.cc index d9e907d646..8ae662910f 100644 --- a/mindspore/lite/src/runtime/kernel/arm/fp32/sparse_to_dense_fp32.cc +++ b/mindspore/lite/src/runtime/kernel/arm/fp32/sparse_to_dense_fp32.cc @@ -91,14 +91,18 @@ int SparseToDenseRun(void *cdata, int task_id) { int SparseToDenseCPUKernel::GenerateIndices() { auto input0 = in_tensors_.at(0); - index_dim = input0->shape().size(); index_num = input0->shape()[0]; - int *sparse_indices = reinterpret_cast(input0->MutableData()); + if (index_num >= std::numeric_limits::max() / static_cast(sizeof(int *))) { + MS_LOG(ERROR) << "Input dim is invalid, dim: " << index_num; + return RET_ERROR; + } sparse_indices_vect = reinterpret_cast(ctx_->allocator->Malloc(sizeof(int *) * index_num)); if (sparse_indices_vect == nullptr) { MS_LOG(ERROR) << "Null pointer reference: sparse_indices_vect."; return RET_ERROR; } + index_dim = input0->shape().size(); + int *sparse_indices = reinterpret_cast(input0->MutableData()); switch (index_dim) { case 0: case 1: { diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/batchnorm_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/batchnorm_int8.cc index fe99533768..19a3bbabc9 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/batchnorm_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/batchnorm_int8.cc @@ -42,10 +42,10 @@ BatchnormInt8CPUKernel::~BatchnormInt8CPUKernel() { } int BatchnormInt8CPUKernel::InitConstTensor() { - auto input = in_tensors_[0]; - auto mean = in_tensors_[1]; - auto variance = in_tensors_[2]; - auto output = out_tensors_[0]; + auto input = in_tensors_.at(0); + auto mean = in_tensors_.at(1); + auto variance = in_tensors_.at(2); + auto output = out_tensors_.at(0); auto mean_ptr = reinterpret_cast(mean->MutableData()); auto var_ptr = reinterpret_cast(variance->MutableData()); @@ -81,12 +81,12 @@ int BatchnormInt8CPUKernel::InitConstTensor() { } int BatchnormInt8CPUKernel::InitFusedConstTensor() { - auto input = in_tensors_[0]; - auto scale = in_tensors_[1]; - auto offset = in_tensors_[2]; - auto mean = in_tensors_[3]; - auto variance = in_tensors_[4]; - auto output = out_tensors_[0]; + auto input = in_tensors_.at(0); + auto scale = in_tensors_.at(1); + auto offset = in_tensors_.at(2); + auto mean = in_tensors_.at(3); + auto variance = in_tensors_.at(4); + auto output = out_tensors_.at(0); auto scale_ptr = reinterpret_cast(scale->MutableData()); auto offset_ptr = reinterpret_cast(offset->MutableData()); @@ -133,7 +133,7 @@ int BatchnormInt8CPUKernel::InitFusedConstTensor() { } int BatchnormInt8CPUKernel::Init() { - auto input_shapes = in_tensors_[0]->shape(); + auto input_shapes = in_tensors_.at(0)->shape(); auto n_dim = input_shapes.size(); batchnorm_param_->channel_ = input_shapes[n_dim - 1]; batchnorm_param_->units_ = 1; @@ -161,7 +161,7 @@ int BatchnormInt8CPUKernel::Init() { } int BatchnormInt8CPUKernel::ReSize() { - auto input_shapes = in_tensors_[0]->shape(); + auto input_shapes = in_tensors_.at(0)->shape(); batchnorm_param_->unit_ = 1; for (size_t i = 0; i < input_shapes.size() - 1; i++) { batchnorm_param_->unit_ *= input_shapes[i]; diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/bias_add_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/bias_add_int8.cc index 691d89c0b3..3241183e2b 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/bias_add_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/bias_add_int8.cc @@ -21,6 +21,7 @@ #include "include/errorcode.h" using mindspore::lite::KernelRegistrar; +using mindspore::lite::RET_ERROR; using mindspore::lite::RET_OK; using mindspore::schema::PrimitiveType_BiasAdd; @@ -34,14 +35,18 @@ int BiasAddInt8CPUKernel::Init() { int BiasAddInt8CPUKernel::ReSize() { auto bias_param = reinterpret_cast(op_parameter_); - auto dims = in_tensors_[0]->shape(); + auto dims = in_tensors_.at(0)->shape(); bias_param->ndim_ = dims.size(); + if (bias_param->ndim_ < 1 || bias_param->ndim_ > 5) { + MS_LOG(ERROR) << "input shape is invalid"; + return RET_ERROR; + } for (size_t i = 0; i < bias_param->ndim_; i++) { bias_param->in_shape0_[i] = dims[i]; bias_param->in_shape1_[i] = 1; bias_param->out_shape_[i] = dims[i]; } - bias_param->in_shape1_[3] = dims[3]; + bias_param->in_shape1_[bias_param->ndim_ - 1] = dims[bias_param->ndim_ - 1]; return RET_OK; } diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/convolution_1x1_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/convolution_1x1_int8.cc index 60e3679c4a..0aefe0b25f 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/convolution_1x1_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/convolution_1x1_int8.cc @@ -239,7 +239,7 @@ int Convolution1x1Int8CPUKernel::InitWeightBias() { } memset(bias_data_, 0, size * sizeof(int32_t)); if (in_tensors_.size() == 3) { - memcpy(bias_data_, in_tensors_[kBiasIndex]->data_c(), output_channel * sizeof(int32_t)); + memcpy(bias_data_, in_tensors_.at(kBiasIndex)->data_c(), output_channel * sizeof(int32_t)); } InitBiasByzp(filter_tensor->data_c(), input_channel, output_channel, size); @@ -271,7 +271,7 @@ int Convolution1x1Int8CPUKernel::InitWeightBiasArm32() { } memset(bias_data_, 0, col2 * sizeof(int32_t)); if (in_tensors_.size() == 3) { - memcpy(bias_data_, in_tensors_[kBiasIndex]->data_c(), output_channel * sizeof(int32_t)); + memcpy(bias_data_, in_tensors_.at(kBiasIndex)->data_c(), output_channel * sizeof(int32_t)); } InitBiasByzp(filter_tensor->MutableData(), input_channel, output_channel, col2); diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_3x3_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_3x3_int8.cc index 5745219a0a..549ce9e0e5 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_3x3_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_3x3_int8.cc @@ -42,7 +42,7 @@ ConvolutionDepthwise3x3Int8CPUKernel::~ConvolutionDepthwise3x3Int8CPUKernel() { int ConvolutionDepthwise3x3Int8CPUKernel::InitWeightBias() { // init weight, int8 -> int16 - auto weight_tensor = in_tensors_[kWeightIndex]; + auto weight_tensor = in_tensors_.at(kWeightIndex); auto origin_weight = reinterpret_cast(weight_tensor->MutableData()); int channel = weight_tensor->Batch(); if (channel % 8 != 0) { diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_int8.cc index 3a0d1813a7..dcbe7fbeba 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_int8.cc @@ -40,7 +40,7 @@ ConvolutionDepthwiseInt8CPUKernel::~ConvolutionDepthwiseInt8CPUKernel() { int ConvolutionDepthwiseInt8CPUKernel::InitWeightBias() { // init weight, int8 -> int16 - auto weight_tensor = in_tensors_[kWeightIndex]; + auto weight_tensor = in_tensors_.at(kWeightIndex); auto origin_weight = reinterpret_cast(weight_tensor->MutableData()); int channel = weight_tensor->Batch(); int pack_weight_size = channel * weight_tensor->Height() * weight_tensor->Width(); @@ -171,7 +171,8 @@ kernel::LiteKernel *CpuConvDwInt8KernelCreator(const std::vector MS_ASSERT(opParameter != nullptr); MS_ASSERT(desc.type == schema::PrimitiveType_DepthwiseConv2D); kernel::LiteKernel *kernel = nullptr; - auto act_quant_size = MSMAX(inputs[kInputIndex]->quant_params().size(), outputs[kOutputIndex]->quant_params().size()); + auto act_quant_size = + MSMAX(inputs.at(kInputIndex)->quant_params().size(), outputs.at(kOutputIndex)->quant_params().size()); if (act_quant_size == 1) { // per tensor auto conv_param = reinterpret_cast(opParameter); if (primitive != nullptr && primitive->infer_flag()) { diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_slidewindow_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_slidewindow_int8.cc index e3abcb6b27..691b9e5aaa 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_slidewindow_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/convolution_depthwise_slidewindow_int8.cc @@ -44,7 +44,7 @@ ConvolutionDepthwiseSWInt8CPUKernel::~ConvolutionDepthwiseSWInt8CPUKernel() { int ConvolutionDepthwiseSWInt8CPUKernel::InitWeightBias() { // init weight, int8 -> int16 // o, h, w, i -> o/8, h, w, i, 8; o == group, i == 1 - auto weight_tensor = in_tensors_[kWeightIndex]; + auto weight_tensor = in_tensors_.at(kWeightIndex); auto origin_weight = reinterpret_cast(weight_tensor->MutableData()); int OC8 = UP_DIV(weight_tensor->Batch(), C8NUM); int pack_weight_size = C8NUM * OC8 * weight_tensor->Height() * weight_tensor->Width(); diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_depthwise_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_depthwise_int8.cc index 2f2931fb87..e9b614ed9e 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_depthwise_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_depthwise_int8.cc @@ -43,7 +43,7 @@ DeconvolutionDepthwiseInt8CPUKernel::~DeconvolutionDepthwiseInt8CPUKernel() { int DeconvolutionDepthwiseInt8CPUKernel::InitWeightBias() { // init weight: int8 -> int16 // o, h, w, i -> o/8, h, w, i, 8; o == group, i == 1 - auto weight_tensor = in_tensors_[kWeightIndex]; + auto weight_tensor = in_tensors_.at(kWeightIndex); auto origin_weight = reinterpret_cast(weight_tensor->MutableData()); int OC4 = UP_DIV(weight_tensor->Batch(), C4NUM); int pack_weight_size = C4NUM * OC4 * weight_tensor->Height() * weight_tensor->Width(); diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_int8.cc index 642381d786..823624a0d9 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/deconvolution_int8.cc @@ -132,7 +132,7 @@ int DeConvInt8CPUKernel::InitBiasWeight() { } memset(bias_data_, 0, size); if (in_tensors_.size() == 3) { - memcpy(bias_data_, in_tensors_[0]->MutableData(), conv_param_->output_channel_ * sizeof(int32_t)); + memcpy(bias_data_, in_tensors_.at(0)->MutableData(), conv_param_->output_channel_ * sizeof(int32_t)); } size = UP_ROUND(conv_param_->output_channel_, C4NUM) * UP_ROUND(conv_param_->input_channel_, C16NUM) * @@ -143,7 +143,7 @@ int DeConvInt8CPUKernel::InitBiasWeight() { return RET_ERROR; } memset(weight_ptr_, 0, size); - DeConvWeightTransInt8(reinterpret_cast(in_tensors_[1]->MutableData()), weight_ptr_, + DeConvWeightTransInt8(reinterpret_cast(in_tensors_.at(1)->MutableData()), weight_ptr_, conv_param_->input_channel_, conv_param_->output_channel_, conv_param_->kernel_h_ * conv_param_->kernel_w_, support_optimize_); diff --git a/mindspore/lite/src/runtime/kernel/arm/int8/gatherNd_int8.cc b/mindspore/lite/src/runtime/kernel/arm/int8/gatherNd_int8.cc index f898886148..2678af639c 100644 --- a/mindspore/lite/src/runtime/kernel/arm/int8/gatherNd_int8.cc +++ b/mindspore/lite/src/runtime/kernel/arm/int8/gatherNd_int8.cc @@ -64,7 +64,10 @@ int GatherNdInt8CPUKernel::ReSize() { for (int i = 0; i < indices_rank - 1; ++i) { count_ *= indices_shape[i]; } - + if (count_ >= std::numeric_limits::max() / static_cast(sizeof(int))) { + MS_LOG(ERROR) << "count_ is invalid, count_: " << count_; + return RET_ERROR; + } in_offset_ = reinterpret_cast(malloc(count_ * sizeof(int))); if (in_offset_ == nullptr) { MS_LOG(ERROR) << "GatherNdInt8 Malloc in_offset_ error!"; diff --git a/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc b/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc index b4944a93d1..a0f2c374be 100644 --- a/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc +++ b/mindspore/lite/tools/converter/parser/onnx/onnx_conv_parser.cc @@ -164,8 +164,8 @@ STATUS OnnxConvParser::Parse(const onnx::GraphProto &onnx_graph, const onnx::Nod } dims.insert(dims.begin(), iter->ints().begin(), iter->ints().end()); } - attr->channelOut = dims[0]; - attr->channelIn = dims[3] * attr->group; + attr->channelOut = dims.at(0); + attr->channelIn = dims.at(3) * attr->group; } attr->hasBias = onnx_node.input().size() == 3; if (onnx_node.op_type() == "ConvRelu" || onnx_node.op_type() == "Int8ConvRelu") {